Cycle in Undirected Graph Graph Algorithm

Sdílet
Vložit
  • čas přidán 30. 07. 2015
  • Cycle in undirected graph using DFS and disjoint sets.
    / tusharroy25
    github.com/mission-peace/inte...
    github.com/mission-peace/inte...

Komentáře • 84

  • @fatihsonmez
    @fatihsonmez Před 6 lety +116

    "yes, i contain a cycle."
    -that graph

  • @vaibskinikar
    @vaibskinikar Před 8 lety +4

    Great job! Thanks for taking out time and making all of these videos. These are really helpful and very well communicated.

  • @XeCloudeX
    @XeCloudeX Před 7 lety

    Thanks a lot for your videos Tushar. Have a Merry Christmas!

  • @pawankoranga9332
    @pawankoranga9332 Před 4 lety

    your way of explaining concepts is amazing

  • @shritishaw7510
    @shritishaw7510 Před 2 lety

    that smile right at the start has made my day. Beautifully explained. Thank you

  • @fredsladkey904
    @fredsladkey904 Před 7 lety

    Great clear and brief explanation. Thank you.

  • @kapilsharma4434
    @kapilsharma4434 Před 4 lety +31

    5:23 where DFS starts

  • @mclovinf852
    @mclovinf852 Před 8 lety +1

    Very good! I love your explanation.

  • @AnandPandeyHere
    @AnandPandeyHere Před 4 lety +3

    Your videos are really helpful. Keep uploading more such videos on graphs.

  • @aprasad865
    @aprasad865 Před 4 lety

    Great Job Tushar . Very clear and crisp

  • @ganeshkhirwadkar4127
    @ganeshkhirwadkar4127 Před 8 lety

    Appreciable. Simple and understandable teaching.

  • @DivyanshuShekhar
    @DivyanshuShekhar Před 8 lety

    Impressive...!!
    So well communicated that the concept looks sooo simple and clear.
    Thanks Man.

    • @DivyanshuShekhar
      @DivyanshuShekhar Před 8 lety

      +Divyanshu Shekhar Now I have subscribed to your channel.
      I wish I could have done it earlier.

  • @darciogmail
    @darciogmail Před 8 lety

    Great job! Thanks for sharing!

  • @jeevanraajan3238
    @jeevanraajan3238 Před 4 lety

    I owe Tushar my Job !! Awesome.Thanks

  • @ARYANRAJ-cb4gf
    @ARYANRAJ-cb4gf Před 4 lety

    your videos help me a lot, thanks for your great contribution.

  • @KUSHUTRIVEDI
    @KUSHUTRIVEDI Před 9 lety

    Thank you sir for posting this video..!

  • @vyshnavramesh9305
    @vyshnavramesh9305 Před 4 lety +11

    7:48 to 8:10 summary of dfs approach to detect cycle in an undirected graph

  • @AndrewMelnychuk0seen
    @AndrewMelnychuk0seen Před 3 lety

    This is excellent, thank you!

  • @shando_tube
    @shando_tube Před 5 lety +2

    Is it possible to implement this algorithm with BFS as well?

  • @dheerajpatni9810
    @dheerajpatni9810 Před 9 lety

    Hi Tushar,Thanks for the video.
    I wanted to ask if you know any algorithm other than DFS to find the longest cycle in undirected graph? I dont want to use DFS as it is very slow for bigger graph.

  • @nilanjanroy8023
    @nilanjanroy8023 Před 7 lety

    Hi Tushar,
    Thanks for the great video ! Can i get the nodes which are participating in the cycle from this algorithm ? .what i feel is I have to run union find for every node...pls share your thoughts

  • @202Aziz
    @202Aziz Před 8 lety

    great explaining
    thank you

  • @lkez2
    @lkez2 Před 7 lety

    Hey Tushar, is it possible to also use the DFS method for directed graphs here? Since Undirected graphs are just a special case of directed graph which edges in both directions.

  • @OsafMalik
    @OsafMalik Před 8 lety +8

    i cant understand how the time complexity turn out to be O(V) when find() and union() operations have time complexity of O(log(V)) and number of both of these operations are O(V) in the worst case.

    • @TheBugWhisperer1
      @TheBugWhisperer1 Před 6 lety +5

      Find() and Union() is not log(n) , it is O(α (n)). In fact it grows so slowly, that it doesn't exceed 4 for all reasonable n (approximately n

  • @202Aziz
    @202Aziz Před 8 lety

    Do you have the Pseudo-Code for Detecting cycle in Uncorrected graph in BFS algorithm or DFS algorithm and the running time O( m + n )

  • @user-jl2tz3rq9f
    @user-jl2tz3rq9f Před 5 lety

    Very good explanation!Even fool can understand

  • @kamalsmusic
    @kamalsmusic Před 8 lety +5

    Can you make a video about cycles in directed graphs? I think directed is a bit more complex and it'd be nice to see a video on that

    • @SriramGopalGoli030792
      @SriramGopalGoli030792 Před 5 lety

      Shouldn't the same algo work for directed graphs as well?

    • @obinnaubah9045
      @obinnaubah9045 Před 5 lety

      @@SriramGopalGoli030792 I believe it should.

    • @YogeshDarji99
      @YogeshDarji99 Před 5 lety

      @@SriramGopalGoli030792 This algo is considering all the edges, I believe in directed graphs, you will have to keep track of correct representative because of directions, this algo would require slight modification

    • @SriramGopalGoli030792
      @SriramGopalGoli030792 Před 5 lety

      This algo does not work for directed graphs. A simple example below.
      1 -> [2,3]
      2 -> [3]
      3 -> []

  • @ZiadxKabakibi
    @ZiadxKabakibi Před 9 lety

    thank you very much,can you explain about dijikstra's algorithm please

  • @sidharthanrajendran1387

    Plain and simple:)

  • @kartikv3254
    @kartikv3254 Před 8 lety

    Will this work if we have single node loop??

  • @Official-tk3nc
    @Official-tk3nc Před 4 lety +8

    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.

  • @lpatrasco
    @lpatrasco Před 5 lety

    Great Weedeo!

  • @Ronakrktanna
    @Ronakrktanna Před 8 lety +3

    As the first step of detecting a cycle in an undirected graph, why can't we just say that a cycle exists in the graph if the number of edges in the graph are V or above? Since it's an undirected graph, if the number of edges are >=V, wouldn't that result in a cycle?
    If the number of edges < V, a cycle is still possible and then we could go for the Union Find algorithm. Correct me if I'm wrong.

    • @shando_tube
      @shando_tube Před 5 lety +1

      I had the same thought.

    • @shashankprashar1724
      @shashankprashar1724 Před 5 lety

      @@shando_tube yes tht is true but tht doesnt mean that less than V wud give no cycle coz may be all nodes mgt not be connected and others might be connected giving cycle in a sub graph formed ex: build graph from edges 0 1 2 3 3 4 4 2 5 nodes 4 edges still cycle exist.

  • @talaviss123
    @talaviss123 Před 7 lety

    Thanks Tushar. well explained. Do I have always to use disjoint set to solve this problem?

    • @chamanjhinga3946
      @chamanjhinga3946 Před 7 lety

      Disjoint set is one way to detect cycle. DFS and topological sort are other ways to detect cycle.

  • @ShreyanGoswami
    @ShreyanGoswami Před 4 lety

    Thanks for the video. It really helped. The concept of disjoint sets make it easy to find a cycle in the graph. I do have one question though. I have seen different ways that are used to represent the graph. In particular the adjacency list technique. In that if there is an edge between node 0 and node 1 then the adjacency list will have a pointer from node 0 to node 1 as well as from node 1 to node 0. If we apply the algorithm to such a ds it will say that there is a cycle. Even in the matrix representation form I see this as an issue. I am trying to figure out if there is a way to resolve this.

    • @GurpreetSingh-nt2os
      @GurpreetSingh-nt2os Před 3 lety

      You can check his GitHub link in the description section.he had implemented an amazing class for representation of graph .
      Hope it helps

  • @pusarlaaishwarya5035
    @pusarlaaishwarya5035 Před 4 lety

    Sir...which books are preferred to enhance more knowledge on this concepts

  • @11m0
    @11m0 Před 8 lety

    09:22 Where is the class DisjointSet defined? Is this a standard java class? Or was it created somewhere?

    • @achilles1.0
      @achilles1.0 Před 7 lety

      He created it separately. There's also a video on it where he explains the code, you might want to check that out. github.com/mission-peace/interview/blob/master/src/com/interview/graph/DisjointSet.java

  • @namansingh8648
    @namansingh8648 Před 4 lety

    Can we use white gray black method in undirected graph too???

  • @varun__1553
    @varun__1553 Před 4 lety +1

    how you are going to apply topological sort on undirected graph for finding cycle..0:30 ???

    • @abhilakshsharma1275
      @abhilakshsharma1275 Před 4 lety

      yeah I also was thinking the same. I guess he mistakenly have said that since topological sorting is applicable only to Directed Acyclic Graph (DAG).

  • @mousagang2477
    @mousagang2477 Před 2 lety

    can I implement the first algorithm with union-find by size and path compression and say that the time complexity is O(n * log_star(n) ) where n is the number of edges ??

  • @kamalsmusic
    @kamalsmusic Před 8 lety

    In the code that you posted for finding a cycle using DFS, you have 2 methods, hasCycleDFS() and hasCycleDFSUtil(). Why do you have to initiate hasCycleDFSUtil() on every vertex in the graph? If we just pick any vertex and run hasCycleDFSUtil(), surely that is enough to figure out if there is a cycle or not?

    • @kamalsmusic
      @kamalsmusic Před 8 lety

      Ah ok. So in that case, since you have to execute multiple depth first searches, would the runtime be O(|V|(|E|+|V|))?
      And so, if the graph is connected, starting at any one vertex is sufficient, right?

    • @kamalsmusic
      @kamalsmusic Před 8 lety

      Wait a second, isnt the runtime still O(|V|+|E|)? We still explore every vertex only once since we keep track of the ones we've visited early.. I think I analyzed it wrong. Can you confirm that the runtime is O(|E|+|V|)?

  • @shando_tube
    @shando_tube Před 5 lety +1

    Perhaps this sounds naive, but couldn't we just count the number of edges in our undirected graph? If length(E) > |V|-1, then not only is the graph connected, but a cycle also exists.

    • @ultra_max_pro
      @ultra_max_pro Před 5 lety

      This is clearly not true. Consider the graph with two connected components, one is an isolated vertex, and the other is k_4, (complete graph with 4 vertices, 6 edges). Then the inequality you mention holds, but the graph is not connected. Also, if the graph has, say 100 isolated vertices and one cycle with 3 vertices, the equality does not hold, but the graph has a cycle. Basically, you don't know about how the graph is connected.

    • @shando_tube
      @shando_tube Před 5 lety +2

      @@ultra_max_pro yea u know what man, I'm just happy I passed the class

  • @pritamsarkar3371
    @pritamsarkar3371 Před 3 lety

    is not "for(Edge edge: graph.getAllEdges())" taking O(E) times ?

  • @SharifKhanitexpart
    @SharifKhanitexpart Před 7 lety

    sir, which mathematics are require for algoritm designer

  • @vishwanath501
    @vishwanath501 Před 9 lety

    nice explanation!!!!!!
    but how to find all cycles that exist in graph....

  • @vivekr5321
    @vivekr5321 Před 7 lety

    Hi, Tushar. I have a question in your DFS explanation. You said you're passing the parent so that you know you don't have to go in that direction again. This makes sense, but what if there is another edge from the parent to the same child? In this case, this is a genuine loop. In the diagram below, A-B is itself a genuine loop, so if you pass parent A while exploring B, you will not detect it.
    Eg:
    ____
    | |
    A ---- B ----- C
    | | |
    F E------D

    • @treyquattro
      @treyquattro Před 6 lety

      Eventually the DFS will return to the second edge A-B and if the B-C-D-E cycle hadn't been detected, the A-B-A would be. B would be already in the visited set.

    • @MrCnareshkumar
      @MrCnareshkumar Před 6 lety

      In a directed graph, there exists one path at the maximum from one node to other

  • @arijitjash9375
    @arijitjash9375 Před 6 lety

    if u uploaded the next lesson for me sir....plz

  • @surendrapalSingh
    @surendrapalSingh Před 7 lety

    Hi Tushar,
    Nice explanation. With some help from google I came up with following code to detect cycle in undirected graph using modified DFS. Though I have conceptual clarity but I am failing to understand what does 'else if (temp->dest != parent)' do (marked with ## in code below)?
    bool hasCycleDFSUtil(int v, bool visited[], int parent, struct Graph* graph)
    {
    visited[v] = true;
    struct AdjListNode* temp=graph->array[v].head;
    while(temp!=NULL)
    {
    if(visited[temp->dest] == false)
    {
    if(hasCycleDFSUtil(temp->dest, visited, v, graph))
    return true;
    }
    else if (temp->dest != parent) // ##
    return true;
    temp=temp->next;
    }
    return false;
    }
    Please help.

    • @puneetkumar-vv9go
      @puneetkumar-vv9go Před 2 lety

      because in the adjacent list of V node, there should be not any other node visited except its parent(because v derived from its parent). if it is , then there is cycle

  • @anshulmishra5633
    @anshulmishra5633 Před 7 lety

    Nice work big man... u convey tricky concepts with ease and clarity.. thnx alot ;)

  • @parasmishra5461
    @parasmishra5461 Před 8 lety

    pls do a video of directed graphs also thnx in advance

  • @Pariharyash
    @Pariharyash Před 7 lety

    You evaluated time complexity, have you considered operation findset's and union's time ?

  • @sasipotu9833
    @sasipotu9833 Před 8 lety

    Hi tushar i need pseudo code for dfs for find in cycle in undirected graph

  • @gauraonandekar5820
    @gauraonandekar5820 Před 3 lety

    Graaaafffffffff

  • @ashirbadbehera5544
    @ashirbadbehera5544 Před 4 lety

    thaak about....

  • @ankushvirmani9039
    @ankushvirmani9039 Před 7 lety

    in ur video there is problem in loading

  • @dibyendubrinto3051
    @dibyendubrinto3051 Před 8 lety

    all of your code are in java which are difficult to understand for beginner. if you translate this into c++ then it will be easy