-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphAlgorithms.java
More file actions
84 lines (53 loc) · 2.12 KB
/
Copy pathGraphAlgorithms.java
File metadata and controls
84 lines (53 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.awt.Color;
import java.util.*;
import java.util.Iterator;
public class GraphAlgorithms{
/* FloodFillDFS(v, writer, fillColour)
Traverse the component the vertex v using DFS and set the colour
of the pixels corresponding to all vertices encountered during the
traversal to fillColour.
*/
public static void FloodFillDFS(PixelVertex v, PixelWriter writer, Color fillColour){
Hashtable<PixelVertex, Integer> dict = new Hashtable<PixelVertex, Integer>();
FloodFillDFS(v,writer,fillColour,dict);
}
private static void FloodFillDFS(PixelVertex v, PixelWriter writer, Color fillColour, Hashtable<PixelVertex, Integer> dict){
writer.setPixel(v.getX(), v.getY(), fillColour);
dict.put(v,0);
LinkedList<PixelVertex> neighbours = v.getNeighbours();
Iterator<PixelVertex> i = neighbours.listIterator();
while (i.hasNext()){
PixelVertex cur = i.next();
if (!dict.containsKey(cur)){
FloodFillDFS(cur,writer,fillColour,dict);
}
}
}
/* FloodFillBFS(v, writer, fillColour)
Traverse the component the vertex v using BFS and set the colour
of the pixels corresponding to all vertices encountered during the
traversal to fillColour.
*/
public static void FloodFillBFS(PixelVertex v, PixelWriter writer, Color fillColour){
writer.setPixel(v.getX(), v.getY(), fillColour);
// TODO: implement this method
LinkedList<PixelVertex> q = new LinkedList<PixelVertex>();
Hashtable<PixelVertex, Integer> dict = new Hashtable<PixelVertex, Integer>();
LinkedList<PixelVertex> neighbours = v.getNeighbours();
PixelVertex first = neighbours.remove();
q.add(first);
while(q.size() != 0 ){
PixelVertex cur = q.remove();
LinkedList<PixelVertex> curNeighbours = cur.getNeighbours();
Iterator<PixelVertex> i = curNeighbours.listIterator();
while (i.hasNext()){
PixelVertex next = i.next();
if (!dict.containsKey(next)){
dict.put(next,1);
writer.setPixel(next.getX(), next.getY(), fillColour);
q.add(next);
}
}
}
}
}