Question

Write an algorithm to find the longest path in a DAG, where the length of the...

Write an algorithm to find the longest path in a DAG, where the length of the path is measured
by the number of edges that it contains. What is the asymptotic complexity of your algorithm?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Begin
initially mark all nodes as unvisited
for each node i in the graph,do
if i is not visited, then
topological sort(i,visited,stack)
done

make distances of all vertices as -infinite
dist[start] :=0
while stack is not empty,do
pop stack and take into nextVert
if dist[nextVert] is not equal to -infinite,then
for each vertices v which is adjacent to nextVert,do
if cost[nextVert,v] is not equal to -infinite,then
if dist[v]<dist[nextVert]+cost[nextVert,v],then
dist[v] :=dist[nextVert]+cost[nextVert,v]
done
done

for all vertices i in the graph do
if dist[i]=-infinite,then
display infinty
else display dist[i]
done
End

FOR TOPOLOGICALSORT(U,VISITED,STACK)

Begin
mark u as visited
for all vertex v,which is connected with u,do
if v is not visited then
topologicalsort(v,visited,stack)
done
push u into stack
End

Asymptotic complexity for topological sorting is O(V+E) so the overall asymptotic complexity is also considered as O(V+E) where V represents Vertices and E represents Edges




Add a comment
Know the answer?
Add Answer to:
Write an algorithm to find the longest path in a DAG, where the length of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT