Topological sort | Course schedule 2 | Leetcode

Sdílet
Vložit
  • čas přidán 6. 07. 2020
  • This video explains a very important programming interview concept which is based on graph algorithm and is known as topological sort.This is a very important concept for solving graph problems based on ordering.We can find the topological sort using the simple DFS along with a STACK.Topological sort for a graph is only possible if it is a directed acyclic graph (DAG).I have explained the entire algorithm by taking proper examples and have also shown the code implementation using a problem course schedule 2 which is from leetcode #210.I have shown the code walk through at the end of the video.CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
    ========================================================================
    INSTAGRAM : / surya.pratap.k
    SUPPORT OUR WORK: / techdose
    LinkedIn: / surya-pratap-kahar-47b...
    WEBSITE: techdose.co.in/
    TELEGRAM Channel LINK: t.me/codewithTECHDOSE
    TELEGRAM Group LINK: t.me/joinchat/SRVOIxWR4sRIVv5...
    =======================================================================
    CODE LINK: gist.github.com/SuryaPratapK/...
    USEFUL VIDEOs:-
    Course Schedule: • Course Schedule | Dead...
    DFS: • Depth first search | D...

Komentáře • 206

  • @codestorywithMIK
    @codestorywithMIK Před 4 lety +89

    You teach a topic and then you solve a question based on that topic. This is the best approach of teaching. Thanks

    • @techdose4u
      @techdose4u  Před 4 lety +4

      Welcome

    • @neghatnazir1668
      @neghatnazir1668 Před 3 lety

      @@techdose4u hey i am a bit confused would you pls clear it? if we start at node 4 the order comes out to b 5 3 0 4 1 2 which is not correct i guess? would pls explain this to me i am not getting it?

    • @willturner3440
      @willturner3440 Před 3 lety

      @@neghatnazir1668 you are correct

    • @sreesayihrudai9541
      @sreesayihrudai9541 Před 3 lety

      @@neghatnazir1668 Try to run a loop from starting vertex to end vertex you will get the answer

    • @manishkumar-qn6lx
      @manishkumar-qn6lx Před rokem

      ​@@techdose4u Even if there exists a loop, still there is the possibility to cover all the courses.
      Ex. [[0,1], [2,0], [2,1], [3,1], [3,2]]. ( [0, 1], indicates that to take course 0 you have to first take course 1 )
      In the above example, there exists a loop but still, we can complete all the courses in the following order : [0,2,1,3].

  • @sainathy9225
    @sainathy9225 Před 4 lety +25

    @techdose, you are doing great job . I used to follow nich white and Kevin naughton . Now iam waiting for your videos. Keep doing!

  • @JayDonga
    @JayDonga Před 3 lety +4

    Great video, thank you for clear explanation. One addition here, instead of doing double work (pushing elements to stack and popping every element to get answer), you can add each new element on front of LinkedList and then return the LinkedList.

  • @amith1989
    @amith1989 Před 3 lety +3

    Quick, crisp and clear!! Fantastic way of explaining Topological sorting in 13 minutes.!

  • @kartikbhatt5924
    @kartikbhatt5924 Před 2 měsíci

    Thank you so much this channel, this channel focusses on solving all difficult problems based on advanced concepts. Love it

  • @rakeshsahni_
    @rakeshsahni_ Před 3 lety +3

    This video Clear all my doubts regarding topological sort, thank you sir

  • @meghamalviya8495
    @meghamalviya8495 Před 4 lety

    Thank you for great explanation..your approach to explain problems is best!

  • @shivammaniharsahu1228
    @shivammaniharsahu1228 Před 4 lety +1

    really good one keep sharing such vedios

  • @pepetikesavasiddhardha7852

    awesome explination of topological sort algorithm

  • @sheldon9415
    @sheldon9415 Před 4 lety +2

    Such a good and clear explanation ! Thank you for the time you put in making this awesome video!

  • @sharadmaheshwari5319
    @sharadmaheshwari5319 Před 3 lety +5

    your explanations goes smoothly in my mind, thanks for help !

  • @sakshamgupta2740
    @sakshamgupta2740 Před 3 lety +3

    graph videos are a real delight. Thanks for the content

  • @smartwork7098
    @smartwork7098 Před 10 dny

    Great work, man!

  • @ashishg656
    @ashishg656 Před 4 lety +1

    Great content :)

  • @mr_tpk
    @mr_tpk Před 3 lety

    Awesome 🔥🔥🔥

  • @electric336
    @electric336 Před 2 lety

    Very helpful video. You explained it very well.

  • @thedisciplinedguy
    @thedisciplinedguy Před 4 lety

    Doing a great work brother... Keep this spirit up... Your videos are really helpful.. 👌😊😊

  • @MarioFernandez-hx1cm
    @MarioFernandez-hx1cm Před rokem

    in three hours ive got my last exam at my carrer as engeneer, and this helped a lot. Hopepefully ill aprove, thanks a lot

  • @noobninja4882
    @noobninja4882 Před 4 lety +3

    perfect explanation sir, thanks a lot , following your graph series from the beginning , please continue this as i am learning a lot from this, and graph is my biggest fear

  • @harshbhadoria7200
    @harshbhadoria7200 Před 3 lety +3

    The day before yesterday I came to youtube for a solution to a question, as usual, randomly I picked your video. I don't know what happened after that day, whenever I search for the question on youtube I'm writing Techdose at the end.
    Conclusion: The way you teach and make us understand every topic is too good.

  • @superneutral1663
    @superneutral1663 Před 2 lety

    brilliant explanations,love all your videos

  • @joydeepbhattacharjee5305
    @joydeepbhattacharjee5305 Před 3 lety +1

    one of the best channel for CS in youtube...thank u so much for such awesome contents... and keep helping us like this..

  • @FWTteam
    @FWTteam Před 3 lety

    In search this was 3rd result, I'm glad I chose this.

  • @rajatgarg6859
    @rajatgarg6859 Před 2 lety

    It was amazing ,thanks buddy :)

  • @cristianandrei811
    @cristianandrei811 Před 2 lety

    great video

  • @sohamshinde1258
    @sohamshinde1258 Před 3 lety +2

    Best in class channel with the best in class explanation. Thankyou so much.

  • @thiyagarajanr225
    @thiyagarajanr225 Před 4 lety +2

    Content at its best! How much hours did you spend in making the videos bro? Efforts behind the scenes matters a lot! Usually i dont comment in any videos, but i used to comment to value your efforts 💖

  • @kiranathavanad8050
    @kiranathavanad8050 Před rokem

    good one

  • @pradnyabapat9531
    @pradnyabapat9531 Před 2 lety

    Thank you!!

  • @galeforceonline3847
    @galeforceonline3847 Před 3 lety +1

    Thanks. its good that you explain the subject using pseudo code aswell.

  • @spetsnaz_2
    @spetsnaz_2 Před 4 lety +11

    Such simple and elegant explanation can only be expected on TECH DOSE🍺

  • @bostonlights2749
    @bostonlights2749 Před 3 lety +5

    Quality content every time

  • @iampatelajeet
    @iampatelajeet Před 2 lety +1

    Amazing way of teaching bro, loved it..
    Keep it up.
    Thanks!

  • @agileprogramming7463
    @agileprogramming7463 Před 4 lety +3

    Wonderfully explained as always!

  • @shagunlamba6481
    @shagunlamba6481 Před 3 lety +3

    great explanation, recommending to everyone

  • @vidyakore3930
    @vidyakore3930 Před 2 lety +1

    thanks a lott, explained so nicely... keep making such videos!!

  • @AyushPandey0
    @AyushPandey0 Před 4 lety +2

    Excellent explanation sir, keep uploading more videos on graph problem.

  • @hiranyam
    @hiranyam Před 2 lety

    beautiful!

  • @poojanshah9302
    @poojanshah9302 Před 2 lety +1

    Very thorough explanation. I am always looking for your explanation before solving any Leetcode problem.

  • @fadiahetrakeshkumar9454
    @fadiahetrakeshkumar9454 Před 3 lety +1

    Nice video
    Best video for kahn algorithm

  • @shridharsarraf2188
    @shridharsarraf2188 Před 2 lety +1

    This is best playlist on CZcams to learn Graphs ❤❤❤❤

  • @kailashjangir1530
    @kailashjangir1530 Před 3 lety +2

    what if in starting we goes to node 3 rather than going to 2 in dfs, then answer would be changed na?

  • @manmeetsingh4272
    @manmeetsingh4272 Před 3 lety +1

    In the given example, from node 0 you first traversed to 2 then to 3 so the stack will be { 2, 1, 3, 0, ....} but if we traversed node 3 first before 2 then the stack will be { 1, 3, 2, 0, ....} as we see the stack is not in topological order. Because when we run our program in pc we don't know what node will be traversed first. Can you explain this case?

  • @anumonto
    @anumonto Před 4 lety +1

    simple and clear explanation.. Great!.

  • @amitavamozumder73
    @amitavamozumder73 Před 3 lety +2

    in 4:00 if I start from node 3, then the order will get messed up, right?

  • @AmanGupta-ud6sy
    @AmanGupta-ud6sy Před 3 lety +1

    Amazing video.....best explanation.

  • @pranjalgupta9427
    @pranjalgupta9427 Před 3 lety +2

    Can i also start from other vertex rather than 0?

  • @yihongliu3850
    @yihongliu3850 Před 3 lety +1

    really nice and clear

  • @swagatpatra2139
    @swagatpatra2139 Před 3 lety

    What about loops in the schedule? We need to return an empty array in that case.

  • @mwshubham
    @mwshubham Před 2 lety +2

    How could something be such easy. Thanks 😊🙏

  • @darshansimha2166
    @darshansimha2166 Před 3 lety +1

    Amazing Explanation!

  • @neerajpant4071
    @neerajpant4071 Před 4 lety +3

    last few seconds was awesome it's more like listening to an terms and conditions for an insurance policy 🤣😄

    • @techdose4u
      @techdose4u  Před 4 lety +4

      😅 I just want to wrap the unnecessary talks quickly 😅

    • @neerajpant4071
      @neerajpant4071 Před 4 lety +2

      @@techdose4u hehehe..correct
      Nice explanation. Always worth watching your videos.👍

    • @techdose4u
      @techdose4u  Před 4 lety +1

      Thanks.

  • @kunalsoni7681
    @kunalsoni7681 Před 4 lety +1

    😊😊😇😇☺️.. great video

  • @arindamchakraborty5880

    @TECH DOSE I am having one doubt, Can we write 0 before 4 in the top sort? like 5, 0, 4 likewise? Please clarify.

  • @willturner3440
    @willturner3440 Před 3 lety +1

    Thanks sir for such a great explanation, genius 🥰

  • @davngo
    @davngo Před 4 lety

    Your explanations and solutions are so beautiful.

  • @shaziasamreen8584
    @shaziasamreen8584 Před 4 lety +1

    Excellent Explanation..... Please upload leetcode daily challenge questions also..Your videos were very helpful..

  • @Learner010
    @Learner010 Před 4 lety +1

    very nicely explained !!!

  • @vishal40007
    @vishal40007 Před 2 lety

    I didn't think of using the stack. What is the thought process for solving the problem? How did you come up with stack?

  • @pranjalgupta9427
    @pranjalgupta9427 Před 3 lety +2

    Very nice explanation 👍👏😊

  • @learnfromramayan8176
    @learnfromramayan8176 Před 3 lety

    sir why cant we push the processed node and return answer following the exact method you taught in course schedule i. when we make a course processed=1 at that time why cant we push and add to result? Thank you in advance

  • @taskmaster4824
    @taskmaster4824 Před 3 lety

    buddy it is showing TLE in graph coloring ??

  • @mohammadshafihamidi1778

    if its possible i need some application of topological sort in field of providing accessibility in organic areas

  • @pcccmn
    @pcccmn Před rokem

    This approach is accepted by LC. Naturally because of the method `detectCycle`, the solution is beaten by many who got it done within 1 dfs method..But honestly, this solution should be taught first before attempting NeetCode's. This solution is way simpler to understand and implement!

  • @akm1237
    @akm1237 Před 3 lety +5

    Hello sir. Does it matter which edge we start with for the topological sort? Say we begin with 3, wouldn't 3 come before 2 in the stack?

    • @aparnamittal8653
      @aparnamittal8653 Před 3 lety

      No, it does not matter. 3 will come before 2 in stack but when we pop elements, it will result in coming at end in the order which is correct.

  • @i_mchick5311
    @i_mchick5311 Před 3 lety +2

    Hello sir ! can u also share the notes that u write while teaching ??

  • @jineshdave6451
    @jineshdave6451 Před 3 lety +2

    Best explanation ✨✨

  • @deepakjoshi4374
    @deepakjoshi4374 Před 6 měsíci

    I solved this Course Schedule I/II QS using the concept of Detect Cycle in a Graph (Using Colour Method). If there is a cycle you will not be able to complete the courses. else you can always take all the courses.
    For printing the courses, you can put into an array when you visited all its descendants.

  • @codebits2120
    @codebits2120 Před 3 lety +1

    Thanks 😊

  • @ayyappareddy4461
    @ayyappareddy4461 Před 3 lety +1

    Super explaination sirr

  • @anmolwadali9227
    @anmolwadali9227 Před 4 lety +2

    gud explanation !! Plz Make videos on Minium spanning tree kruskal & prims algo

    • @techdose4u
      @techdose4u  Před 4 lety

      It will eventually come. Making videos topicwise in order.

  • @shashanksinha6966
    @shashanksinha6966 Před 3 lety +1

    there is an extra recursive call to check cycle , we can do it using bfs in less time complexity

  • @Cat-uk2jx
    @Cat-uk2jx Před 2 lety

    Hello! I was doing this exercise I started with 4to 2 then I went to 4to 1. I then restarted at 5 to 0 to 3 and I got the following result 5,0,3,4,1,2. Is this correct? I read that one can start with any node (not just 5 or 4). Also I saw another comment with I different topological ordering which I also got 5,0,3,4,2,1. Can anyone explain why these are wrong? I am a beginner and I am having some difficulty grasping this concept. Thank you!

  • @supratikk
    @supratikk Před 3 lety +2

    I have implemented the same algo but just used the cycle detection method shown by you in the "course schedule1" problem. But its giving me a time Limit Exceeding error. Is that the reason why you slightly tweaked the cycle detention method in this problem?

    • @techdose4u
      @techdose4u  Před 3 lety +1

      I don't exactly remember the reason 😅

  • @pranavsharma7479
    @pranavsharma7479 Před 3 lety +1

    thanks for your work

  • @abhishekkumardwivedi3817

    sir, please try to add a video for egg dropping puzzle if possible

  • @intelligentprogrammer
    @intelligentprogrammer Před 3 lety

    Very well explained the logic :))
    But I could not get one thing in the code-->
    How are we making adjacency list??
    //Make adjacency list
    for(int i=0;i

  • @be_like__prateek
    @be_like__prateek Před 3 lety +1

    U r just awesome 💕

  • @josevalbuena9423
    @josevalbuena9423 Před 2 lety

    Why is the motivation to perform Khan’s algorithm/Topological Sort with a DFS approach to solve this problem while in the explanation of the topic is based of BFS? What’s the reason? Can both approaches be used to apply Topological sort?

  • @shivam.priyam
    @shivam.priyam Před 3 lety

    what if we add two more edge (5 -> 6) and (6 -> 2)

  • @manishkumar-qn6lx
    @manishkumar-qn6lx Před rokem

    ​ @TECH DOSE Even if there exists a loop, still there is the possibility to cover all the courses.
    Ex. [[0,1], [2,0], [2,1], [3,1], [3,2]]. ( [0, 1], indicates that to take course 0 you have to first take course 1 )
    In the above example, there exists a loop but still, we can complete all the courses in the following order : [0,2,1,3].

    • @techdose4u
      @techdose4u  Před rokem

      There is no cycle in your graph :)

  • @prasannakumarz
    @prasannakumarz Před 3 lety +1

    very good explanation

  • @vamsiKrishna96
    @vamsiKrishna96 Před 4 lety +1

    Tech Dose, Awesome Explanation. Your reach would increase if you can share the solutions in java.

    • @techdose4u
      @techdose4u  Před 4 lety +5

      Yea...but I think people should focus on learning logic and implement themselves. Already codes are available in all languages.

    • @vamsiKrishna96
      @vamsiKrishna96 Před 4 lety +1

      @@techdose4u Agreed! Great, explanation, Man! Cheers!

  • @mayankpatel6735
    @mayankpatel6735 Před 3 lety

    Bhai tabiyat sahi rakho aur videos banate raho, katai aag ho aap

  • @ShubhamKumar-zy7wy
    @ShubhamKumar-zy7wy Před 3 lety +2

    Bhagwan hai bhai ye

  • @gaurangmittal5563
    @gaurangmittal5563 Před 4 lety +1

    Sir why did u used a graph coloring method for a detecting cycle in a directed graph, as in your vidoe you have used another approach.

    • @techdose4u
      @techdose4u  Před 4 lety

      Graph Coloring is simple and works in O(V+E) otherwise we will have to use stack.

  • @harshithg5455
    @harshithg5455 Před 3 lety +1

    Super video! I applauded for ₹200.00 👏👏👏

  • @TomJerry-bp9ig
    @TomJerry-bp9ig Před rokem

  • @shreyasbhujbal8777
    @shreyasbhujbal8777 Před 4 lety +1

    baap!

  • @ameynaik2743
    @ameynaik2743 Před 3 lety +1

    Nice video. How did you find cycles in the graph?

    • @Sportamrina
      @Sportamrina Před 3 lety +1

      watch the course schedule video - it checks for cycles in the graph

  • @nayanjain5761
    @nayanjain5761 Před 3 lety +1

    In 1st video CS-I (Course schedule I) you created ajd by adj[i[0]].push_back(i[1]); i.e. 0->1 , and here inverse adj[i[1]].push_back(i[0]); 1->0. Also I checked for CS-I adj[i[1]].push_back(i[0]); is also passing, please explain. Thanks

    • @techdose4u
      @techdose4u  Před 3 lety +1

      If you are checking cycle then it doesn't matter how you take. It should be same for all the edges

    • @nayanjain5761
      @nayanjain5761 Před 3 lety

      @@techdose4u Thanks for this quick response and and all amazing content.

  • @JohnWick-kh7ow
    @JohnWick-kh7ow Před 2 lety +1

    Using recursion-
    bool isCyclic(vector adj[],vector& vis,vector& order,int curr){
    if(vis[curr]==0){
    return true;
    }
    if(vis[curr]==-1){
    vis[curr]=0;
    for(auto e:adj[curr]){
    if(isCyclic(adj,vis,order,e)){
    return true;
    }
    }
    order.push_back(curr);
    vis[curr]=1;
    }
    return false;
    }
    vector findOrder(int numCourses, vector& prerequisites) {
    vector adj[numCourses];
    vector vis(numCourses,-1);
    vector order;
    for(int i=0;i

  • @AmarKumar-vo2bv
    @AmarKumar-vo2bv Před 4 lety +1

    Please make video on finding articulation point

    • @techdose4u
      @techdose4u  Před 4 lety +2

      It will come in connectivity topic. I am covering topicwise. Have patience.

  • @afnaanrafique366
    @afnaanrafique366 Před rokem +1

    Why is simple BFS not a solution to this problem?

  • @gontlakowshik2345
    @gontlakowshik2345 Před 4 lety +2

    Sir, can you explain how find cycle in a graph

    • @spetsnaz_2
      @spetsnaz_2 Před 4 lety +3

      Already explained for directed and undirected graph... Check his Graph playlist

  • @rahulchudasama9363
    @rahulchudasama9363 Před 3 lety +1

    Awesome explaination, One suggestion Topological sort explaination video should come before the Reconstruct Itinerary video. So that flow can be maintained.

    • @techdose4u
      @techdose4u  Před 3 lety

      Okay i will reorder. Thanks for letting us know.

  • @programmingstuff5741
    @programmingstuff5741 Před 4 lety

    Bro please explain Leetcode Island Parameter problem

  • @tech_wizard9315
    @tech_wizard9315 Před 4 lety +3

    1)
    m a fresher, working in IT company of Jodhpur(9 hours job). If I prep DSA with c++ , 3-4 hrs each day for 3 months(leetcode) , can I gt job at Google,FB, Apple ? If not, then hw much time/months need?
    2) If.i have one project in my resume, is it enuf for project round of FAANG

    • @techdose4u
      @techdose4u  Před 4 lety +3

      You can crack job at AMAZON , Microsoft with this preparation but Google and apple requires you to distinguish yourself from the rest. Your resume should show what sets you apart from the rest. This prep is done by thousands of jobseekers and hence you need either an exceptional coding profile or an exceptional dev profile or both in a balanced way.

    • @tech_wizard9315
      @tech_wizard9315 Před 4 lety +1

      @@techdose4u is one project enuf?

    • @ManojKumar-so9kw
      @ManojKumar-so9kw Před 4 lety

      @@tech_wizard9315 nope man it wont be enough at any day for any of the product based companies

    • @techdose4u
      @techdose4u  Před 4 lety

      @@tech_wizard9315 one project is enough but it depends on what work you had done, the complexity and level of responsibility you were handling and the amount of effort you put. This defines your character as an SDE. Don't worry if you have 1 project, but the important thing is your dedication alongwith complexity and level of ownership of your project.

    • @exodus5948
      @exodus5948 Před 4 lety

      Bro , I am in a same situation 2020 batch and more from electronics batch from a tier 3 college, But I suggest you must make 2-3 Projects, OtherWise interviewer is definitely going to grill a lot. Best of luck for your preparations.

  • @lokeshpatel3914
    @lokeshpatel3914 Před 4 lety

    what if we get more than one components in graph and 1 component forms DAG and other does not ?

    • @AjayKumar-un2fz
      @AjayKumar-un2fz Před 4 lety +1

      Simply , we can't do topological sort because one of component is not DAG . We will got stuck in cycle while selecting courses for that component.

    • @lokeshpatel3914
      @lokeshpatel3914 Před 4 lety

      @@AjayKumar-un2fz
      But in such case an empty array can't be returned as output coz there must be some courses we can take as valid courses which forms different component .

    • @techdose4u
      @techdose4u  Před 4 lety +2

      Read this: stackoverflow.com/questions/36712146/if-topological-sort-uses-dfs-how-can-it-succeed-on-disconnected-graphs

  • @umapathybabu8397
    @umapathybabu8397 Před 4 lety +1

    After graph series, can you start with greedy