-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
84 lines (83 loc) · 2.55 KB
/
Graph.java
File metadata and controls
84 lines (83 loc) · 2.55 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
package interview;
import java.util.*;
public class Graph {
private final HashMap<Integer,java.util.LinkedList<Integer>> graph;
private final boolean IS_DIRECTED;
private final int SIZE;
public Graph(int N,boolean IS_DIRECTED){
if(N < 0)
throw new RuntimeException("SIZE CANNOT BE NEGATIVE");
this.IS_DIRECTED=IS_DIRECTED;
this.SIZE=N;
graph=new HashMap<>();
for(int i=1;i<=N;i++)
graph.put(i,new java.util.LinkedList<>());
}
public void addEdge(int x,int y){
java.util.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 DFS(int v){
Stack<Integer> stack=new Stack<>();
int visited[]=new int[SIZE];
stack.push(v);
visited[v]=1;
while(!stack.isEmpty()){
Integer x=stack.pop();
System.out.print(" "+x);
for(int y:graph.get(x)){
if(visited[y]==0){
stack.push(y);
visited[y]=1;
}
}
}
}
public boolean isConnected(){
Stack<Integer> stack=new Stack<>();
int visited[]=new int[SIZE];
stack.push(1);
while(!stack.isEmpty())
for(int x:graph.get(stack.pop()))
if(visited[x]==0)
visited[x]=1;
for(int i=0 ;i< visited.length ; i++)
if(visited[i]==0)
return false;
return false;
}
public void BFS(int v){
java.util.LinkedList<Integer> Queue=new java.util.LinkedList<>();
boolean visited[]=new boolean[SIZE];
Queue.add(v);
visited[v]=true;
while(Queue.size()!=0){
int x=Queue.poll();
System.out.print(x+" ");
java.util.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,java.util.LinkedList<Integer>> mapped:graph.entrySet()){
System.out.print(" "+mapped.getKey()+"->");
for(int y:mapped.getValue()){
System.out.print(" "+y);
}
System.out.println();
}
}
}