-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
BreadthFirstTraversal.java
124 lines (106 loc) · 3.86 KB
/
BreadthFirstTraversal.java
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.jwetherell.algorithms.graph;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import com.jwetherell.algorithms.data_structures.Graph;
import com.jwetherell.algorithms.data_structures.Graph.Edge;
import com.jwetherell.algorithms.data_structures.Graph.Vertex;
/**
* Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the tree root (or some arbitrary node of a graph, sometimes referred to as a
* 'search key') and explores the neighbor nodes first, before moving to the next level neighbors.
* <p>
* @see <a href="https://en.wikipedia.org/wiki/Breadth-first_search">Breadth-First Search (Wikipedia)</a>
* <br>
* @author Justin Wetherell <[email protected]>
*/
public class BreadthFirstTraversal {
@SuppressWarnings("unchecked")
public static final <T extends Comparable<T>> Graph.Vertex<T>[] breadthFirstTraversal(Graph<T> graph, Graph.Vertex<T> source) {
// use for look-up via index
final ArrayList<Vertex<T>> vertices = new ArrayList<Vertex<T>>();
vertices.addAll(graph.getVertices());
// used for look-up via vertex
final int n = vertices.size();
final Map<Vertex<T>,Integer> vertexToIndex = new HashMap<Vertex<T>,Integer>();
for (int i=0; i<n; i++) {
final Vertex<T> v = vertices.get(i);
vertexToIndex.put(v,i);
}
// adjacency matrix
final byte[][] adj = new byte[n][n];
for (int i=0; i<n; i++) {
final Vertex<T> v = vertices.get(i);
final int idx = vertexToIndex.get(v);
final byte[] array = new byte[n];
adj[idx] = array;
final List<Edge<T>> edges = v.getEdges();
for (Edge<T> e : edges)
array[vertexToIndex.get(e.getToVertex())] = 1;
}
// visited array
final byte[] visited = new byte[n];
for (int i = 0; i < visited.length; i++)
visited[i] = -1;
// for holding results
final Graph.Vertex<T>[] arr = new Graph.Vertex[n];
// start at the source
Vertex<T> element = source;
int c = 0;
int i = vertexToIndex.get(element);
int k = 0;
arr[k] = element;
visited[i] = 1;
k++;
final Queue<Vertex<T>> queue = new ArrayDeque<Vertex<T>>();
queue.add(source);
while (!queue.isEmpty()) {
element = queue.peek();
c = vertexToIndex.get(element);
i = 0;
while (i < n) {
if (adj[c][i] == 1 && visited[i] == -1) {
final Vertex<T> v = vertices.get(i);
queue.add(v);
visited[i] = 1;
arr[k] = v;
k++;
}
i++;
}
queue.poll();
}
return arr;
}
public static int[] breadthFirstTraversal(int n, byte[][] adjacencyMatrix, int source) {
final int[] visited = new int[n];
for (int i = 0; i < visited.length; i++)
visited[i] = -1;
int element = source;
int i = source;
int arr[] = new int[n];
int k = 0;
arr[k] = element;
visited[i] = 1;
k++;
final Queue<Integer> queue = new ArrayDeque<Integer>();
queue.add(source);
while (!queue.isEmpty()) {
element = queue.peek();
i = 0;
while (i < n) {
if (adjacencyMatrix[element][i] == 1 && visited[i] == -1) {
queue.add(i);
visited[i] = 1;
arr[k] = i;
k++;
}
i++;
}
queue.poll();
}
return arr;
}
}