-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.java
52 lines (52 loc) · 1.53 KB
/
Graph.java
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
package fastds;
import java.util.*;
public class Graph {
private final HashMap<Integer,LinkedList<Integer>> graph;
private final boolean IS_DIRECTED;
private final int SIZE;
public Graph(int N,boolean IS_DIRECTED){
this.IS_DIRECTED=IS_DIRECTED;
this.SIZE=N;
graph=new HashMap<>();
for(int i=1;i<=N;i++)
graph.put(i,new LinkedList<>());
}
public void addEdge(int x,int y){
LinkedList<Integer> list;
if(IS_DIRECTED){
list=graph.get(y);
list.add(x);
graph.put(y, list);
}
list=graph.get(x);
list.add(y);
graph.put(x, list);
}
public void BFS(int v){
LinkedList<Integer> Queue=new LinkedList<>();
boolean visited[]=new boolean[SIZE];
Queue.add(v);
visited[v]=true;
while(Queue.size()!=0){
int x=Queue.poll();
System.out.print(x+" ");
Iterator<Integer> itr=graph.get(x).iterator();
while(itr.hasNext()){
int i=itr.next();
if(!visited[i]){
visited[i]=true;
Queue.add(i);
}
}
}
}
public void print(){
for(Map.Entry<Integer,LinkedList<Integer>> mapped:graph.entrySet()){
System.out.print(" "+mapped.getKey()+"->");
for(int y:mapped.getValue()){
System.out.print(" "+y);
}
System.out.println();
}
}
}