Detect Cycle in Directed Graph Algorithm

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ต.ค. 2024

ความคิดเห็น • 166

  • @szilagyizoltan7226
    @szilagyizoltan7226 7 ปีที่แล้ว +109

    Hope TH-cam gives a lot of money for your videos because they are priceless:D

  • @DeadJuicebox
    @DeadJuicebox 5 ปีที่แล้ว +10

    This is one of the best videos of explaining concepts on TH-cam, no joke.

  • @sankalparora9261
    @sankalparora9261 4 ปีที่แล้ว +1

    This is the best video on this topic. If anybody is reading this. You don't need to search for more!!

  • @VIJAYKUMAR-dj6kf
    @VIJAYKUMAR-dj6kf 2 ปีที่แล้ว

    old is gold here
    there are lot of new TH-camrs 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

  • @masonliu4207
    @masonliu4207 8 ปีที่แล้ว +1

    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.

  • @rohishd
    @rohishd 7 ปีที่แล้ว +4

    Couldn't find a better explanation on web than this one. Awesome job Tushar!

  • @jenspeter437
    @jenspeter437 7 ปีที่แล้ว +11

    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.

  • @himanshusainig
    @himanshusainig 5 ปีที่แล้ว +17

    for every tedious problem, i read through multiple things and final destination always turns out to be your videos. :D

  • @jazzochannel
    @jazzochannel 4 ปีที่แล้ว +1

    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 7 ปีที่แล้ว +3

    Congrats, Roy.
    What a good job. The best explanation I've seen so far!

  • @giaonguyen5187
    @giaonguyen5187 7 ปีที่แล้ว +2

    The best explaining video I've found in the internet !

  • @akshyagarg2398
    @akshyagarg2398 3 ปีที่แล้ว

    seriously man you are the only youtuber who tells the intuition. Other youtubers just dry run the code of gfg.

  • @LindseyMysse
    @LindseyMysse 3 ปีที่แล้ว +3

    This was exactly what I needed. Thank you!

  • @FirefoxGuy18
    @FirefoxGuy18 5 ปีที่แล้ว +7

    Thank you this was useful . We can even use topological sort done by calculating in-degree to find a cycle

  • @ngc35ster
    @ngc35ster 3 ปีที่แล้ว

    Thanks Tushar, this by far the most clear explanation for this algorithm!!

  • @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.

  • @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 ปีที่แล้ว +27

    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.

  • @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 TH-cam. Thank You very much!

  • @wakandapanther7551
    @wakandapanther7551 2 ปีที่แล้ว

    Thank you so very much!!! I finally understand the course schedule leetcode problem because of this video!!

  • @arpitvaish89
    @arpitvaish89 5 ปีที่แล้ว

    you make Indians proud sir.

  • @tanujabharti8043
    @tanujabharti8043 4 ปีที่แล้ว

    Your videos clear all my doubts...thank you

  • @ravichauhan03
    @ravichauhan03 4 ปีที่แล้ว

    Nicely explained Tushar!

  • @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.... !!!

  • @wenchehsu8915
    @wenchehsu8915 6 ปีที่แล้ว +1

    Amazing! Thank you for your effort, clean and clear tutorial!

  • @easifier
    @easifier 2 ปีที่แล้ว

    thank you for this clear explanation with passing all steps with details
    it was very helpful :)

  • @trucduong1076
    @trucduong1076 ปีที่แล้ว

    very nice explanation. Thank you!

  • @CaratacusAD
    @CaratacusAD 4 ปีที่แล้ว

    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 :)

  • @srikanth26mar
    @srikanth26mar 6 ปีที่แล้ว

    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.

  • @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.

  • @abhasarya3253
    @abhasarya3253 6 ปีที่แล้ว

    Very lucid explanation Tushar!

  • @aahpandasrun
    @aahpandasrun 3 ปีที่แล้ว

    your handwriting is amazing!

  • @idiot7leon
    @idiot7leon 4 ปีที่แล้ว +1

    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?

  • @ppsanyal
    @ppsanyal 5 ปีที่แล้ว

    The DFS approach is not discussed in code, the code accompanying the explanation is a Union Find.

  • @Official-tk3nc
    @Official-tk3nc 4 ปีที่แล้ว +1

    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 :)

  • @cswalker21
    @cswalker21 5 ปีที่แล้ว

    Thanks Tushar, great explanation.

  • @lkez2
    @lkez2 7 ปีที่แล้ว +3

    You are borderline amazing

    • @suspiciousbird487
      @suspiciousbird487 6 ปีที่แล้ว +3

      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?

  • @anandkulkarni2111
    @anandkulkarni2111 7 ปีที่แล้ว

    Excellent presentation :) Loved it!!

  • @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

  • @edtrecuay
    @edtrecuay 6 ปีที่แล้ว

    Thank you Tushar, you are the man!

  • @imyashdeepsharma
    @imyashdeepsharma 8 ปีที่แล้ว

    VERY CLEAR AND SIMPLE EXPLANATION :)

  • @krishnakmishra
    @krishnakmishra 4 ปีที่แล้ว

    cheers this helped me to understand it in much simpler way.

  • @basavarajsunkad4630
    @basavarajsunkad4630 7 ปีที่แล้ว

    Very good explanation. Thank you

  • @SUJITDAS-bx2zs
    @SUJITDAS-bx2zs 3 ปีที่แล้ว

    Best explanation.

  • @Ronakrktanna
    @Ronakrktanna 8 ปีที่แล้ว

    You, my friend, are GOD.

  • @guilhas98fcp
    @guilhas98fcp 6 ปีที่แล้ว

    Very good example and well explained

  • @MinhLe-xk5rm
    @MinhLe-xk5rm 6 ปีที่แล้ว

    Awesome tutorial! Thank you so much!!!

  • @IzanBF
    @IzanBF 7 ปีที่แล้ว

    Great video, well explained

  • @ruoxuan666
    @ruoxuan666 5 ปีที่แล้ว

    Thank you. This video is great!

  • @vishalvatsalya1439
    @vishalvatsalya1439 8 ปีที่แล้ว

    u r doing a great job tushar!...keep going like this...it will be a great benefit for us...! :D

  • @shettyanvita
    @shettyanvita 5 ปีที่แล้ว +1

    thank u for the videos! 2019 fall!

  • @s.subedi
    @s.subedi 8 ปีที่แล้ว

    Which program are you using to display sets and graph in gui? Thanks

  • @twistertee
    @twistertee 6 ปีที่แล้ว +2

    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.

  • @tebohomokoena8483
    @tebohomokoena8483 6 ปีที่แล้ว

    Yoh wow. That's so simple. Perfect tutorial

  • @lordozb
    @lordozb 7 ปีที่แล้ว +1

    Why don't your use adjacency list or matrix to represent graph ?

  • @sukritkapil2562
    @sukritkapil2562 4 ปีที่แล้ว

    nice explanation!! thanks.

  • @donotreportmebro
    @donotreportmebro 9 หลายเดือนก่อน

    I like a simplified version where you use just two collections: seen, visiting

  • @srinivaspochincharla4098
    @srinivaspochincharla4098 8 ปีที่แล้ว

    Excellent explanation !!

  • @TheAlDuude
    @TheAlDuude 8 ปีที่แล้ว +1

    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.

  • @100bands
    @100bands 4 ปีที่แล้ว

    Still the best in 2020!!!

  • @captainjackrana
    @captainjackrana 8 ปีที่แล้ว

    wonderful explanation

  • @ThatShibaInu
    @ThatShibaInu 8 ปีที่แล้ว +1

    Hey Tushar, would you mind doing a video on heavy-light tree decomposition?

  • @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).

  • @muskanroxx22
    @muskanroxx22 3 ปีที่แล้ว

    this looks very similar to using topological sorting to detect cycle, what's the difference?

  • @chesslover6781
    @chesslover6781 3 ปีที่แล้ว +2

    Why aren’t my professors like him?
    * Even though after taking our money*

  • @blasttrash
    @blasttrash 3 ปีที่แล้ว

    why introduce the concept of black, white and gray? why not just call them visited, visiting, unvisited sets or something like that?

  • @trickpsv
    @trickpsv 8 ปีที่แล้ว

    Real nice video! Thank you!

  • @JohanSurya_BlueFlame93
    @JohanSurya_BlueFlame93 8 ปีที่แล้ว

    Thanks..i try to implement this white/gray/black set for detecting the cycle and it works..

  • @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?

  • @Stella-se1lg
    @Stella-se1lg 4 ปีที่แล้ว

    Great explanation :)

  • @melohu1790
    @melohu1790 8 ปีที่แล้ว

    you are one of the best

  • @高航-o3x
    @高航-o3x 8 ปีที่แล้ว

    thanks for you sharing

  • @SaurabhPatel
    @SaurabhPatel 8 ปีที่แล้ว

    Tushar, I have not that much knowledge about graphs. Here I would like to ask one question : Only two sets is not enough to track of visited or unvisited vertices ?
    www.geeksforgeeks.org/detect-cycle-in-a-graph/ like this?

  • @11m0
    @11m0 8 ปีที่แล้ว +1

    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.

  • @9ShivamSharma
    @9ShivamSharma 7 ปีที่แล้ว +1

    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 .

  • @rudolphous
    @rudolphous 7 ปีที่แล้ว

    Thanks for your video!

  • @PratikShah123
    @PratikShah123 4 ปีที่แล้ว

    Thanks for this nice explanation. By the way what's the name of this Algorithm ?

  • @jialiao6328
    @jialiao6328 8 ปีที่แล้ว

    Hello, I would like to know if it is possible to download the package .graph? Cuz some the Vertex type is not recognized .
    Thanks

  • @Gudsfralsare
    @Gudsfralsare 7 ปีที่แล้ว +3

    Can this also be used to find the largest cycle in a directed graph?

    • @philippheller9439
      @philippheller9439 7 ปีที่แล้ว +2

      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?

  • @atulrawat9588
    @atulrawat9588 5 ปีที่แล้ว +2

    Best video

  • @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!

  • @mohitejaikumar
    @mohitejaikumar ปีที่แล้ว

    Nice 👍

  • @duartemelao
    @duartemelao 3 ปีที่แล้ว

    What´s the O() complexity of this algorithm?

  • @rafamassa
    @rafamassa 8 ปีที่แล้ว

    great lecture!

  • @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.

  • @SantoshKumar-bu2qr
    @SantoshKumar-bu2qr 5 ปีที่แล้ว

    Umm isn't just finding visited node twice using dfs will work?

  • @yuesun1578
    @yuesun1578 8 ปีที่แล้ว

    Great job

  • @karandugar4741
    @karandugar4741 5 ปีที่แล้ว

    we can use kosarajus method also?

  • @gunjansingh5206
    @gunjansingh5206 6 ปีที่แล้ว +2

    Heyy Tushar can you please explain this question using dfs and a recursion stack ??
    It would be of quite help...

  • @chirag8627
    @chirag8627 4 ปีที่แล้ว

    You are awesome!

  • @goutamkreddy
    @goutamkreddy 5 ปีที่แล้ว

    can we just print the grey set to get the cycle?

  • @flukalpes
    @flukalpes 7 ปีที่แล้ว +1

    How to find all cycles using dfs?

  • @rajat5356
    @rajat5356 6 ปีที่แล้ว

    why can't this be done as same as in undirected graph using parent reference and visited set.

  • @vishnuvardhannayakam2991
    @vishnuvardhannayakam2991 5 ปีที่แล้ว

    if graph contains duplicates it is not possiblle right.
    we are not able to put them in set even , if we put them in set we lost one node

    • @DubstepRS
      @DubstepRS 5 ปีที่แล้ว

      If you were given input as each node containing their own adjacency list then you could tell that two nodes with the same value are actually different nodes. Otherwise it wouldn't make sense to have the input as a list of edges anyways.

  • @dixitparkhiya9316
    @dixitparkhiya9316 3 ปีที่แล้ว

    AMAZING!

  • @AjazAnsari-zl9yp
    @AjazAnsari-zl9yp 7 ปีที่แล้ว

    really amazing