G-22. Kahn's Algorithm | Topological Sort Algorithm | BFS

Поділитися
Вставка
  • Опубліковано 4 вер 2022
  • GfG-Problem Link: bit.ly/3PvBfsm
    C++/Java/Codes and Notes Link: takeuforward.org/data-structu...
    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
    ---------------------------------------------------------------------------------------------------------------------------

КОМЕНТАРІ • 172

  • @takeUforward
    @takeUforward  Рік тому +57

    Let's continue the habit of commenting “understood” if you got the entire video. Please give it a like too, you don't 😞
    Do follow me on Instagram: striver_79

  • @GirishJain
    @GirishJain Рік тому +40

    FYI - for someone thinking why visited array is not used here
    Visited array is not required in this case because we are only inserting the node into the queue whenever the degree of that node becomes 0 and this will only happen once. Suppose like in the above example 4 was trying to visit 0 but it did not insert 0 into the queue for the first time as the indegree was not 0. and that;s why when 5 was trying to insert 0 , it did not have to check if 0 was visited earlier

    • @stl8857
      @stl8857 Рік тому

      that means once it becomes 0 next time it will go negative??thats why it will only get into the que once

    • @codingalley6229
      @codingalley6229 Рік тому +4

      @@stl8857 nah bro, once its zero it mean incoming edges to it is 0 so how can it even reduce further?

    • @stl8857
      @stl8857 Рік тому +1

      @@codingalley6229 okier

    • @Moch117
      @Moch117 10 місяців тому +1

      Correct. I realized that too after running through the algorithm. In a sense, a visited array here is the inDegree check

    • @aadharjain313
      @aadharjain313 10 місяців тому +7

      in simple language , an element's indegree will become 0 only when all the parent nodes(nodes pointing to it) are withdrawn that is put into the topo list. Since every possible node (which could have that element as a neighbour) is already taken out of the queue and put into the topo list , so our neighbour node won't be touched again. And for not touching an already touched node , visited array is required , and it purpose is already solved in kahn's algorithm itslef , so no vis list is required here !!

  • @Amankumar-sb4jn
    @Amankumar-sb4jn Рік тому +55

    Sir, I only respect a few people for their way of teaching people, and you are among them for sure. Hats off to you and YES "UNDERSTOOD".

  • @cinime
    @cinime Рік тому

    Understood! Extremely wonderful explanation as always, thank you very much!!

  • @tanaysingh5348
    @tanaysingh5348 Рік тому +1

    thanks for making the graph series even better

  • @sayandey2492
    @sayandey2492 Рік тому +47

    Note: Componentwise checking not required here because every component has at least one node with indeg=0 and hence atleast one node of every component will get included in the queue initially

    • @RajeevCanDev
      @RajeevCanDev 11 місяців тому +1

      But to find those nodes we need to traverse the graph first

    • @namesurname2223
      @namesurname2223 11 місяців тому +2

      @@RajeevCanDev we are already having a loop (V) in starting while checking the inDegree

    • @RajeevCanDev
      @RajeevCanDev 11 місяців тому +6

      @@namesurname2223 yes that's what I'm saying.. to find those nodes we first need to traverse all nodes for finding the indegree

  • @visheshagrawal8676
    @visheshagrawal8676 Рік тому

    understood !! ........... Hey he is amazing I learned a new algo (kahn's).... in just 15 mins with its code because of him .... he is a gem guy!!.......

  • @sahllsaharn4664
    @sahllsaharn4664 Рік тому

    you've reallly worked on your explanation a great one sir it is a good change and a great teacher

  • @saibunny1253
    @saibunny1253 2 місяці тому

    striver thank you ! i understood this kahn's algorithm using a slightly different bfs

  • @sripriyapotnuru5839
    @sripriyapotnuru5839 Рік тому

    Thank you, Striver 🙂

  • @jatinkumar4410
    @jatinkumar4410 2 місяці тому

    Amazing explanation 🙌

  • @DevashishJose
    @DevashishJose 5 місяців тому

    Understood, Thank you so much .

  • @sohailulislamalvi8656
    @sohailulislamalvi8656 10 місяців тому

    insanely good!

  • @vakhariyajay2224
    @vakhariyajay2224 Рік тому

    Thank you very much. You are a genius.

  • @stith_pragya
    @stith_pragya 7 місяців тому +1

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

  • @ventacode
    @ventacode 14 днів тому

    thanks striver it helped

  • @shyren_more
    @shyren_more Рік тому

    understood, thanks!

  • @karthiklv29
    @karthiklv29 Рік тому

    understood, and thank you for the clear cut explanation and the series

  • @vijayanks1714
    @vijayanks1714 2 місяці тому +3

    FAQ 1:
    why we don't use boolean[] visited in Kahn's algo?
    The standard bfs algo is generic if it is cycle or acyclic or directed or undirected work well
    the Kahn's only apply on DAG so no cycle, no need to worry about infinite loop condition then what about getting same node twice(it won't happy bcz we reach 0 only one time)
    so the need to visited not require we process each node once by using indegree array

    • @jaishriharivishnu
      @jaishriharivishnu Місяць тому

      since, we are only putting in queue when indegree become zero........ so it is not possible for any node later on , that it visit an already visited node again.

  • @CodeMode9313
    @CodeMode9313 11 місяців тому

    Thanks habibi tum acha kaam karti ...understood

  • @adebisisheriff159
    @adebisisheriff159 6 місяців тому

    Thanks so much!!!

  • @santhoshs7028
    @santhoshs7028 11 місяців тому

    understood, thank you

  • @kamikaze9245
    @kamikaze9245 Рік тому

    Understood bhaiya 🔥

  • @ujjalsaha428
    @ujjalsaha428 Рік тому +1

    Understood ❤️

  • @lonersunited
    @lonersunited 5 місяців тому +1

    Understood 😊

  • @loudcapricorn5513
    @loudcapricorn5513 11 місяців тому

    UNDERSTOOD 🤞🙌

  • @sukritinarang5016
    @sukritinarang5016 11 годин тому

    Understood!

  • @ashutosh..
    @ashutosh.. Рік тому

    At the end of the day... I understood it well :-)

  • @pranayjain._
    @pranayjain._ Рік тому

    Understood Sir!!

  • @ayushsinghchauhan6917
    @ayushsinghchauhan6917 Рік тому

    u are just amazing

  • @pawandeepchor89
    @pawandeepchor89 Рік тому

    Top class content, thanks for all these videos.

  • @UECAshutoshKumar
    @UECAshutoshKumar 9 місяців тому +1

    Thank you sir 😁

  • @user-tk2vg5jt3l
    @user-tk2vg5jt3l 2 місяці тому

    Thank you bhaiya

  • @chandantaneja6388
    @chandantaneja6388 Рік тому

    Understood 👍

  • @gautamsaxena4647
    @gautamsaxena4647 2 місяці тому

    Understood Bhaiya

  • @_hulk748
    @_hulk748 Рік тому

    Understood sir❤🙏🙇‍♂

  • @suryakiran2970
    @suryakiran2970 Рік тому

    Understood❤

  • @sayakghosh5104
    @sayakghosh5104 Рік тому

    understood!!

  • @Chandraprakash-kx4ic
    @Chandraprakash-kx4ic Рік тому

    understood sir❤

  • @LoayYounesMusic
    @LoayYounesMusic Місяць тому

    Understood^^

  • @cypher7536
    @cypher7536 Рік тому

    understood ❤️❤️

  • @The_Shubham_Soni
    @The_Shubham_Soni 10 місяців тому

    UNDERSTOOD.

  • @nikitakeshri9233
    @nikitakeshri9233 6 днів тому

    Understood :)

  • @sannareddymonesh7598
    @sannareddymonesh7598 Рік тому

    Understood!!

  • @adarshkumarrao3478
    @adarshkumarrao3478 Рік тому +1

    UNDERSTOOD

  • @original_gangsta_
    @original_gangsta_ Рік тому

    Understood 😁😁

  • @ChutHunter
    @ChutHunter 7 місяців тому

    understood 👍

  • @ishangujarathi10
    @ishangujarathi10 Рік тому +1

    amazingg intuition

  • @pheonix166
    @pheonix166 3 дні тому

    Understood

  • @GungunRamchandani
    @GungunRamchandani 4 дні тому

    understood

  • @subee128
    @subee128 10 місяців тому

    Thanks

  • @oqant0424
    @oqant0424 Рік тому

    Understood !!!!!!!!!!!!!

  • @snehiltiwari28
    @snehiltiwari28 Рік тому

    understood :)

  • @therangeplus
    @therangeplus 2 місяці тому

    Bro it is amazing how you explain things so clearly. I learned sm more from this video than others tysm!!

  • @shreyanshkumarsahu6534
    @shreyanshkumarsahu6534 Рік тому

    Understand 🥰🥰

  • @avadhut325
    @avadhut325 Рік тому

    Does kahn's algorithm work for Iterative DFS or does it only work for BFS ?

  • @ow_aqua8875
    @ow_aqua8875 7 місяців тому

    im a bit confused on the assumed form of this adjacency matrix. are we assuming a 2d array where arr[i][j] = 1 if there is a directed edge from i to j?

  • @bablushaw6856
    @bablushaw6856 Рік тому +4

    My laziness raised 1 question that keeps my head scratched: "When easier one algo is there (about DFS) then why this algo exists?". I believe I will get answers and uses in next couple of videos in the series.

    • @decepticonWave
      @decepticonWave Рік тому +21

      The problem with using the dfs approach is that if you are given a directed graph that contains a cycle in the solution. The algorithm wont be able to detect, it will spill out an ordering which is wrong. But when using khans algorithm, you will be able to detect whether top. sort was possible or not. You can do this by checking if the ordering has the same length as the number of nodes. if it is true then top sort was possible.
      So you have to know this just in case your interviewer adds such an edge case

    • @TheOfficialAyush
      @TheOfficialAyush Рік тому +1

      @@decepticonWave you are absolutely correct ! Just in case anyone Tends to use DFS and still wanna detect CYCLE u must see How to detect Cycle in direted graph using DFS of Striver u just need to add one array which stores Self visited[ ] and boom problem is solved in one line

    • @shivarajpatil8699
      @shivarajpatil8699 Рік тому

      @@decepticonWave true.

    • @whateverittakes7929
      @whateverittakes7929 11 місяців тому

      because if u use dfs u hv to do cycle detection also

  • @googleit2490
    @googleit2490 10 місяців тому +2

    Revision: Forgot everything about indegree, was trying to use bfs the same as dfs and ans.push_back(q.front());
    Sep'6, 2023 03:52 pm

  • @UECSoumyaRay
    @UECSoumyaRay Рік тому

    Sometimes I just want to give up, cuz I feel that remembering so many intuitions is impossible for me! And remembering all of these for a lifetime! I am seriously thinking of shifting to Data Analytics. Salary kam milegi, lekin ek acche company ka tag to mil jayga na!!!!

  • @mansisethi8127
    @mansisethi8127 6 місяців тому

    can we not do a thing that sort the indices in ascending order of indegrees?

  • @SanidhyaPatel-q9s
    @SanidhyaPatel-q9s 6 днів тому

    Which app do u use for teaching DSA in iPad?

  • @atifhu
    @atifhu Рік тому +2

    when did top changed to topo

  • @anishraj66
    @anishraj66 Рік тому

    What will be the time req to find indegree??

  • @codingalley6229
    @codingalley6229 Рік тому

    🐐

  • @pratyakshhhhhhhhhhhhhhhhhhhhh
    @pratyakshhhhhhhhhhhhhhhhhhhhh 7 місяців тому

    🎉

  • @TheSpiritualOne401
    @TheSpiritualOne401 8 місяців тому

    Striver how do you remember all this?

  • @aniketambat6334
    @aniketambat6334 Рік тому

    understood++

  • @vaalarivan_p
    @vaalarivan_p Рік тому +1

    4:00 - 8:30

  • @Piyush-yp2po
    @Piyush-yp2po 8 місяців тому +1

    Isn't calculating indegree is O(V*E)??

  • @UmeshYadav-qx4hr
    @UmeshYadav-qx4hr 7 місяців тому

    file not found for -- C++/Java/Codes and Notes Link

  • @jaishriharivishnu
    @jaishriharivishnu Місяць тому

    only similarity between Kahn's Algorithm and BFS is using queue Data Structure.... BFS is when we search in level, but here we are searching when indegree is getting zero. So, we can't relate BFS and Kahn's Algo.
    ~ Kahn

  • @ishan5033
    @ishan5033 Рік тому +2

    Can we say a Directed graph whose none of the indegree is zero has a cycle?

    • @rahulmehndiratta3856
      @rahulmehndiratta3856 9 місяців тому

      No , That means that is a not connected graph think huh?

  • @harshdeepsinghlongia6040
    @harshdeepsinghlongia6040 Рік тому +1

    we can use stack also in this implementation instead of a queue.. why doesnt it make a difference

    • @rushidesai2836
      @rushidesai2836 9 місяців тому

      Kahn's is Topo sort with BFS, that's why.

  • @rayyansyed2998
    @rayyansyed2998 9 місяців тому

    "understood"

  • @shlokaggarwal7714
    @shlokaggarwal7714 Рік тому

    We dont even need a stack, we can directly add index whose value is zero.

  • @kushagramishra5638
    @kushagramishra5638 Рік тому +2

    "Understood"

  • @soicooc3500
    @soicooc3500 2 місяці тому

    4:00

  • @shivamnegi7552
    @shivamnegi7552 Рік тому

    whats the intuition

  • @sohammukherjee1392
    @sohammukherjee1392 Рік тому +1

    What if the graph has multiple components???

    • @ujwalgupta4508
      @ujwalgupta4508 Рік тому +2

      then in the beginning, the node will in-degree 0 from each component will be pushed in the queue

  • @shivyanshgarg2641
    @shivyanshgarg2641 6 місяців тому

    First question in which visited array is not used . And it makes complete sense. But still it feels weird and i can't digest it.

  • @rickk3300
    @rickk3300 Рік тому

    Why didn't we take a visited array in this code?

    • @GirishJain
      @GirishJain Рік тому +3

      I was also thinking the same but I got the reason now. Visited array is not required in this case because we are only inserting the node into the queue whenever the degree of that node becomes 0 and this will only happen once. Suppose like in the above example 4 was trying to visit 0 but it did not insert 0 into the queue for the first time as the indegree was not 0. and that;s why when 5 was trying to insert 0 , it did not have to check if 0 was visited earlier

  • @antassachan1782
    @antassachan1782 Рік тому

    leetcode link for the question?

  • @akashkumarsingh8369
    @akashkumarsingh8369 Рік тому

    understood
    lo bhai kr diya comment ye emoji(😞😞😞) mt kra kro yarr

  • @mdsameer533
    @mdsameer533 Рік тому

    us

  • @deepakjain4481
    @deepakjain4481 4 місяці тому

    //here is the code both by bfs and dfs
    #include
    using namespace std;
    void dfs(vector & adj, vector & visited,int node,stack & st);
    vector sol(vector adj, vector & visited){
    stack st;
    for(int i=0;i

  • @aryankumar3018
    @aryankumar3018 Рік тому

    Understood ntr

  • @cypher7536
    @cypher7536 Рік тому +3

    0:19 kya kaha 🤣🤣...?

  • @bemypro8602
    @bemypro8602 Рік тому

    what happened to ur neck

  • @pranavkhairnar1290
    @pranavkhairnar1290 Рік тому

    Hey can anyone explain why he did not used visited array in this problem??????

    • @shivamjain9968
      @shivamjain9968 Рік тому

      because here we are storing the no. of incoming edges in the node so it can be 2,3 and so on but visited array is used only to check if the node has been visited or not and it store only 0 & 1.

  • @AnmolSharma-br2eg
    @AnmolSharma-br2eg Рік тому

    Understood!

  • @suhaanbhandary4009
    @suhaanbhandary4009 Рік тому

    understood!!

  • @mriduljain6809
    @mriduljain6809 Рік тому

    Understood Bhaiya

  • @dreamyme543
    @dreamyme543 Рік тому

    Understood:)

  • @RohitSharma-yc8lm
    @RohitSharma-yc8lm Рік тому

    UNDERSTOOD

  • @user-nb1ye5tf1r
    @user-nb1ye5tf1r 5 місяців тому

    Understood

  • @skblabla
    @skblabla 6 днів тому

    understood

  • @pihus3498
    @pihus3498 Рік тому

    us

  • @soubhikghosh6564
    @soubhikghosh6564 Рік тому

    Understood!

  • @AyushSharma-ux4fk
    @AyushSharma-ux4fk Рік тому

    Understood!