Because it is dynamic programming, and like in every dinamic programming problem, you have to proceed vertices in sorted order. Answer for some vertex X depends only on answers to vertices which are on the one side from it in your topsort order, and you need these answers to calculate correct one for vertex X.
What about graph having more than one vertex without incoming edges? If your question is about vertex s only, like it is in that article - yes, you can limit yourself to vertices which are reachable from S only; however, for longest walk in general you’ll need all graph.
Yes, it can. Your logic is correct.
Yes, it is faster. Complexity is mentioned at the end of that article, and you can compare it with Dijkstra’s complexity:) Using Dijkstra for this kind of graphs sounds stupid
I still have problem with the first one. If we do not use topological sorting, rather use a simple dfs, we would still be getting the answer, won’t we? Simply setting the answer to each vertex to -INF and using the given algorithm will yield the same result in same time as dfs, I think
Concerning the order in which you search:
If you’re missing out one node, the result of the next node will be ultimately correct, but the result for the node after that will in general not. Consider a DAG with 4 nodes 1…4, source 1 and edges
1->2
2->3
1->4
4->2
and edge weight constant 1.
A possible dfs order is 1,2,3,4. This yields a path length of 2 for 1->3, since 3 is not updated again after the path length to 2 is updated to 2.
What is the difference between dfs ans “topological sorting” here? DFS itself is producing topological sorting So you may not store that sorting itself, if you want - but that doesn’t change things.
No, but it’s a different problem from the one given n the link.
You compute the maximal length of a path starting any node in the subDAG starting at the source. The problem from the link computes all maximal paths from the source to any of those node. For the source both are obviously identical. The first problem transports information from the leafs and is directly solvable by dfs postorder as you’ve shown. I still don’t see how the other, transporting information from source to leafs can be implemented only using dfs order.
Oh, sorry then; yep, you are right - for a different problem you are talking about, it actually doesn’t sound as a dfs - we have some sort of reversed problem here
Frankly, it seems so much easier to write and understand than a iteration over the topologically-sorted vertices. Well, that might be logical too, but still.