Topological Sort Algorithm | Graph Theory

  Рет қаралды 487,840

WilliamFiset

WilliamFiset

Күн бұрын

Пікірлер: 172
@translucentleaf6163
@translucentleaf6163 5 жыл бұрын
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 жыл бұрын
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 жыл бұрын
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 Жыл бұрын
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 жыл бұрын
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 жыл бұрын
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 жыл бұрын
The way you explain stuff visually helps me understand the algorithms well. Thanks a ton!
@aryangod2003
@aryangod2003 Жыл бұрын
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 жыл бұрын
I understood enough to complete my quiz at 1:06, great video!
@mohsinhayatt
@mohsinhayatt 5 жыл бұрын
This is the best explanation video for a graph algorithm. Can't get any better
@y2k898
@y2k898 2 жыл бұрын
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 жыл бұрын
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 жыл бұрын
Thanks William. Watched the whole MIT DFS class for Topological sort, finally understood it here.
@dirkvanbeveren5042
@dirkvanbeveren5042 6 жыл бұрын
This is a great tutorial! Really well explained; visually and in detail.
@SrushtiPawar-vi6ns
@SrushtiPawar-vi6ns 11 ай бұрын
The number of times I have watched this video and number of times I have forgotten this concept is really funny!
@otabek_kholmirzaev
@otabek_kholmirzaev 2 ай бұрын
yes :)
@akshaymalhotra597
@akshaymalhotra597 5 жыл бұрын
The best explanation I could find online!
@garamburito
@garamburito 3 жыл бұрын
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 жыл бұрын
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 Жыл бұрын
Thanks a lot for this video, the algoritham is easy to understand because of the animation!
@GMatos-qg4ec
@GMatos-qg4ec Жыл бұрын
Finally a great video! Thanks William
@diamantberg
@diamantberg 4 жыл бұрын
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 жыл бұрын
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 жыл бұрын
Your explanation is clear and examples are interesting.
@Theycallmehoff
@Theycallmehoff 2 жыл бұрын
Wow this tutorial is amazing! Made perfect sense to me!
@孙贺-g5m
@孙贺-g5m 2 жыл бұрын
Thank you, very clear and precise explanation.
@Kalessin89
@Kalessin89 4 жыл бұрын
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 жыл бұрын
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 жыл бұрын
Well structured and well explained tutorials. Thank you.
@chenjason2598
@chenjason2598 Жыл бұрын
Clear explanation and nice code!
@danverzhao9912
@danverzhao9912 3 жыл бұрын
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 жыл бұрын
Thanks a lot William ! This is really helpful.
@tinfuyiu3940
@tinfuyiu3940 3 жыл бұрын
This is a really good tutorial! Thank you for your work!
@sido7740
@sido7740 2 жыл бұрын
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 жыл бұрын
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 жыл бұрын
This guy knows how to live life
@aarzoo_chourasia
@aarzoo_chourasia 6 жыл бұрын
This video saved so much time. God bless you pal : ))
@umairalvi7382
@umairalvi7382 3 жыл бұрын
The good things is you went from total basics and that's what matters for understanding
@Anubis10110
@Anubis10110 4 жыл бұрын
Thank you so much, so clear and concise. Appreciate it.
@gokulkumarbhoomibalan5413
@gokulkumarbhoomibalan5413 3 жыл бұрын
Clean, precise, awesome!!
@harinijeyaraman8789
@harinijeyaraman8789 4 жыл бұрын
Loved this !!!! Thaaaaanks a bunch ! Well explained !!
@akshitg
@akshitg 6 жыл бұрын
Kudos for explaining the topological sort in simplest way possible 🙂
@sanskarsharma9494
@sanskarsharma9494 3 жыл бұрын
Thanks for this amazing video !
@Hugesbaz
@Hugesbaz 7 ай бұрын
Great explanation! Thank you.
@sc5shout
@sc5shout 2 жыл бұрын
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 жыл бұрын
you are genius
@evgenirusev818
@evgenirusev818 4 жыл бұрын
Awesome video. Thanks!
@conde3915
@conde3915 Жыл бұрын
can you explain it in fortnite terms?
@Quietlamacakes
@Quietlamacakes 23 күн бұрын
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 жыл бұрын
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 жыл бұрын
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 жыл бұрын
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 жыл бұрын
Can you also do a video based on Kahn's Algorithm for topological sort, based on inDegrees rather than outDegrees?
@annazaitseva6213
@annazaitseva6213 3 жыл бұрын
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 жыл бұрын
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!
@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?
@eug_gino
@eug_gino 5 жыл бұрын
thanks so much, may I ask, what do you mean by "canonical" example? (1:59)
@SchoolVideosGoHere
@SchoolVideosGoHere 5 жыл бұрын
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++!
@lautarolavecchia5995
@lautarolavecchia5995 5 жыл бұрын
very useful
@jadanabil8044
@jadanabil8044 3 жыл бұрын
SSSP finding using topological sort approach, when would I not use this? Seems fastest for the usecase.
@stefaniaionita6694
@stefaniaionita6694 2 ай бұрын
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?
@iitianexplains6405
@iitianexplains6405 4 жыл бұрын
THANKS
@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.
@maksimisayenka2058
@maksimisayenka2058 2 жыл бұрын
great video
@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 жыл бұрын
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
@XnDxNdXnD
@XnDxNdXnD 5 жыл бұрын
Exellent work
@DiggOlive
@DiggOlive 5 жыл бұрын
the short form that I've heard is "toposort" rather than "topsort"
@usman8872
@usman8872 3 жыл бұрын
if i have to reach D, the array says , i have to visit K ???
@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 күн бұрын
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?
@extremeloco23
@extremeloco23 6 жыл бұрын
Edward Huahui random
@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"); } }
Topological Sort | Kahn's Algorithm | Graph Theory
13:32
WilliamFiset
Рет қаралды 135 М.
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 411 М.
СИНИЙ ИНЕЙ УЖЕ ВЫШЕЛ!❄️
01:01
DO$HIK
Рет қаралды 3,3 МЛН
When you have a very capricious child 😂😘👍
00:16
Like Asiya
Рет қаралды 18 МЛН
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН
TSAAI M5-20. Travelling salesman problem
6:29
Laboratorio Inteligencia Artificial Aplicada UMA
Рет қаралды 66
Lecture 14: Depth-First Search (DFS), Topological Sort
50:31
MIT OpenCourseWare
Рет қаралды 454 М.
The hidden beauty of the A* algorithm
19:22
Polylog
Рет қаралды 918 М.
G-21. Topological Sort Algorithm | DFS
13:30
take U forward
Рет қаралды 357 М.
How Dijkstra's Algorithm Works
8:31
Spanning Tree
Рет қаралды 1,4 МЛН
Rust Functions Are Weird (But Be Glad)
19:52
Logan Smith
Рет қаралды 147 М.
Coding Adventure: Boids
8:35
Sebastian Lague
Рет қаралды 1,6 МЛН
What is DAG?
5:22
ness-intricity101
Рет қаралды 103 М.