Topological Sort Algorithm with DFS | Course Schedule in JAVA | Code and Implementation

Поділитися
Вставка
  • Опубліковано 25 сер 2024
  • Please consume this content on nados.pepcoding.com for a richer experience. It is necessary to solve the questions while watching videos, nados.pepcoding.com enables that.
    NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. This video explains the Topological Sort algorithm for acyclic directed graphs with weights. Topological Sort algorithm is a permutation of vertices for a directed acyclic graph if for all directed edges uv, u appears before v in the graph. We then code and implement the job sequencing problem where:
    1. You are given a directed acyclic graph. The vertices represent tasks and edges represent dependencies between tasks.
    2. You are required to find and print the order in which tasks could be done. The task that should be done at last should be printed first and the task which should be done first should be printed last. This is called topological sort.
    Check out the video for details.
    For a better experience and more exercises, VISIT: www.pepcoding....
    #graphs #topologicalsort #algorithms
    Have a look at our result: www.pepcoding....
    Follow us on our FB page: / pepcoding
    Follow us on Instagram: / pepcoding
    Follow us on LinkedIn: / pepcoding-education
    Join us on Telegram: t.me/joinchat/...

КОМЕНТАРІ • 141

  • @nishantbanjade920
    @nishantbanjade920 3 роки тому +58

    Sir u spent 40 minutes explained so nice 4-5 times , and at last still said agar samaj nahi aaya toh dubara banaunga. you won my heart

    • @glorysingh
      @glorysingh 2 роки тому +2

      so true.. sir aap ne bhot achha samjhaya.. thank you soooooo much sirr.....

  • @sudhanshusharma9123
    @sudhanshusharma9123 3 роки тому +21

    Thank you sir for the clear explanation of Topological Sort! No additional explanation is required.

    • @Pepcoding
      @Pepcoding  3 роки тому +2

      I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem.
      If you like our efforts, we request a review
      g.page/Pepcoding/review?rc
      You can subscribe to our channel here
      ua-cam.com/users/Pepcodingabout?view_as=subscriber

    • @dark_techyy
      @dark_techyy 2 роки тому +1

      Bhai cyclic ke liye kaam ni kr rha ye algo?

    • @prashantgupta6160
      @prashantgupta6160 8 місяців тому

      Topological sort ki requirement h directed acyclic graph

  • @vikasupadhyay7916
    @vikasupadhyay7916 2 роки тому +5

    Generally I don't comment on videos, but this is really brilliant... all the questions and doubts, which were there in my mind were covered in this video. For ex: i knew preOrder will not work, but an accurate explaination was not clear to me. This gave me the explaination. Also i have seen many pepcoding videos, these are alway best

  • @adyakshsharma5057
    @adyakshsharma5057 3 роки тому +9

    I don't think there's a better explanation of this out there 💯

    • @Pepcoding
      @Pepcoding  3 роки тому +1

      I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem.
      If you like our efforts, we request a review
      g.page/Pepcoding/review?rc
      You can subscribe to our channel here
      ua-cam.com/users/Pepcodingabout?view_as=subscriber

    • @glorysingh
      @glorysingh 2 роки тому

      truuuuee........

  • @0_0-0_0.
    @0_0-0_0. 2 роки тому +2

    Mujhe lagta tha topological sort bahut jyada hard hoga but aapke explain krne ke baad mai kisi ko bhi topological sort padha sakta hu 🔥🔥

  • @angadrajsingh4311
    @angadrajsingh4311 3 роки тому +2

    Sir matlab alag hi dedication hai aapka humaare lie.. pata hi ni chalta aapke saath padke time ho gaya . Btw great explanation sir

  • @alkas7366
    @alkas7366 2 роки тому +1

    Most underrated channel on youtube. Awesome videos.

    • @Pepcoding
      @Pepcoding  2 роки тому +1

      Glad you liked it.
      Keep learning and growing.
      And for better experience and well organised content explore nados.pepcoding.com

  • @sanjeev0075
    @sanjeev0075 3 роки тому +6

    amazing lecture....at 15:25 to explain why preorder will not work, not only independent component but in single component also preorder will not work e.g. the component with nodes 4, 5 and 6...if traversal was considering the node 6 first from 4 then preorder will give 4, 6, 5 as output but there is edge 5, 6 in the component. I hope it helps.

  • @abhishekankitsoti
    @abhishekankitsoti 2 роки тому +1

    understood in first 7 minutes...mazaa aane laga hai ab

  • @shoaibakhtar9194
    @shoaibakhtar9194 3 роки тому +4

    Best tutorial for graph, I've ever seen. I will surely recommend this to my friends

  • @tushar2jain
    @tushar2jain Рік тому

    I dnot know how to describe my feelings . itna acha explanation maine baar sunna . Hats Off to you Sumeet sir !! 🫡

  • @glorysingh
    @glorysingh 2 роки тому +1

    Sir this is the best explanation ever..... thanks a million times.... 🤗😊

  • @ankitamehra5045
    @ankitamehra5045 4 роки тому +3

    Amazing sir.. bahut ache se samajh aaya. don't worry about it

    • @Pepcoding
      @Pepcoding  4 роки тому +2

      Chalo cool hai. Mujhe lga repeat karna padega.

  • @umangkumar2005
    @umangkumar2005 2 роки тому +1

    I had seen many tutorial on youtube but your level of explaining is top notch please made more video

    • @Pepcoding
      @Pepcoding  2 роки тому +2

      nados.io pe jaie aur content dekhie. wahan humari 10K videos hain, youtube pe sirf 3K. wo bhi free he hai.

  • @ritiktwopointo2691
    @ritiktwopointo2691 4 роки тому +4

    kya video thi ye waali :), iteratiev dfs bhi bahut badiya thi :)

  • @samadeepsengupta
    @samadeepsengupta Рік тому

    Respect and Love... Always ⭐

  • @gauravsrivastava7958
    @gauravsrivastava7958 3 роки тому +1

    Best explanation ever! Hats off to Sumeet Malik

    • @Pepcoding
      @Pepcoding  3 роки тому +3

      Glad to know that you liked the content and thank you for appreciating.
      The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos.
      So, keep motivating, keep learning and keep loving Pepcoding😊

  • @aman6561
    @aman6561 3 роки тому +3

    haaa sr baat smjh me aagyi kaafi efforts daale hai aapne

  • @harshagarwal1218
    @harshagarwal1218 2 роки тому +1

    OP Explanation🔥🔥

  • @doomhead9109
    @doomhead9109 2 роки тому

    boht acche se smj aaya sir

  • @BharatSingh-qv1wj
    @BharatSingh-qv1wj 3 роки тому

    best video ...!!! here and sir what an explanation loved itt........!!!

    • @Pepcoding
      @Pepcoding  3 роки тому +2

      Thanks a ton. keep motivating, keep learning and keep loving Pepcoding😊

  • @rohanraonallani561
    @rohanraonallani561 4 роки тому +1

    Very much excited for level up

    • @kampatiraviteja7398
      @kampatiraviteja7398 4 роки тому

      does sir post level up videos also on youtube?

    • @rohanraonallani561
      @rohanraonallani561 4 роки тому

      @@kampatiraviteja7398 yes

    • @Pepcoding
      @Pepcoding  4 роки тому +1

      I pray to god that I atleast do 10 questions per day.

    • @kampatiraviteja7398
      @kampatiraviteja7398 4 роки тому

      @@Pepcoding if pray to god to give me such a dedication u had ...respect !!

  • @bhautiksavaliya3635
    @bhautiksavaliya3635 3 роки тому +1

    Great content!
    Thank you sir..

  • @rounakchourasia2030
    @rounakchourasia2030 3 роки тому +1

    Thanks a lot for the free content :)

    • @Pepcoding
      @Pepcoding  3 роки тому +2

      Glad to know. If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms )

  • @abhishekankitsoti
    @abhishekankitsoti 2 роки тому +2

    One small request...if you could please upload one video...vertical order traversal where overlapping nodes need sorting...I know Rajneesh Sir has already uploaded it but need to understand it even better...Thank you

  • @fudankumar5206
    @fudankumar5206 3 роки тому +1

    Amazing sir ....🙏🙏🙏🙏🙏🙏

    • @Pepcoding
      @Pepcoding  3 роки тому +1

      Thankyou beta!
      I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
      If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )

  • @mansi3042
    @mansi3042 2 роки тому +2

    Excellent explanation 👍

    • @Pepcoding
      @Pepcoding  2 роки тому +1

      Glad, it was helpful. For complete content, check out nados.pepcoding.com

  • @amandixit3555
    @amandixit3555 3 роки тому +2

    What if there is a cycle in the graph , then topological sort is not possible and Ans should rather be -1.

  • @amolgautam488
    @amolgautam488 2 роки тому

    Thank you so much.

    • @Pepcoding
      @Pepcoding  2 роки тому

      Always welcome. For better experience and precisely arranged content visit on nados.io, even you can post your doubts on nados.io

  • @SandeepSharma-co5tl
    @SandeepSharma-co5tl 3 роки тому

    Imandari ki misaal ho sir aap

  • @ishanjain6935
    @ishanjain6935 3 роки тому

    Superb explaination🎉🎉

  • @anjneykumarsingh4461
    @anjneykumarsingh4461 3 роки тому

    Sir bhut ache se clear, thanku

  • @ayushkoul5065
    @ayushkoul5065 3 роки тому

    Great Explaination !!!

  • @ishikajain6115
    @ishikajain6115 4 роки тому +1

    Very well explained sir

  • @manpreetsinghrana7751
    @manpreetsinghrana7751 Рік тому

    Sumeet Malik the OG!

  • @anckoor
    @anckoor 2 роки тому

    excellent

    • @Pepcoding
      @Pepcoding  2 роки тому

      Thank you! Cheers! For better experience and well organised content sign up on nados.io

  • @sourabhsharma9070
    @sourabhsharma9070 4 роки тому +4

    sir please try to cover LinkedList , Stack&Queue first in the levelup course. These topics are small and not linked with other topics like recursion or /tree.

    • @Pepcoding
      @Pepcoding  4 роки тому +8

      bet stack and queues foundation mei he kaafi sahi ki hai. levelup ka order hoga
      backtracking -> bits -> dynamic programming -> trees -> hashmap and heaps -> graphs
      Stack and Queues and Linked Lists last mei karenge

  • @harshitkaushik4144
    @harshitkaushik4144 3 роки тому

    Sir crystal clear .

    • @Pepcoding
      @Pepcoding  3 роки тому

      😍🥰❤️🙏🏼

  • @lavishmalik5520
    @lavishmalik5520 4 роки тому +5

    Sir i just finished basic ds algo concepts....now i want to start practicing question should i directly start with your list or first practice from somewhere else?
    Pls tell...

    • @Pepcoding
      @Pepcoding  4 роки тому +6

      You can do this
      docs.google.com/spreadsheets/u/1/d/1XdXJbn9NC7fx1CeavItkxR0Yos8rQAC9xXjnH4-f6Eg/edit#gid=0
      Or I will start levelup 2mrw. 10 questions everyday. You could also do that.

  • @jayeshtak2530
    @jayeshtak2530 5 місяців тому

    Will it handle back edges of if the graph is cyclic? , I am getting some errors. There are no conditions to check if the graph is cyclic.

  • @nandinibagga4857
    @nandinibagga4857 3 роки тому

    Thankyou for sharing!

  • @akashmishra3369
    @akashmishra3369 Рік тому +1

    Sir, what to do if the graph has a cycle ?

  • @shreyasaxena3967
    @shreyasaxena3967 3 роки тому

    Sir faang list k solutions ki Bh vedios banadijeye plss...!!! It will be soo helpful .

  • @umangkumar2005
    @umangkumar2005 2 роки тому

    sir please made more videos on this graph

  • @tarat.techhh
    @tarat.techhh 3 роки тому

    thank you

  • @w3java265
    @w3java265 3 роки тому

    Osm sir ji

  • @AnkitSharma-wj2tb
    @AnkitSharma-wj2tb 4 роки тому +1

    Waiting for this .

    • @Pepcoding
      @Pepcoding  4 роки тому +1

      A very important question here is
      1. Did you watch it complete?
      2. Did you get the "Whys" that I tried to explain?
      I am not satisfied with this video. Are you?

    • @AnkitSharma-wj2tb
      @AnkitSharma-wj2tb 4 роки тому

      @@Pepcoding 1) i watched it 4 times sir .❤️
      2) i have doubts but parallel to this i take reference from gfg Because i code in c++. And as far as the satisfaction level concerned For me it is 4/5 . I am satisfied.

    • @Pepcoding
      @Pepcoding  4 роки тому

      I will redo it. Mza nahi aya

    • @rajendrakumawat9568
      @rajendrakumawat9568 3 роки тому

      @Pepcoding Sir can you please put this Java code here , I have a bit confusion while writing the code 🙏

  • @arijitdey8419
    @arijitdey8419 3 роки тому

    Sir plz continue with graph lectures...

  • @ashigoel4828
    @ashigoel4828 4 роки тому +1

    Sir levelup ke roadmap ki ek video daal dijiye, ki hum kis order mein konse konse topics padenge

    • @Pepcoding
      @Pepcoding  4 роки тому +2

      here it is
      1. Backtracking
      2. Bit Manipulation
      3. Dynamic Programming
      4. Trees
      5. Hashmap and Heaps
      6. Graphs
      Iske baad dekhenge

  • @kartik4063
    @kartik4063 2 роки тому

    Sir I think there is some mistake because you said order of compilation is reverse of topological sort but in the end you did not reverse it. Popping elements from the stack will actually give the topological sort. Thank you

    • @kashba
      @kashba 2 роки тому

      that is asked in the question, it is just sir wanted us to know order of work is reverse of topo sort

  • @amitshah817
    @amitshah817 3 роки тому

    Great content 👍

    • @Pepcoding
      @Pepcoding  3 роки тому

      Glad you think so! and If you like the content could you post something on LinkedIn about us? This will help us in reaching out to more people and help a lot of other students as well
      Something like this
      Sumeet Malik from Pepcoding is making all his content freely available to the community
      You can check it out here - www.pepcoding.com/resources
      /
      Also, this is the youtube channel - ua-cam.com/users/Pepcodingplaylists?view_as=subscriber

    • @amitshah817
      @amitshah817 3 роки тому

      @@Pepcoding sure sir , will share it with friends

  • @darshankumar-ev5zi
    @darshankumar-ev5zi 3 роки тому

    indegree lekar bi to kar sakte hai na ? sare nodes with indegree equals zero queue mein jayege.. Then bfs as usual

  • @rikinjain5309
    @rikinjain5309 3 роки тому +1

    Sir, sari videos hai but bridges in graph ki video nahi mili

    • @Pepcoding
      @Pepcoding  3 роки тому +1

      hanji, level2 mei dalunga. wo bhi aur union-find bhi

  • @aditya7778
    @aditya7778 4 роки тому +2

    Sir, I am new here please guide me.From where I have to start?
    How much time I have to give?
    What should be done first?...
    Please sir help me..

    • @Pepcoding
      @Pepcoding  4 роки тому +2

      ua-cam.com/video/2IdlrHxdla4/v-deo.html
      refer this video

    • @aditya7778
      @aditya7778 4 роки тому

      @@Pepcoding That was epic!
      Thank you so much sir,
      One more query sir,
      Sir the 300 questions that you said in the video about 'concepts' are present in your foundation course or not? Have a nice day sir..
      You are awesome.

  • @utkarshsharma6650
    @utkarshsharma6650 3 роки тому +1

    adj list use karna thi rehta h ya adj matrix?

  • @ayushgoel9584
    @ayushgoel9584 4 роки тому +1

    Sir the mock interview which you will conduct on 20th webinar , will be open ended like you will ask questions to students on youtube or you will conduct it with some person on zoom.

    • @Pepcoding
      @Pepcoding  4 роки тому +4

      beta, ek favourite student hai jisne bhot struggles ki aur apne bal boote tier 3 college se microsoft, amazon aur traveloka crack kia. He has been a part of my teaching journey. We as a teacher-student pair failed and succeeded together. Use bhot insight hai, in saari struggles ki. Referrals wagaerah ki bhi. Uska mock interview mai lunga aur last mei usse advise lenge.

    • @kashba
      @kashba 2 роки тому

      @@Pepcoding representing GTBIT tier 3 //belongs to the sardars but claimed by JEE dropouts :P

  • @bradbale1510
    @bradbale1510 4 роки тому +2

    Sirji levelup ? checking channel every 5 mins :)

    • @Pepcoding
      @Pepcoding  4 роки тому +5

      beta jo aj bnaunga use kal padhna. 6 bje he shuru karta hun. 12 bje tak bnaunga. 10 ban jaengi. tum mujhse ek din peeche chalo. mai aj 10 bnaunga, aap kal 10 padho.

    • @bradbale1510
      @bradbale1510 4 роки тому +1

      @@Pepcoding Ok Sir thank you

  • @someshraichandani
    @someshraichandani 4 роки тому +1

    sir post order me hmesha children pe pehle kaam hoga uske parent se ?

    • @Pepcoding
      @Pepcoding  4 роки тому +2

      Yes. kyunki children ka post order parent se pehle aata hai

  • @AKASHKUMAR-li7li
    @AKASHKUMAR-li7li 2 місяці тому

    Can there be multiple toplogical sort of a graph...??

  • @priyanshu-1922
    @priyanshu-1922 3 роки тому

    Sir,.
    Like push_front in vectors.. Is there any function in arraylist?
    If it is so. , then we can use arraylist instead of stack

    • @Bankai-kamishininoyari
      @Bankai-kamishininoyari 9 місяців тому

      for that in java we use queue , and if you need both the functionalities of a stack adding at last and adding in front like a queue , in java there is a different datastructure for that know as deque, and if you only want to use the array list and push in front then you can do arr.add(index , element ) this will push the element in the specific index but the time complexity will become O(n) as all the elements will be needed to shift one place

  • @siddharthrawat7063
    @siddharthrawat7063 2 роки тому +1

    Need help in leetcode 207 question.

    • @Pepcoding
      @Pepcoding  2 роки тому +1

      For solving all your doubts visit on community tab on nados.io

  • @Melody-ld8kp
    @Melody-ld8kp 3 роки тому

    Can you explain some examples?

  • @jemmydangi3230
    @jemmydangi3230 2 роки тому

    you missed the edge case when there is input forms cycle

    • @Pepcoding
      @Pepcoding  2 роки тому +1

      topological sort works only for acyclic graphs (when there is a cycle, topological sort is not possible)

  • @angadrajsingh4311
    @angadrajsingh4311 3 роки тому

    Sir bs ek request thi , arrays and strings ke bhi daal do

    • @Pepcoding
      @Pepcoding  3 роки тому +1

      hanji. Kar rhe hain beta.

  • @satyamsharma8718
    @satyamsharma8718 4 роки тому +1

    Hello sir.
    Sir apne ek comment main kaha tha ki 16 August se levelup ke question Aa jayega
    So jo question ayenge wo foundation main he add ho jayenge ki levelup ka ek agal se Playlist rahega.
    Or ip ka bhi?

    • @Pepcoding
      @Pepcoding  4 роки тому

      Alag playlist bnega beta aur, site pe yahan milega
      www.pepcoding.com/resources/data-structures-and-algorithms-in-java-levelup

    • @Pepcoding
      @Pepcoding  4 роки тому +1

      IP ka bhi alag playlist and alag course

    • @satyamsharma8718
      @satyamsharma8718 4 роки тому

      @@Pepcoding but sir link open ho raha hai but ek bhi question nahi hai
      Means topic hai but question nahi hai usame..

    • @satyamsharma8718
      @satyamsharma8718 4 роки тому

      @@Pepcoding thank you sir.
      For always replying 😇

  • @saarthakaggarwal7018
    @saarthakaggarwal7018 3 роки тому +1

    What if the topological sort is not possible? for example: [[0->1],[1->0]]

  • @ishikajain6115
    @ishikajain6115 4 роки тому +1

    Sir level up ki video kb upload kroge app?

    • @Pepcoding
      @Pepcoding  4 роки тому +1

      beta subh se kuch kuch nipta rha tha. abhi shuru karunga. raat tak 10 daal ke he sounga. aap mujhse ek din peeche chalein (time waste nahi hoga). Mai roj 10 daalunga (aap next day 10 kar dein)

  • @CodeCraftWithVinayak
    @CodeCraftWithVinayak 4 роки тому +1

    First viewer me.

    • @Pepcoding
      @Pepcoding  4 роки тому +3

      A very important question here is
      1. Did you watch it complete?
      2. Did you get the "Whys" that I tried to explain?
      I am not satisfied with this video. Are you?

    • @CodeCraftWithVinayak
      @CodeCraftWithVinayak 4 роки тому +1

      @@Pepcoding yes I watched completely, understood Whys as well. you are trying to tell if an edge running from U to V, u must be completed before V. and using stack to
      keep dependency vertices first
      I guess with queues also we can do it but completely different approaches.
      anyways thank you Sumeet sir for the detailed explanation.

    • @Pepcoding
      @Pepcoding  4 роки тому +3

      @@CodeCraftWithVinayak should I redo this one?

    • @CodeCraftWithVinayak
      @CodeCraftWithVinayak 4 роки тому +3

      Not required. Thanks for reply.

  • @jatinbhatoya8420
    @jatinbhatoya8420 2 роки тому

    sir agar main directed cyclic graph de doon tbb bhi yeh code chal rha hai. tbb toh nhi chalna chaiye tha. aisa kyu

    • @kashba
      @kashba 2 роки тому

      kyuki vo visited mark kar deta hai. isliye ruk jaata hai cyclic ghumne se
      fir us component se stack me daal ke next component pe chale jaate hai
      hume saara tam jhaam karne se pehle ye check karna cahiye ke cyclic hai to return -1