forked from joharbatta/DataStructure-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphSetMap.java
More file actions
83 lines (78 loc) · 1.81 KB
/
graphSetMap.java
File metadata and controls
83 lines (78 loc) · 1.81 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
import java.util.*;
public class graphSetMap {
//keys=Integer,values,neighbours=HashSet
HashMap<Integer,HashSet<Integer>> graph=new HashMap<>();
boolean addVertex(int vertex)
{
if(!graph.containsKey(vertex))
{
graph.put(vertex, new HashSet<>());
return true;
}
return false;
}
void addEdge(int u,int v)
{
//purane set ko vapis leke aane ke liye
// HashSet<Integer> neighbour=graph.get(u);
// neighbour.add(v);
// graph.put(u, neighbour);
graph.get(u).add(v);
//instead of upper 3 line
// neighbour=graph.get(v);
// neighbour.add(u);
// graph.put(v,neighbour);
graph.get(v).add(u);
}
void bfs(int s)
{
System.out.println("---------bfs----------");
HashSet<Integer> visited=new HashSet<>();
Queue<Integer> queue=new LinkedList<>();
queue.add(s);
visited.add(s);
while(!queue.isEmpty())
{
s = queue.poll();
System.out.println(s+" ");
for( Integer n:graph.get(s))
{
if(!visited.contains(n))
{
queue.add(n);
visited.add(n);
}
}
}
System.out.println();
}
void print()
{
//vertex shi print hogye
System.out.println(graph.keySet());
System.out.println();
for(Integer key:graph.keySet())
{
System.out.println("Adjaency List for Node: "+key);
System.out.println(graph.get(key));
}
}
public static void main(String[] args) {
graphSetMap g=new graphSetMap();
g.addVertex(0);
g.addVertex(1);
g.addVertex(2);
g.addVertex(3);
g.addVertex(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(0, 3);
//no error ,only copy of this element is store
// g.addEdge(0, 3);
g.addEdge(1,3);
g.addEdge(2,4);
g.addEdge(3,4);
g.print();
g.bfs(0);
}
}