Detect cycle in an undirected graph | GeeksforGeeks

Sdílet
Vložit
  • čas přidán 9. 07. 2024
  • Explanation for the article: www.geeksforgeeks.org/detect-c...
    This video is contributed by Illuminati.

Komentáře • 42

  • @ionuttrif8378
    @ionuttrif8378 Před 3 lety

    I made a function that detect if there is a cycle in a graph using bfs, but i don't know how to make it print the members of the cycle, any tips?

  • @BrianKeeganMusic
    @BrianKeeganMusic Před 6 lety +11

    Honestly the best video I have seen on this so far. Other videos did not explain the parent node.

  • @ishitamittal1546
    @ishitamittal1546 Před 3 lety +1

    Does this code work for non continuous graph?

  • @nitishgupta6802
    @nitishgupta6802 Před 5 lety +5

    Isn't it sufficient to check that if the number of edges in the connected graph is more than n-1 (where n is the number of vertices), then it must have a cycle.

    • @evapine2795
      @evapine2795 Před 4 lety

      no

    • @brkekr
      @brkekr Před 3 lety +2

      @Ash Win Yes I totally agree with you, but in the case that E>n-1, that would be a smart move to place an if to check whether it has more than n-1 edges at the very beginning of the function isCyclicUtil.

    • @tarekadel7631
      @tarekadel7631 Před 2 lety

      This approach makes assumptions that may not hold true:
      1) The graph is connected --> But it could have more than 1 connected component
      2) Edges >= Vertices

  • @mohitsoni4925
    @mohitsoni4925 Před 5 měsíci

    It will be better if we also explain why the statement is sufficient enough to justify a cycle in graph. This will help us to not memorise the statement but visualise it.

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

    Sir , aap nain kamaaal kr dyaa........... What a Explanation.....!

  • @parthgupta5058
    @parthgupta5058 Před 4 lety

    wouldnt it just stuck in an infinite loop if there is an edge with u v equal

  • @greerhanshaw
    @greerhanshaw Před 7 lety +4

    Thanks for the great video!

  • @kmishy
    @kmishy Před 2 lety

    3:00 Should you change it to- For any visited vertex

  • @parishapranayraj9978
    @parishapranayraj9978 Před 6 lety +2

    can you please put the proof of correctness of the algorithm .

  • @MushafAli-rc2su
    @MushafAli-rc2su Před rokem

    A great explanation thankfull to you❤

  • @mohammedelhag7565
    @mohammedelhag7565 Před rokem

    thank you, appreciate the hard work

  • @coderbuddy3875
    @coderbuddy3875 Před 3 lety

    Nice explanation 👍

  • @shubhammalhotra7244
    @shubhammalhotra7244 Před rokem

    For every visited vertex v, if there is an adjacent u. Such that u is already visited & u is not the parent of v. So, cycle in the graph is present/

  • @anirudhreddybasani3555
    @anirudhreddybasani3555 Před 5 lety +5

    Then why didn't we used the same logic for directed graphs..there we've used a recursion stack and here we're just using a parent variable...

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

      Anirudh reddy Basani consider the following directed graph and do a dry run: (1,2),(2,3),(1,3)
      Note: there is no directed edge in undirected graph, so tracking of parent is enough which is not the case in directed graphs.

    • @Aditya-pl3xg
      @Aditya-pl3xg Před 2 lety

      you can all you need to do is run a dfs for each vertex in a loop and that's it. actually this way will cover for case of both the undirected graph (since it'll exit in first iteration itself) and for non-connected graphs too.
      (but don't tell GfG of this common sense they will write a shitty code full of classes and OOPS on their website that'll confuse everyone 😁)

  • @mujtabaamer6542
    @mujtabaamer6542 Před 5 lety

    What is the output

    • @ela2316
      @ela2316 Před 3 měsíci

      true if there's a cycle otherwise false

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

    Wrong! After backtracking from 1 to 0. 0 will again check its each connected node if its visited or not. since 1 is visited and not parent it will return true.

    • @sumanthchakravarthi6853
      @sumanthchakravarthi6853 Před 3 lety

      why would you check for 1 again it will move to 2 right?

    • @brkekr
      @brkekr Před 3 lety

      Well it does not; because in isCyclicUtil, the first if checks if there is any UNVISITED node, if yes, it recurs from there without evaluating the else if (else if checks if there is a visited children, what you said would be true in case of else-if and if statements swapping places )

  • @xuantungnguyen9719
    @xuantungnguyen9719 Před 2 lety

    your code failed in this case
    g = Graph(3)
    g.addEdge(1, 2)
    g.addEdge(2, 3)
    g.addEdge(3, 1)

  • @vaibhavimacherla1603
    @vaibhavimacherla1603 Před 6 lety

    it checks only for triangles???

    • @LawZist
      @LawZist Před 6 lety

      i would like also to know

    • @KjellBoeije
      @KjellBoeije Před 6 lety +2

      It detects a cycle whenever a node has a child node that's already visited, and is not it’s parent. If I'm correct this would work for cycles with any number of nodes.

    • @GalibFida
      @GalibFida Před 5 lety

      the cycles in this graph are triangles but they don't have to be cycle of three vertices; a cycle can have any number of vertices and this algorithm will still work

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

      No
      it will check for any figure if it forms a cycle.

    • @hamidansari9331
      @hamidansari9331 Před 4 lety

      @@akhilvegunta6263 you r late

  • @jdleanne
    @jdleanne Před 4 lety

    This is wrong. He is indeed using BFS not DFS, as it was traversing around the neighbor

    • @kasyapdharanikota8570
      @kasyapdharanikota8570 Před 2 lety

      i also felt that he is using bfs instead of DFS

    • @dylankrejci9965
      @dylankrejci9965 Před 10 měsíci

      @@kasyapdharanikota8570 this graph is kinda weird in that the way its laid out, BFS and DFS are essentially the same. It's DFS in the sense that each node visited follows directly from its parent, but the organization of this particular graph also means that you are in a sense checking the parent and then all of its children in order. The best way to distinguish from within the context of this video is the fact that the parent of node 4 is 3, whereas in BFS it would be 2. Hope this helps

    • @adnanhaque1871
      @adnanhaque1871 Před měsícem

      ​@@dylankrejci9965 well BFS uses a queue FIFO while DFS uses stack LIFO. so not the same!

  • @sjrohan4042
    @sjrohan4042 Před rokem

    bahut loud video hai🤣