Shortest/Longest path on a Directed Acyclic Graph (DAG) | Graph Theory

Поділитися
Вставка
  • Опубліковано 22 вер 2024

КОМЕНТАРІ • 75

  • @DriLLFreAK100
    @DriLLFreAK100 6 років тому +5

    Thank you very much William. This just saved my life.

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

      But I don't get you, how can this video save your life?

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

      @@adhishmalviya2408 He might have some crazy loan and this video helped him crack an interview which led to repayment 🤷‍♂️
      So much more you can think of.

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

      @@rashmidhant3364 Yes, you might be right.

  • @MrMacAwsome
    @MrMacAwsome 6 років тому +56

    For the longest path, can't we just change the shortest length function to look for the max between the two distances we are comparing?

    • @vincentleclere70
      @vincentleclere70 4 роки тому +12

      Yes, it the same as changing sign. If you code from scratch it's better to use max, if you use available code the trick is usefull.

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

      Yeah I think both solutions work just fine but this is a Computing algorithm, meaning that it is likely present in code and it is much more efficient to just negate all the values than to rewrite an entire function.

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

      @Gabriel Wilder fuck off and stop trying to scam people

    • @bionictortoise
      @bionictortoise 2 роки тому +6

      You'd also have to initialize the initial values to negative infinity first instead of positive infinity

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

      @@bionictortoise Unless you're using Java in which case "Integer dist = new Integer[totalNodes]" will default to all null values.

  • @ythalorossy
    @ythalorossy 5 років тому +2

    @WilliamFiset Thanks you so much for the explanation.
    I am watching and sharing your work. These materials are amazing.

  • @GOUSETEIN
    @GOUSETEIN 6 років тому +4

    Love ur explanation , waiting for next set of videos on algorithms. All the best :)

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

      what explanation ? the guy is just talking to himself .. this is not how explanations are done there is a fundamental difference between actually explaining and just reading his code .

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

      This is one of best lectures on graph theory on UA-cam dude. He worked for 2 years to prepare this. This guy is really good in teaching. Only thing is you need to have patience.

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

    On the first graph at 0:38, is that not a cycle middle lower portion of the graph, between three nodes? (You said it was acyclic)

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

    Great video, just what I was looking for. Thanks!

  • @nareshnepal6879
    @nareshnepal6879 5 років тому +6

    This gives solution only for the source taken as the first node that comes after the topological sorting is done.... how can this be extended so that any node can be source??

    • @ythalorossy
      @ythalorossy 5 років тому +1

      Could you create a video explaining the problem and the solution for "how can this be extended so that any node can be source??" ?

    • @noname-it2up
      @noname-it2up 4 роки тому +3

      Just change the distance to your starting node to 0 in the dist array and make the rest null or infinity and then only nodes reachable from your start node will be updated when iterating over the topological sorting of nodes

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

      You can directly go to the source node(preferred) in the topological order and start the same algo from the source node in the topological order.
      And the shortest paths to the nodes that are lying to the left of source node is "infinity" (Because there exists no path).

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

      it might also happen that there is no path to some elements on the right of the source node in the topological order. For instance, take the example from the video, use as source node node B and remove the edge from B to C. The topological order would still be correct but now there is no path from B to C. This might break the code I think if no other changes are made than those suggested above.

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

    Hi William. I think there is a mistake when you say that all trees are DAGs by definition (in the first minute), since trees are undirected (as you note a few seconds later in the video), please correct me if I'm wrong. Thank you very much for the video series :)

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

      I couldn't find any definition of trees where they talk about whether trees are supoosed to be directed or undirected by definition. So, whether a tree is a DAG or not ultimately boils down to whether it is directed or undirected, IMHO.
      Hope this helps.

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

    How does it work for -be edges ? As for example if D to E has weight of -20 then shortest path to E should have been updated as per this algo. Here d to e is mentioned as -4 which might be the reason that edge was not considered.

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

    Hi William, question regarding the code that you shared. On line 81 you are walking over all the node(assuming nodes are from 0 to numNodes). but in case the topological ordering is such that all the nodes from 0 to start node actually fall behind the start node in topological order, in this case wont we have wrong values for the distance for all those nodes(infinite?). eg in a grpah as: 4->0->1->2->3 where 4 is strtNode value, line 81 will go from 0->4, but topological order will be 4,0,1,2,3, so by the time we reach node 4 we will only update dist for 0 . am i missing something? please let me know. also i am confused regarding the use of topsort array in general in the code snippet.

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

      Not null check on line 84 will take care of that, distance will be calculated only when start index comes in the loop, thus only the descendants of start will be given values and taken care, others would be null (i.e infinity according to his logic)

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

    How about showing the shortest and longest path instead of calculating weight/distance?

  • @菲尔-w3u
    @菲尔-w3u Рік тому

    When we do Longest paths by flipping signs for edge weights, do we need to topologically sort the vertices or no? Is topological sort necessary for every instance where we have negative edges in our DAG?

  • @ahsanulameensabit
    @ahsanulameensabit 5 років тому

    joss👍 the idea of longest path is fantastic...

  • @mehranjalali5023
    @mehranjalali5023 5 років тому +1

    William great explanation. How would you solve this problem for a cyclic graph?

    • @WilliamFiset-videos
      @WilliamFiset-videos  5 років тому +3

      On a regular graph, I would recommend Dijkstras algorithm. There's also Bellman ford and Floyd warshall u can look into.
      ua-cam.com/video/pSqmAO-m7Lk/v-deo.html

  • @Am-ug9np
    @Am-ug9np 2 роки тому

    But how do you backtrack to actually find the path? Following along with this, all I could manage was finding the length of the path. I don't see how knowing the shortest path to every node actually helps you backtrack.

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

      Keep a parent array and update the parent node at each relaxing step.

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

    I love you bro, I literally love you

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

    how to modify this so i can find the longest from all paths in the graph and not the one with source A to all others? any help?

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

    @WilliamFiset I can see that we have found the length (!) of the shorthest path, but how to deduct the shorthest path itself? Can you hint how to incorporate this in the code?

    • @WilliamFiset-videos
      @WilliamFiset-videos  2 роки тому +1

      I believe you should be able to capture the path of nodes which make up the path with some extra information. Check out the path reconstruction method in my BFS video, it should be very similar concept: ua-cam.com/video/oDqjPvD54Ss/v-deo.html

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

      @@WilliamFiset-videos replying vaguely cryptically doesn't actually answer her question . with the amount of characters you typed just to give a vague reply you could have just given her the direct pseudo steps . that's would have been more helpful to remain on point.

    • @Aditya-pl3xg
      @Aditya-pl3xg 2 роки тому

      make a parent array and in the loop for(auto u : adj[v]) - parent[u] = v. this way you can retrace path by following pointers. This is a general technique for tracing paths.

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

    can we use BFS as well to update the mins for each node in the shortest path problem?

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

    Hi William, what would happen in the case when there are two nodes with indegree 0?

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

    what is NP-Hard??

  • @anuruddhapremalal
    @anuruddhapremalal 6 років тому +2

    Great explanation!. How do we obtain a list of nodes for the shortest paths?

    • @WilliamFiset-videos
      @WilliamFiset-videos  6 років тому +10

      Anuruddha Premalal You'll need additional memory to do that. Allocate an array called "previous" of size n with all null values. Every time u can relax an edge, update the parent of node i in the previous array. Then work backwards from the end node rebuilding the path. Does that make sense? See the BFS example in my Algorithms repo. Specifically the 'reconstructPath' method:
      github.com/williamfiset/Algorithms/blob/master/com/williamfiset/algorithms/graphtheory/BreadthFirstSearchAdjacencyListIterative.java

  • @MayankKumar-mn3dg
    @MayankKumar-mn3dg 3 роки тому +1

    I tried Doing the longest path problem with Dijkstra on a DAG with negative edges. But it keeps getting TLEd (even with priority queue) .... what is the reason behind this ??

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

    Instead of finding the topological sort, can't we just process the nodes in breadth first search manner?

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

    Super!

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

    For the SSSP algo, you are assuming that the source node is the node that is first when we topological sort the DAG. What if the source node was say D at 3:40?

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

      To find shortest distance from D using this Kahn's algorithm, I think after getting the top sorted array, we can start from the 1st node and start removing all their outgoing edges, till we reach node D. After this "preprocessing", D will be the single source on the new graph and Kahn's algorithm can be used to find shorted path from D. However, it's also possible that after the "preprocessing" there may be other 0-incoming-edge nodes other than D, i.e., the new graph has multiple sources, such as X and Y. In this case, Kahn's algorithm cannot be used to find the shortest path, as the shortest dist to a non-source node may be from Y while the intended source is X, and we cannot tell whether it's from X or from Y.

  • @aiomixrecords
    @aiomixrecords 6 років тому +1

    U are great

  • @konstantinrebrov675
    @konstantinrebrov675 5 років тому +3

    What does NP-Hard mean?

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

      If you can not find answer in O(n^k) k is a constant such as 0,1,2... and also you can’t verify if answer is correct in O(n^k) -> NP-hard

  • @harishvemula6709
    @harishvemula6709 5 років тому +1

    I have a doubt , does this algorithm works for negative edges

    • @RR8401674
      @RR8401674 5 років тому +2

      As mentioned in the video, this linear O(V+E) solution works for DAG (where you can topologically sort the vertices, in other words no cycles). For more general case with cycles your best bet is Bellman-Ford (with much worse runtime: O(VE))

  • @edenberdugo6947
    @edenberdugo6947 5 років тому

    that helped me very much thank you !

  • @GOODBOY-vt1cf
    @GOODBOY-vt1cf 3 роки тому +1

    1:00

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

    How would this work for C++ where you want to avoid use of NULL.

  • @GOODBOY-vt1cf
    @GOODBOY-vt1cf 3 роки тому +1

    1:21

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

    0:32 there is a cycle in the graph!! It is not a DAG!

  • @tweakter
    @tweakter 5 років тому +1

    Much surprised and happy that you use java over cpp 😍

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

    Dont mesmerize people into graph theory with your voice.

  • @truekili9215
    @truekili9215 6 місяців тому

    Why would you title the second chapter Dijkstra algortithm if it's not dijkstra's algorithm and it's just DAG relaxation. It's extremely misleading and annoying to have to go watch your other videos in order to know that you're not even talking about the algorithm you titled the chapter after.

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

    For longest path, it cannot work😢 we DELETE A C G H one by one.... cannot get the chance to find H Through B