Breadth First Search Algorithm | Shortest Path | Graph Theory
Key Takeaways
The Breadth First Search (BFS) algorithm is explained, including its time complexity of O(V + E) and its use in finding the shortest path on an unweighted graph. The algorithm is demonstrated through pseudo-code and an example, showcasing its layered exploration of nodes and edges in a graph.
Full Transcript
hello and welcome back my name is William and today's topic is the breath for search graph traversal algorithm alongside the depth first search the breath first search algorithm is another one of those fundamental search algorithms used to explore nodes and edges of a graph it runs in a Time complexity of Big O of V plus e that is vertices plus edges and is often used as a building block in other algorithms it differs from a depth for search in the way that it explores the graph the bread for search algorithm is particularly useful for one thing finding the shortest path on an unweighted graph a breath for search starts at a node in the graph and explores its neighbor nodes first before moving on to explore the next level of Neighbors in a sense a breath first search explores nodes in layers so if we start breath first search at zero we would visit zero first then visit all of Zero's neighbors then we would visit all of Zero's neighbors the nodes in yellow before moving on to the next layer of nodes then we would visit all their neighbors and so on so as you saw a breath for search explores a graph in a layered fashion it does this by maintaining a cue of which node it should visit next this is most easily seen with with an example let's begin a breath for search at node 0 once more so let's add Zer to the Q on the left I will denote the current node in Red so zero is the current node and we want to explore all of Zero's unvisited neighbors and add them to the Q so we would add 9 to the Q 7 to the Q and 11 to the CU so zero has no more unvisited neighbors so we move on so nine is next up in the que so we add all of Nine's unvisited neighbors to the que so that is 10 and eight then there are no more neighbors of nine to visit so we move on to the next node in our que which is seven then we add all of Seven's unvisited neighbors to the que so we try and visit node 11 but node 11 is already in the queue so we don't want to add it again so we skip it then we would add six to the Q and 3 to the Q then this process goes on and on until we run out of nodes in the Q so I will let the animation play and that's how you do a breath for search in a nutshell in the previous animation we relied on a q to help us track which node we should visit next upon reaching a new node the algorithm adds it to the que to visit it later the queue data structure works like a realworld queue such as a waiting line in a restaurant people can either enter the waiting line that is get enced or get seated dced let's look at some pseudo code for the breath for search first things first we'll need two variables n the number of nodes in our graph and G the adjacency list representing our unweighted graph this breath for search function takes two arguments S and E the start and end node indices of the the search the return value for this function is the shortest path of nodes from s to e I've divided the function into two methods for Simplicity first we solve the problem by executing the bre for search and then we reconstruct the path from s to e so let's take a look at the solve method so here we are inside the solve method the first thing I do is initialize the Q data structure that we'll need and add add the starting node to it this Q should support at minimum the NQ and DQ operations I just talked about then initialize a Boolean array with all false values and mark the starting node as visited this array tracks whether or not node I has been visited if the value at index I is true then the node has either been visited or is being visited and is on the queue in the animation this corresponds to the gray and yellow nodes the last thing we'll need is an array I called prev which will help us reconstruct the shortest path from the start to the end node initially this array should be initialized with all null values this array tracks who the parent of node I was so we can reconstruct the path later let's Loop while the Q is not empty and pull out the top node from the que by issuing a DQ operation then reach inside the adjacency list and get all the neighbors of this node Loop over each unvisited node once we find a next unvisited node enq it to the Q mark it as visited and keep track of the parent node of the next node in the prev array once the queue is empty and our breath first search is complete simply return the prev array back inside the breath first search method take the output of the solve method which gave us the prev array and call the reconstruct path method here we are inside the reconstruct path method the first thing we do is actually reconstruct the path by looping backwards from the end node and making our way back to the start node that is assuming we can reach it the reason the prev array had to be initialized to all null values is because that is the way I'm checking whether or not the for Loop should stop since we Loop through the prev array backwards starting with the end node we need to reverse the order of the nodes so that the path starts at the start node and ends at the end node last but not least we actually have to make sure the path between nodes s and E exists it might not be possible to reach node E from node s if the graph is disjoint if this is the case then simply return an empty path and that's it for this breath for search video thank you for watching please give this video a thumbs up and subscribe for more mathematics and computer science videos
Original Description
Breadth First Search (BFS) algorithm explanation video with shortest path code
Algorithms repository:
https://github.com/williamfiset/algorithms#graph-theory
Video Slides:
https://github.com/williamfiset/Algorithms/tree/master/slides/graphtheory
=====================================
Practicing for interviews? I have used, and recommend `Cracking the Coding Interview` which got me a job at Google. Link on Amazon: https://amzn.to/3cvMof5
A lot of the content on this channel is inspired by the book `Competitive Programming` by Steven Halim which I frequently use as a resource and reference. Link on Amazon: https://amzn.to/3wC2nix
Support me by purchasing the full graph theory course on Udemy which includes additional problems, exercises and quizzes not available on YouTube:
https://www.udemy.com/course/graph-theory-algorithms
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from WilliamFiset · WilliamFiset · 0 of 60
← Previous
Next →
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
JES Image Manipulation - 2 - Installation
WilliamFiset
JES Image Manipulation - 3 - User Interface
WilliamFiset
JES Image Manipulation - 5 - Negative
WilliamFiset
JES Image Manipulation - 6 - Black & White
WilliamFiset
JES Image Manipulation - 4 - Grayscale
WilliamFiset
JES Image Manipulation - 8 - Blur
WilliamFiset
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
JES Image Manipulation - 9 - Blend
WilliamFiset
JES Image Manipulation - 10 - Matte
WilliamFiset
JES Image Manipulation - 13 - Rotate90
WilliamFiset
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
JES Image Manipulation - 11 - Crop Image
WilliamFiset
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
Java Fractal Explorer [6/8]
WilliamFiset
Java Fractal Explorer [4/8]
WilliamFiset
Java Fractal Explorer [8/8]
WilliamFiset
Java Fractal Explorer [5/8]
WilliamFiset
Java Fractal Explorer [2/8]
WilliamFiset
Java Fractal Explorer [7/8]
WilliamFiset
Java Fractal Explorer [1/8]
WilliamFiset
Java Fractal Explorer [3/8]
WilliamFiset
Introduction [Programming Competition Problems]
WilliamFiset
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
Logic 1 [Programming Competition Problems]
WilliamFiset
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
Introduction to Big-O
WilliamFiset
Dynamic and Static Arrays
WilliamFiset
Dynamic Array Code
WilliamFiset
Linked Lists Introduction
WilliamFiset
Doubly Linked List Code
WilliamFiset
Stack Introduction
WilliamFiset
Stack Implementation
WilliamFiset
Stack Code
WilliamFiset
Queue Introduction
WilliamFiset
Queue Implementation
WilliamFiset
Queue Code
WilliamFiset
Priority Queue Introduction
WilliamFiset
Priority Queue Min Heaps and Max Heaps
WilliamFiset
Priority Queue Inserting Elements
WilliamFiset
Priority Queue Removing Elements
WilliamFiset
Priority Queue Code
WilliamFiset
Union Find Introduction
WilliamFiset
Union Find Kruskal's Algorithm
WilliamFiset
Union Find - Union and Find Operations
WilliamFiset
Union Find Path Compression
WilliamFiset
Union Find Code
WilliamFiset
Binary Search Tree Introduction
WilliamFiset
Binary Search Tree Insertion
WilliamFiset
Binary Search Tree Removal
WilliamFiset
Binary Search Tree Traversals
WilliamFiset
Binary Search Tree Code
WilliamFiset
Fenwick Tree range queries
WilliamFiset
Fenwick Tree point updates
WilliamFiset
Fenwick Tree construction
WilliamFiset
Fenwick tree source code
WilliamFiset
Hash table hash function
WilliamFiset
Hash table separate chaining
WilliamFiset
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
Morris Pre Order Traversal
Dev.to · Jaspreet singh
Advanced Stack ApplicationsData Structures and Algorithms Deep‑Dive — Advanced Stack Applications…
Medium · Programming
The Minecraft anvil is a tree-cost optimization problem in disguise
Dev.to · Mark
KMP Algorithm (Knuth-Morris-Pratt): The Smart Way to Perform String Matching in O(N)
Dev.to · Jaspreet singh
🎓
Tutor Explanation
DeepCamp AI