-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgo.java
More file actions
51 lines (43 loc) · 1007 Bytes
/
Copy pathAlgo.java
File metadata and controls
51 lines (43 loc) · 1007 Bytes
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
package bfs;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Algo {
protected String end;
private Vertex start;
private List<String> path;
public Algo(Vertex start, String end) {
this.start = start;
this.end = end;
this.path = new ArrayList<>();
}
private void create_path(Vertex current, String start) {
while (!current.getName().equals(start)) {
path.add(0, current.getName());
current = current.getParent().remove(0);
}
}
public void bfs_search() {
Queue<Vertex> queue = new LinkedList<>();
start.setVisited(true);
queue.add(start);
while (!queue.isEmpty()) {
Vertex data = queue.remove();
if (data.getName() == end) {
create_path(data, start.getName());
break ;
}
for (Vertex x : data.getNeigbours()) {
if (!x.isVisited()) {
x.setVisited(true);
x.setParent(data);
queue.add(x);
}
}
}
}
public List<String> getPath() {
return path;
}
}