-
Notifications
You must be signed in to change notification settings - Fork 17
/
Graph.java
125 lines (118 loc) · 3.77 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package SummerTrainingGFG.Graph;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Queue;
/**
* @author Vishal Singh
* 20-01-2021
*/
public class Graph {
static void addEdge(ArrayList<ArrayList<Integer>> adj, int u, int v){
adj.get(u).add(v);
adj.get(v).add(u);
}
static void print(ArrayList<ArrayList<Integer>> adj){
for (ArrayList<Integer> integers : adj) {
for (Integer integer : integers) {
System.out.print(integer + " ");
}
System.out.println();
}
}
static void bfs(ArrayList<ArrayList<Integer>> adj,int v,int s){
System.out.println("Graph.bfs");
boolean[] visited = new boolean[v];
Queue<Integer> queue = new ArrayDeque<>();
queue.add(s);
visited[s] = true;
while (!queue.isEmpty()){
int x = queue.poll();
System.out.print(x+" ");
for (int u:adj.get(x)){
if (!visited[u]){
queue.add(u);
visited[u] = true;
}
}
}
System.out.println();
}
static void bfsDisconnected(ArrayList<ArrayList<Integer>> adj,int s,boolean[] visited){
Queue<Integer> queue = new ArrayDeque<>();
queue.add(s);
visited[s] = true;
while (!queue.isEmpty()){
int x = queue.poll();
System.out.print(x+" ");
for (int u:adj.get(x)){
if (!visited[u]){
queue.add(u);
visited[u] = true;
}
}
}
System.out.println();
}
static int bfsDisconnected(ArrayList<ArrayList<Integer>> adj,int noOfVertices){
System.out.println("Graph.bfsDisconnected");
boolean[] visited = new boolean[noOfVertices];
int count = 0;
for (int i = 0; i < noOfVertices; i++) {
if (!visited[i]){
bfsDisconnected(adj,i,visited);
count++;
}
}
return count;
}
static void dfsRecursion(ArrayList<ArrayList<Integer>> adj,int source,boolean[] visited){
visited[source] = true;
System.out.print(source +" ");
for (int u : adj.get(source)) {
if (!visited[u])
dfsRecursion(adj,u,visited);
}
}
static void dfs(ArrayList<ArrayList<Integer>> adj,int vertex,int source){
System.out.println("Graph.dfs");
boolean[] visited = new boolean[vertex];
dfsRecursion(adj,source,visited);
System.out.println();
}
static int dfsDisconnected(ArrayList<ArrayList<Integer>> adj,int vertex){
System.out.println("Graph.dfsDisconnected");
boolean[] visited = new boolean[vertex];
int noOfConnectedComponents = 0;
for (int i = 0; i < vertex; i++) {
if (!visited[i]){
dfsRecursion(adj,i,visited);
System.out.println();
noOfConnectedComponents++;
}
}
return noOfConnectedComponents;
}
public static void main(String[] args){
/*
* Adjacency List*/
int v = 7;
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for (int i = 0; i < v; i++) {
adj.add(new ArrayList<>());
}
addEdge(adj,0,1);
addEdge(adj,0,2);
addEdge(adj,2,3);
addEdge(adj,1,3);
addEdge(adj,4,5);
addEdge(adj,5,6);
addEdge(adj,4,6);
print(adj);
bfs(adj,v,0);
bfsDisconnected(adj,v);
System.out.println("No of island: "+bfsDisconnected(adj, v));
dfs(adj,v,0);
dfsDisconnected(adj,v);
System.out.println("No of connected componenets: "+dfsDisconnected(adj, v));
}
}