Algorithms: Graph Search, DFS and BFS

HackerRank · Beginner ·⚡ Algorithms & Data Structures ·9y ago
Learn the basics of graph search and common operations; Depth First Search (DFS) and Breadth First Search (BFS). This video is a part of HackerRank's Cracking The Coding Interview Tutorial with Gayle Laakmann McDowell. http://www.hackerrank.com/domains/tutorials/cracking-the-coding-interview?utm_source=video&utm_medium=youtube&utm_campaign=ctci

What You'll Learn

Graph search algorithms including Depth First Search and Breadth First Search are demonstrated

Full Transcript

Hi, I'm Gail Lock McDow, author of Kraken Coding Interview. Today I want to talk about graphs and common operations like breath first search and depth first search. So to go back to the beginning, a graph is basically a collection of nodes where each node might point to other nodes and these these edges can be directed. So like one-way streets or undirected so kind of like two-way streets. Now suppose you want to go walk through this graph and specifically suppose you want to do something like figure out is there a path from one node to another. There are two common ways of doing this and we'll talk about both of them. The first one is a little bit more simplistic and it's called depth first search and it's a typically recursive algorithm. And the way it looks like is saying okay we h have this initial node that I'm going to call S. Now you basically are asking a question hey S do you have a path to node T? And S says hm I'm not sure. Let me go ask my children. And first S goes to node A and it says, "Hey A, do you have a path to um node T? If you do, then hey, I'm done. I can give you my answer." Um, if you don't, then let me go ask B and then C and then D. And so, but the very first person we ask is A. So A gets asked, hey, A, do you have a path to T? I'm not sure. Let me go ask my children. And eventually we might get to get to a node who says why yes of course I have a path I am T. And so then we go and sayoop boop all the way back up. Yes there's a path. And that's the basics of depth depth first search. It's called depth first search because we go deep into some node before we even ask any of the children. Now the problem with this is that we might run really really far away. So imagine for example that B actually has a path directly to node T has an edge directly to node T. A might go to all of its children, all of its children, all of its children before even get to B and T was right there. There could have been a really fast connection. So that's why we might often prefer to use breath first search instead. Breath first search says, hey, go level by level out. So, first we ask S, hey, do you have a a path to T? And S will say, well, let me check if any of my nodes are T. No, there's no edge right there. So, each of those get in line. And then we ask the second level out and then the third level out and the next level and next level, next level. And so, we go level by level breadth. We go wider before we go deep. And that's why it's called breath for search. So I want to talk at a high level about the implementation of each before I dive into some of the details. So depth for search is implemented with a recursive algorithm. It's probably the simpler algorithm to implement. The only little trick is that you have to make sure to use an isvisited flag so that you don't wind up in some sort of infinite loop where there is you know a cycle and you keep asking each note if it has you start one around running around in a circle. With breath first search, the main trick you need to remember is you want to use a Q. So when you look when you look at S, you say, "Hey, do you have a path to T, you're going to go add all of its children to the Q and rather than going recursively, you pull out the first element to from the Q, check if it has a P, you check if it is this final element, and if not, go add all of its children to it. So you use a Q so that you go go through things in the correct order. So that's a high level how it works. Let's turn to the real details of the implementation. Now, I've gotten a bit of a head start on the implementation, but I'll show you just quickly what I've done. So, first we have this uh node class here, and it it's going to have some sort of ID that represents the node ID. Uh, and what I've done is give ourselves a mapping of from node ID to the actual node. And this is mostly going to be used for things like get node and add edge. This way we can actually just go and get um get immediate access to the node with particular ID. And then I've also given us this uh has path depth first search method and it's going to call out to this recursive method. So if you remember with with depth first search we need to have a way of flagging nodes to say hey I've already visited this don't retry it. So one thing we could do is we could actually modify the node class to give ourselves an isvisited flag, but that requires then making sure we clear the that flag later on. Another way of doing it is giving ourselves a hashet uh that lists all of the IDs that I've already of the nodes that I've already visited. It's sort of a replacement for a flag. So I'm going to do it this way. This way I don't have to modify and add a whole bunch of flags in and then make sure to clear them later on. So I get the source node. I get the destination node and then I get this create this visited hash set and then I go out and call the this recursive method. So now is where the fun begins. So first if I've already visited this node if visited.contains contains this source ID then return false because there is no path then uh okay otherwise then what I want to do is so now I want to go and make sure I you know update this mark this node as visited and then I want to say okay if if I'm at my source then return if I'm at my destination rather then return true because yes there certainly does a path then otherwise go and check all my children see if any of them have a path because if there's a path from a child to me then then there's certainly a uh final then there's certainly a path from me to my destination. So for each ch node to child in source adjacent if there's a path from child to destination passing in again visited then return true and that'll bubble all the way up the stack. If I get down to the very end and I haven't found a path yet then there is no path for me to my destination. Now let's turn to how breath first search works. Okay. So, breath first search. So, what I need with breath first search is I need a link list of sort of what I'll call the like next up. So, these are the I'll call this next to visit. These are the nodes that I need to visit next. And just as before, I need this visited hash set that represents everything I've already visited. And then I want to say next to visit first thing because the first thing I need to visit is in fact my source then as long as there is nothing in this. So while visited sorry now I'll next to visit is empty. So while it's not empty, keep going. All right. So first thing, so I need to look gra my very first node to visit. So node node equals next to visit dot I'll call this remove. So remove the very first node in that that list. If this is my destination then there is certainly a path. Now I also want to do my visited checking. So if visited.contains um of node ID then actually just continue. Let's go to the next value. Uh otherwise visited.add add of node ID. So mark it as visited and then go and actually add my children. So for each node child in node.adjacent go and add each of those to my next to visit. So next to visit add of child. All right. And that's all there is to depth. uh to breath first search and then of course if I get down to the very end and I haven't found a path yet return false. Let's walk through this code again and make sure that this makes sense. So hazpath dfs takes in the source and destination ids and then I get those nodes and then I create this hash set that should be a hash set not a hashmap and then goes and actually does this recursive method. So the recursive method take has this visited thing. So we use the visited hash set instead of marking the actual nodes with a flag. So visited check if it checks if it contains a source ID. Uh if it does contain that if I've already visited this return false uh because there's no path then otherwise go and add this visited mark this node as visited re you know check check my source as my destination shows I'm already there return true. um otherwise go and search all of my children then so that and then if I haven't found a path go return false. So with BFS we are taking in a source and a destination. So here I've used nodes I'll switch this actually to be symmetric and use ids again and so I'll make that public and this private now. Okay. So I take in the source and destination ids and then I go and call this recursive method. So my recursive method or sorry this other just helper method. So this takes in the source and destination. I create this list of the next nodes I have to visit and this visited hashet that should be hashet again. Uh and this marks all the nodes that have already visited. So I say okay next one to visit is my source. Then as long as there's there's something left to visit, pull out the next node to visit. Check if I'm where I should be. If so, return true. Otherwise, check and update my visited my sort of who's who I visited. And then go and ceue up my children at the very end of the queue to be visited next. And those will and this will this will ensure that my children are not visited immediately but are visited once sort of everything in you know everything scheduled has already been visited. So it ensure it'll visit level by level by level and then if I get found the very end and I haven't found a destination after all of this then just return false. So that's how breath first search operates. In many cases when we want to find if there's actually a path breath first search is often the better approach because otherwise we could wind up searching really really far away when there's actually a very short connection and it's certainly better when we want to find the shortest path. So now that you've seen breath first and depth first search why don't you try these out on a new problem. Good luck.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from HackerRank · HackerRank · 18 of 60

1 HackerRank for Professors
HackerRank for Professors
HackerRank
2 Why CareerCup's Gayle Laakmann McDowell Loves HackerRank [Testimonial]
Why CareerCup's Gayle Laakmann McDowell Loves HackerRank [Testimonial]
HackerRank
3 How to Leverage Data-Driven Insights to Improve Recruiting Results
How to Leverage Data-Driven Insights to Improve Recruiting Results
HackerRank
4 Hire Top Linux Talent and System Admins with SudoRank [Webinar]
Hire Top Linux Talent and System Admins with SudoRank [Webinar]
HackerRank
5 How Zenefits Hired 10 Engineers in 10 Days with HackerRank's CodeSprint
How Zenefits Hired 10 Engineers in 10 Days with HackerRank's CodeSprint
HackerRank
6 Hire the hottest SQL talent with DbRank from HackerRank [Tutorial]
Hire the hottest SQL talent with DbRank from HackerRank [Tutorial]
HackerRank
7 How to Use HackerRank's CodePair Tool [Tutorial]
How to Use HackerRank's CodePair Tool [Tutorial]
HackerRank
8 Find Linux Talent with SudoRank challenges [Tutorial]
Find Linux Talent with SudoRank challenges [Tutorial]
HackerRank
9 VMware's Mat Connot details his favorite new way to engage top programmers
VMware's Mat Connot details his favorite new way to engage top programmers
HackerRank
10 An Inside Look at Coinbase's Recruiting Strategy [webinar]
An Inside Look at Coinbase's Recruiting Strategy [webinar]
HackerRank
11 How Evernote Enhanced Their Tech Recruiting Process
How Evernote Enhanced Their Tech Recruiting Process
HackerRank
12 5 strategies for being a tech recruiting champion with HackerRank [Webinar]
5 strategies for being a tech recruiting champion with HackerRank [Webinar]
HackerRank
13 HackerRank Story - Lets make the world flat!
HackerRank Story - Lets make the world flat!
HackerRank
14 Data Structures: Hash Tables
Data Structures: Hash Tables
HackerRank
15 Data Structures: Anagram Problem Solution
Data Structures: Anagram Problem Solution
HackerRank
16 Algorithms: Solve 'Lonley Integer' Using Bit Manipulation
Algorithms: Solve 'Lonley Integer' Using Bit Manipulation
HackerRank
17 Big O Notation
Big O Notation
HackerRank
Algorithms: Graph Search, DFS and BFS
Algorithms: Graph Search, DFS and BFS
HackerRank
19 Algorithms: Bit Manipulation
Algorithms: Bit Manipulation
HackerRank
20 Data Structures: Solve 'Find the Running Median' Using Heaps
Data Structures: Solve 'Find the Running Median' Using Heaps
HackerRank
21 Data Structures: Heaps
Data Structures: Heaps
HackerRank
22 Algorithms: Memoization and Dynamic Programming
Algorithms: Memoization and Dynamic Programming
HackerRank
23 Algorithms: Sort An Array with Comparator
Algorithms: Sort An Array with Comparator
HackerRank
24 Algorithms: Solve 'Coin Change' Using Memoization and DP
Algorithms: Solve 'Coin Change' Using Memoization and DP
HackerRank
25 Data Structures: Solve 'Contacts' Using Tries
Data Structures: Solve 'Contacts' Using Tries
HackerRank
26 Data Structures: Tries
Data Structures: Tries
HackerRank
27 Algorithms: Bubble Sort
Algorithms: Bubble Sort
HackerRank
28 Algorithms: Merge Sort
Algorithms: Merge Sort
HackerRank
29 Algorithms: Quicksort
Algorithms: Quicksort
HackerRank
30 Data Structures: Binary Search Tree
Data Structures: Binary Search Tree
HackerRank
31 Data Structures: Trees
Data Structures: Trees
HackerRank
32 Algorithms: Solve 'Shortest Reach' Using BFS
Algorithms: Solve 'Shortest Reach' Using BFS
HackerRank
33 Algorithms: Solve 'Connected Cells' Using DFS
Algorithms: Solve 'Connected Cells' Using DFS
HackerRank
34 Algorithms: Solve 'Recursive Staircase' Using Recursion
Algorithms: Solve 'Recursive Staircase' Using Recursion
HackerRank
35 Algorithms: Binary Search
Algorithms: Binary Search
HackerRank
36 Algorithms: Solve 'Ice Cream Parlor' Using Binary Search
Algorithms: Solve 'Ice Cream Parlor' Using Binary Search
HackerRank
37 Data Structures: Linked Lists
Data Structures: Linked Lists
HackerRank
38 Data Structures: Cycles in a Linked List
Data Structures: Cycles in a Linked List
HackerRank
39 Data Structures: Stacks and Queues
Data Structures: Stacks and Queues
HackerRank
40 Data Structures: Queue With Two Stacks
Data Structures: Queue With Two Stacks
HackerRank
41 Data Structures: Balanced Parentheses in Expression
Data Structures: Balanced Parentheses in Expression
HackerRank
42 Algorithms: Recursion
Algorithms: Recursion
HackerRank
43 How to Hire for Your Startup [Collision Panel Video]
How to Hire for Your Startup [Collision Panel Video]
HackerRank
44 HackerRank main() San Francisco | November 8, 2018
HackerRank main() San Francisco | November 8, 2018
HackerRank
45 The Enterprise Playbook for Technical Hiring
The Enterprise Playbook for Technical Hiring
HackerRank
46 Remote Hiring Colloquy with EY's Dipika Kapadia & BigBasket's Rupesh Kumar
Remote Hiring Colloquy with EY's Dipika Kapadia & BigBasket's Rupesh Kumar
HackerRank
47 Remote Hiring Colloquy with Hackerrank's Hari K, Akamai's Neha Akhouri & Infosys's Rafee Tarafdar
Remote Hiring Colloquy with Hackerrank's Hari K, Akamai's Neha Akhouri & Infosys's Rafee Tarafdar
HackerRank
48 Building a Culture for Developer Innovation
Building a Culture for Developer Innovation
HackerRank
49 Developer Trends in 2020 and Beyond
Developer Trends in 2020 and Beyond
HackerRank
50 HackerRank's Livestream: Accelerating Developer Innovation at PhonePe
HackerRank's Livestream: Accelerating Developer Innovation at PhonePe
HackerRank
51 Aadil Bandukwala converses with Rahul Chari about HackerRank's upcoming livestream
Aadil Bandukwala converses with Rahul Chari about HackerRank's upcoming livestream
HackerRank
52 Accelerating Developer Innovation at PhonePe with Rahul Chari
Accelerating Developer Innovation at PhonePe with Rahul Chari
HackerRank
53 The Future of Work for Developers
The Future of Work for Developers
HackerRank
54 Scaling AI from Proof of Concept to Production
Scaling AI from Proof of Concept to Production
HackerRank
55 The Secret Sauce to getting hired in Myntra’s Tech Team
The Secret Sauce to getting hired in Myntra’s Tech Team
HackerRank
56 Reimagining Money with Paypal
Reimagining Money with Paypal
HackerRank
57 Engineering Predictions for 2021 and Beyond! 🎬
Engineering Predictions for 2021 and Beyond! 🎬
HackerRank
58 Future-forward Tech & Careers with BNY Mellon
Future-forward Tech & Careers with BNY Mellon
HackerRank
59 How Atlassian Approaches Diversity and Inclusion with Balance & Belonging
How Atlassian Approaches Diversity and Inclusion with Balance & Belonging
HackerRank
60 Connecting Global Tech Ecosystems: Andela Shines a Light on Africa’s Developer Talent
Connecting Global Tech Ecosystems: Andela Shines a Light on Africa’s Developer Talent
HackerRank

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →