Breadth First Search Algorithm | Shortest Path | Graph Theory

WilliamFiset · Advanced ·⚡ Algorithms & Data Structures ·8y ago

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 JES Image Manipulation - 2 - Installation
JES Image Manipulation - 2 - Installation
WilliamFiset
2 JES Image Manipulation - 3 - User Interface
JES Image Manipulation - 3 - User Interface
WilliamFiset
3 JES Image Manipulation - 5 - Negative
JES Image Manipulation - 5 - Negative
WilliamFiset
4 JES Image Manipulation - 6 - Black & White
JES Image Manipulation - 6 - Black & White
WilliamFiset
5 JES Image Manipulation - 4 - Grayscale
JES Image Manipulation - 4 - Grayscale
WilliamFiset
6 JES Image Manipulation - 8 - Blur
JES Image Manipulation - 8 - Blur
WilliamFiset
7 JES Image Manipulation - 7 - Edge Detection
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
8 JES Image Manipulation - 9 - Blend
JES Image Manipulation - 9 - Blend
WilliamFiset
9 JES Image Manipulation - 10 - Matte
JES Image Manipulation - 10 - Matte
WilliamFiset
10 JES Image Manipulation - 13 - Rotate90
JES Image Manipulation - 13 - Rotate90
WilliamFiset
11 JES Image Manipulation - 12 - Mirroring Picture
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
12 JES Image Manipulation - 11  - Crop Image
JES Image Manipulation - 11 - Crop Image
WilliamFiset
13 JES Image Manipulation - 14 - Stretch picture
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
14 Java Fractal Explorer [6/8]
Java Fractal Explorer [6/8]
WilliamFiset
15 Java Fractal Explorer [4/8]
Java Fractal Explorer [4/8]
WilliamFiset
16 Java Fractal Explorer [8/8]
Java Fractal Explorer [8/8]
WilliamFiset
17 Java Fractal Explorer [5/8]
Java Fractal Explorer [5/8]
WilliamFiset
18 Java Fractal Explorer [2/8]
Java Fractal Explorer [2/8]
WilliamFiset
19 Java Fractal Explorer [7/8]
Java Fractal Explorer [7/8]
WilliamFiset
20 Java Fractal Explorer [1/8]
Java Fractal Explorer [1/8]
WilliamFiset
21 Java Fractal Explorer [3/8]
Java Fractal Explorer [3/8]
WilliamFiset
22 Introduction [Programming Competition Problems]
Introduction [Programming Competition Problems]
WilliamFiset
23 String Manipulation 1 [Programming Competition Problems]
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
24 String Manipulation 2 [Programming Competition Problems]
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
25 Graph Theory 1 [Programming Competition Problems]
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
26 Logic 1 [Programming Competition Problems]
Logic 1 [Programming Competition Problems]
WilliamFiset
27 Grid Problems 1 [Programming Competition Problems]
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
28 Dynamic Programming 1 [Programming Competition Problems]
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
29 Introduction to Big-O
Introduction to Big-O
WilliamFiset
30 Dynamic and Static Arrays
Dynamic and Static Arrays
WilliamFiset
31 Dynamic Array Code
Dynamic Array Code
WilliamFiset
32 Linked Lists Introduction
Linked Lists Introduction
WilliamFiset
33 Doubly Linked List Code
Doubly Linked List Code
WilliamFiset
34 Stack Introduction
Stack Introduction
WilliamFiset
35 Stack Implementation
Stack Implementation
WilliamFiset
36 Stack Code
Stack Code
WilliamFiset
37 Queue Introduction
Queue Introduction
WilliamFiset
38 Queue Implementation
Queue Implementation
WilliamFiset
39 Queue Code
Queue Code
WilliamFiset
40 Priority Queue Introduction
Priority Queue Introduction
WilliamFiset
41 Priority Queue Min Heaps and Max Heaps
Priority Queue Min Heaps and Max Heaps
WilliamFiset
42 Priority Queue Inserting Elements
Priority Queue Inserting Elements
WilliamFiset
43 Priority Queue Removing Elements
Priority Queue Removing Elements
WilliamFiset
44 Priority Queue Code
Priority Queue Code
WilliamFiset
45 Union Find Introduction
Union Find Introduction
WilliamFiset
46 Union Find Kruskal's Algorithm
Union Find Kruskal's Algorithm
WilliamFiset
47 Union Find - Union and Find Operations
Union Find - Union and Find Operations
WilliamFiset
48 Union Find Path Compression
Union Find Path Compression
WilliamFiset
49 Union Find Code
Union Find Code
WilliamFiset
50 Binary Search Tree Introduction
Binary Search Tree Introduction
WilliamFiset
51 Binary Search Tree Insertion
Binary Search Tree Insertion
WilliamFiset
52 Binary Search Tree Removal
Binary Search Tree Removal
WilliamFiset
53 Binary Search Tree Traversals
Binary Search Tree Traversals
WilliamFiset
54 Binary Search Tree Code
Binary Search Tree Code
WilliamFiset
55 Fenwick Tree range queries
Fenwick Tree range queries
WilliamFiset
56 Fenwick Tree point updates
Fenwick Tree point updates
WilliamFiset
57 Fenwick Tree construction
Fenwick Tree construction
WilliamFiset
58 Fenwick tree source code
Fenwick tree source code
WilliamFiset
59 Hash table hash function
Hash table hash function
WilliamFiset
60 Hash table separate chaining
Hash table separate chaining
WilliamFiset

The BFS algorithm is a fundamental search algorithm used to explore nodes and edges in a graph, with a time complexity of O(V + E). It is particularly useful for finding the shortest path on an unweighted graph. The algorithm uses a queue data structure to keep track of nodes to visit next.

Key Takeaways
  1. Initialize queue with starting node
  2. Mark starting node as visited
  3. Loop until queue is empty
  4. Dequeue node and explore its neighbors
  5. Mark neighbors as visited and add to queue
  6. Reconstruct shortest path from start to end node
💡 The BFS algorithm explores nodes in a layered fashion, making it efficient for finding the shortest path on an unweighted graph.

Related Reads

Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →