Find Bridges in a graph using Tarjans Algorithm | Cut Edge

Sdílet
Vložit
  • čas přidán 15. 09. 2020
  • This video explains what is a bridge along with its application and how to find all the bridges in a graph using tarjans algorithm.I have first explained the concept of bridges and then showed the observations needed to understand the algorithm.I have shown all the required conditions using simple examples.I have also shown the dry run explanation for finding bridges.At the end of the video, I have also shown the code for this algorithm.CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
    ========================================================================
    Join this channel to get access to perks:
    / @techdose4u
    INSTAGRAM : / surya.pratap.k
    SUPPORT OUR WORK: / techdose
    LinkedIn: / surya-pratap-kahar-47b...
    WEBSITE: techdose.co.in/
    TELEGRAM Channel LINK: t.me/codewithTECHDOSE
    TELEGRAM Group LINK: t.me/joinchat/SRVOIxWR4sRIVv5...
    =======================================================================
    CODE LINK: gist.github.com/SuryaPratapK/...
    USEFUL LINKS:-
    Tarjans strongly connected components algorithm: • Tarjans strongly conne...
    Find Articulation Points using Tarjans Algorithm: • Find Articulation Poin...
    Codeforces: codeforces.com/blog/entry/71146

Komentáře • 113

  • @saurabhkale4495
    @saurabhkale4495 Před 3 lety +13

    all 3 videos of tarjans algo are perfectly explained!!Thanks . You made them simple.

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

      I am glad that you found it helpful 😃

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

    Such a good explanation.This videos made me saw your entire series..

  • @serendipity_lol
    @serendipity_lol Před 2 lety

    Awesome detailed explanation! Thank you so much!! :D

  • @shourabhpayal1198
    @shourabhpayal1198 Před 3 lety +10

    All three videos of tarjan (cut edge, cut vertex and scc) are top quality.

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

    Another top level video! Love your channel!

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

    Initially at the SCC I struggled to understand that concept, once that is done... Next both the videos are literally easy and simple bcz, u have teached that much level 😅 in SCC. Thank you sir for delivering these algorithms.

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

    Hands down the best source for graphs. Thanks a lot

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

    This is the best explanation I have seen on this topic.

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

    Great Video thanks a lot for making these things simple.

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

    Best explanation ever seen! Great work ,huge respect!!

  • @syedkaja2045
    @syedkaja2045 Před 3 lety +11

    google company deserves you 😀

    • @techdose4u
      @techdose4u  Před 3 lety +22

      Company deserves good students. Some should be teachers too 😁

    • @anonymous-sk2pr
      @anonymous-sk2pr Před 2 lety +2

      @@techdose4u now you are in Google
      😀😀😀 Congrats

    • @creativeakk4919
      @creativeakk4919 Před 2 lety

      Now he is in Google 🔥🔥

  • @starc701
    @starc701 Před 3 lety +3

    Having knowledge and delivering knowledge are two different things which can be proved by Tech Dose . People are out there who have tons of programming skills but they dont have the way to deliver that.

  • @VikasSingh-ur3io
    @VikasSingh-ur3io Před rokem

    NICE Explaination.

  • @nisaacdz
    @nisaacdz Před rokem

    Muchas Gracias

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

    Very nice explanation. Can you please clarify this..
    After giving low[] values for all nodes, then by checking each edge(u,v) in graph if low[u]!==low[v] it is a bridge.

  • @naveenverma3683
    @naveenverma3683 Před 3 lety

    In APs video the condition for a node to not have a back edge and be an AP was disc[u]

  • @danym-98
    @danym-98 Před 2 lety

    Best explanation

  • @vaibhavjain4710
    @vaibhavjain4710 Před 3 lety

    You are a genius.

  • @KrishnaKumar-yn9wf
    @KrishnaKumar-yn9wf Před 3 lety

    Very well done

  • @manishkumar-uw5mw
    @manishkumar-uw5mw Před 3 měsíci

    Amazing 😍

  • @kingaakashgoswami7735
    @kingaakashgoswami7735 Před 3 lety +6

    sir please its my humble request to make video on " 78. Subset problem of leetcode" .please sir.
    Big fan of urs ..thankyou

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

    Good Workkkk!!

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

    A great Teacher

  • @anujgangwar3245
    @anujgangwar3245 Před 2 lety

    Awesome

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

    Do this works for directed graph too?

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

    you are champion sir....

  • @chitrabasukhare2998
    @chitrabasukhare2998 Před 3 lety

    @TECH DOSE, Thanks, it was a very good tutorial. There is an issue i found in your code. Your code gives the incorrect ans for below test
    adj[1].pb(3);
    adj[1].pb(4);
    adj[2].pb(1);
    adj[3].pb(2);
    adj[4].pb(2);
    adj[4].pb(3);
    Here there should be no bridge, but your algo returns one bridge, 1->4. Could, i be missing something ?

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

    Excellent explaination

  • @rishabhnitc
    @rishabhnitc Před 2 lety

    very nice

  • @Nikhil-qd9up
    @Nikhil-qd9up Před 3 lety +2

    great Content🔥

  • @dysonfilmstw4764
    @dysonfilmstw4764 Před 3 lety

    Can someone please explain, what exactly is low[ ]. Am not able to figure it out?

  • @Nikhil-qd9up
    @Nikhil-qd9up Před 3 lety +3

    can we also go for this algo
    1.for each src vertex find if there is an cycle(cycle detection in undirected graph) that reaches itsef.
    2.if(true) then it is not a part of bridge
    else all vertex connected to src vertex will be a bridge.

    • @ManishKumar-no2ek
      @ManishKumar-no2ek Před 3 lety +1

      Even I have the same doubt.
      I think all the edges that does not lie in a cycle are the only bridges. Please correct us if we are wrong.

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

    Thanks for the video! Do we really need parents array? Can it be replaced by an int param that identifies who is this vertex's parent?

    • @techdose4u
      @techdose4u  Před 3 lety

      Try it :)

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

      @@techdose4u I havent tried that yet, but my java version of your code passing all cases on leetcode.
      Do you have a java version please?
      Mine fails the case with 1000 inputs and 0 expected, but I am returning these: [[880,975],[618,86],[256,52],[214,104],[718,543],[591,412]]
      Do you see anything wrong with it please?
      class Solution {
      // trajans
      Map adj = new HashMap();
      List toReturn = new ArrayList();
      int time = 0;
      public List criticalConnections(int n, List connections) {
      int[] discoverys = new int[n];
      int[] lows = new int[n];
      int[] parents = new int[n];
      // init adj garph
      for (int i = 0; i < n; i++){
      adj.put(i, new ArrayList());
      discoverys[i] = -1;
      lows[i] = -1;
      parents[i] = -1;
      }
      for (List edge : connections){
      adj.get(edge.get(0)).add(edge.get(1));
      adj.get(edge.get(1)).add(edge.get(0));
      }
      // for (int i = 0; i < n; i++){
      // if (discoverys[i] == -1){
      // dfs(discoverys, lows, parents, i);
      // }
      // }
      dfs(discoverys, lows, parents, 0);
      return toReturn;
      }
      // returns the low value at edge
      // parents array is not relaly needed
      private void dfs(int[] discoverys, int[] lows, int[] parents, int edge){
      discoverys[edge] = time;
      lows[edge] = time;
      time++;
      for (int neighbour: adj.get(edge)){
      // not visited yet
      if (discoverys[neighbour] == -1){
      // set this edge as neighbours parent
      parents[neighbour] = edge;
      // dont dfs on nodes already visiting/visited
      if (discoverys[neighbour] == -1){
      dfs(discoverys, lows, parents, neighbour);
      }
      // its a regular tree edge, therefore its not explored yet
      lows[edge] = Math.min(lows[edge], lows[neighbour]);
      // possibly update the list of bridges
      // neighbour has greated discovery # than our vertex, means this is a bridge
      if (lows[neighbour] > lows[edge]){
      List toAdd = Arrays.asList(edge, neighbour);
      toReturn.add(toAdd);
      }
      } else if (neighbour != parents[edge] ){ // neighbour is already visited, its a backedge
      // dont do calculation on backedge
      lows[edge] = Math.min(lows[edge], discoverys[neighbour]);
      }
      }
      }

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

      @@allen724 Your implementation is wrong. The condition to add the bridge is low[neighbour] > disc[edge] but you have written low[neighbour] > low[edge].

  • @jaskaransingh0304
    @jaskaransingh0304 Před rokem

    Should time be declared outside function? Because it gets back to 0 every DFS call

  • @rishikeshpawar3230
    @rishikeshpawar3230 Před 3 lety

    While dfs, if we found any visited node, then we update the low value of current node as minimum betwn low of current and discovery time of visited node. I think it could be good if we compare betwn low values of both nodes (current and visited) and assign minimum from them. Plz reply me if I am wrong..Caz I found correct results so in dry run

    • @sudhanshugupta8931
      @sudhanshugupta8931 Před 3 lety

      I think the values of discovery time and low value for the ancestor will be the same since the ancestor is the start node in the strongly connected component. Therefore, I think both would give a correct result.

  • @yashwantraghuwanshi777

    big fan

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

    nice video!! only one question, if v has been visited before, why is low[u] = min(low[u], disc[v]). why is it not low[u] = min(low[u][, low[v])?

    • @techdose4u
      @techdose4u  Před 2 lety

      Coz only 1 back edge is taken and not multiple back edges.

  • @LeoLeo-nx5gi
    @LeoLeo-nx5gi Před 3 lety +2

    Sir are you also going to cover *network flows* ? btw thanx a lot for the videos :)

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

      Umm....I will just put one more video for Eulerian path and circuit. After that I will solve leetcode questions.

    • @shakif3460
      @shakif3460 Před 3 lety

      @@techdose4u Yes sir, that is the good decision u thought. thanks a lot sir...

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

    Why are we not differentiating between back edge and cross edge in the case of Articulation Points and finding bridges?

    • @akashparihar515
      @akashparihar515 Před 2 lety

      Because in case of Undirected graph cross edge doesn't exist

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

    i just have one question, why are we checking parent when we do not check in Tarjan's Algorithm ?

  • @VineetGandham
    @VineetGandham Před 3 lety

    why isn't it low[v]>low[u], that should also work right? But it doesnt i guess. Can someone explain pls

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

    Best always

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

    Great Great 😀

  • @sebantichandra9318
    @sebantichandra9318 Před 3 lety +5

    Great video!! But why the condition is (low[v] > disc[u]) in case of Bridge but (low[v] >= disc[u]) in case of Articulation Point?

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

      because back-edge can't be bridge-edge.

    • @vikasupadhyay7916
      @vikasupadhyay7916 Před 2 lety

      because, low[v]==disc[u] means that there is a backedge from "child subgraph of u"(i.e. either v or node ahead) to the node u itself. So, in articulation point, we are removing nodes, therefore all the associated edges (wheter backedge to itself or simple tree edge to subgraph) are removed, resulting in disconnected graph.
      Whereas, in edge cut, we are not removing node (i.e. removing tree edge only), so if there is low([v]==dis[u], a backedge to u is present. So removing tree edge doesn't disconnect the graph (as it is still connected using backedge to u).
      Hope it helps!!!!!!!!

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

    Hello Surya - I left you a comment on the GItHub repo, would appreciate if you could look at it - Thank you for all that you are doing for us!

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

      I am really not getting time for any replies currently otherwise I would have 😅

    • @QDem19
      @QDem19 Před 3 lety

      @@techdose4u No worries, please get to it if and when you can!

  • @sharinganuser1539
    @sharinganuser1539 Před 3 lety

    here is bit confusion that i want to state . This is in regard to a node that's already been visited and is neighbour of our current node and NOT PARENT . THE CODE SAYS FOR THIS -> lowtime[curNode]=min(lowtime[curNode] , desctime[neighbourNode]) . Now suppose the neighbour node is connected to start node so its lowtime=0 and desctime some +ve val . so shouldn't our current nodes low time be 0 rather then some +ve val less then its current low time . // lowtime[curNode]=min(lowtime[curNode] ,lowtime[neighbourNode])

  • @princeanthony8525
    @princeanthony8525 Před 2 lety

    Tarjans is arguably the toughest concept in DSA.

  • @adnanniazi9954
    @adnanniazi9954 Před 2 lety

    Sir reply please
    To remove an articulation point from a graph we need to ?
    A: remove an edge
    B: add an edge
    C: both a and b
    D: none of the above

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

    why we have taken low[v]>disc[u] condition check instead of low[v]>=disc[u] as discussed in articulation points video

    • @techdose4u
      @techdose4u  Před 3 lety

      Because in a bridge, we have a line which has two ends U and V, whereas an articulation point has only 1 point.

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

      because in articulation point when we remove the point we remove all of its adjacent edges so even if low[v]==disc[u] then its valid , here however if low[v]==disc[u] , that means even if we remove current edge , everythings gonna be connected by that back edge , hope i am making sense :)

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

    Sir please make a video on 1192. Critical Connections in a Network. I am preparing for amazon interview.

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

      I will do it.

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

      Finding bridges is same as critical connections in a network man

    • @techdose4u
      @techdose4u  Před 3 lety

      @@harishwarreddy9114 yes but maybe just for an example solution :)

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

      @@techdose4u okay , btw your video made complex topic so easy for me Thank you . You are the best in explaining any topic

    • @techdose4u
      @techdose4u  Před 3 lety

      Thanks for your appreciation ❤️

  • @krynx9.965
    @krynx9.965 Před 3 lety +1

    The video really helped me understand. But I'm still unable to understand why are we not caring about crossedges here?

    • @techdose4u
      @techdose4u  Před 3 lety

      They won't alter your result in any way

  • @ronylpatil
    @ronylpatil Před 3 lety

    Why we are not considering child to parent edge? I think the reason behind not taking child to parent edge because as it is undirected graph, where each and every edges are bi directional in nature. Please tell me one specific reason so if someone ask me about it i can easily explain him.

  • @yashwantraghuwanshi777

    bro u are famous ..my roommate knows u already

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

    why low[u]=time,i couldn't understood that?help anyone please

  • @adnanniazi9954
    @adnanniazi9954 Před 2 lety

    To remove an articulation point from a graph we need to ?
    A: remove an edge
    B: add an edge
    C: both a and b
    D: none of the above
    Please sir reply

  • @BaljeetSingh-bx8yj
    @BaljeetSingh-bx8yj Před 3 lety +2

    Sirji pranam 🙏🏻

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

    Sir....please make videos on coding questions for tcs digital and ncr corporation....only 10 days left for the exam...please help sir.....

    • @techdose4u
      @techdose4u  Před 3 lety

      What videos ?

    • @prachipriya9451
      @prachipriya9451 Před 3 lety

      @@techdose4u videos suggesting some coding questions related to these exams....which could practiced in this short span of time...

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

    Sir, I really request you to make a video for students in 7th sem, and cover every student (whether his preparation level is 20 percent or he has just started)
    TRUST ME, THIS IS THE MOST WATCHED VIDEO ON CODING BLOCKS CHANNEL, and unfortunately that video is not worth
    watching..:/
    Sir, please consider this..and try to make a video on it asap..
    Because we need you the most..this time..
    How will we strategise our preparation..
    Only you can help

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

    This solution is giving Time Limit Exceeded Error on leetcode

    • @techdose4u
      @techdose4u  Před 3 lety

      Which problem ?

    • @nitishgoyal9910
      @nitishgoyal9910 Před 3 lety

      @@techdose4u 1192. Critical Connections in a network

    • @techdose4u
      @techdose4u  Před 3 lety

      @@nitishgoyal9910 there was no problem. I solved that yesterday. I will make that video today. You can see the code.

    • @nitishgoyal9910
      @nitishgoyal9910 Před 3 lety

      @UCP3oO5eS2kWCKGs5H86FlyQ thanks

  • @adnanniazi9954
    @adnanniazi9954 Před 2 lety

    To remove an articulation point from a graph we need to ?
    A: remove an edge
    B: add an edge
    C: both a and b
    D: none of the above

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

    We could also use just single integer as parent and pass "u" every time in DFS function and check this parent with "v". First call to function pass -1 as parent

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

    To hard for me