Topological sort for longest path in DAG. Does this code really use the idea of topological sorting? Is this code correct? - directed-acyclic-graphs

I am reading a book about "Practical Algorithm Skill Test" by Shinya Iwashita and Kenko Nakamura.
Let G be a directed acyclic graph.
Find the length of the longest directed path in G. Here, the length of a directed path is the number of edges in it.
https://atcoder.jp/contests/dp/tasks/dp_g (please see English page)
The authors explain that the above problem needs topological sorting.
The following is the code for this problem.
I think this code doesn't use topological sorting at all.
Does this code really use the idea of topological sorting?
Is this code correct?
https://github.com/kenkoooo/pastbook-source-code/blob/master/chukyu/python/chapter06/section04/6-4-8.py

Related

Visit all nodes exactly once in a directed graph

I have a directed graph and I want to find a path that visits every node exactly one time. I want to do this with a good complexity. Is this possible? And if yes, how?
You are searching for a Hamiltonian path, which is a simple open path that contains each node exactly once.
Finding a Hamiltonian path in a given graph is NP-complete. In fact, determining whether a given (directed or undirected) graph contains a Hamiltonian path is already NP-complete (proven via reduction from e.g. the vertex cover problem).
If you still want to code it, here is an implementation on github. If you want a fast solution, maybe a heuristic is sufficient (for instance inspired by DNA molecules, or a solution that works fast on a subset of graphs. For instance, if you have a DAG, you can do a topological sort and then check if successive vertices are connected. If so, the topological sort gives a Hamiltonian path.

Ford-Falkerson's algorithm for undirected graphs (What am I missing?)

I "found" an algorithm for finding maximum flow in undirected graph which I think isn't correct, but I can't find my mistake. Here is my algorithm:
We construct a new directed graph in the following way: for every edge ${u,v}$ we create edges $(u,v)$ and $(v,u)$ with $c((u,v))=c((v,u))=c({u,v})$. Then we apply Ford-Falkerson's algorithm on new graph. Now we make a flow in our first graph in the following way: Let's $f((u,v))\ge f((v,u))$, than we direct edge ${u,v}$ from $u$ to $v$ and take $f'((u,v))=f((u,v))-f((v,u))$. Now it will be maximum flow for our undirected graph, because otherwise we will construct a flow for corresponding directed graph, which is contradiction.
The reason that I think I have missed something is that there is an article on the Internet about this problem and I don't think anybody would wrote an article about such a trivial problem.
And this is the article: http://www.inf.ufpr.br/pos/techreport/RT_DINF003_2004.pdf
Thanks!
Ford-Fulkerson is not the best algorithm to find maximum flow in a directed graph, and in the undirected case it is possible to do much better (close to linear-time if I recall correctly).
You don't cite the article you are talking about, but it is most likely that it describe an algorithm which is much better than yours.
For many problems in optimization, it is not difficult to find an algorithm that gives an optimal solution; nevertheless there is a lot of work to find the most efficient ones.

DAG shortest path

Given a directed acyclic graph G = (V,E) and two distinguished vertices s and t. Both the edges and vertices are assigned real-valued weights. The weight of a path is defined as the sum of all the edges and vertices on the path. The problem is to find a shortest weighted simple path from s to t.
(a) Design a dynamic programming algorithm and briefly describe it.
(b) Design a greedy algorithm and briefly describe it.
(c) Provide upper and lower bounds of one of your algorithms as tight as possible.
How do I do this? Can Dijkstra be used?
I think for greedy algorithm Dijkstra's can be used. I dont know about the rest, sorry mate.

Data structures implementation code understanding

Im taking a course in algorithms and data structers, and my instructor wants me to implement several data structers (such as BST, stack etc.), and algorithms (such as quick search, DFS, etc.).
I want to belive that I understand the basics, but everytime Im starting to plan the code I have the same difficulty:
here's my current assigment: my instructor wants me to implement a DFS (depth first search) for a directed graph (using c++).
my question is- how do I suppose to implement the graph? should I use adjacency matrix? or should I use adjacency list? neither this nor that??
so I asked my instructor, and his answare was this: "think of the graph as a black box"...
more confused than before, I rashed to stackoverflow, and here i am posting this question...
I dont look for someone to tell me how to implement DFS (or any other algorithm- I can google too!)- I just need someone to explain what should I get as input, and what should I provide as output?
I'll appreciate any comment! thanks!
What he means by a black box is just that you cannot see the nodes and how they connect before you do your DFS. You will probably just get the root node and your algorithm with have to explore from there. As for what you should output- that depends on the assignment. Are you looking for specific data? if not, perhaps a detail of which nodes were visited in which order.

Application of List Ranking algorithm

I've been reading about List Ranking Algorithm from many sources like
http://www.cs.cmu.edu/~scandal/alg/listrank.html
I found that it is useful in Parallel Tree Contraction,Euler tour of tree etc.but i'm not getting actual use of this list algorithm in above applications.Does anyone have any idea of how List ranking is useful in these or any algorithms?
The first thing I can imagine is that you can easily compute the height of all nodes in a tree. It's not exactly an algorithm, but can be pretty useful in some cases.