You are on page 1of 15

DP in Graph Algorithms (All pair Shortest Paths)

• G = (V, E) is a weighted directed graph with weight function w: E → R.


• Want to output a table so that the entry in u’s row and v’s column will be the
weight of a shortest path from u to v.

• If we use Bellman Ford algorithm – then advantage is that –ve weight cycles can
now be present –O(V2 E), for dense graphs O(V4).

• If we use Dijkstra’s algorithm, and if min-priority queue is implemented


• i) as array, then O(|V|3 + |V||E|) = O(|V|3)
• ii) as binary heap – O(|V|2log|V| + |V||E|log |V|)
• iii) Fibonacci heap – O(|V|2log|V| + |V||E|)
Starting Basics
• Assume vertices are numbered 1, 2,…, n = |V|, so that input is a n x n
matrix W representing the edge-weights of an n-vertex directed graph
G = (V, E), W = (wij)

• wij= 0 if i = j
= ∞ if i ≠ j, (i, j) does not belong to E
= the weight of the directed edge (i, j), if i ≠ j, and (i, j) belongs to E
Some more trivia
• Assume –ve weight edges are allowed, but no –ve weight cycles are allowed.
• Output is an n x n matrix D = (dij), where entry dij contains the weight of a
shortest path from vertex i to vertex j.
• Also will compute predecessor matrix π = (πij),
• where πij = nil if either i = j, or there is no path from i to j,
• otherwise it is the predecessor of j in some shortest path from i.
Floyd-Warshall Algorithm
• -ve weight edges may be present, but we assume that there are no -ve weight
cycles.
• A recursive solution:

• The weight of a shortest path from vertex i to vertex j for which all intermediate vertices
are in the set {1, 2, …., k}.
• When k = 0, a path from vertex i to vertex j with no intermediate vertex at all.
O(n3)
An example
Another DP problem(Longest Common Sub-sequence
problem)
• A sample application
• A strand of DNA can be expressed as a string over the finite set {A, C, G, T}.
• For example, the DNA of one organism may be –
S1 = ACCGGTCGAGTGCGCGGAAGCCGGCCGAA, S2 = GTCGTTCGGAATGCCGTTGCTCTGTAAA.
• Now we want to measure how closely related the two organisms are? By, comparing two strands of
DNA.

• Given 2 sequences X = <x1, x2, ……………., xm> and Y = <y1, y2, ……………., yn>, find a
maximum length common subsequence of X and Y.

• Eg. X = <ABCDEFG>, Y = <BCEDGF>, then <BCDF>, <BCDG>, <BCEF> and <BCEG> are all
LCS for these 2 sequences. (Note that it have to be a strictly increasing sequence).

• Brute force approach – Have to consider 2m subsequences of X and 2n subsequences of


Y and hence exponential.
Natural Class of sub-problems
• Given a sequence X=<x1, x2,…,xm>, we define the ith prefix of X, for i= 0, 1, …m, as -
Xi =<x1, x2,…,xi>,. For example, if X=<A, B, C, B, D, A, B>, then X4 =<A, B, C, B> and X0
is the empty sequence.
• Consider pairs of prefixes of both the sequences and identify the optimal
substructure in this context.
• Theorem –
• If X = <x1, x2, …., xm> and Y = <y1, y2, …., yn>, and let Z = <z1, z2, …., zk> be any LCS of X and Y.
Then
• (i) If xm = yn, then zk= xm = yn, and Zk-1 is an LCS of Xm-1 and Yn-1.
• (ii) If xm ≠ yn, then zk≠ xm implies Z is an LCS of Xm-1 and Y.
• (iii) If xm ≠ yn, then zk≠ yn implies Z is an LCS of X and Yn-1.
• An example –
• (i) X = ABCDE, Y = CBADE, so Z = ADE
• (ii) X = ABCDE, Y = CBACD, so Z = BCD as z3 ≠ x5
• (iii) X = ABCDE, Y = ABBEJ, so Z = ABE as z3 ≠ y5
• (ii) & (iii) X = ABCDE, Y = BCDAJ, Z = BCD as z3 ≠ x5≠ y5, z3 = x4 ≠ y4, z3 = x4 = y3
The Optimal substructure
• An LCS of two sequences contains within it an LCS of prefixes of the two
sequences.

• c[i, j] to be the length of an LCS of the sequences Xi and Yj, where,


X = <x1, x2,…. xm> and Y = <y1, y2,…. yn>
Overlapping sub problems property of LCS

• To find an LCS of X and Y, we may need to find the LCSs of X and Yn-1
and of Xm-1 and Y .

• But each of these sub-problems has the sub-sub-problem of finding


an LCS of Xm-1 and Yn-1. Many other sub-problems share sub-sub-
problems.
The Algo.

O(mn)

X = (A, B, C, B, D, A, B) Y = (B, D, C, A, B, A), LCS = (B, C, B, A), Length : 4


The recursive procedure prints out an LCS of X and Y
in the proper, forward order.

O(m + n), since it decrements at


least one of i and j in each
recursive call.

You might also like