-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
79 lines (63 loc) · 2.04 KB
/
Graph.java
File metadata and controls
79 lines (63 loc) · 2.04 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
package com.company;
import java.util.*;
public class Graph {
public static void main(String[] args){
Node zero = new Node(0);
Node one = new Node(1);
Node two = new Node(2);
Node three = new Node(3);
zero.addEdge(one);
zero.addEdge(two);
one.addEdge(two);
two.addEdge(zero);
two.addEdge(three);
three.addEdge(three);
Node four = new Node(4);
System.out.println(hasBFSPath(one, three));
System.out.println(hasDFSPath(one, three));
}
public static class Node{
private int data;
LinkedList<Node> adjacent = new LinkedList<Node>();
public Node(int data){
this.data = data;
}
public void addEdge(Node destination){
this.adjacent.add(destination);
}
}
public static boolean hasDFSPath(Node source, Node destination){
HashSet<Node> visited = new HashSet<Node>();
return hasDFSPath(source, destination, visited);
}
private static boolean hasDFSPath(Node source, Node destination, HashSet<Node> visited){
if(visited.contains(source)) return false;
visited.add(source);
if(source == destination) return true;
for(Node child : source.adjacent) {
if(hasDFSPath(child, destination, visited)){
return true;
}
}
return false;
}
public static boolean hasBFSPath(Node source, Node destination){
LinkedList<Node> nextToVisit = new LinkedList<Node>();
HashSet<Node> visited = new HashSet<Node>();
nextToVisit.add(source);
while(!nextToVisit.isEmpty()) {
Node node = nextToVisit.remove();
if (node == destination) {
return true;
}
if(visited.contains(node)){
continue;
}
visited.add(node);
for (Node child : node.adjacent) {
nextToVisit.add(child);
}
}
return false;
}
}