Тёмный

Detect Cycle in Directed Graph Algorithm 

Tushar Roy - Coding Made Simple
Подписаться 244 тыс.
Просмотров 194 тыс.
50% 1

/ tusharroy25
github.com/mis...
github.com/mis...
www.educative....
Detect cycle in directed graph using depth first search.

Опубликовано:

 

16 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 166   
@szilagyizoltan7226
@szilagyizoltan7226 7 лет назад
Hope RU-vid gives a lot of money for your videos because they are priceless:D
@DeadJuicebox
@DeadJuicebox 5 лет назад
This is one of the best videos of explaining concepts on RU-vid, no joke.
@sankalparora9261
@sankalparora9261 4 года назад
This is the best video on this topic. If anybody is reading this. You don't need to search for more!!
@rohishd
@rohishd 7 лет назад
Couldn't find a better explanation on web than this one. Awesome job Tushar!
@masonliu4207
@masonliu4207 7 лет назад
I spent two hrs just search online try to understand this kinda problem until I saw your video. Thank you so much. U are the only one I saw that really want to explain in detail to us.
@himanshusainig
@himanshusainig 5 лет назад
for every tedious problem, i read through multiple things and final destination always turns out to be your videos. :D
@debayondharchowdhury2680
@debayondharchowdhury2680 4 года назад
strangely, this is true for me also.
@hansenhe7993
@hansenhe7993 4 года назад
I don't think this is very strange, though.
@jenspeter437
@jenspeter437 7 лет назад
I want to thank you for the effort you've put in these videos. I've had a really bad teacher on this topic, but everything makes sense when you explain it.
@VIJAYKUMAR-dj6kf
@VIJAYKUMAR-dj6kf Год назад
old is gold here there are lot of new RU-vidrs just giving solutions for the qouestions where this guy taught the very basic algo of those solution in simplest way a long ago thank you buddy
@akshyagarg2398
@akshyagarg2398 3 года назад
seriously man you are the only youtuber who tells the intuition. Other youtubers just dry run the code of gfg.
@jazzochannel
@jazzochannel 4 года назад
i spent 1/3 today at work figuring out how to detect cycles in a statemachine. In the end I decided to represent the states and events (transitions) as a graph. My final solution was close to yours but I operate with one stack for explored nodes and if the the neighbor is in there i have a cycle. I think maybe your solution is better because you don't have redundand explorations (ever), but my solution might have redundant traversals. It's not significant for me because the tree only contains 6-40 nodes, but for bigger trees your solution is absolutely better! excellent video :) ty!
@tiagOOOOOOOliveira
@tiagOOOOOOOliveira 6 лет назад
Congrats, Roy. What a good job. The best explanation I've seen so far!
@giaonguyen5187
@giaonguyen5187 7 лет назад
The best explaining video I've found in the internet !
@LindseyMysse
@LindseyMysse 3 года назад
This was exactly what I needed. Thank you!
@FirefoxGuy18
@FirefoxGuy18 5 лет назад
Thank you this was useful . We can even use topological sort done by calculating in-degree to find a cycle
@kmathews934
@kmathews934 4 года назад
Thanks so much for this! I was stuck on a problem for weeks and was out of ideas. This helped me think it through from a different perspective and figure it out. I really appreciate your time and effort in putting these tutorials online.
@ngc35ster
@ngc35ster 2 года назад
Thanks Tushar, this by far the most clear explanation for this algorithm!!
@akshayjaiswal2795
@akshayjaiswal2795 3 года назад
Too good Tushar!! Simplest solution for detecting cycle in directed graphs. 👏🏻
@achintsatsangi3056
@achintsatsangi3056 4 года назад
This was very helpful. Clearly explained with example, and is indeed comprehensive. Thanks @Tushar Roy!
@ashleycole3054
@ashleycole3054 8 лет назад
Great Job Tushar!!
@bharat_arora
@bharat_arora 3 года назад
This code is perfect example of good code quality. At the same time code make sure the system is in consistent state , most of the other youtube videos will leave the color and visited array in inconsistent state.
@ShubhamSingh-ku2ow
@ShubhamSingh-ku2ow 8 лет назад
Awsum Explaination!! Spent hours reading the theory... BUT HERE, problems solved in minutes..... Thanks alot Tushar.. Keep posting... All the best.... !!!
@wakandapanther7551
@wakandapanther7551 2 года назад
Thank you so very much!!! I finally understand the course schedule leetcode problem because of this video!!
@emmanuels.r9957
@emmanuels.r9957 5 лет назад
Greetings from Peru, this video will help me a lot tomorrow in my presentation at the university
@hugorobson4233
@hugorobson4233 7 лет назад
Greatest explanation on RU-vid. Thank You very much!
@CaratacusAD
@CaratacusAD 3 года назад
Many thanks for this excellent video. It's by far the best and clearest explanation and demonstration of this problem. The theory from this helped me write in C the functions i needed for my data set. You're a star :)
@idiot7leon
@idiot7leon 4 года назад
Thanks very much! I leave my questions here before find out answers: 1. if there is no cycle, can I use this approach to print out the elements in topological order? How? 2. if there is a cycle, can I use this approach to print out the cycle? How? 3. if there are more than one cycle, can I use this approach to print out all cycles? How?
@wenchehsu8915
@wenchehsu8915 6 лет назад
Amazing! Thank you for your effort, clean and clear tutorial!
@trucduong1076
@trucduong1076 11 месяцев назад
very nice explanation. Thank you!
@arpitvaish89
@arpitvaish89 5 лет назад
you make Indians proud sir.
@srikanth26mar
@srikanth26mar 5 лет назад
If we look at your other programs, they use a set called visited and sometimes a stack ( the child nodes which have been processed/explored). I feel instead of introducing new terminologies here, you could use " visited" = grey set, "explored" = black set, with one key difference which is "visited" is wiped clean each time dfs() is called. This will main continuity. And white set is of course all vertices.
@SUJITDAS-bx2zs
@SUJITDAS-bx2zs 3 года назад
Best explanation.
@atulrawat9588
@atulrawat9588 5 лет назад
Best video
@ravichauhan03
@ravichauhan03 4 года назад
Nicely explained Tushar!
@tanujabharti8043
@tanujabharti8043 4 года назад
Your videos clear all my doubts...thank you
@easifier
@easifier 2 года назад
thank you for this clear explanation with passing all steps with details it was very helpful :)
@Hav0c1000
@Hav0c1000 4 года назад
White set is not necessary. If you have the graph, just DFS from every node as long as it is not already in the black set.
@mohitejaikumar
@mohitejaikumar Год назад
Nice 👍
@abhasarya3253
@abhasarya3253 6 лет назад
Very lucid explanation Tushar!
@100bands
@100bands 4 года назад
Still the best in 2020!!!
@Official-tk3nc
@Official-tk3nc 4 года назад
If you are watching this in lockdown believe me you are one of the rarest species on the earth who are willing to acheive something in life. Many students are wasting their time in watching netflix, webseries, playing games, watching movies, ludo, chatting , etc. NITj student here :)
@edtrecuay
@edtrecuay 6 лет назад
Thank you Tushar, you are the man!
@59sharmanalin
@59sharmanalin 3 года назад
There is much confusion for directed and undirected graph cycle detection, the code should be same in which we check for a back edge, expect for the fact that Directed graph also detects a back edge bw only two node cycle for this we can simply check neighbour is already visited or not but for Undirected we need to check back edge which ensures there are at least three nodes in a cycle. However what Tushar is explaining here is case for multiple graphs(as in disconnected graphs given as one graph).. This video need not be for Directed G alone, it's for both Directed and Undirected G. Please point out any mistake if you find so!! Thanks Cheers
@chirag8627
@chirag8627 4 года назад
You are awesome!
@ppsanyal
@ppsanyal 4 года назад
The DFS approach is not discussed in code, the code accompanying the explanation is a Union Find.
@xxbighotshotxx
@xxbighotshotxx 5 лет назад
Awesome solution. After looking at solutions to this problem on Leetcode, one of the solutions that a user came up with was to use Kahn's algorithm to detect the cycle. You can also find the cycle this way too. It might take a couple more for loops than the solution that you share here though
@sukritkapil2562
@sukritkapil2562 4 года назад
nice explanation!! thanks.
@donotreportmebro
@donotreportmebro 8 месяцев назад
I like a simplified version where you use just two collections: seen, visiting
@lkez2
@lkez2 7 лет назад
You are borderline amazing
@suspiciousbird487
@suspiciousbird487 6 лет назад
Sometimes I wish I was given the great compliment of being "borderline amazing". But the only thing I get called is "borderline".
@alexstewart6535
@alexstewart6535 5 лет назад
@@suspiciousbird487 What are you talking about Lydia?
@vishalvatsalya1439
@vishalvatsalya1439 8 лет назад
u r doing a great job tushar!...keep going like this...it will be a great benefit for us...! :D
@anandkulkarni2111
@anandkulkarni2111 7 лет назад
Excellent presentation :) Loved it!!
@cswalker21
@cswalker21 5 лет назад
Thanks Tushar, great explanation.
@dixitparkhiya9316
@dixitparkhiya9316 3 года назад
AMAZING!
@IzanBF
@IzanBF 7 лет назад
Great video, well explained
@imyashdeepsharma
@imyashdeepsharma 8 лет назад
VERY CLEAR AND SIMPLE EXPLANATION :)
@Ronakrktanna
@Ronakrktanna 8 лет назад
You, my friend, are GOD.
@krishnakmishra
@krishnakmishra 4 года назад
cheers this helped me to understand it in much simpler way.
@basavarajsunkad4630
@basavarajsunkad4630 7 лет назад
Very good explanation. Thank you
@chesslover6781
@chesslover6781 3 года назад
Why aren’t my professors like him? * Even though after taking our money*
@MinhLe-xk5rm
@MinhLe-xk5rm 6 лет назад
Awesome tutorial! Thank you so much!!!
@ruoxuan666
@ruoxuan666 5 лет назад
Thank you. This video is great!
@shettyanvita
@shettyanvita 5 лет назад
thank u for the videos! 2019 fall!
@tebohomokoena8483
@tebohomokoena8483 6 лет назад
Yoh wow. That's so simple. Perfect tutorial
@aahpandasrun
@aahpandasrun 3 года назад
your handwriting is amazing!
@guilhas98fcp
@guilhas98fcp 6 лет назад
Very good example and well explained
@高航-o3x
@高航-o3x 8 лет назад
thanks for you sharing
@trickpsv
@trickpsv 8 лет назад
Real nice video! Thank you!
@srinivaspochincharla4098
@srinivaspochincharla4098 7 лет назад
Excellent explanation !!
@mysterytech9324
@mysterytech9324 4 года назад
Great!
@xFrostByte7
@xFrostByte7 7 лет назад
Thank you so much for this video!!
@yucentao5624
@yucentao5624 4 года назад
The time complexity will be O(N), where N is the number of nodes in the graph. Space complexity will be O(N).
@JohanSurya_BlueFlame93
@JohanSurya_BlueFlame93 7 лет назад
Thanks..i try to implement this white/gray/black set for detecting the cycle and it works..
@captainjackrana
@captainjackrana 8 лет назад
wonderful explanation
@melohu1790
@melohu1790 7 лет назад
you are one of the best
@Stella-se1lg
@Stella-se1lg 4 года назад
Great explanation :)
@rudolphous
@rudolphous 7 лет назад
Thanks for your video!
@yuesun1578
@yuesun1578 8 лет назад
Great job
@lordozb
@lordozb 7 лет назад
Why don't your use adjacency list or matrix to represent graph ?
@twistertee
@twistertee 6 лет назад
Where can I find the code for printing the path? The DFS parent map looks like it was only for illustrative purposes.
@59sharmanalin
@59sharmanalin 3 года назад
Just return true in function if you find a back edge and return true all the back and read if function returned true keep collected parent into a collection.
@ThatShibaInu
@ThatShibaInu 8 лет назад
Hey Tushar, would you mind doing a video on heavy-light tree decomposition?
@secondIncomePartTime
@secondIncomePartTime 2 года назад
waah ldou
@rafamassa
@rafamassa 8 лет назад
great lecture!
@Manas047
@Manas047 7 лет назад
Why not use an enumeration with three states like Unvisited, Visiting and Visited and add a reference of this enumeration on the graph-node object itself. Might be a little more efficient in terms of not using extra space for the three collections. Great explanation though!
@TheAlDuude
@TheAlDuude 8 лет назад
Instead of returning T/F, is there a way to return the verticies in the cycle? (such as a tuple of (4,5,6) ?
@TheAlDuude
@TheAlDuude 8 лет назад
thank you Tushar, I realized. Great video.
@SanmoyRay
@SanmoyRay 7 лет назад
great.. thanks a lot..
@9ShivamSharma
@9ShivamSharma 7 лет назад
But if we deleted the edge of the cycle and it was also the part of some other cycle we will never be able to give the right answer . Deleting an edge can go wrong .
@pavanbitsgoa
@pavanbitsgoa 8 лет назад
Love this!
@AjazAnsari-zl9yp
@AjazAnsari-zl9yp 7 лет назад
really amazing
@muskanroxx22
@muskanroxx22 3 года назад
this looks very similar to using topological sorting to detect cycle, what's the difference?
@11m0
@11m0 8 лет назад
The 2 algorithms for directed and undirected graphs seem so similar, and I cant seem to find the differences
@cswalker21
@cswalker21 4 года назад
For directed, it's only considered a neighbor if it is an outgoing edge.
@PratikShah123
@PratikShah123 4 года назад
Thanks for this nice explanation. By the way what's the name of this Algorithm ?
@rishabhsrivastava9734
@rishabhsrivastava9734 4 года назад
Thanks a lot!
@mohdshoaib4350
@mohdshoaib4350 7 лет назад
Very helpful tutorial ... many thanks...The code takes in a Graph as an input , can i find the code/class for that graph ADT?
@apoorvwatsky
@apoorvwatsky 4 года назад
this is a rather complicated solution, atleast for me. Why not just keep track of visited nodes and backtrack when we are nowwhere to go at some node? It's much easier.
@ankushgupta630
@ankushgupta630 3 года назад
The OG.
@aaryankatoch5466
@aaryankatoch5466 3 года назад
haha lol he is a real OG 100%
@gunjansingh5206
@gunjansingh5206 6 лет назад
Heyy Tushar can you please explain this question using dfs and a recursion stack ?? It would be of quite help...
@blasttrash
@blasttrash 2 года назад
why introduce the concept of black, white and gray? why not just call them visited, visiting, unvisited sets or something like that?
@imajt5
@imajt5 3 года назад
thank you
@dailycompetitivecoding8565
@dailycompetitivecoding8565 4 года назад
sir please enable join option we want to support you . Thankyou
@ankush363
@ankush363 4 года назад
respect!!!
@Gudsfralsare
@Gudsfralsare 7 лет назад
Can this also be used to find the largest cycle in a directed graph?
@philippheller9439
@philippheller9439 7 лет назад
of course. just continue if you found a cycle, note all cycles and if every node is visited then you just compare the lengths between them
@gladyouseen8160
@gladyouseen8160 2 года назад
@@philippheller9439 can we use same algorithm for cycle in undirected graph?
Далее
Detect cycle in a directed graph
7:47
Просмотров 141 тыс.
Каха и жена (недопонимание)
00:37
ГОЧА ПРО NISSAN 400Z
00:51
Просмотров 35 тыс.
Cycle Detection
11:02
Просмотров 19 тыс.
System Design Introduction For Interview.
27:23
Просмотров 559 тыс.
Breadth First Search (BFS): Visualized and Explained
10:41
How Fast can Python Parse 1 Billion Rows of Data?
16:31
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36