-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
BreadthFirstSearchAdjacencyListIterativeFastQueue.java
165 lines (133 loc) · 4.71 KB
/
BreadthFirstSearchAdjacencyListIterativeFastQueue.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* An implementation of an iterative BFS with an adjacency list Time Complexity: O(V + E)
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.graphtheory;
import java.util.*;
// A custom implementation of a circular integer only queue which is
// extremely quick and lightweight. In terms of performance it can outperform
// java.util.ArrayDeque (Java's fastest queue implementation) by a factor of 40+!
// However, the downside is you need to know an upper bound on the number of elements
// that will be inside the queue at any given time for this queue to work.
class IntQueue {
private int[] ar;
private int front, end, sz;
// max_sz is the maximum number of items
// that can be in the queue at any given time
public IntQueue(int max_sz) {
front = end = 0;
this.sz = max_sz + 1;
ar = new int[sz];
}
public boolean isEmpty() {
return front == end;
}
public int peek() {
return ar[front];
}
// Add an element to the queue
public void enqueue(int value) {
ar[end] = value;
if (++end == sz) end = 0;
if (end == front) throw new RuntimeException("Queue too small!");
}
// Make sure you check is the queue is not empty before calling dequeue!
public int dequeue() {
int ret_val = ar[front];
if (++front == sz) front = 0;
return ret_val;
}
}
public class BreadthFirstSearchAdjacencyListIterativeFastQueue {
static class Edge {
int from, to, cost;
public Edge(int from, int to, int cost) {
this.from = from;
this.to = to;
this.cost = cost;
}
}
// Perform a breadth first search on a graph with n nodes
// from a starting point to count the number of nodes
// in a given component.
static int bfs(Map<Integer, List<Edge>> graph, int start, int n) {
int count = 0;
boolean[] visited = new boolean[n];
IntQueue queue = new IntQueue(n + 1);
// For each breadth first search layer gets separated by a DEPTH_TOKEN
// to easily augment this method for additional functionality
int DEPTH_TOKEN = -1;
// Start by visiting the starting node
queue.enqueue(start);
queue.enqueue(DEPTH_TOKEN);
visited[start] = true;
// Continue until the BFS is done
while (true) {
Integer node = queue.dequeue();
// If we encounter a depth token this means that we
// have finished the current frontier and are about
// to start the new layer (some of which may already
// be in the queue) or have reached the end.
if (node == DEPTH_TOKEN) {
// No more nodes to process
if (queue.isEmpty()) break;
// Add another DEPTH_TOKEN
queue.enqueue(DEPTH_TOKEN);
} else {
count++;
List<Edge> edges = graph.get(node);
if (edges != null) {
// Loop through all edges attached to this node. Mark nodes as
// visited once they're in the queue. This will prevent having
// duplicate nodes in the queue and speedup the BFS.
for (Edge edge : edges) {
if (!visited[edge.to]) {
visited[edge.to] = true;
queue.enqueue(edge.to);
}
}
}
}
}
return count;
}
// Example usage of DFS
public static void main(String[] args) {
// Create a fully connected graph
int numNodes = 8;
Map<Integer, List<Edge>> graph = new HashMap<>();
addDirectedEdge(graph, 1, 2, 1);
addDirectedEdge(graph, 1, 2, 1); // Double edge
addDirectedEdge(graph, 1, 3, 1);
addDirectedEdge(graph, 2, 4, 1);
addDirectedEdge(graph, 2, 5, 1);
addDirectedEdge(graph, 3, 6, 1);
addDirectedEdge(graph, 3, 7, 1);
addDirectedEdge(graph, 2, 2, 1); // Self loop
addDirectedEdge(graph, 2, 3, 1);
addDirectedEdge(graph, 6, 2, 1);
addDirectedEdge(graph, 1, 6, 1);
long nodeCount = bfs(graph, 0, numNodes);
System.out.println("DFS node count starting at node 0: " + nodeCount);
nodeCount = bfs(graph, 2, numNodes);
System.out.println("DFS node count starting at node 4: " + nodeCount);
// Complete graph with self loops
graph.clear();
numNodes = 100;
for (int i = 0; i < numNodes; i++)
for (int j = 0; j < numNodes; j++) addDirectedEdge(graph, i, j, 1);
nodeCount = bfs(graph, 6, numNodes);
System.out.println("BFS node count starting at node 6: " + nodeCount);
if (nodeCount != 100) System.err.println("Error with BFS");
}
// Helper method to setup graph
private static void addDirectedEdge(Map<Integer, List<Edge>> graph, int from, int to, int cost) {
List<Edge> list = graph.get(from);
if (list == null) {
list = new ArrayList<Edge>();
graph.put(from, list);
}
list.add(new Edge(from, to, cost));
}
}