G-54. Strongly Connected Components - Kosaraju's Algorithm

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 พ.ย. 2022
  • GfG-Problem Link: bit.ly/3R6LzID
    C++/Java/Codes and Notes Link: takeuforward.org/graph/strong...
    DP Series: • Striver's Dynamic Prog...
    SDE Sheet: takeuforward.org/interviews/s...
    Check out our Website for curated resources:
    Our Second Channel: / @striver_79
    In case you are thinking to buy courses, please check below:
    Code "takeuforward" for 15% off at GFG: practice.geeksforgeeks.org/co...
    Code "takeuforward" for 20% off on sys-design: get.interviewready.io?_aff=takeuforward
    Crypto, I use the Wazirx app: wazirx.com/invite/xexnpc4u
    Take 750 rs free Amazon Stock from me: indmoney.onelink.me/RmHC/idje...
    Earn 100 rs by making a Grow Account for investing: app.groww.in/v3cO/8hu879t0
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    ---------------------------------------------------------------------------------------------------------------------------

ความคิดเห็น • 190

  • @ShreyaLanka-1912
    @ShreyaLanka-1912 10 วันที่ผ่านมา +1

    Understood!! Thank you for this amazing explanation.

  • @cinime
    @cinime ปีที่แล้ว +9

    Understood! Super excellent explanation as always, thank you very much!!

  • @kushagrasrivastava1443
    @kushagrasrivastava1443 ปีที่แล้ว +99

    I thought I was done with graphs 😅

    • @SomeshCodes
      @SomeshCodes 5 หลายเดือนก่อน +3

      Literally me too...😅

  • @adithyabhat4770
    @adithyabhat4770 9 หลายเดือนก่อน +10

    Your videos and the SDE sheet are helping me so much to brush up the DSA concepts and learn the ones I ignored in college. A BIG THANK YOU!!

  • @muskanchaurasia1532
    @muskanchaurasia1532 9 หลายเดือนก่อน +2

    Just amazing.
    Great Explanation.

  • @rahulsangvikar7973
    @rahulsangvikar7973 3 หลายเดือนก่อน +2

    Just realised one thing. You can avoid reversing the graph if you were to put the time in a queue instead of a stack.

    • @reee896
      @reee896 3 หลายเดือนก่อน

      No you need stack cause if you use Queue it will keep going on to next SSC using the visited araay

  • @andreasleonidou3620
    @andreasleonidou3620 ปีที่แล้ว +4

    Excellent tutorial and really helpful, thanks!!

  • @kritikarawat2180
    @kritikarawat2180 ปีที่แล้ว

    Man, amazing explaination. Hats off to you buddy

  • @veekykumar4211
    @veekykumar4211 ปีที่แล้ว

    great work sir i just watch ur few video and ur content is awesome

  • @KaushalDhruw
    @KaushalDhruw 9 หลายเดือนก่อน

    Finally understood kosaraju algo. Thank you striver.

  • @Mr_Cat_11
    @Mr_Cat_11 ปีที่แล้ว +6

    Long time no see sir 😊
    Thank you for posting 🥰🔥💖

  • @bhavikpunmiya9641
    @bhavikpunmiya9641 ปีที่แล้ว

    Thanks for Explaining this concept :) really liked your explaination

  • @AmanKumar-wd2mq
    @AmanKumar-wd2mq ปีที่แล้ว

    You explain so well ❤❤

  • @amitp277
    @amitp277 ปีที่แล้ว

    Awesome work 👏

  • @rohinianekar61
    @rohinianekar61 ปีที่แล้ว +2

    understood, nice explanation

  • @augustinradjou3909
    @augustinradjou3909 5 หลายเดือนก่อน

    Going to end the graph..❤ understood

  • @249abhi
    @249abhi 10 หลายเดือนก่อน

    awesome explanation!

  • @krishnavyaskondle5707
    @krishnavyaskondle5707 ปีที่แล้ว

    Thanks for Explaining the logic

  • @sahilbheke4083
    @sahilbheke4083 ปีที่แล้ว

    You are a very good teacher apse sikhna easy lagta hai

  • @stith_pragya
    @stith_pragya 7 หลายเดือนก่อน +1

    Thank You So Much for this wonderful video..............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @adebisisheriff159
    @adebisisheriff159 6 หลายเดือนก่อน +5

    Honestly, this playlist deserves Millions views and comments..... Thanks for all you do Striver!!!

  • @Emperor723
    @Emperor723 2 หลายเดือนก่อน +1

    mazaaaaaaa aaaaaaaagyaaaaaaaaaaa
    this is first time i have come across any of your videos
    der aaye par durust aaye....
    let me subscribe!

  • @sauravchandra10
    @sauravchandra10 ปีที่แล้ว

    Understood, thanks!

  • @devanshubilthare5277
    @devanshubilthare5277 8 หลายเดือนก่อน

    Amazing content🔥

  • @paruldamahe7969
    @paruldamahe7969 5 หลายเดือนก่อน

    amazing content!

  • @user-oh9qm7pe8h
    @user-oh9qm7pe8h ปีที่แล้ว +27

    The above code gave me `TLE`
    so, with few minor changes it will work for you.
    To optimize the given code, you can make a few changes to improve its efficiency:
    1. Pass the `adj` vector as a reference to the `dfs` function: Currently, the `adj` vector is passed by value, which creates a copy of the vector in each recursive call. Instead, pass it by reference to avoid unnecessary copies.
    2. Use a vector of vectors (`adjT`) instead of an array of vectors for the transpose graph: Declaring `vector adjT[V]` as a variable-length array may cause a stack overflow for large values of `V`. Instead, use a vector of vectors to represent the transpose graph (`adjT`).
    3. Reserve memory for the transpose graph vectors: Before pushing elements into `adjT`, reserve memory for each vector based on the size of the corresponding adjacency list in the original graph. This avoids frequent reallocations and improves performance.
    4. Use vectors instead of stacks: Instead of using a stack to store the finish times of the nodes in the first DFS, you can use a vector and append nodes at the end. This eliminates the need for reversing the stack later on.
    Here's the optimized code with these changes:
    Code :
    class Solution {
    private:
    void dfs(int node, vector& adj, vector& vis, vector& finishOrder) {
    vis[node] = 1;
    for (int it : adj[node]) {
    if (!vis[it]) {
    dfs(it, adj, vis, finishOrder);
    }
    }
    finishOrder.push_back(node);
    }
    void dfs2(int node, vector& adj, vector& vis) {
    vis[node] = 1;
    for (int it : adj[node]) {
    if (!vis[it]) {
    dfs2(it, adj, vis);
    }
    }
    }
    public:
    // Function to find the number of strongly connected components in the graph.
    int kosaraju(int V, vector& adj) {
    vector vis(V, 0);
    vector finishOrder;
    for (int i = 0; i < V; i++) {
    if (!vis[i]) {
    dfs(i, adj, vis, finishOrder);
    }
    }
    // Creating the transpose graph (adjT)
    vector adjT(V);
    for (int i = 0; i < V; i++) {
    vis[i] = 0;
    for (int it : adj[i]) {
    adjT[it].push_back(i);
    }
    }
    // Last DFS using the finish order
    int scc = 0;
    for (int i = V - 1; i >= 0; i--) {
    int node = finishOrder[i];
    if (!vis[node]) {
    scc++;
    dfs2(node, adjT, vis);
    }
    }
    return scc;
    }
    };
    These optimizations should improve the efficiency of the code.

    • @HanumaVamsi2001
      @HanumaVamsi2001 11 หลายเดือนก่อน +2

      Thanks a lot man!!

    • @muditkhanna8164
      @muditkhanna8164 11 หลายเดือนก่อน +1

      yeah that improves the code a lot!

    • @khubanzehra7632
      @khubanzehra7632 4 หลายเดือนก่อน +1

      Thank you so much

  • @dishant930
    @dishant930 ปีที่แล้ว +27

    I think after seeing the video if you read the striver sheet explanation for the same question it will give you more insight since it is really well written. I really liked it!

    • @siddharth.chandani
      @siddharth.chandani 9 หลายเดือนก่อน

      Can you provide me link of that..

    • @nanireddy3606
      @nanireddy3606 5 หลายเดือนก่อน

      It is in the description

  • @mathematics7746
    @mathematics7746 ปีที่แล้ว

    awsm explanation thank u so much

  • @sukhpreetsingh5200
    @sukhpreetsingh5200 ปีที่แล้ว +1

    Awesome thanks a lot

  • @chandrachurmukherjeejucse5816
    @chandrachurmukherjeejucse5816 10 หลายเดือนก่อน +1

    And here comes the real OG Striver.❤

  • @parshchoradia9909
    @parshchoradia9909 ปีที่แล้ว

    Understood Sir!

  • @visheshagrawal8676
    @visheshagrawal8676 ปีที่แล้ว

    amazing content

  • @ritulraj6119
    @ritulraj6119 ปีที่แล้ว

    🔥🔥🔥🔥
    Explanation

  • @UECAshutoshKumar
    @UECAshutoshKumar 7 หลายเดือนก่อน +1

    Thank you sir 🙏

  • @mohitsingh13
    @mohitsingh13 13 วันที่ผ่านมา

    Understood ❤

  • @oracle_ae
    @oracle_ae ปีที่แล้ว +1

    Thanks buddy

  • @GANESHSINGH-uc1gk
    @GANESHSINGH-uc1gk ปีที่แล้ว

    crystal clear

  • @Parthj426
    @Parthj426 7 วันที่ผ่านมา

    Understood after a little bit of effort.

  • @nitinkumar5974
    @nitinkumar5974 2 หลายเดือนก่อน

    thanks for the video

  • @divyareddy7622
    @divyareddy7622 ปีที่แล้ว

    thank you very muchhh! You've literally changed so many lives ✌️

  • @rishabhjain6272
    @rishabhjain6272 2 หลายเดือนก่อน

    the first step is same as topo sort using dfs technique, will not work with bfs (khann's algo) due to directed cyclic graphs.but if you apply topo sort using dfs in directed cyclic graphs ,it will work.

  • @herculean6748
    @herculean6748 ปีที่แล้ว

    Thanks🙌

  • @eswartharun3394
    @eswartharun3394 ปีที่แล้ว +9

    This is much intutive than the previous video on kosaraju(other graph playlist)

    • @takeUforward
      @takeUforward  ปีที่แล้ว +35

      Yes I read the comments, and then made this one.

    • @dhirendrasingh6071
      @dhirendrasingh6071 ปีที่แล้ว +6

      @@takeUforward you have changed the lives of so many students, you have blessings of many. ♥️

    • @tanmaypal2003
      @tanmaypal2003 ปีที่แล้ว +6

      ​@@takeUforward when we sort all the edges according to finishing time and store it in stack like this 0,1,2,3,4,5,6,7 , so instead of storing in stack can we use queue and then when we pop out elements from queue we start DFS from 7 instead of 0 and 7 is not connected to anyone so we found 1st SCC then we pop 6 and do DFS and found 2nd SCC then we pop 3 and do DFS and got 3rd SCC and so on. In this approach we don't need to reverse the graph. Can we do like this?

    • @vishalsinghrajpurohit6037
      @vishalsinghrajpurohit6037 ปีที่แล้ว +1

      @@tanmaypal2003 Nice observation. I think this should work fine.

    • @theSoberSobber
      @theSoberSobber ปีที่แล้ว +1

      @@tanmaypal2003 yes

  • @-VLaharika
    @-VLaharika ปีที่แล้ว

    Understood 👍

  • @wilhelmrudolphfittig3577
    @wilhelmrudolphfittig3577 หลายเดือนก่อน

    maza aaala re ala striver aala

  • @suryakiran2970
    @suryakiran2970 ปีที่แล้ว

    Understood❤

  • @ms-ej4gd
    @ms-ej4gd ปีที่แล้ว

    Big fan of your work striver

  • @adamyasharma_0135
    @adamyasharma_0135 5 หลายเดือนก่อน

    amazing..

  • @divyatejaswinivengada6368
    @divyatejaswinivengada6368 8 หลายเดือนก่อน

    Understooddd!!

  • @piyushacharya7696
    @piyushacharya7696 ปีที่แล้ว

    reach++
    Loved it

  • @Kupo3.0
    @Kupo3.0 ปีที่แล้ว

    Understood!

  • @evilpollination1916
    @evilpollination1916 2 หลายเดือนก่อน

    Understood.

  • @abhinavbhardwaj3372
    @abhinavbhardwaj3372 11 หลายเดือนก่อน

    understood sir

  • @dumpster-jackson
    @dumpster-jackson 3 หลายเดือนก่อน

    Understood!!

  • @h6_wingers
    @h6_wingers ปีที่แล้ว

    mast padha dia bahiya

  • @manojnavinjamuri5867
    @manojnavinjamuri5867 ปีที่แล้ว +1

    understood

  • @nisanduathsara5242
    @nisanduathsara5242 4 หลายเดือนก่อน

    understooood

  • @sathvikmalgikar2842
    @sathvikmalgikar2842 5 หลายเดือนก่อน +1

    guys just in case any one wondering why that stack of dfs calls was required that is only if u r solving for the second case of actually printing the components and not just finding number of components.
    like based on the stack trace u can make a note of elements while popping and put them into groups and print

    • @vishalcheeti8374
      @vishalcheeti8374 20 วันที่ผ่านมา

      without using stack, how to do solve the first case of finding no. of compo? I mean to find no. of components also we need it in sorted way right?

  • @jaishriharivishnu
    @jaishriharivishnu หลายเดือนก่อน

    Note: we can't say this as a Topological sort(as topological sort using DFS will get you stuck in Loop i.e cycle), but its a bit different. Its topological Sort for the SCC.

  • @LearnerForever123
    @LearnerForever123 10 หลายเดือนก่อน +1

    Understood

  • @AryanMathur-gh6df
    @AryanMathur-gh6df 9 หลายเดือนก่อน

    UNDERSTOOD

  • @ManishKumar-zm9rj
    @ManishKumar-zm9rj ปีที่แล้ว +3

    We can use the same dfs again by passing a dummy stack:
    public void dfs(ArrayList adj, int[] vis, Stack st, int node){
    vis[node] = 1;
    for(int it : adj.get(node)){
    if(vis[it] == 0){
    dfs(adj, vis, st, it);
    }
    }
    st.push(node);
    }
    //Function to find number of strongly connected components in the graph.
    public int kosaraju(int V, ArrayList adj)
    {
    int[] vis = new int[V];
    Stack st = new Stack();
    for(int i = 0; i < V; i++){
    if(vis[i] == 0){
    dfs(adj, vis, st, i);
    }
    }
    ArrayList adjT = new ArrayList();
    for(int i = 0; i < V; i++) adjT.add(new ArrayList());
    for(int i = 0; i < V; i++){
    vis[i] = 0;
    for(int it : adj.get(i)){
    // previously: i -> it
    // Now make it: i i
    adjT.get(it).add(i);
    }
    }
    int scc = 0;
    while(!st.isEmpty()){
    int node = st.pop();
    if(vis[node] == 0){
    scc++;
    dfs(adjT, vis, new Stack(), node);
    }
    }
    return scc;
    }

  • @KratosProton
    @KratosProton ปีที่แล้ว

    great

  • @UjjawalPathak-qy3uj
    @UjjawalPathak-qy3uj หลายเดือนก่อน

    HEY STRIVER, We can also reduce the time and steps if first getting there there finishing time and store in a queue rather then stack so because of FIFO we know first element to finish so we don't need to reverse the graph either we can directly go through the finishing elements which are already reverse with respect to graph it will reduce steps and clean code too.
    correct me if I m wrong

  • @muditkhanna8164
    @muditkhanna8164 11 หลายเดือนก่อน

    please make videos on Eulerian paths also.

  • @abcsumits
    @abcsumits ปีที่แล้ว +3

    sorting according to finish time can be done using toposort:)

    • @deviprasad_bal
      @deviprasad_bal ปีที่แล้ว +1

      that is toposort only. Toposort can be done using stack and that's what it is here.

    • @DheerajDivaker
      @DheerajDivaker ปีที่แล้ว

      @abcsumit Toposort banega hi nahi, wo sirf DAG mein work karta hai.

    • @DheerajDivaker
      @DheerajDivaker ปีที่แล้ว

      @@deviprasad_bal Toposort banega hi nahi, wo sirf DAG mein work karta hai.

    • @Anonymous_Coder
      @Anonymous_Coder ปีที่แล้ว +2

      @@DheerajDivaker Yes you are right but here since graph can be cyclic as well as acyclic , so incase of CYCLIC one edge which is causing cycle is not stored , like 1->2->3->1 , is only stored as [ 1, 2 ,3 ] .
      So ultimately we should not call it as a toposort ,but implementation is exactly same.

    • @DheerajDivaker
      @DheerajDivaker ปีที่แล้ว

      @@Anonymous_Coder yes absolutely correct.

  • @p38_amankuldeep75
    @p38_amankuldeep75 ปีที่แล้ว

    understood💛💙💛

  • @afraztanvir973
    @afraztanvir973 11 หลายเดือนก่อน

    Is there any relation in this algo and topo sort as we get reverse topo from stack

  • @udaypratapsingh8923
    @udaypratapsingh8923 ปีที่แล้ว

    understood 🥶

  • @durgeshjadhav01
    @durgeshjadhav01 หลายเดือนก่อน

    nice

  • @user-pr2ju2zf4q
    @user-pr2ju2zf4q ปีที่แล้ว

    hey striver can we do bfs to sort the graph according to their finishing time ? is it possible ?

  • @dhanashreegodase4445
    @dhanashreegodase4445 7 หลายเดือนก่อน

    ultimate

  • @mraryanrajawat9693
    @mraryanrajawat9693 ปีที่แล้ว +6

    class Solution
    {
    private:
    void dfs(int node,vectoradj[],vector&vis,stack&st) // dfs for adj
    {
    vis[node]=1;
    for(auto it:adj[node])
    {
    if(vis[it]==0)
    {
    dfs(it,adj,vis,st);
    }
    }
    st.push(node);
    }
    void dfs2(int node,vectoradjT[],vector&vis) // dfs for adjT
    {
    vis[node]=1;
    for(auto it:adjT[node])
    {
    if(vis[it]==0)
    {
    dfs2(it,adjT,vis);
    }
    }
    }
    public:
    //Function to find number of strongly connected components in the graph.
    int kosaraju(int V, vector adj[])
    {
    vectorvis(V,0);
    stackst;
    vector adjT[V]; // adj list after reversing edges
    for(int i=0;i

  • @space_ace7710
    @space_ace7710 ปีที่แล้ว +3

    GFG compiler giving TLE, don't know why?? :(

  • @ayushman_sr
    @ayushman_sr 7 หลายเดือนก่อน

    thought process is very tricky

  • @dhruvn6522
    @dhruvn6522 11 หลายเดือนก่อน +2

    Sir is sorting edges according to finishing time same as topological sort?

    • @discuss1
      @discuss1 3 หลายเดือนก่อน

      Will topological sort work in cyclic graphs?

  • @discuss1
    @discuss1 3 หลายเดือนก่อน

    How do I pick the starting node for sorting.
    In the above example if we would have started sorting from Node 3, I would have never reached 0, 1, 2. So how to calculate this increasing time for all nodes efficiently.

  • @iKnowThatSong
    @iKnowThatSong 8 หลายเดือนก่อน

    Instate on transposing the graph, can't we just start from the last node?
    someone please explain....

  • @no---on
    @no---on 5 หลายเดือนก่อน

    In given example if we just make a little change with 3->2 then after reversing the edge will be 2->3 so in step 3 dfs call 2 will also go to 3 . I feel like it should not go to 3

    • @abhishekchoudhary7030
      @abhishekchoudhary7030 4 หลายเดือนก่อน

      i have the same confusion that if we start with 7 in the start rather than 0 , we will get wrong connected components

  • @sharathkumar8338
    @sharathkumar8338 ปีที่แล้ว +7

    Understood brother. Could you please upload videos on sliding window and 2 pointers?? I saw most amazon OA will be based on that only.

  • @vaibhavrawat4008
    @vaibhavrawat4008 5 หลายเดือนก่อน

    wow

  • @Incurabl
    @Incurabl ปีที่แล้ว

    16:00

  • @PankajGupta-cx3ji
    @PankajGupta-cx3ji 11 หลายเดือนก่อน

    choit💥💥💥

  • @PalashAgrawalECE
    @PalashAgrawalECE ปีที่แล้ว

    19:55 Why did you mention to write as private function. Is there any advantage of it over public. As I tested in run time and got public working faster than private. Also from outer source noone is calling that dfs function. So kindly elaborate please

  • @mr.dependable4885
    @mr.dependable4885 ปีที่แล้ว +4

    Why is it important to reverse the edges, can't we just start the dfs in reverse order ?
    Like in the example start from 7
    can any1 please explain

    • @arpitrajput6424
      @arpitrajput6424 ปีที่แล้ว

      we can't .try dryrun on 1st example ,
      suppose u created vector which store the timing then timing will be
      {1 2 4 3 0} if you try dfs accoording to this you still cannot find scc.

    • @shivamsangwan7268
      @shivamsangwan7268 ปีที่แล้ว +1

      Yes, you can do that. But, this video is about Kosaraju's algorithm, which is implemented this way.

  • @danushbasanaboyina1306
    @danushbasanaboyina1306 หลายเดือนก่อน

    In the first step.sorting all the edges ...looks like topological sorting..... Is it the same I am thinking....

  • @mayankgarg8103
    @mayankgarg8103 ปีที่แล้ว

    at 13:00 while traversing at scc1 from the node 0 using dfs you have taken the edges in the already present way(or say initial way) but since we have reversed the edges it should move in another way and you are also using the fact of of the edges being reversed at 13:05 when you are trying to access 3 from the dfs(2) you have said that . Then again same thing when doing same thing for dfs(3) we are not using the fact that edges are reversed. Can anyone here clear my doubt that where I am understanding it wrong?

    • @iamnoob7593
      @iamnoob7593 8 หลายเดือนก่อน

      I get ur point , Striver used scc1->scc2->scc3->scc4 , Iniital config something like scc1scc3->scc4 can be used for better understanding

  • @anonymous-ws2ij
    @anonymous-ws2ij ปีที่แล้ว +2

    Hey Striver.. I had a question.. If we store the nodes according to their finishing time in a queue and rather than reversing all the nodes can't we simply do tranversal from the nodes in the queue in the similar fashion as you did.

  • @pavanrai6196
    @pavanrai6196 ปีที่แล้ว +1

    Can't we use queue and then not reversing the edges and just count no of scc of back, but it's not working idk

    • @DheerajDivaker
      @DheerajDivaker ปีที่แล้ว

      edges ko reverse karne se SCCs separate ho rahe hai..

  • @amitp277
    @amitp277 ปีที่แล้ว

    👏👏

  • @tanmaybro3812
    @tanmaybro3812 หลายเดือนก่อน

    Isn't the first step same as topological sort?

  • @aktiwari3379
    @aktiwari3379 ปีที่แล้ว +1

    ♥️♥️✌️

  • @pratyakshhhhhhhhhhhhhhhhhhhhh
    @pratyakshhhhhhhhhhhhhhhhhhhhh 7 หลายเดือนก่อน

    🎉🎉

  • @ravipatel-xu5qi
    @ravipatel-xu5qi 5 หลายเดือนก่อน

    Why sorting is needed. Can't be simply reverse the edges and then do dfs on all the nodes. We can get group of nodes which are there in cycle which is ultimately our strongly connected components.

    • @lavanyam3224
      @lavanyam3224 4 หลายเดือนก่อน

      By doing that you've completely ignored the "Finish time" concept. While doing the dfs in the transpose graph( with reverse edges) you should start with the component which does not have an edge going to the other scc.. that's why we use a stack and pop the top most element to perform dfs. If u want, I'll post my python code

  • @sahiljaiswal9270
    @sahiljaiswal9270 11 หลายเดือนก่อน

    I am confused about the first step of arranging nodes on their finishing times. I thought that instead just doing normal DFS on reversed graph skipping the first step might also work but it doesnt, Can anyone explain this with an example

    • @harshitpant07
      @harshitpant07 9 หลายเดือนก่อน

      if you reversed the graph and then perform the dfs how will you be able to reach form[0,1,2] --> [3] so to tackle this we store them in a stack to remember their order form start to finish .

  • @083_h_nitishkumarjha3
    @083_h_nitishkumarjha3 ปีที่แล้ว

    can't we just use visited array to count the scc in reversed graph , similar to counting different components

    • @DheerajDivaker
      @DheerajDivaker ปีที่แล้ว

      starting vertex kon hoga, fir ye kaise pta lagaoge? jiska finishing time sabse jyada hoga wahi starting point hoga aur iske liye hume stack mein Finishing time ke according vertices rakhne padenge.

  • @adityavaste8963
    @adityavaste8963 ปีที่แล้ว +2

    Can we skip the step 1, and just reverse all the edges ? Because after reversing edges, components will be logically separated and we can run DFS on it separately, and can find the nodes in each component.
    Please can anyone guide me on this ?

    • @adityavaste8963
      @adityavaste8963 ปีที่แล้ว

      Ohh, got it. It's for the efficiency

    • @Jinxed192
      @Jinxed192 ปีที่แล้ว +2

      for eg, in the same example as striver's, if you reverse the edge between 2 & 3, with your process, when the graph is reversed, the dfs(0) has 3 as well in it, this is the loop hole. Whereas, in the algo, when we do dfs first and calculate finishing times, we get 3 at the top of the stack, so now we can make different dfs calls for each scc. Hope you understand. Have a great day!

    • @adityavaste8963
      @adityavaste8963 ปีที่แล้ว +1

      ​@@Jinxed192 Thanks for explaination.
      That means we must know from where to start, to void futher conflicts.

  • @vardhanbolla-kz7xe
    @vardhanbolla-kz7xe 11 หลายเดือนก่อน +1

    This code is giving tle in gfg, the below code is same as what you thought
    class Solution
    {
    private:
    void dfs(int node, vector &vis,vector adj,stack &st)
    {
    vis[node]=1;
    for(auto it: adj[node])
    {
    if(vis[it]==0)
    {
    dfs(it,vis,adj,st);
    }
    }
    st.push(node);
    }

    void dfs1(int node, vector &vis, vector adjT[])
    {
    vis[node]=1;
    for(auto it: adjT[node])
    {
    if(vis[it]==0)
    {
    dfs1(it,vis,adjT);
    }
    }
    }
    public:
    //Function to find number of strongly connected components in the graph.
    int kosaraju(int V, vector& adj)
    {
    //code here
    vector vis(V,0);
    stack st;
    for(int i=0;i

    • @Lullurluli
      @Lullurluli 7 หลายเดือนก่อน

      To resolve this issue, make sure that adjT is declared as a vector of vectors
      vectoradjT(V);

  • @sandeepmourya8922
    @sandeepmourya8922 ปีที่แล้ว

    isn't the timestamp same as toposort ? or there is any difference

  • @anishraj66
    @anishraj66 ปีที่แล้ว +2

    bhaiya can,t we solve it by reversing the stack instead of reversing the graph

    • @visheshagrawal8676
      @visheshagrawal8676 ปีที่แล้ว +1

      stack reversal will reverse the finish time but reversing the graph does not allow us to go from one scc to another scc