-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckIfPathExists.java
More file actions
46 lines (42 loc) · 1.32 KB
/
CheckIfPathExists.java
File metadata and controls
46 lines (42 loc) · 1.32 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
package datastructure.graph;
import java.util.LinkedList;
public class CheckIfPathExists {
public static boolean checkPath(AdjacencyListGraph graph,int source,int destination){
LinkedList<Integer> list = null;
if (graph.adjacencyList[source]!=null)
list=graph.adjacencyList[source];
for(int i=0;i<list.size();i++){
if(list.get(i).equals(destination)){
return true;
}
else {
return checkPath(graph, list.get(i),destination);
}
}
return false;
}
public static void main(String args[]) {
AdjacencyListGraph g1 = new AdjacencyListGraph(9);
g1.addEdge(0,2);
g1.addEdge(0,5);
g1.addEdge(2,3);
g1.addEdge(2,4);
g1.addEdge(5,3);
g1.addEdge(5,6);
g1.addEdge(3,6);
g1.addEdge(6,7);
g1.addEdge(6,8);
g1.addEdge(6,4);
g1.addEdge(7,8);
g1.printGraph();
System.out.println("Path exists: " + checkPath(g1, 0, 7));
System.out.println();
AdjacencyListGraph g2 = new AdjacencyListGraph(4);
g2.addEdge(0,1);
g2.addEdge(1,2);
g2.addEdge(1,3);
g2.addEdge(2,3);
g2.printGraph();
System.out.println("Path exists: " + checkPath(g2, 3, 0));
}
}