Ordered Graph Problem

690 Views Asked by At

Let G = (V, E) be a directed graph with nodes v1, v2, . . . , vn.

We say that G is an ordered graph if it has the following properties.

(i) Each edge g Let G = (V, E) be a directed graph with nodes v1, v2, . . . , vn. We say that G is an ordered graph if it has the following properties. (i) Each edge goes from a node with a lower index to a node with a higher index. That is, every directed edge has the form vi , vj with i < j.

(ii) Each node except vn has at least one edge leaving it. That is, for every node vi , i = 1, 2, . . . , n − 1, there is at least one edge of the form (vi , vj ). goes from a node with a lower index to a node with a higher index. That is, every directed edge has the form vi , vj with i < j. (ii) Each node except vn has at least one edge leaving it. That is, for every node vi , i = 1, 2, . . . , n − 1, there is at least one edge of the form (vi , vj ).

b) ] Give an efficient algorithm using dynamic programming approach that takes an ordered graph G and returns the length of the longest path that begins at v1 and ends at vn

For question b) I am not really confident with dynamic programming so this is the algorithm i chose

Let M[v(i)] be the length of the longest path for v(1) to v(i).

for i=1 to i=n
   
    assign -∞ to M[v(i)]

M[v(1)]=0

for i=1 to i=n

 if M[v(i)] >= 0 

   for each v' reachable from v(i)

     M[v']=max(M[v(i)] + 1, M[v'])

return(M[v(n)]

Is the algorithm correct? I also can't figure out the running time whether it is O(m) or O(m+n).