Topological Sort Algorithm | Graph Theory

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ม.ค. 2025

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

  • @translucentleaf6163
    @translucentleaf6163 5 ปีที่แล้ว +52

    The world needs more heroes like you who can spend less than 15 minutes explaining what takes certain professors at least two hours. This was really intuitive for both newcomers and detailed enough to actually wrote code with it. Thank you!

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

    Clear, concise, great animation! Thank you so much buddy.

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

      I read comments before watching a tutorial, thanks for the comment.

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

    Mentioning not only DAG but Tree gives me a lot of intuitions... That's why I watch this channel even after I graduated and got a job. Studying never ends definetly. Thank you William.

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

    I searched for a video on this topic and this is the only one I found that explained it so well. Great job, I will share it with my classmates.

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

    These tutorials are excellent. I wish I could thumbs up 10 times. I'm studying for coding interviews and this is the best tool I have come across for understanding graph theory problems.

  • @patrickchan7891
    @patrickchan7891 5 ปีที่แล้ว +13

    Omg!You are just the perfect one demonstrating these concepts so clearly.It's really friendly for beginners to learn.Thank you very much!

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

    The way you explain stuff visually helps me understand the algorithms well. Thanks a ton!

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

    Finally understood it, much clearer than the exposition in my class. I now understand even why it works to give a topological ordering based on how you pop the call stack.

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

    I understood enough to complete my quiz at 1:06, great video!

  • @mohsinhayatt
    @mohsinhayatt 5 ปีที่แล้ว +5

    This is the best explanation video for a graph algorithm. Can't get any better

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

    The fact you can clearly explain the concept and Algo under 15mins, proves you have mastered this topic and gain deep understanding, kudos.

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

    Thanks!

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

    Thank you for this. I have a final in my Data Structures and Algorithms course today, and I didn't understand this subject. Now I do. Thank you.

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

    Thanks William. Watched the whole MIT DFS class for Topological sort, finally understood it here.

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

    This is a great tutorial! Really well explained; visually and in detail.

  • @SrushtiPawar-vi6ns
    @SrushtiPawar-vi6ns 11 หลายเดือนก่อน +3

    The number of times I have watched this video and number of times I have forgotten this concept is really funny!

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

    The best explanation I could find online!

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

    It was not clear to me why the sorting process could starts from any node of the graph. I finally found the answear in your animiation. Thanks a lot.

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

    I understand that I’m a bit late to the party but this is genuinely a brilliant explanation of Top Sort! Thank you so much!❤️❤️

  • @MotivationYoucanandyouwi-hg2zt
    @MotivationYoucanandyouwi-hg2zt ปีที่แล้ว +1

    Thanks a lot for this video, the algoritham is easy to understand because of the animation!

  • @GMatos-qg4ec
    @GMatos-qg4ec ปีที่แล้ว +1

    Finally a great video! Thanks William

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

    Another great mind at explaining things the way I like to understand them.
    First video I've seen on your channel. Instant subscribe and set notification on 'All'!

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

    Man you make difficult topic easy to understand, very nice video. It is very great , how you represent topics in abstract way:)

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

    Your explanation is clear and examples are interesting.

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

    Wow this tutorial is amazing! Made perfect sense to me!

  • @孙贺-g5m
    @孙贺-g5m 2 ปีที่แล้ว +1

    Thank you, very clear and precise explanation.

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

    Your videos are very well produced, thank you very much for the efforts you put into them! Like some other folks have said, I wish Kahn's algorithm was also covered, it is pretty intuitive too and allows for cycle detection.

    • @WilliamFiset-videos
      @WilliamFiset-videos  4 ปีที่แล้ว +2

      Thanks for the suggestion Louis, Kahns truly is a really great algorithm. It's especially good for beginners because it's soo simple and intuitive. I'll put together a video if I find the time

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

    Well structured and well explained tutorials. Thank you.

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

    Clear explanation and nice code!

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

    Very well explained! Thank you! At this point of my online uni, i'm just skim through the lecture recording and just search up related topic videos on youtube to learn. Why can't the uni just gave us a list of youtube videos to learn.

  • @davidm.johnston8994
    @davidm.johnston8994 3 ปีที่แล้ว +1

    Thanks a lot William ! This is really helpful.

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

    This is a really good tutorial! Thank you for your work!

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

    An application of DAGs is representing circuits as a collection of gates, where subsequent gates require the inputs of previous gates, which is a perfect problem to use topological sorting on as it allows for iterative solutions to circuit propagation!

  • @Lime-rr6te
    @Lime-rr6te 4 ปีที่แล้ว +3

    its the best video ever uploaded to you tube.
    EVER,
    LIKE FOR EVER.
    (DABs like a boss)
    God Damm it feels good to be me.

    • @Lime-rr6te
      @Lime-rr6te 4 ปีที่แล้ว +1

      This guy knows how to live life

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

    This video saved so much time. God bless you pal : ))

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

    The good things is you went from total basics and that's what matters for understanding

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

    Thank you so much, so clear and concise.
    Appreciate it.

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

    Clean, precise, awesome!!

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

    Loved this !!!! Thaaaaanks a bunch ! Well explained !!

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

    Thanks for this amazing video !

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

    Kudos for explaining the topological sort in simplest way possible 🙂

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

    Great explanation! Thank you.

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

    Nice and easy explanation. Love it! I've got one little question, though. Let's take this tree you presents at 4:46 and let's pretend that these nodes are some functions in a program. When A got execute, B, C and D don't depend anymore on anything and could potentially run on separate threads. Let's also say that I know how threads and synchronization primitives work. How to split/detect what nodes can run concurrently? Is there any algorithm for it?

  • @chinmaygajjardeveloper
    @chinmaygajjardeveloper 2 ปีที่แล้ว

    Thank you for great explanation!

  • @devanshmesson2777
    @devanshmesson2777 3 ปีที่แล้ว

    Thank you so much.
    A clear explanation!!

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

    you are genius

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

    Awesome video. Thanks!

  • @conde3915
    @conde3915 ปีที่แล้ว +47

    can you explain it in fortnite terms?

    • @Quietlamacakes
      @Quietlamacakes 24 วันที่ผ่านมา +1

      When you’re getting in a game you gotta first gather up all the OG’s (good players) first and then get the newbies (bad players) if you don’t have any more OG’s left. You need someone (Topological sort) to tell you which players are better than others in order to do this.

  • @bbatroll
    @bbatroll 25 วันที่ผ่านมา

    data structures final on monday 😿💔saved me a ton of stress, thank you! 💗

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

    GREAT, love the animation.

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

    The tree example is unconnected with your algorithm for TopSort; If the TopSort algorithm is using indegree then the tree example will be really good as an introduction as they are using similar idea. It'd be great if you could always explain the runtime complexity. For this one, why it is O(V+E)?

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

    Might be good to explain how to use the topo sort result to then actually solve the course scheduling problem (for example).

  • @08JuHan
    @08JuHan 2 ปีที่แล้ว

    kudos! thanks a lot for posting

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

    Thank you very much!!

  • @reyou7
    @reyou7 3 ปีที่แล้ว

    such a great animation, thanks!

  • @kmishy
    @kmishy 3 ปีที่แล้ว

    Lots of hard work sir

  • @mtjokro
    @mtjokro 3 ปีที่แล้ว

    Amazing viz, thank you!

  • @huuarethey
    @huuarethey 3 ปีที่แล้ว

    thank you william

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

    Again, Such a great video!

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

    nice explanation.!

  • @prabhakarpalanivel6472
    @prabhakarpalanivel6472 5 ปีที่แล้ว

    Nicely done, thanks!

  • @jingchaozhang408
    @jingchaozhang408 4 ปีที่แล้ว

    10:59 We already has an array V to record the visited node. Why do you define another array visitedNodes?

  • @BreadWinner330
    @BreadWinner330 4 ปีที่แล้ว

    Thanks man, you're the best!

  • @raymondyoo5461
    @raymondyoo5461 6 ปีที่แล้ว

    Wow Thanks so much. Animation helped a lot

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

    THANK YOU SO MUCH

  • @oussamaabdelwahed5594
    @oussamaabdelwahed5594 4 ปีที่แล้ว

    GREAT explanation , Thank's

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

    Hi, First of all thank you for your explanation. I find your videos very helpful. But I have a question about the example code of your git repository about topological sort.
    graph.get(0).add(new Edge(0, 1, 3));
    graph.get(0).add(new Edge(0, 2, 2));
    graph.get(0).add(new Edge(0, 5, 3));
    graph.get(1).add(new Edge(1, 3, 1));
    graph.get(1).add(new Edge(1, 2, 6));
    graph.get(2).add(new Edge(2, 3, 1));
    graph.get(2).add(new Edge(2, 4, 10));
    graph.get(3).add(new Edge(3, 4, 5));
    graph.get(5).add(new Edge(5, 4, 7));
    int[] ordering = topologicalSort(graph, N);
    // // Prints: [6, 0, 5, 1, 2, 3, 4]
    System.out.println(java.util.Arrays.toString(ordering));
    So my question: if node 6 doesn't have relation with any node in any role, why the program return array : [6, 0, 5, 1, 2, 3, 4]?

    • @WilliamFiset-videos
      @WilliamFiset-videos  ปีที่แล้ว

      Node 6 doesn't have any dependencies, and no other node depends on it, therefore it can be placed anywhere in the topological ordering

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

    Thank you.

  • @nguyenvankhanhduy3958
    @nguyenvankhanhduy3958 3 ปีที่แล้ว

    Great content man.

  • @greaterthanKTWS
    @greaterthanKTWS 6 ปีที่แล้ว

    Awesome work man.

  • @zss123456789
    @zss123456789 4 ปีที่แล้ว

    Sorry if this is a dumb question...
    From the animation you said that you could do topological sorting by choosing nodes at random and dfs down their children.
    But in the code, if I'm reading it correctly, you're just going in the order of the adjacency list.
    Are there any trade-offs here?
    Thanks, and great video for a tough topic!

    • @WilliamFiset-videos
      @WilliamFiset-videos  4 ปีที่แล้ว

      You don't usually know how your DAG is structured/labelled exactly, so beginning at node 0 -> node n-1 is effectively random. You could traverse all the nodes in any permutation and you'll still get a topological sort. To answer your question, I'm not aware of any performance benefits of trying one particular permutation over another.

    • @zss123456789
      @zss123456789 4 ปีที่แล้ว

      @@WilliamFiset-videos I see, that makes sense, thank you so much!

  • @japhethobala3753
    @japhethobala3753 3 ปีที่แล้ว

    Thanks a lot.

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

    Can you also do a video based on Kahn's Algorithm for topological sort, based on inDegrees rather than outDegrees?

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

    Hi! visitedNodes plays a role of a stack, why you iterate it from the first element but not pop last to get order[i]?

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

    top top explanation

  • @rohan8arora
    @rohan8arora 5 ปีที่แล้ว

    thank you. extremely helpful. :D

  • @studytime4048
    @studytime4048 3 ปีที่แล้ว

    In case of a rooted tree, is it sufficient to do the level order traversal to get the topological ordering?

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

    Thanks for this awesome video, I have a question.
    What if when only C and B vertices are remaining and we choose B vertex randomly, will that be correct too? Because choosing B will result in B being added to list and then again selecting C randomly(the only vertex remaining and that will be added at last)
    Will that be correct?

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

      the result is still CB..., i think the key of post order is, let's say we have A and B, if A can lead to B then B would be after A(A...B), but if A can't lead to B, then we have 2 possible results: 1. B lead to A(B...A) 2. B and A don't have anything to do with each other, the relative position of them doesn't matter anymore(that also means A...B, or B...A are both accepted). That's why we can have multiple results.

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

    Simply beautiful

  • @justinmlawrence
    @justinmlawrence 3 ปีที่แล้ว

    So good!

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

    thanks so much, may I ask, what do you mean by "canonical" example? (1:59)

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

      canonical (adj)
      MATHEMATICS
      relating to a general rule or standard formula.
      Basically, it just means this is a very common example throughout computer science, often used when teaching or describing the topic (toposort that is).

  • @ra-ph-ael
    @ra-ph-ael 4 ปีที่แล้ว

    This is sooo good!

  • @shreyashachoudhary480
    @shreyashachoudhary480 3 ปีที่แล้ว

    Concise++!

  • @alicianguyen2625
    @alicianguyen2625 2 ปีที่แล้ว

    Why is it important that we only append the value when we're finishing the recursive call, and then reverse it, rather than appending when we mark a node as visited and not needing to reverse the output?

  • @lautarolavecchia5995
    @lautarolavecchia5995 5 ปีที่แล้ว

    very useful

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

    i have a question. it might be dumb but anyway, when discussing topological sorting, shouldn't we start from a source node, i.e., a node that doesn't depend on any other nodes? In your example, you started from node H; is that correct?

  • @jadanabil8044
    @jadanabil8044 3 ปีที่แล้ว

    SSSP finding using topological sort approach, when would I not use this? Seems fastest for the usecase.

  • @maksimisayenka2058
    @maksimisayenka2058 2 ปีที่แล้ว

    great video

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

    any reason why you use a list and have to worry about/handle an index, when you could just use a stack? and pop from stack to get the ordering? it seems much easier to do without having to code up extra stuff to handle the index.

  • @iitianexplains6405
    @iitianexplains6405 4 ปีที่แล้ว

    THANKS

  • @XnDxNdXnD
    @XnDxNdXnD 5 ปีที่แล้ว

    Exellent work

  • @enoshcohen
    @enoshcohen 4 ปีที่แล้ว

    Thank you for this video!
    What happens in a case that we have a cycle. I'm pretty sure that the algorithm that you provided does not cover this end-case.

    • @deepakr.sastry756
      @deepakr.sastry756 4 ปีที่แล้ว +1

      You can't topologically sort a graph that has a cycle. 3:45

    • @enoshcohen
      @enoshcohen 4 ปีที่แล้ว

      @@deepakr.sastry756 that’s correct.
      You still need to check end cases. A user can input a graph that is not a DAG.

    • @deepakr.sastry756
      @deepakr.sastry756 4 ปีที่แล้ว

      @@enoshcohen 4:39 check using Tarjan's

  • @usman8872
    @usman8872 3 ปีที่แล้ว

    if i have to reach D, the array says , i have to visit K ???

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

    the short form that I've heard is "toposort" rather than "topsort"

  • @jasonli1060
    @jasonli1060 3 ปีที่แล้ว

    BEAUTIFUL

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

    Will this algorithm work if the graph has cycle? Or being a DAG is a prerequisite for using this algorithm?

    • @bbatroll
      @bbatroll 25 วันที่ผ่านมา +1

      late reply but yes it is a prerequisite

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

    Lovely!

  • @harsha4692
    @harsha4692 4 ปีที่แล้ว

    9:17 from the given solution , K is given before I but K does not have a path to I. Is it supposed to happen,can you please tell me?

  • @edwardhuahui6197
    @edwardhuahui6197 6 ปีที่แล้ว

    How should i know which node is the starting or randomly choose?

  • @vishaltk3013
    @vishaltk3013 3 ปีที่แล้ว

    The numerical node values and counters were giving me a hard time understanding the code. So I wrote the same code for a different type of graph data. You don't need to maintain index for ordering array as the graph node data is of string type and we can easily concat them. Here's the code.
    package com.graph;
    import com.google.common.collect.Lists;
    import java.util.*;
    public class TSortPractice {
    static class EdgeV2 {
    String to;
    String from;
    public EdgeV2(String from, String to) {
    this.to = to;
    this.from = from;
    }
    }
    static int n = 5;
    static Map graph;
    static String ordering = " ";
    static Set visitRegister;
    public static void main(String[] args) {
    graph = new HashMap(n);
    visitRegister = new HashSet(n);
    graph.put("a", Lists.newArrayList(new EdgeV2("a", "b"), new EdgeV2("a", "c")));
    graph.put("b", Lists.newArrayList(new EdgeV2("b", "d")));
    graph.put("c", Lists.newArrayList(new EdgeV2("c", "d")));
    graph.put("d", Lists.newArrayList(new EdgeV2("d", "e")));
    topSort();
    System.out.println(ordering);
    }
    static boolean notVisited(String node){
    return !visitRegister.contains(node);
    }
    static void doVisit(String node) {
    visitRegister.add(node);
    }
    static void dfs(String currentNode) {
    doVisit(currentNode);
    if(graph.containsKey(currentNode)) {
    for(EdgeV2 edgeV2: graph.get(currentNode)) {
    String nextNode = edgeV2.to;
    if(notVisited(nextNode))
    dfs(nextNode);
    }
    }
    ordering = currentNode+" "+ordering;
    }
    static void topSort() {
    Set nodes = graph.keySet();
    for(String node: nodes) {
    if(notVisited(node))
    dfs(node);
    }
    System.out.println("top sort done");
    }
    }