Question

Problem 1: Dynamic Programming in DAG Let G(V,E), |V| = n, be a directed acyclic graph...

Problem 1: Dynamic Programming in DAG

Let G(V,E), |V| = n, be a directed acyclic graph presented in adjacency list representation, where the vertices are labelled with numbers in the set {1, . . . , n}, and where (i, j) is and edge inplies i < j. Suppose also that each vertex has a positive value vi, 1 ≤ i ≤ n. Define the value of a path as the sum of the values of the vertices belonging to the path. Give an O(|V | + |E|) algorithm that finds the path of maximum value in the graph.

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

Algorithm:

Approach:

We run a dfs to the graph and we calculate longest path from a node.

The longest path from a node is 1 plus maximum of longest path among all of it's children.

After calculating maximum distance for all the nodes of the directed acyclic graph we find the maximum among them and that will be the longest path in the graph.

Steps:

  • V = vertices count
  • val [ ] = array of values of vertices
  • dist[ V+1 ] = { 0 }
  • visited [ V+1 ] = {false}
  • for u = 1 to V
    • if visited[u] == false
      • then call dfs(u, visited, value, dist) // this will fill the dist[] array
  • longest_path = 0
  • for u = 1 to V
    • longest_path = max(longest_path, dist[u])

// recursive dfs

  • dfs(u, visited[ ], value[ ], dist[ ])
    • visited[u] = true
    • dist[u] = value[u]
    • for v in adj[u]
      • if visited[v] == false
        • dfs(v, visited, value, dist[ ]) // recursion
      • dist[u] = max(dist[u] , value[u] + dist[v] ) // calculating dist

Implementation in C++:

1 #include <bits/stdc++.h> using namespace std; void dfs(int node, vector int> adj[], int dp[], bool vis[]) { vis(node] = tru​​​​​​​

Sample output:

3

Time complexity:

Since we are running a dfs in the graph and we have adjacency matrix given. So, the time complexity of this algorithm would be same as dfs which is O(|V|+|E|) .

If the answer helped please upvote, it means a lot and for any query please comment.

Add a comment
Know the answer?
Add Answer to:
Problem 1: Dynamic Programming in DAG Let G(V,E), |V| = n, be a directed acyclic graph...
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
  • Let G = (V, E) be a directed acyclic graph with n vertices and m edges....

    Let G = (V, E) be a directed acyclic graph with n vertices and m edges. Give an O(n + m) time algorithm that determines if G contains a directed path that touches every vertex in G exactly once. The graph G is given by its adjacency list representation.

  • Viterbi algorithm We can use dynamic programming on a directed graph G = (V, E) for...

    Viterbi algorithm We can use dynamic programming on a directed graph G = (V, E) for speech recognition. Each edge (u, v) in E is labeled with a sound s(u, v) from a finite set S of sounds. The labeled graph is a formal model of a person speaking a restricted language. Each path in the graph starting from a distinguished vertex v0 in V corresponds to a possible sequence of sounds produced by the model. The label of a...

  • Consider a directed acyclic graph G = (V, E) without edge lengths and a start vertex...

    Consider a directed acyclic graph G = (V, E) without edge lengths and a start vertex s E V. (Recall, the length of a path in an graph without edge lengths is given by the number of edges on that path). Someone claims that the following greedy algorithm will always find longest path in the graph G starting from s. path = [8] Ucurrent = s topologically sort the vertices V of G. forall v EV in topological order do...

  • You are given an unweighted DAG (Directed Acyclic Graph) G, along with a start node s...

    You are given an unweighted DAG (Directed Acyclic Graph) G, along with a start node s and a target node t. Design a linear time (i.e., runtime O(IV+ EI)) dynamic programming algorithm for computing the number of all paths (not necessarily shortest) from s to t.

  • Given a directed graph with positive edge lengths and a specified vertex v in the graph,...

    Given a directed graph with positive edge lengths and a specified vertex v in the graph, the "all-pairs" v-constrained shortest path problem" is the problem of computing for each pair of vertices i and j the shortest path from i to j that goes through the vertex v. If no such path exists, the answer is . Describe an algorithm that takes a graph G= (V; E) and vertex v as input parameters and computes values L(i; j) that represent...

  • 10. You are given a directed graph G(V, E) where every vertex vi E V is...

    10. You are given a directed graph G(V, E) where every vertex vi E V is associated with a weight wi> 0. The length of a path is the sum of weights of all vertices along this path. Given s,t e V, suggest an O((n+ m) log n) time algorithm for finding the shortest path m s toO As usual, n = IVI and m = IEI.

  • Suppose G is a DAG,i.e., a directed acyclic graph. Lets and t be two nodes in...

    Suppose G is a DAG,i.e., a directed acyclic graph. Lets and t be two nodes in the graph. Describe an O(n+m) algorithm that computes the number of paths from s to t . (Hint: start by topologically ordering G.)

  • Problem 3 (15 points). Let G (V,E) be the following directed graph. a. 1. Draw the...

    Problem 3 (15 points). Let G (V,E) be the following directed graph. a. 1. Draw the reverse graph G of G. 2. Run DFS on G to obtain a post number for each vertex. Assume that in the adjacency list representation of G, vertices are stored alphabetically, and in the list for each vertex, its adjacent vertices are also sorted alphabetically. In other words, the DFS algorithm needs to examine all vertices alphabetically, and when it traverses the adjacent vertices...

  • 2. Design a deterministic algorithm to solve the following problem. input: A directed acyclic graph G...

    2. Design a deterministic algorithm to solve the following problem. input: A directed acyclic graph G = (V, E) stored using adjacency lists. output: A Hamiltonian path, if such a path exists. Otherwise, return NONE. Your algorithm must take O(|V| + |E|) time. You must describe your algorithm in plain English (no pseudocode) and you must explain why the running time of your algorithm is O(|V| + |E|). Maximum half a page

  • 114points Let G- (V,E) be a directed graph. The in-degree of a vertex v is the...

    114points Let G- (V,E) be a directed graph. The in-degree of a vertex v is the number of edges (a) Design an algorithm (give pseudocode) that, given a vertex v EV, computes the in-degree of v under (b) Design an algorithm (give pseudocode) that, given a vertex v E V, computes the in-degree of v incident into v. the assumption that G is represented by an adjacency list. Give an analysis of your algorithm. under the assumption that G is...

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