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

Поделиться
HTML-код
  • Опубликовано: 19 ноя 2024

Комментарии • 191

  • @takeUforward
    @takeUforward  2 года назад +69

    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 Год назад +56

    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 Год назад +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

      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 Год назад +62

    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".

    • @Moch117
      @Moch117 Год назад +2

      His graph series is impeccable

  • @sayandey2492
    @sayandey2492 Год назад +58

    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 Год назад +1

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

    • @namesurname2223
      @namesurname2223 Год назад +2

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

    • @RajeevCanDev
      @RajeevCanDev Год назад +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 Год назад +2

    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!!.......

  • @hellothere4732
    @hellothere4732 7 дней назад

    Your explanation is great and helpful. Wishing your channels great success!

  • @gayatrichaudhary580
    @gayatrichaudhary580 3 месяца назад

    We are very lucky because we have this amazing playlist of graph . All thanks to you brother.❤❤

    • @VarshaSingh-hi2sb
      @VarshaSingh-hi2sb 2 месяца назад

      Why calculating indegree takes O(N) and not O(N^2) ?

  • @tanaysingh5348
    @tanaysingh5348 2 года назад +1

    thanks for making the graph series even better

  • @vijayanks1714
    @vijayanks1714 6 месяцев назад +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 5 месяцев назад

      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.

  • @stith_pragya
    @stith_pragya 11 месяцев назад +1

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

  • @CodeMode9313
    @CodeMode9313 Год назад +1

    Thanks habibi tum acha kaam karti ...understood

  • @saibunny1253
    @saibunny1253 7 месяцев назад

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

  • @FooBar-lb5wf
    @FooBar-lb5wf 4 месяца назад

    FYI - Kahn's algorithm works well even if you replace the queue with a stack. It just produces a different topological sort sequence. Here's an excerpt from Wikipedia article.
    "...Reflecting the non-uniqueness of the resulting sort, the structure S can be simply a set or a queue or a stack. Depending on the order that nodes n are removed from set S, a different solution is created..."

  • @sahllsaharn4664
    @sahllsaharn4664 Год назад

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

  • @googleit2490
    @googleit2490 Год назад +3

    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

  • @therangeplus
    @therangeplus 6 месяцев назад

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

  • @cinime
    @cinime 2 года назад

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

  • @jatinkumar4410
    @jatinkumar4410 7 месяцев назад

    Amazing explanation 🙌

  • @sripriyapotnuru5839
    @sripriyapotnuru5839 2 года назад

    Thank you, Striver 🙂

  • @ashutosh..
    @ashutosh.. Год назад

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

  • @DevashishJose
    @DevashishJose 9 месяцев назад

    Understood, Thank you so much .

  • @UECAshutoshKumar
    @UECAshutoshKumar Год назад +1

    Thank you sir 😁

  • @bablushaw6856
    @bablushaw6856 Год назад +5

    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 Год назад +23

      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 Год назад

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

  • @loudcapricorn5513
    @loudcapricorn5513 Год назад

    UNDERSTOOD 🤞🙌

  • @karthiklv29
    @karthiklv29 2 года назад

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

  • @sohailulislamalvi8656
    @sohailulislamalvi8656 Год назад

    insanely good!

  • @vakhariyajay2224
    @vakhariyajay2224 Год назад

    Thank you very much. You are a genius.

  • @ujjalsaha428
    @ujjalsaha428 2 года назад +1

    Understood ❤️

  • @kaichang8186
    @kaichang8186 2 месяца назад

    understood, thanks for the effort

  • @The_Shubham_Soni
    @The_Shubham_Soni Год назад

    UNDERSTOOD.

  • @pranayjain._
    @pranayjain._ Год назад

    Understood Sir!!

  • @vaalarivan_p
    @vaalarivan_p Год назад +1

    4:00 - 8:30

  • @kamikaze9245
    @kamikaze9245 2 года назад

    Understood bhaiya 🔥

  • @ayushsinghchauhan6917
    @ayushsinghchauhan6917 Год назад

    u are just amazing

  • @kulvindersingh5764
    @kulvindersingh5764 17 дней назад

    very helpful!

  • @ventacode
    @ventacode 4 месяца назад

    thanks striver it helped

  • @pawandeepchor89
    @pawandeepchor89 Год назад

    Top class content, thanks for all these videos.

  • @TON-108
    @TON-108 2 месяца назад

    Understood ✨✨

  • @santhoshs7028
    @santhoshs7028 Год назад

    understood, thank you

  • @_hulk748
    @_hulk748 Год назад

    Understood sir❤🙏🙇‍♂

  • @mriduljain6809
    @mriduljain6809 Год назад

    Understood Bhaiya

  • @chandantaneja6388
    @chandantaneja6388 2 года назад

    Understood 👍

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 6 месяцев назад

    Thank you bhaiya

  • @RohitSharma-yc8lm
    @RohitSharma-yc8lm 2 года назад

    UNDERSTOOD

  • @shyren_more
    @shyren_more 2 года назад

    understood, thanks!

  • @adebisisheriff159
    @adebisisheriff159 10 месяцев назад

    Thanks so much!!!

  • @jaishriharivishnu
    @jaishriharivishnu 5 месяцев назад

    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

  • @gangsta_coder_12
    @gangsta_coder_12 Год назад

    Understood 😁😁

  • @AnmolSharma-br2eg
    @AnmolSharma-br2eg 2 года назад

    Understood!

  • @princenagar1686
    @princenagar1686 4 месяца назад

    Yeah understood

  • @oqant0424
    @oqant0424 2 года назад

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

  • @Chandraprakash-kx4ic
    @Chandraprakash-kx4ic Год назад

    understood sir❤

  • @AtharvaRao0104
    @AtharvaRao0104 Месяц назад

    Everything else is fine. But for someone who watches this video will not understand why we need the vertices in this order.
    It is not enough to say topological sort gives an ordering of vertices .. we should also know why we need that ordering .. what problems it solves if we process the graph nodes in this ordering.. these basics are missing .. I still feel William Fiset is the best or reading books like Sedgewick will give a very good idea of applications

  • @subee128
    @subee128 Год назад

    Thanks

  • @LoayThePhrygian
    @LoayThePhrygian 6 месяцев назад

    Understood^^

  • @sannareddymonesh7598
    @sannareddymonesh7598 2 года назад

    Understood!!

  • @shlokaggarwal7714
    @shlokaggarwal7714 Год назад

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

  • @AnkitKumar-ko5tr
    @AnkitKumar-ko5tr 2 года назад

    Understood

  • @cypher7536
    @cypher7536 2 года назад

    understood ❤️❤️

  • @Piyush-yp2po
    @Piyush-yp2po Год назад +1

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

  • @ishangujarathi10
    @ishangujarathi10 Год назад +1

    amazingg intuition

  • @sayakghosh5104
    @sayakghosh5104 2 года назад

    understood!!

  • @dreamyme543
    @dreamyme543 2 года назад

    Understood:)

  • @atifhu
    @atifhu Год назад +2

    when did top changed to topo

  • @shreyanshkumarsahu6534
    @shreyanshkumarsahu6534 2 года назад

    Understand 🥰🥰

  • @kushagramishra5638
    @kushagramishra5638 2 года назад +2

    "Understood"

  • @teetanrobotics5363
    @teetanrobotics5363 2 года назад

    understood

  • @UECSoumyaRay
    @UECSoumyaRay Год назад +1

    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!!!!

  • @snehiltiwari28
    @snehiltiwari28 Год назад

    understood :)

  • @ishan5033
    @ishan5033 Год назад +2

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

  • @harshdeepsinghlongia6040
    @harshdeepsinghlongia6040 Год назад +1

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

    • @rushidesai2836
      @rushidesai2836 Год назад +1

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

  • @ow_aqua8875
    @ow_aqua8875 Год назад

    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?

  • @rayyansyed2998
    @rayyansyed2998 Год назад

    "understood"

  • @anishraj66
    @anishraj66 Год назад

    What will be the time req to find indegree??

  • @mansisethi8127
    @mansisethi8127 10 месяцев назад

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

  • @TheSpiritualOne401
    @TheSpiritualOne401 Год назад

    Striver how do you remember all this?

  • @codingalley6229
    @codingalley6229 Год назад

    🐐

  • @avadhut325
    @avadhut325 Год назад

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

  • @shivyanshgarg2641
    @shivyanshgarg2641 10 месяцев назад

    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.

  • @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

  • @pratyakshhhhhhhhhhhhhhhhhhhhh
    @pratyakshhhhhhhhhhhhhhhhhhhhh 11 месяцев назад

    🎉

  • @SanidhyaPatel-q9s
    @SanidhyaPatel-q9s 4 месяца назад

    Which app do u use for teaching DSA in iPad?

  • @Kiruthika_vlogs
    @Kiruthika_vlogs 2 месяца назад

    where are you working bro

  • @shivamnegi7552
    @shivamnegi7552 Год назад

    whats the intuition

  • @deepakjain4481
    @deepakjain4481 8 месяцев назад

    //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

  • @soicooc3500
    @soicooc3500 7 месяцев назад

    4:00

  • @UmeshYadav-qx4hr
    @UmeshYadav-qx4hr 11 месяцев назад

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

  • @aniketambat6334
    @aniketambat6334 Год назад

    understood++

  • @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

  • @vaibhavjangid3112
    @vaibhavjangid3112 3 месяца назад

    :beatin💋💋💋

  • @akashkumarsingh8369
    @akashkumarsingh8369 Год назад

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

  • @aryankumar3018
    @aryankumar3018 Год назад

    Understood ntr

  • @mdsameer533
    @mdsameer533 2 года назад

    us

  • @antassachan1782
    @antassachan1782 2 года назад

    leetcode link for the question?

  • @cypher7536
    @cypher7536 2 года назад +3

    0:19 kya kaha 🤣🤣...?

  • @bemypro8602
    @bemypro8602 2 года назад

    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.

  • @lonersunited
    @lonersunited 9 месяцев назад +1

    Understood 😊

  • @gautamsaxena4647
    @gautamsaxena4647 6 месяцев назад

    Understood Bhaiya

  • @udaypratapsingh8923
    @udaypratapsingh8923 2 года назад +1

    understood