What is an algorithm to find a longest path in a unweighted directed acyclic graph?
-
Note that it can be from any node to any other node. In other words, how can someone find the (maximum) diameter of a graph (unweighted and directed acyclic)?
-
Answer:
is correct that we could apply the recurrence: dist(to_node) = max{dist(from_node) + 1} (For each directed edge from from_node to to_node) And then pick the maximum possible value from dist as the answer. However, just a note regarding implementation: If you do it top-down with memoization, there is actually no need to explicitly compute a topological sorting. For example, in Python, you may write something like this: memo = {} def get_longest(to_node): if to_node in memo: return memo[to_node] best = 0 for from_node in from_nodes[to_node]: best = max(best, get_longest(from_node) + 1) memo[to_node] = best return best length, node = max([(get_longest(to_node), to_node) for to_node in all_nodes]) print 'Length of a longest path: length = %d, ending at %s' % (length, node) By adding some code in the for loop inside get_longest, you could easily backtrack to find one (or even all) longest path.
Chun-Ho Hung at Quora Visit the source
Other answers
When you have DAG the very first things that comes to mind is to get topological order of the graph (http://en.wikipedia.org/wiki/Topological_sort) This will give us complexity of O(V+E). Now suppose that we have our vertices so that there are only arcs going forward, meaning if you get the vertex on position i all of the adjacent vertices will have positions bigger than i. Let's now have vector<int> distances which will store the max distance to the particular vertex, so distances[i] is the max path to vertex i, no mater where we started. Iterate vertices in topological order and for all adjacent vertices j, make distances[j] = max(distances[j], distances[i] + 1); The longest path will be *max_element(distances.begin(), distances.end()) So, overal complexity will be O(V+E).
Roman Dzhabarov
If it's a acyclic graph(tree), then this algorithm should work This will run in [math]O(E+v)[/math]
Piyush Maheshwari
replacing the edge weights with ((LCM of all edges)/(weight of the edge)) makes the longest edge as smallest and smallest edge as longest. The whole increasing order of edge weights now gets inverted. Now you can you use the shortest path algorithms and find the shortest paths. The shortest path you have got is actually your longest path! Now again taking the LCM of edges / weight of edge gives you back the edges you started with.
Manikanta Raju Lakkaraju
Your problem is simply to find diameter in an unweighted DAG.This problem has a general solution, for any type of graph in O(V+E).The solution is stated below : Apply BFS by choosing any vertex as source. Pick one of the nodes in the last level of this BFS traversal. Let this node be S. Now, apply BFS again with S as source. Now the path from S to all nodes at the last level, is the length of the diameter. In short in this particular BFS traversal the highest level, is the diameter. Thus by these 2 simple steps you can find diameter of any graph.
Aditya Prakash
Related Q & A:
- Where can I find a good guide for a civil service exam?
- What do I do to find a old article from a newspaper?Best solution by Yahoo! Answers
- HOW CAN I FIND A PERSON WITH JUST A EMAIL ADDRESS?Best solution by Yahoo! Answers
- Can i find a profile with only a partial e-mail address?Best solution by Yahoo! Answers
- How do I find a registered owner or a vehicle in CA?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.