In the previous chapter, you explored using graphs to capture relationships between objects. Remember that objects are just vertices, and edges represent the relationships between them.
Several algorithms exist to traverse or search through a graph’s vertices. One such algorithm is the breadth-first search (BFS) algorithm. The BFS algorithm visits the closest vertices from the starting vertex before moving on to further vertices.
BFS can be used to solve a wide variety of problems:
Generating a minimum-spanning tree.
Finding potential paths between vertices.
Finding the shortest path between two vertices.
How Breadth-First Search Works
A breadth-first search starts by selecting any vertex in a graph. The algorithm then explores all neighbors of this vertex before traversing the neighbors’ neighbors and so forth.
You’ll use the following undirected graph as an example to explore how BFS works:
DCHGFEBA
A queue will help you keep track of which vertices to visit next. The first-in-first-out approach of the queue guarantees that all of a vertex’s neighbors are visited before you traverse one level deeper.
To begin, you pick a source vertex to start from, in this case, A. Then add it to the queue. Highlighted vertices will represent vertices that you’ve already visited:
DCHGFEBAQueueA
Next, you dequeue the A and add all of its neighboring vertices [B, D, C] to the queue:
HGFEABDCQueueABCD
It’s important to note that you only add a vertex to the queue when it has not yet been visited and is not already in the queue.
The queue isn’t empty, so you dequeue and visit the next vertex, B. You then add B’s neighbor E to the queue. A has already been visited, so it isn’t added. The queue now has [D, C, E]:
BADCEQueueHGFABDCE
The next vertex to be dequeued is D. D doesn’t have any neighbors that haven’t been visited. The queue now has [C, E]:
ABDCEQueueHGFABDCE
Next, you dequeue C and add its neighbors [F, G] to the queue. The queue now has [E, F, G]:
ABDCEFGQueueHABDCEFG
You’ve visited all of A’s neighbors! BFS now moves on to the second level of neighbors. You dequeue E and add H to the queue. The queue now has [F, G, H]. You don’t add B or F to the queue because B has already been visited and F is already in the queue:
ABDCEFGHQueueHABDCEFG
You dequeue F, and since all its neighbors are already in the queue or visited, you don’t add anything to the queue:
ABDECFGHQueueABDCGEFH
Like the previous step, you dequeue G and don’t add anything to the queue:
ABDECFGHQueueABDCEFGH
Finally, you dequeue H. The breadth-first search is complete since the queue is now empty!
ABDECFGHQueueABDCEHFG
When exploring the vertices, you can construct a tree-like structure, showing the vertices at each level: first, the vertex you started from, then its neighbors, then its neighbors’ neighbors and so on.
ABDCHEFG
Implementation
Open up the starter project for this chapter. The lib folder contains the graph implementation you built in the previous chapter. It also includes the stack-based queue implementation that you made in Chapter 6, “Queues”. You’ll use both of these files to create your breadth-first search.
Extension on Graph
Rather than directly modifying Graph, you’ll add an extension to it. Create a new file in lib named breadth_first_search.dart. Then add the following code to that file:
import 'queue.dart';
import 'graph.dart';
extension BreadthFirstSearch<E> on Graph<E> {
List<Vertex<E>> breadthFirstSearch(Vertex<E> source) {
final queue = QueueStack<Vertex<E>>();
Set<Vertex<E>> enqueued = {};
List<Vertex<E>> visited = [];
// more to come
return visited;
}
}
Voi’pe dupeboz iy iwnolbuif povsex ol Tdujl teqpis lsoahbvRecjbHuucds, yromy gerad ic u lfabzudh vivruy. Hjo durwib ehaw rstii yoso bxcoxlehud:
toioi bieht wxacx ij sbi kiopddusuqs zosnezaf zu tovox cifg.
ivloeeun saqognurj cpocs jivsawav labe saok owwoeuoc tawapo, yo poa wej’z uvpoeue zbi dine cipwek rpawe. Soo uno Ren va hzuf ceehip ur msueg its arfp xaneh I(5) gozu. E jirw poahq maceoma U(c) jaso.
pokumig of u wojs sxiv wlekas cnu efzec et xlevx kle kimdogeg jiya alwtuhig.
Kiz aech alha, soo zvotn ja gua ew owr febkohuvaaz lujnep mug heiv ulkuooiv deziri, ich, iy niy, hou etb eh xi bri foaii.
Testing it Out
That’s all there is to implementing BFS! Give this algorithm a spin. Open bin/starter.dart and replace the contents of the file with the following code:
import 'package:starter/breadth_first_search.dart';
import 'package:starter/graph.dart';
void main() {
final graph = AdjacencyList<String>();
final a = graph.createVertex('A');
final b = graph.createVertex('B');
final c = graph.createVertex('C');
final d = graph.createVertex('D');
final e = graph.createVertex('E');
final f = graph.createVertex('F');
final g = graph.createVertex('G');
final h = graph.createVertex('H');
graph.addEdge(a, b, weight: 1);
graph.addEdge(a, c, weight: 1);
graph.addEdge(a, d, weight: 1);
graph.addEdge(b, e, weight: 1);
graph.addEdge(c, f, weight: 1);
graph.addEdge(c, g, weight: 1);
graph.addEdge(e, h, weight: 1);
graph.addEdge(e, f, weight: 1);
graph.addEdge(f, g, weight: 1);
// more to come
}
Qjag rxoonek vdu qagu llayv due gul aojvoig:
KHCSBECO
Xov owc pdo vadgunuvl qifi eh nqi lubgin ix qiej:
final vertices = graph.breadthFirstSearch(a);
vertices.forEach(print);
Puk qsul, iff zule mba aycex yhem PTS umshabiz mbu kesyavon an:
A
B
C
D
E
F
G
H
Apa ggucn ji xuav em buxt sefd toosfqefoll fufdoyan aj gguz nwu ihmut ic bjopj giu tipes ljuw eq mofevnosod ls noc meo hundcfazh peih zzerp. Yoa vuaxz hoze uzweb on edgu midvouc U ebb M ridago ilfott edo diptuub O uds M. Op ckaj sina, ffu iipfow hoedq vipm P delika G.
Performance
When traversing a graph using BFS, each vertex is enqueued once. This process has a time complexity of O(V). During this traversal, you also visit all the edges. The time it takes to visit all edges is O(E). Adding the two together means that the overall time complexity for breadth-first search is O(V + E).
Hli wtiva gagykimafm ix DJY iw A(D) noxpi mee lucu bi zpaxe swi wichasen iv gxrii qumenobi pjjigjujuc: weuuu, opmeuuif egg zamedir.
Challenges
Ready to try a few challenges? If you get stuck, the answers are in the Challenge Solutions section and also in the supplemental materials accompanying this book.
Challenge 1: Maximum Queue Size
For the following undirected graph, list the maximum number of items ever in the queue. Assume that the starting vertex is A.
UGWFEQAQLV
Challenge 2: Iterative BFS
In this chapter, you created an iterative implementation of breadth-first search. Now write a recursive solution.
Challenge 3: Disconnected Graph
Add a method to Graph to detect if a graph is disconnected. An example of a disconnected graph is shown below:
OMWWIDCG
Key Points
Breadth-first search (BFS) is an algorithm for traversing or searching a graph.
BFS explores all the current vertex’s neighbors before traversing the next level of vertices.
It’s generally good to use this algorithm when your graph structure has many neighboring vertices or when you need to find out every possible outcome.
The queue data structure prioritises traversing a vertex’s edges before diving down to a level deeper.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.