\$\begingroup\$
\$\endgroup\$
5
I have improved my BFS in Java according to vnp's suggestions. Again, we wish to find shortest paths in directed unweighted graphs using BFS, competitive style (no Map
s, Set
's or List
s):
import java.util.Arrays;
class BFS {
static int[] bfs(int[][] graph, int sourceNode, int targetNode) {
int[] queue = new int[graph.length];
int[] parents = new int[graph.length];
for (int i = 0; i < parents.length; i++) {
parents[i] = -1; // -1 denotes 'not used'
}
int queueStartIndex = 0;
int queueEndIndex = 1;
queue[0] = sourceNode;
parents[sourceNode] = -2;
while (queueStartIndex < queueEndIndex) {
int currentNode = queue[queueStartIndex++];
if (currentNode == targetNode) {
return buildPath(targetNode, parents);
}
for (int childNode : graph[currentNode]) {
if (parents[childNode] == -1) {
parents[childNode] = currentNode;
queue[queueEndIndex++] = childNode;
}
}
}
return null;
}
private static int[] buildPath(int targetNode, int[] parents) {
int pathLength = 0;
int node = targetNode;
while (node >= 0) {
pathLength++;
node = parents[node];
}
int[] path = new int[pathLength];
int pathIndex = path.length - 1;
int currentNode = targetNode;
while (currentNode >= 0) {
path[pathIndex--] = currentNode;
currentNode = parents[currentNode];
}
return path;
}
/* B ----+
/ |
A E
\ /
C - D
*/
public static void main(String[] args) {
int a = 0;
int b = 1;
int c = 2;
int d = 3;
int e = 4;
int[][] graph = new int[5][];
graph[a] = new int[]{ c, b };
graph[b] = new int[]{ e };
graph[c] = new int[]{ d };
graph[d] = new int[]{ c, e };
graph[e] = new int[]{ b, d };
// A -> B -> E
int[] path = bfs(graph, a, e);
System.out.println(Arrays.toString(path));
// A <- B <- E does not exist:
System.out.println(Arrays.toString(bfs(graph, e, a)));
graph = new int[4][];
graph[a] = new int[]{ b, c };
graph[b] = new int[]{ d };
graph[c] = new int[]{ a, d };
graph[d] = new int[]{ b, c };
/* B
/ \
A D
\ /
C
*/
// A -> B -> D
path = bfs(graph, a, d);
System.out.println(Arrays.toString(path));
path = bfs(graph, d, a);
// D -> C -> A:
System.out.println(Arrays.toString(path));
}
}
Please tell me anything that comes to mind.
asked May 5, 2019 at 16:47
lang-java
int
value. (2) The above requires no Objects... \$\endgroup\$