file management
This commit is contained in:
23
bfs_visualization/algorithm.pde
Normal file
23
bfs_visualization/algorithm.pde
Normal file
@@ -0,0 +1,23 @@
|
||||
ArrayList<int[]> bfs(int start, int end) {
|
||||
ArrayList<Integer> q = new ArrayList<Integer>();
|
||||
ArrayList<int[]> res = new ArrayList<int[]>();
|
||||
q.add(start);
|
||||
int[] edge;
|
||||
int q_size = 1;
|
||||
while (q_size > 0) {
|
||||
int next = q.get(0);
|
||||
q.remove(0);
|
||||
q_size--;
|
||||
vis[next] = true;
|
||||
g.add_edge(0, 0);
|
||||
for (int neighbor : g.adj.get(next)) {
|
||||
if (vis[neighbor]) continue;
|
||||
q.add(neighbor);
|
||||
edge = new int[]{next, neighbor};
|
||||
res.add(edge);
|
||||
q_size++;
|
||||
if (neighbor == end) return res;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
Reference in New Issue
Block a user