Topological Sorting | GeeksforGeeks

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

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

  • @genericperson8238
    @genericperson8238 6 лет назад +283

    I just want to express my appreciations to my Indian colleagues for producing so many high quality CS related content!

    • @ShayAxelod22
      @ShayAxelod22 5 лет назад +7

      CS as in Counter-Strike? C Sharp? Computer Science?

    • @Ajay-qv6fk
      @Ajay-qv6fk 5 лет назад +1

      @Taslim Arif I think he meant India produces good content for other two areas as well

    • @samirhossain6951
      @samirhossain6951 5 лет назад +1

      Fax

    • @modernlogix
      @modernlogix 5 лет назад

      @@Ajay-qv6fk XD

  • @deepkiran7249
    @deepkiran7249 6 лет назад +208

    Watch at 1.5 X speed

    • @timetraveler6780
      @timetraveler6780 6 лет назад +10

      watch at 2X.

    • @aadishjain6838
      @aadishjain6838 5 лет назад +27

      @@timetraveler6780 watch at 3 * 10^8 m/sec

    • @iiaamm
      @iiaamm 5 лет назад

      @@aadishjain6838 😂😂😂

    • @markevans179
      @markevans179 4 года назад

      @@aadishjain6838 Nailed it...Brooooooooooooo🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣

    • @avu72
      @avu72 4 года назад +2

      Try to listen at 0.5x, it's much more funnier🤣🤣🤣🤣🤣

  • @MincongHuang
    @MincongHuang 5 лет назад +12

    Thank you Illuminati for contributing this video, the explanation is clear and easy to understand.

    • @noumanmustafa7435
      @noumanmustafa7435 5 лет назад

      why are you calling him illumnati

    • @backslash8874
      @backslash8874 4 года назад +2

      @@noumanmustafa7435 Look at the description ! XD

  • @vishwajeetpawar3009
    @vishwajeetpawar3009 3 года назад +12

    really time saving and appropriate explanation ! we need these types of explanations in viva's

  • @allenchang8790
    @allenchang8790 4 года назад +7

    be careful that the graph you use for this has no cycles, if it does the algorithm will give you an incorrect ordering (impossible to do topological sort with cycles in the graph)

  • @baobaoma1937
    @baobaoma1937 7 лет назад +8

    It can also be applied when we want to assign level of each vertex

  • @chandrashekhar9470
    @chandrashekhar9470 4 года назад +5

    Best Ever Explanation.Please upload more video on Graphs as we have limited videos on Graph topics.Thanks

  • @raviprakash2413
    @raviprakash2413 6 лет назад +7

    According to geeksforgeeks "The first vertex in topological sorting is always a vertex with in-degree as 0 (a vertex with no incoming edges)". Why you here chose 'A' as starting vertex?

    • @trunksgx1
      @trunksgx1 6 лет назад +7

      The first vertex in a topologically sorted array of vertices has an in-degree of 0. We haven't yet built a topologically sorted array at the time of picking 'A'. We pick 'A' in order to begin building that array.

    • @raviprakash2413
      @raviprakash2413 6 лет назад

      Fine

    • @will-ye
      @will-ye 4 года назад +7

      To clarify what he said, when you START topological sorting, you can choose any vertex. However, the output after top sorting is a vertex with an in-degree of 0.

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

    Настали времена когда индусы объясняют лучше чем лекторы с вмк

  • @ABHMughal
    @ABHMughal 5 лет назад +19

    GeeksforGeeks is illuminati, CONFIRMED!
    **plays X-files theme**

  • @subhamsahoo451
    @subhamsahoo451 6 лет назад +3

    The video is understood easily. It would be better understood if u can take an array of elements and construct the graph and then do the topological sort.

  • @indiansoftwareengineer4899
    @indiansoftwareengineer4899 5 лет назад +2

    can we use BFS for Topological sort?
    if not then why can't?
    DFS vs Topology? would they give same/different results?

  • @SaddamHussain-tl8kp
    @SaddamHussain-tl8kp 4 года назад +4

    what is illuminati??

  • @sheerakm2929
    @sheerakm2929 3 года назад +6

    question for anyone who can answer: How do we know we start from a source vertex? thanks

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

      just start at the beginning of your bool visited array and skip items if they get turned to true along the way

  • @AmitSharma-wx1wj
    @AmitSharma-wx1wj 6 лет назад +2

    Ok I have a question here! I understood the tutorial but how the for loop is running from "0 to V" . I mean the code is written for A->B... vertices and the vertices in the code snippet (in function topologicalSort) is shown for vertices 0->1... ; I know the question sounds stupid but what if the vertices were assigned numbers starting from 99? I think m missing something.. thanks?

    • @borgir6368
      @borgir6368 3 года назад +1

      indexing always start with 0 that A, B, C.. are just representation similarly you can't start a vertex from 99..

  • @AmanAbidi1
    @AmanAbidi1 5 лет назад +1

    you didn't discuss the condition for starting node i.e. can we start DFS which has an incoming edge e.g. swap the id's of A and B, then start DFS with A?

    • @it41tanmayaron26
      @it41tanmayaron26 4 года назад

      You got the answer???

    • @AmanAbidi1
      @AmanAbidi1 4 года назад

      @@it41tanmayaron26 Since we only have one way directed edges and there is no cycle we have to provide the condition for starting nodes. I used a naive approach to avoid starting with such nodes which have an incoming edge. We add an extra cost of O(|V|) for finding the root nodes by traversing the whole DAG, say ROOTS. Then we perform Topological sort starting from each root node i in ROOTS. The extra cost can be neglected w.r.t to the total runtime of Topological sort.

  • @debaratighatak2211
    @debaratighatak2211 3 года назад +1

    Thanks a lot Illuminati!

  • @robertsun5529
    @robertsun5529 5 лет назад +1

    Despite the heavy accent, the material is really clearly instructed. Gj

  • @Asgard961
    @Asgard961 7 лет назад +6

    Splendid explanation! Btw, as a non-native speaker i'm incredibly thankful for your accent:D

  • @Coldking12345
    @Coldking12345 7 лет назад +4

    Now implement that script but with weighted edges.

    • @RAJATTHEPAGAL
      @RAJATTHEPAGAL 4 года назад

      I think then you can resort to take a edge list implementation, where the list is a min heap or max heap based on requirement. So that it doesn't only picks based on unvisited but also considers weight. I think something like this is used in task schedulers like spark driver or yarn 😅 not sure

  • @ivanvs289
    @ivanvs289 6 лет назад +4

    Thanks for sharing your knowledge mate! Very well explained

  • @kazialrefatpranta2479
    @kazialrefatpranta2479 3 года назад

    the example has cycles in it, right? but topological sort is for acyclic graphs? right?

  • @NatanGoldstein
    @NatanGoldstein 5 лет назад +2

    Very clear and easy to follow, thanks!

  • @SomchaiRitik
    @SomchaiRitik 6 лет назад +4

    I got stuck at geeksforgeeks code, after seeing this video it completely cleared

  • @danishahmadansari7410
    @danishahmadansari7410 3 года назад

    Respect! GFG is lifeline fro indian CSE students

  • @ShayAxelod22
    @ShayAxelod22 5 лет назад +1

    Do they force Indians to make tutorial videos as an assignment? They just seem to be the only ones with tutorials on RUclips even though they aren't the only ones that know this. Good job people

  • @MRDrums240
    @MRDrums240 5 лет назад +5

    DAMMIT!!! SUCH A GOOD EXPLANATION! :D

  • @sushs8812
    @sushs8812 6 лет назад +4

    Very well explained, thanks!

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

    Nice Video, I like explanation and sweet voice :)

  • @nightwingrobin2958
    @nightwingrobin2958 7 лет назад

    how do you implement this code for a graph of letters. I'm getting an error "Segmentation Fault: 11"

    • @sarthaksahu117
      @sarthaksahu117 7 лет назад

      Use unordered_map if you are using c++, if you use java then use hashmap.

  • @thefuntech2810
    @thefuntech2810 4 года назад +1

    I think that i also done this same question my B.F.S also kis kisko lagta hai ki BFS se bhi ho sakta hai 🙄

  • @RAJATTHEPAGAL
    @RAJATTHEPAGAL 4 года назад

    In the explanation, I did not understand how G is never inserted to stack till very end. since all it's neighbours are visted and SortUtil end statement is reachable for G node ryt. So G should be much deeper in stack. How did it jumped to D. Am I getting something wrong. Great explanation though.

    • @vaiebhavpatil2340
      @vaiebhavpatil2340 3 года назад

      there can be more than one topological ordering for any DAG, so u can construct another topological ordering with G much deeper in the stack too

  • @abdelazizshokair
    @abdelazizshokair 5 лет назад +3

    Would the time and space complexity of this algorithm both be O(N)?

    • @amey7064
      @amey7064 4 года назад

      What is N here?

    • @anthonyruffino2212
      @anthonyruffino2212 3 года назад

      At the end he says the time complexity is O(V + E). This is just his way of explicitly naming what the factors are. V + E is linear meaning that, the time complexity is O(N).
      He did not answer about the space complexity, but I think it would also be constant. Each node can hold the data about whether it was visited and which neighbors it has. There should be no reason to have to exponentially save more data when the list of nodes increases. It would be linear.

    • @borgir6368
      @borgir6368 3 года назад

      @@anthonyruffino2212 space complexity is O(V) due to stack

  • @Dan-tg3gs
    @Dan-tg3gs 4 года назад

    how do you know which node to start with

  • @wessamo.1338
    @wessamo.1338 6 лет назад +1

    thank you so much💕. You saved me! I have a final exam today.

  • @ruvixreyez
    @ruvixreyez 4 года назад

    Is it Depth first traversal is De same to Topoligal sort? Like why is has Different name but same algo?

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

    Quick and clear, thank you so much!!!!

  • @codeforfun6685
    @codeforfun6685 5 лет назад +1

    Can we apply this for weighted graph?

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

    DFS and Topological are the same?

  • @KanagawaMarcos
    @KanagawaMarcos 6 лет назад +2

    After pushing A to the stack, how did he visit C?

    • @travelspurs
      @travelspurs 6 лет назад +4

      Marcos C. Santos he is moving alphabetically and so after pushing A he moved to B but as B is already visited, he then moved to C.😊✌

    • @KanagawaMarcos
      @KanagawaMarcos 6 лет назад +1

      Real Bonanza thank you. :D

    • @amanraj5116
      @amanraj5116 6 лет назад +1

      @@travelspurs but u can visit any unvisited node like G ,D and then procede because u can see topological sort is not unique

    • @amanraj5116
      @amanraj5116 6 лет назад +1

      @@travelspurs am i right?

    • @amanraj5116
      @amanraj5116 5 лет назад

      @Taslim Arif Thanks

  • @jindal0001
    @jindal0001 7 лет назад +3

    Really well explained. Thanks a lot!

  • @clee6030
    @clee6030 5 лет назад

    so how do you check if there is a cycle?

    • @n9wox
      @n9wox 4 года назад

      If using an adjency matrix to represent the graph, then for a given edge adjMatrix[i][j] check adjMatrix[j][i] to see if there's a corresponding edge. If so, then the graph is cyclical.

    • @it41tanmayaron26
      @it41tanmayaron26 4 года назад

      Try DFS. If program doesn't stop compiling, then you are stuck in a cycle

  • @jaydeepmojidra133
    @jaydeepmojidra133 3 года назад

    GreeksforGeeks Need no introduction 👌

  • @jayshree7574
    @jayshree7574 4 года назад

    simple short and useful

  • @KURD1SHONE
    @KURD1SHONE 7 лет назад

    How come after we push D into the stack we go to G and not J? Would we get the same result if we visited J then G?

    • @darkstarggez2103
      @darkstarggez2103 6 лет назад +2

      like he said at the start of the video a graph can have many topological sorts the one represented above is just one of many others

    • @RAJATTHEPAGAL
      @RAJATTHEPAGAL 4 года назад

      @@darkstarggez2103 Graph can have many topological sort based on starting node I suppose. But for starting node of G, G should be deep inside the stack ryt after C is put inside. ..... for this one. According to the C++ code the recursion os sortutil would end with insertion of G to stack

  • @bhargavkrishnamurthy2411
    @bhargavkrishnamurthy2411 7 лет назад +60

    Author Illuminati! 😂 xD

  • @manasratanmallik1866
    @manasratanmallik1866 7 лет назад +2

    you did not explain time complexity........

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 лет назад +7

      Hi Manasratan,
      Topological Sorting takes the DFS approach to print the nodes. And, it's time complexity is same as that of a DFS: O(V+E). The only difference here is that we maintain an extra stack where we temporarily store nodes. So, the space complexity becomes O(V). Hope it helps.

  • @savin1999
    @savin1999 6 лет назад +1

    Can i get a C code for this question?

  • @vetriveltamilcalendar4674
    @vetriveltamilcalendar4674 7 лет назад +1

    Clean explanation. Thanks.

  • @sachinpandey1057
    @sachinpandey1057 5 лет назад

    Afyer sorting A, why u directly move to D, why not G???

    • @sriv
      @sriv 5 лет назад

      You can move to any unvisited node, but the person explaining is visiting nodes in alphabetical order

  • @ijyoyo
    @ijyoyo 3 года назад

    Real Great! Thank you

  • @tomreingold4024
    @tomreingold4024 3 года назад

    Best explanation!

  • @jungkookseuphoricfield4199
    @jungkookseuphoricfield4199 4 года назад +1

    Mann...wait...
    Contributed by Illuminati.????

  • @kiranmallikarjun8618
    @kiranmallikarjun8618 6 лет назад

    here also we use only stack but why

    • @n9wox
      @n9wox 4 года назад

      You can use an array. When you're done, the nodes in the array are in reverse order.

  • @avu72
    @avu72 4 года назад +1

    Thoda jaldi bola karo yaar kuuch logon ko subah panvel nikalna hai

  • @jefinagilbert5977
    @jefinagilbert5977 3 года назад

    this topological sort doesnt make sense

  • @sheemaalmaas4774
    @sheemaalmaas4774 7 лет назад +8

    so is topological sorting same as DFS ??

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 лет назад +14

      No.
      In DFS, we print a vertex and then recursively call DFS for its adjacent vertices. In topological sorting, we need to print a vertex before its adjacent vertices. So Topological sorting is different from DFS. For example, a DFS of the shown graph is “5 2 3 1 0 4”, but it is not a topological sorting.
      This better explained with the help of a diagram over here: www.geeksforgeeks.org/topological-sorting/

    • @sheemaalmaas4774
      @sheemaalmaas4774 7 лет назад

      Ok understood it better
      Thank you

    • @coversongs8732
      @coversongs8732 6 лет назад +1

      noooooooooo, in no way, its sorting technique whereas dfs is traversal

    • @leeamraa
      @leeamraa 6 лет назад +2

      simply put: topological ordering uses DFS to scan/traverse the graph in every round.

    • @ShujathHussain0
      @ShujathHussain0 6 лет назад +4

      from programming point, traversal method is same, while DFS uses queue to print its values, Topological sort uses stack.
      Bottom line: reversing the DFS values gives you topological sort

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

    Does it work if graph have cycles

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

    Very helpful! Thank you :)

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

    beautiful 2see old school c++ code
    xD

  • @kautukraj
    @kautukraj 4 года назад +1

    Very helpful!

  • @rohitbisht4036
    @rohitbisht4036 7 лет назад +1

    Your subtitles comes over the video content

  • @bhuvneshmahajan6573
    @bhuvneshmahajan6573 6 лет назад

    Very clearly explained

  • @sachinpandey1057
    @sachinpandey1057 5 лет назад

    using this technique my answer is little Deferent,

  • @aazimtamboli8006
    @aazimtamboli8006 7 лет назад

    very nice explanation

  • @harshakonda5
    @harshakonda5 7 лет назад +3

    great explanation!

  • @rohitkumarsinha746
    @rohitkumarsinha746 5 лет назад +1

    Thank for making good content video

  • @anverHisham
    @anverHisham 5 лет назад

    Nice explanation. Thanks a lot :-)

  • @mnchester
    @mnchester 3 года назад

    great vid

  • @feng915
    @feng915 7 лет назад +1

    really clear explanation!!

  • @coversongs8732
    @coversongs8732 6 лет назад

    bearable only at speed of 2

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

    Thank you sir

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

    thank you

  • @edoran3315
    @edoran3315 4 года назад

    illuminated explanation
    funny accent

  • @hyperbola_cc
    @hyperbola_cc 5 лет назад

    Good tutorial

  • @harshavardhans4681
    @harshavardhans4681 4 года назад

    1.75X speed

  • @ResidualSkill
    @ResidualSkill 5 лет назад

    Amazing

  • @geekgeek5397
    @geekgeek5397 6 лет назад +1

    illuminati members hit like

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

    3:10

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

    Ratta marke bola sub kuch😂😂😂😂😂😂😂😂😂😂😂😂😂

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

    thank you!:D

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

    Improve voice plz

  • @pauljose7849
    @pauljose7849 6 лет назад

    Jesus bless you.Thank you

  • @maamaamoo
    @maamaamoo 7 лет назад

    Thank you

  • @vineetkothari2839
    @vineetkothari2839 7 лет назад

    great going

  • @coversongs8732
    @coversongs8732 6 лет назад

    great explanation

  • @santhoshn_norm
    @santhoshn_norm 5 лет назад

    Thank 🙏💕u so much sir

  • @lokeshraj5460
    @lokeshraj5460 6 лет назад

    that funny fake voice tone!

  • @TOONSSTATION
    @TOONSSTATION 5 лет назад

    Hey Illuminati.🤣🤣

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

    1.25x

  • @jananisridhar8266
    @jananisridhar8266 6 лет назад

    thanks man!

  • @hardikagarwal7652
    @hardikagarwal7652 4 года назад

    Very poor explanantion