-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphSearchEngineImpl.java
More file actions
60 lines (55 loc) · 1.51 KB
/
GraphSearchEngineImpl.java
File metadata and controls
60 lines (55 loc) · 1.51 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
import java.io.*;
import java.util.*;
import java.util.stream.*;
import java.util.function.*;
/**
* Implements the GraphSearchEngine interface.
*/
public class GraphSearchEngineImpl implements GraphSearchEngine {
public GraphSearchEngineImpl () {
}
/**
* Finds the shortest path by using BFS search.
* @param s is the actor1 which is where we're starting the search.
* @param t is the actor2 which we are trying to find the path to.
* Returns a list of the nodes during it's path
* @return the node list path.
*/
public List<Node> findShortestPath(Node s, Node t) {
List<Node> visited = new ArrayList<Node>();
Queue<Node> notVisited = new LinkedList<Node>();
Map<Node, Integer> sDistance = new HashMap<Node, Integer>();
List<Node> path = null;
notVisited.add(s);
sDistance.put(s, 0);
while (notVisited.size() > 0) {
Node node = notVisited.poll();
visited.add(node);
if (node.equals(t)) {
path = new ArrayList<Node>();
int distance = sDistance.get(node);
for (int i = 0; i < distance; i++) {
path.add(node);
for (Node neighbor : node.getNeighbors()) {
if (visited.contains(neighbor) && sDistance.get(neighbor) == sDistance.get(node) - 1) {
node = neighbor;
break;
}
}
}
path.add(s);
break;
} else {
for (Node k : node.getNeighbors()) {
if (!visited.contains(k)) {
notVisited.add(k);
sDistance.put(k, sDistance.get(node) + 1);
}
}
}
}
if (path != null)
Collections.reverse(path);
return path;
}
}