Detect Cycle in Directed Graph Algorithm

Поділитися
Вставка
  • Опубліковано 29 жов 2024

КОМЕНТАРІ • 166

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

  • @DeadJuicebox
    @DeadJuicebox 5 років тому +10

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

  • @szilagyizoltan7226
    @szilagyizoltan7226 7 років тому +109

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

  • @VIJAYKUMAR-dj6kf
    @VIJAYKUMAR-dj6kf 2 роки тому

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

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

  • @himanshusainig
    @himanshusainig 5 років тому +17

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

  • @rohishd
    @rohishd 7 років тому +4

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

  • @akshyagarg2398
    @akshyagarg2398 3 роки тому

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

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

  • @FirefoxGuy18
    @FirefoxGuy18 5 років тому +7

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

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

  • @tiagOOOOOOOliveira
    @tiagOOOOOOOliveira 7 років тому +3

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

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

  • @LindseyMysse
    @LindseyMysse 3 роки тому +3

    This was exactly what I needed. Thank you!

  • @akshayjaiswal2795
    @akshayjaiswal2795 3 роки тому

    Too good Tushar!! Simplest solution for detecting cycle in directed graphs. 👏🏻

  • @ashleycole3054
    @ashleycole3054 8 років тому +27

    Great Job Tushar!!

  • @giaonguyen5187
    @giaonguyen5187 7 років тому +2

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

  • @achintsatsangi3056
    @achintsatsangi3056 4 роки тому

    This was very helpful. Clearly explained with example, and is indeed comprehensive. Thanks @Tushar Roy!

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

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

  • @emmanuels.r9957
    @emmanuels.r9957 5 років тому

    Greetings from Peru, this video will help me a lot tomorrow in my presentation at the university

  • @wakandapanther7551
    @wakandapanther7551 2 роки тому

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

  • @hugorobson4233
    @hugorobson4233 8 років тому

    Greatest explanation on UA-cam. Thank You very much!

  • @arpitvaish89
    @arpitvaish89 5 років тому

    you make Indians proud sir.

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

  • @wenchehsu8915
    @wenchehsu8915 6 років тому +1

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

  • @tanujabharti8043
    @tanujabharti8043 4 роки тому

    Your videos clear all my doubts...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 :)

  • @ravichauhan03
    @ravichauhan03 4 роки тому

    Nicely explained Tushar!

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

  • @atulrawat9588
    @atulrawat9588 5 років тому +2

    Best video

  • @SUJITDAS-bx2zs
    @SUJITDAS-bx2zs 3 роки тому

    Best explanation.

  • @abhasarya3253
    @abhasarya3253 7 років тому

    Very lucid explanation Tushar!

  • @edtrecuay
    @edtrecuay 6 років тому

    Thank you Tushar, you are the man!

  • @aahpandasrun
    @aahpandasrun 3 роки тому

    your handwriting is amazing!

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

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

  • @ppsanyal
    @ppsanyal 5 років тому

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

  • @mohitejaikumar
    @mohitejaikumar Рік тому

    Nice 👍

  • @Ronakrktanna
    @Ronakrktanna 8 років тому

    You, my friend, are GOD.

  • @100bands
    @100bands 4 роки тому

    Still the best in 2020!!!

  • @vishalvatsalya1439
    @vishalvatsalya1439 8 років тому

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

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

  • @anandkulkarni2111
    @anandkulkarni2111 7 років тому

    Excellent presentation :) Loved it!!

  • @cswalker21
    @cswalker21 5 років тому

    Thanks Tushar, great explanation.

  • @imyashdeepsharma
    @imyashdeepsharma 8 років тому

    VERY CLEAR AND SIMPLE EXPLANATION :)

  • @MinhLe-xk5rm
    @MinhLe-xk5rm 6 років тому

    Awesome tutorial! Thank you so much!!!

  • @lkez2
    @lkez2 8 років тому +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?

  • @donotreportmebro
    @donotreportmebro 9 місяців тому

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

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

  • @basavarajsunkad4630
    @basavarajsunkad4630 7 років тому

    Very good explanation. Thank you

  • @JohanSurya_BlueFlame93
    @JohanSurya_BlueFlame93 8 років тому

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

  • @ThatShibaInu
    @ThatShibaInu 8 років тому +1

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

  • @IzanBF
    @IzanBF 7 років тому

    Great video, well explained

  • @tebohomokoena8483
    @tebohomokoena8483 6 років тому

    Yoh wow. That's so simple. Perfect tutorial

  • @ruoxuan666
    @ruoxuan666 5 років тому

    Thank you. This video is great!

  • @guilhas98fcp
    @guilhas98fcp 6 років тому

    Very good example and well explained

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

  • @trickpsv
    @trickpsv 8 років тому

    Real nice video! Thank you!

  • @shettyanvita
    @shettyanvita 5 років тому +1

    thank u for the videos! 2019 fall!

  • @lordozb
    @lordozb 7 років тому +1

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

  • @krishnakmishra
    @krishnakmishra 4 роки тому

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

  • @sukritkapil2562
    @sukritkapil2562 4 роки тому

    nice explanation!! thanks.

  • @高航-o3x
    @高航-o3x 8 років тому

    thanks for you sharing

  • @srinivaspochincharla4098
    @srinivaspochincharla4098 8 років тому

    Excellent explanation !!

  • @captainjackrana
    @captainjackrana 8 років тому

    wonderful explanation

  • @muskanroxx22
    @muskanroxx22 3 роки тому

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

  • @xFrostByte7
    @xFrostByte7 8 років тому

    Thank you so much for this video!!

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

  • @chirag8627
    @chirag8627 4 роки тому

    You are awesome!

  • @chesslover6781
    @chesslover6781 3 роки тому +2

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

  • @melohu1790
    @melohu1790 8 років тому

    you are one of the best

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

  • @yuesun1578
    @yuesun1578 8 років тому

    Great job

  • @rudolphous
    @rudolphous 8 років тому

    Thanks for your video!

  • @Stella-se1lg
    @Stella-se1lg 4 роки тому

    Great explanation :)

  • @PratikShah123
    @PratikShah123 4 роки тому

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

  • @dixitparkhiya9316
    @dixitparkhiya9316 3 роки тому

    AMAZING!

  • @s.subedi
    @s.subedi 8 років тому

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

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

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

  • @dailycompetitivecoding8565
    @dailycompetitivecoding8565 4 роки тому

    sir please enable join option we want to support you . Thankyou

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

  • @ankushgupta630
    @ankushgupta630 4 роки тому

    The OG.

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

  • @SantoshKumar-bu2qr
    @SantoshKumar-bu2qr 5 років тому

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

  • @AjazAnsari-zl9yp
    @AjazAnsari-zl9yp 7 років тому

    really amazing

  • @duartemelao
    @duartemelao 3 роки тому

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

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

  • @rishabhsrivastava9734
    @rishabhsrivastava9734 4 роки тому

    Thanks a lot!

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

  • @karandugar4741
    @karandugar4741 5 років тому

    we can use kosarajus method also?

  • @SanmoyRay
    @SanmoyRay 7 років тому +1

    great.. thanks a lot..

  • @mysterytech9324
    @mysterytech9324 4 роки тому

    Great!

  • @goutamkreddy
    @goutamkreddy 5 років тому

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