0% found this document useful (0 votes)
16 views

5.QuizDynamicProgrammingonGraphs (Quiz)

The document contains quiz questions about dynamic programming on graphs. It includes 10 multiple choice questions related to topics like all pairs shortest path algorithms, maximum path sum in a grid, topological sorting, counting unique paths in a DAG, and implementing DP to count paths of a given length in a graph. The questions test understanding of graph DP techniques like state evaluation ordering, base cases, and recurrence relations.

Uploaded by

akmmbhakmmbh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

5.QuizDynamicProgrammingonGraphs (Quiz)

The document contains quiz questions about dynamic programming on graphs. It includes 10 multiple choice questions related to topics like all pairs shortest path algorithms, maximum path sum in a grid, topological sorting, counting unique paths in a DAG, and implementing DP to count paths of a given length in a graph. The questions test understanding of graph DP techniques like state evaluation ordering, base cases, and recurrence relations.

Uploaded by

akmmbhakmmbh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Quiz questions

0. Title: DP on Graph #1
Description: Which of the following graph algorithms is a good example of the DP on
graphs technique?

0.Dijkstra
1.Floyd-Warshall
2.Kosaraju's Algorithm
3.None of these.

1. Title: DP on Graph #2
Description: According to the introductory video on DP + Graphs, any DP solution
has which of the two fundamental inherent properties?

0.A unique DP state


1.A state evaluation ordering
2.Both A and B
3.None of these.

2. Title: DP on Graph #3
Description: Given a 2D grid of size NxM. Each cell of the grid is an integer
representing a score for landing on that cell.

You start at 1,1 and you should reach N,M. In one move you can go from cell(i,j) to
cell(i+1, j) or cell(i, j+1).

Among all possible paths that go from (1,1) to (N,M), find out the maximum possible
score of a path.

Score of a path is the sum of grid[i][j] over all (i,j) visited by the path.

Let us define :

DP[i][j] = SCORE OF MAX SCORE PATH FROM (i,j) to (N,M).

choose the correct recurrence :

0.DP[i][j] = grid[i][j] + DP[i + 1][j] + DP[i][j + 1]


1.DP[i][j] = grid[i][j] + min( DP[i + 1][j], DP[i][j + 1])
2.DP[i][j] = grid[i][j] + max( DP[i + 1][j], DP[i][j + 1])
3.DP[i][j] = grid[i][j] + grid[i + 1][j] + grid[i][j + 1] + DP[i][j+1] + DP[i + 1]
[j]

3. Title: DP on Graph #4
Description: For a DAG which of the following provides the correct state
evaluation order?

0.A depth first search of the DAG


1.A topological sort of the DAG
2.A breadth first search of the DAG
3.An A* search of the DAG

4. Title: DP on Graph #5
Description: In reference to the video on DAGCNT.

If node A can reach node B and C, then Node A can reach any node that is reachable
from either B or C. Thus we use this relation and node B and node C results to get
all nodes reachable from node A.

What is the time complexity of this approach discussed in the video?

For as many as 20,000 nodes what standard template library DS is suggested to speed
up the process?

0.Time complexity is O(N) and suggested library is the set.


1.Time complexity is O(NrootN) and suggested library is the multiset.
2.None of the above options is absolutely correct.
3.Time complexity is O(N^2) and suggested library is the bitset.

5. Title: DP on Graph #6
Description: What is the topological sorting of a graph?

0.it sorts the nodes based on data in the nodes.


1.linear ordering of vertices such that for every directed edge u->v, vertex u
comes before v in the ordering.
2.linear ordering of vertices such that for every directed edge uv, vertex u comes
after v in the ordering.
3.It is an algorithm to find the shortest path from a given node to every other
node.

6. Title: DP on Graph #7
Description: You are given a DAG, find out the number of unique paths that start
from a given node.
For example for a simple graph with 2 nodes and a directed edge from node 1 to node
2, the number of nodes that start at node 1 is 2(1, 1->2) and that at node number 2
is 1(with path length 0).

suppose we wish to calculate this value for node V of the graph.


choose the correct recurrence :

0.DP[V] = 1 + product of DP[X] for all directed edges X -> V.


1.DP[V] = 1 + product of DP[X] for all directed edges V -> X.
2.DP[V] = 1 + sum of DP[X] for all directed edges V -> X.
3.DP[V] = 1 + sum of DP[X] for all directed edges X -> V.
7. Title: DP on Graph #8
Description: For the previous problem, we need to output the answer for all nodes.
Given that the number of nodes in the graph is V and number of edges is E and it is
a DAG.

What is the optimal time complexity for calculating answers for all the nodes?

0.O(V^2)
1.O(ErootV)
2.O(V^3)
3.O(V + E)

8. Title: DP on Graph #9
Description: Let us modify the previous question. Now you need to find the number
of unique paths of a given length L that start at given node V instead of all
unique paths starting at a given node.

Let the length of a path be the number of edges traversed by the path.
The input graph is a directed graph which may or may not be a DAG.

Define :
<pre>
DP[ V ][ L ] = number of unique paths that start at node V and have length L.
DP[ V ][ 0 ] = 1 for all nodes V.
</pre>
choose the correct recurrence :

0.DP[ V ][ L ] = summation DP[ X ][ L + 1 ] for all directed edges V->X.


1.DP[ V ][ L ] = summation DP[ X ][ L - 1 ] for all directed edges V->X.
2.DP can only be applied to Directed Acyclic graphs not cyclic ones.
3.DP[ V ][ L ] = summation DP[ X ][ L - 1 ] for all directed edges X->V.

9. Title: DP on Graph #10


Description: Let us implement the above algorithm in pseudo-code :
Given graph has N nodes. We are interested in paths of length L.

DP[ V ][ 0 ] = 1 for all V from 1 to N

<pre>
for length from 1 to L :
for node from 1 to N :
for all neighbours X of node
DP[ node ] [ length ] += DP[ X ][ length - 1]
</pre>
what is the time complexity of the above algorithm?

0.O(N*L)
1.O(N^2*L)
2.O((N*L)^2)
3.O(N*L * log (N*L))

You might also like