Detect Cycle in Undirected Graph | Using DFS | Cycle detection in Graph | DSA-One Course #77

Sdílet
Vložit
  • čas přidán 17. 03. 2022
  • Hey guys, In this video, We're going to solve an important problem in Graphs called: Detect cycle in an Undirected Graph
    Practice here: practice.geeksforgeeks.org/pr...
    💸 Use coupon code ANUJBHAIYA on GeeksforGeeks to avail discounts on courses!
    🥳 Join our Telegram Community:
    Telegram channel: telegram.me/realanujbhaiya
    Telegram group: telegram.me/dsa_one
    🚀 Follow me on:
    Instagram: / anuj.kumar.sharma
    Linkedin: / sharma-kumar-anuj
    Twitter: / realanujbhaiya
    📚 Complete DSA Playlist: • DSA-One Course - The C...
    Complete Android Development Playlist: • Android Development Tu...
    Hashtags:
    #anujbhaiya #dsaone
    Tags:
    detect cycle in undirected graph
    detect cycle in an undirected graph
    detect cycle in a undirected graph
    cycle detection in undirected graph
    anuj bhaiya
    cycle detection in graph
    detect cycle in graph
    cycle in undirected graph
    detect cycle in a directed graph
    cycle in graph
    graph data structure
    graph dsa
    undirected graph
    cycle detection
    anuj bhaiya java
    cycle detection in directed graph
    cycle in a graph
    dsa one
    cycle detection graph
    graph in java
    graph playlist
    graphs dsa
    cycle detection dfs
    cycle detection striver
    dfs
    dfs graph
    dfs in graph
    dfs traversal
    diameter of binary tree
    dsa
    find cycle in graph
    finding cycle in a graph
    floyd cycle detection
    graph coding
    graph cycle detection
    graph in data structure
    graph in dsa
    java anuj bhaiya

Komentáře • 45

  • @aishwaryshukla8880
    @aishwaryshukla8880 Před rokem +14

    The way you do dry run after explaining the code, showing the white board side by side to the code editor is amazing! I don't know how did you get that idea but please know that it truly ensures that we have understood the code completely and easily. Please continue the good work and I hope you get whatever you want!

  • @JRK_RIDES
    @JRK_RIDES Před 2 lety +10

    That DRY run at the end was very useful, would've been a little confused otherwise

  • @saniyapathan8968
    @saniyapathan8968 Před 16 dny +1

    Best Explanation ever!

  • @tech_wizard9315
    @tech_wizard9315 Před 2 lety +16

    Please make a 60day roadmap for DSA beginner to crack tech giant's like Microsoft linkedin etc companies in 60days

  • @SauravNrai
    @SauravNrai Před 2 lety +1

    You are amazing... Thank you so much

  • @akankshasrivastava3354
    @akankshasrivastava3354 Před 2 lety +2

    I like your dedication sir🙌

  • @shrutitibile5473
    @shrutitibile5473 Před 2 lety

    Sir all concept ke video bhejo dsa ke bahot help ho rhe hai isase

  • @sandeepdhiman9887
    @sandeepdhiman9887 Před 8 měsíci +1

    very well explained

  • @patelvivekshirinbhai5241

    greate expaination

  • @saritaprasad4295
    @saritaprasad4295 Před rokem

    very useful

  • @harishlodha8794
    @harishlodha8794 Před 2 lety

    Sir background video recorder app se record Kiya hua video app developer ke pass bhi store hota hai kya

  • @dibyendudgp10
    @dibyendudgp10 Před rokem

    I was thinking, union Find bhi to use kar sakte hai isme ? Ya koi edge case hai, jo unionFind cover nahi karega is case mei ? Uska complexity bhi to isse kam hi ana chahiye

  • @Ravi-moq-6589
    @Ravi-moq-6589 Před 5 měsíci

    The best❤

  • @bhushanambhore8378
    @bhushanambhore8378 Před 2 lety +7

    Sir one doubt, where are you updating the parent variable of every vertex in your code? Initially, you set -1 as a parent of 0th vertex but after traversing the graph parent variable is supposed to be updated every time after visiting vertexes.

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

      It's updated in the dfs function call when we pass V as parent argument

    • @piyushanand3451
      @piyushanand3451 Před 2 lety +2

      Same doubt.. please explain if you got it cleared.

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

      when he recursively calls dfs, he passes the current node as the parent to the function.

  • @piyushagarwal8458
    @piyushagarwal8458 Před 2 lety +2

    Guru Ji, Pranam !!

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

    nice

  • @myinspiration4642
    @myinspiration4642 Před rokem +3

    Hi Anuj, in DFS how we are maintaining parent node, Please explain

  • @govindgupta290
    @govindgupta290 Před rokem +1

    exact same is not working for me

  • @mrrishiraj88
    @mrrishiraj88 Před 2 lety +1

    🙏🙏

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

    Happy Teacher's day Bhaiya ❤️

  • @anchalsoni5892
    @anchalsoni5892 Před rokem +1

    bhaiya smjh toh aa gya tha pr saare testcases pass nhi kiye

  • @comp_131_takshpatel9
    @comp_131_takshpatel9 Před rokem

    Aapako bhaiya video me at run time coding karana chahiye , kyuki it is better

  • @abdulmajidshaikh300
    @abdulmajidshaikh300 Před 2 lety +1

    bhaiya code full nehi dekha jarahey , please when show code u can full display

    • @sayitavii
      @sayitavii Před 2 lety

      search it on geeks for geeks you'll get the code almost the same

  • @l11_ayushkumar80
    @l11_ayushkumar80 Před rokem

    sir code ke waqt word wrap kar diya karo naa thoda asaan ho jaata hai

  • @_sf_editz1870
    @_sf_editz1870 Před 6 měsíci +1

    bhayya ke solution ke saamne kuch bol sakthe he kya

  • @Rebelx27
    @Rebelx27 Před 2 lety +1

    Bhaiya mujhe blockchain developer ban sakta hu kya ?

    • @suar_pilla
      @suar_pilla Před 6 měsíci

      aap kya ban paoge Bhaiya kaise bolega bhaya? Chu**** thik banega

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

    Bhai aapne jo code likha hai... usme I think "if(neighbor!=parent)" condition "if(!vis[neighbor])" condition se pahle hoga.... kiuki mai ye changes kiya then only mera 210/210 test cases run kar raha hai... Bhai can you check?

  • @avinashsharma6286
    @avinashsharma6286 Před 2 lety

    always give the code

  • @Akaash449
    @Akaash449 Před 2 lety

    Kya Bhaiya aapka last name hai?

  • @newtanagmukhopadhyay4716
    @newtanagmukhopadhyay4716 Před 6 měsíci

    class Solution {
    public:
    // Function to detect cycle in an undirected graph.
    bool ans = false;
    void dfs(int node, int parent, vector&visited, vectoradj[]){
    visited[node] = true;
    for(int neighbour : adj[node]){
    if(neighbour == parent)
    continue;
    if(visited[neighbour]==true){
    ans = true;
    return;
    }
    dfs(neighbour, node, visited,adj);
    }
    return;
    }
    bool isCycle(int V, vector adj[]) {
    vectorvisited(V, false);
    for(int i=0; i

  • @suar_pilla
    @suar_pilla Před 6 měsíci

    public boolean hasCycleUnDirected(T src) {
    return performDFSCycleDetection(src);
    }
    public boolean performDFSCycleDetection(T src) {

    // Key as Vertex, Value as its Parent
    Map visited = new HashMap();
    Stack stack = new Stack();
    visited.put(src, null);
    stack.add(src);
    while (!stack.isEmpty()) {
    T pop = stack.pop();
    List neighbors = adjList.getOrDefault(pop, Collections.emptyList());
    for (T neighbor : neighbors) {
    // if vertex is not visited
    if (!visited.containsKey(neighbor)) {
    visited.put(neighbor, pop);
    stack.add(neighbor);
    } else { // when vertex is already visited,
    //check who introduced the current element
    T vertexIntroducedBy = visited.get(pop); // parent of the current element
    // if parent is a neighbor or null (in case of starting position)
    // then its fine else there is a cycle.
    // in the case of A B, A's parent is null,
    // but B's parent is A. so when B's neighbors are being evaluated,
    // A is supposed to come as A is B's neighbor,
    // but it will not be a cycle as B as introduced by A
    if (vertexIntroducedBy == neighbor || vertexIntroducedBy == null)
    continue;
    else
    return true;
    }
    }
    }
    return false;
    }