-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMyGraph.java
More file actions
153 lines (135 loc) · 4.14 KB
/
MyGraph.java
File metadata and controls
153 lines (135 loc) · 4.14 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
This is implementation of Graph using Array of LinkedList
& implementation
**/
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class myGraph
{
private LinkedList<Integer> adj[];
// Here, we are initializing Adjacency Array with a LinkedList
@SuppressWarnings("unchecked")
public myGraph(int v)
{
adj = new LinkedList[v];
for(int i=0; i<v; i++)
{
adj[i] = new LinkedList<Integer>();
}
}
// Here we are filling adjacency array with LinkedList
public void addEdge(int source, int destination)
{
adj[source].add(destination);
adj[destination].add(source);
}
//Printing Graph values here
public void printGraph(int edges)
{
for(int k=0; k<edges; k++)
{
System.out.println(adj[k]);
}
}
// Search using BFS search
public int bfs(int source, int destination)
{
boolean[] visited = new boolean[adj.length];
int[] parent = new int[adj.length];
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(source);
parent[source] = -1;
visited[source] = true;
// Here, we insert each neighbor of an element into queue & check if it is visited or not.
while(!queue.isEmpty())
{
int current = queue.poll();
if(source == destination) break;
for(int neighbor : adj[current])
{
if(!visited[neighbor])
{
visited[neighbor] = true;
queue.add(neighbor);
parent[neighbor] = current;
}
}
}
int distance = 0;
int current = destination;
// Here, we traversed the parent array & reaches to source from destination.
while(parent[current] != -1)
{
System.out.println(current+" --> " );
current = parent[current];
distance++;
}
return distance;
}
// Utility method that will make a recursive call and checks if path is possible to the destination or not
public boolean dfsUtils(int source, int destination, boolean[] vis)
{
if(source == destination)
{
return true;
}
for (int neighbor : adj[source]) {
if(!vis[neighbor])
{
vis[neighbor] = true;
boolean isConnected = dfsUtils(neighbor, destination, vis);
if(isConnected) return true;
}
}
return false;
}
// Method to call util method
public boolean dfs(int source, int destination)
{
boolean[] vis = new boolean[adj.length];
vis[source] = true;
return dfsUtils(source, destination, vis);
}
public boolean dfsStack(int source, int destination)
{
Stack<Integer> stack = new Stack<Integer>();
stack.push(source);
boolean visited[] = new boolean[adj.length];
visited[source] = true;
while(!stack.isEmpty())
{
if(source ==destination) return true;
int current = stack.pop();
for(int neighbor : adj[current])
{
if(!visited[neighbor])
{
visited[neighbor] = true;
stack.push(neighbor);
}
}
}
return false;
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int vertices = input.nextInt();
int edges = input.nextInt();
myGraph graph = new myGraph(vertices);
System.out.println("Enter "+edges+" edges");
for(int i=0; i<edges; i++)
{
int source = input.nextInt();
int destination = input.nextInt();
graph.addEdge(source, destination);
}
input.close();
graph.printGraph(edges);
graph.bfs(0, 4);
graph.dfsStack(0, 4);
System.out.println(graph.dfs(0, 3));
}
}