-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbfs.java
More file actions
75 lines (64 loc) · 1.96 KB
/
bfs.java
File metadata and controls
75 lines (64 loc) · 1.96 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
/**
* @author akshay
*/
import java.util.*;
import java.io.*;
public class bfs {
public static void main(String[] args) {
vertex a = new vertex("a");
vertex b = new vertex("b");
vertex c = new vertex("c");
vertex d = new vertex("d");
vertex e = new vertex("e");
vertex f = new vertex("f");
vertex g = new vertex("g");
List<vertex> A = new ArrayList<vertex>(){{add(b); add(c);}};
List<vertex> B = Arrays.asList(a,d,f,e);
List<vertex> C = Arrays.asList(a);
List<vertex> D = Arrays.asList(b);
List<vertex> E = Arrays.asList(b,g);
List<vertex> F = Arrays.asList(b);
List <vertex>G = Arrays.asList(e);
Map<String, List> adj = new Hashtable<String, List>();
adj.put("a", A);
adj.put("b", B);
adj.put("c", C);
adj.put("d", D);
adj.put("e", E);
adj.put("f", F);
adj.put("g", G);
Queue <vertex> Q = new LinkedList<>();
a.setColour("black");
Q.add(a);
while(Q.size() != 0)
{
vertex v = Q.remove();
for(int i=0; i< adj.get(v.getName()).size(); i++){
if(((vertex)adj.get(v.getName()).get(i)).getColour().equals("white"))
{
((vertex)adj.get(v.getName()).get(i)).setColour("gray");
Q.add(((vertex)adj.get(v.getName()).get(i)));
System.out.println(((vertex)adj.get(v.getName()).get(i)).getName());
}
}
v.setColour("black");
}
}
}
class vertex{
String name, colour;
vertex(){};
vertex(String name){
this.name = name;
colour = "white";
}
public void setColour(String c){
this.colour = c;
}
public String getColour(){
return this.colour;
}
public String getName(){
return this.name;
}
}