Dynami mic Programmi mming Jeevani Goone*llake University - - PowerPoint PPT Presentation

dynami mic programmi mming
SMART_READER_LITE
LIVE PREVIEW

Dynami mic Programmi mming Jeevani Goone*llake University - - PowerPoint PPT Presentation

Dynami mic Programmi mming Jeevani Goone*llake University of Colombo School of Compu*ng Sri Lanka Jeevani Goone*llake (University of Colombo - Sri Lanka)


slide-1
SLIDE 1

Dynami mic ¡ ¡Programmi mming

Jeevani ¡Goone*llake ¡ University ¡of ¡Colombo ¡School ¡of ¡Compu*ng ¡ Sri ¡Lanka ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-2
SLIDE 2

2

All-Pairs Shortest Paths Problem (APSP) with SSSP

Dijkstra would take time Θ(V × V2) = Θ(V3) (standard version) Θ(V × E lgV) = Θ(VE lgV) ( Binary Min Heap) Θ(V × (VlgV + E)) = Θ(V2lgV + VE)) (Fibonacci heaps), but doesn’t work with negative-weight edges. Bellman-Ford would take Θ(V × VE) = Θ(V2E). If the graph is dense O(v4)

Jeevani Goonetillake (University of Colombo - Sri Lanka) Lecture 6

slide-3
SLIDE 3

All-­‑

  • ­‑Pairs ¡

¡Shortest ¡ ¡Paths ¡ ¡Problem ¡ m ¡(APSP)

The Floyd Warshall dynamic programming algorithm solves this problem in O(V3) for a graph with V vertices even for dense graphs. Uses adjacency matrices, as opposed to adjacency lists Input: The adjacency matrix of a weighted graph G = (V,E,w). Output: A distance matrix dij such that each entry dij is d(i,j) from i to j, for all i,j ϵ V.

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-4
SLIDE 4

Fl Floyd-­‑

  • ­‑Wa

Warshall ¡ ¡Algorithm m

An ¡arbitrary ¡ordering ¡of ¡ver*ces ¡is ¡enforced. ¡ For ¡a ¡sub ¡problem ¡only ¡prefix ¡of ¡ver*ces ¡are ¡allowed ¡to ¡be ¡used ¡as ¡ internal ¡nodes ¡on ¡a ¡path. ¡ Key ¡idea ¡: ¡ ¡Order ¡the ¡ver*ces ¡from ¡1 ¡to ¡n ¡ ¡V={1,2,3,…,n} ¡arbitrarily. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Let ¡V(k) ¡represents ¡ ¡the ¡prefix ¡of ¡first ¡k ¡ver*ces ¡{1,2,……..k} ¡ ¡ ¡

  • Let ¡P ¡= ¡shortest ¡(cycle ¡free) ¡i ¡to ¡j ¡path ¡with ¡all ¡internal ¡nodes ¡in ¡V(k). ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-5
SLIDE 5

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Fl Floyd-­‑

  • ­‑Wa

Warshall ¡Alg ¡Algorit

  • rithm
  • Op*mal ¡Substructure ¡Lemma: ¡

¡ ¡ ¡ ¡ ¡Suppose ¡G ¡has ¡no ¡nega*ve ¡cost ¡cycle. ¡Let ¡P ¡be ¡a ¡shortest ¡(cycle-­‑free) ¡ path ¡with ¡all ¡internal ¡noes ¡in ¡V(k) ¡. ¡Then ¡ Case ¡1: ¡if ¡k ¡is ¡not ¡internal ¡to ¡P ¡then ¡P ¡is ¡the ¡shortest ¡i-­‑j ¡path ¡with ¡all ¡ internal ¡ver*ces ¡in ¡V(k-­‑1). ¡ ¡ Case ¡2: ¡If ¡k ¡is ¡internal ¡to ¡P ¡then: ¡ ¡ ¡ ¡ ¡ ¡ ¡P1 ¡=> ¡shortest ¡(cycle ¡free) ¡i ¡to ¡k ¡path ¡with ¡all ¡internal ¡nodes ¡in ¡V(k-­‑1). ¡ ¡ ¡ ¡ ¡ ¡P2 ¡=> ¡shortest ¡(cycle ¡free) ¡k ¡to ¡j ¡path ¡with ¡all ¡internal ¡nodes ¡in ¡V(k-­‑1). ¡ ¡

i ¡ k ¡ j ¡

P1 ¡ P2 ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-6
SLIDE 6

Fl Floyd-­‑

  • ­‑Wa

Warshall ¡ ¡Algorithm: m: ¡ ¡Recu ecursiv sive ¡solu e ¡solu@on

  • n
  • Thus ¡if ¡ ¡dij

(k) ¡be ¡the ¡weight ¡of ¡a ¡shortest ¡path ¡from ¡vertex ¡i ¡to ¡vertex ¡j ¡

with ¡all ¡intermediate ¡ver*ces ¡in ¡the ¡set ¡{1,2,…,k}. ¡A ¡recursive ¡defini*on ¡ is ¡given ¡by ¡ ¡ ¡ ¡dij

(k)= ¡ ¡ ¡ ¡ ¡ ¡wij ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡k=0, ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡min(dij

(k-­‑1), ¡dik (k-­‑1)+dkj (k-­‑1)) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡k ¡ ¡ ¡ ¡ ¡ ¡1. ¡

  • The ¡matrix ¡D(n)=(dij

(n)) ¡gives ¡the ¡final ¡answer ¡

  • dij

(n)= ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡all ¡i,j ¡ ¡ ¡ ¡ ¡ ¡ ¡V ¡ ¡since ¡all ¡intermediate ¡ver*ces ¡are ¡in ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡the ¡set ¡{1,2,…,n}. ¡

{

) , ( j i δ

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-7
SLIDE 7

¡Fl Floyd-­‑

  • ­‑Wa

Warshall ¡ ¡Algorithm ¡ m ¡1

FLOYD-­‑WARSHALL(W) ¡ ¡ ¡ ¡ ¡n ¡ ¡ ¡ß ¡ ¡rows[W] ¡ ¡ ¡ ¡ ¡D(0) ¡ ¡ß ¡ ¡W ¡ ¡ ¡ ¡ ¡for ¡ ¡k ¡ß ¡ ¡1 ¡to ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡do ¡ ¡for ¡i ¡ ¡ß ¡ ¡1 ¡to ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡do ¡for ¡j ¡ß ¡ ¡1 ¡to ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡dij

(k) ¡ß ¡min(dij (k-­‑1), ¡dik (k-­‑1)+dkj (k-­‑1)) ¡ ¡

¡ ¡ ¡ ¡ ¡return ¡D(n) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

¡Base ¡Cases: ¡ ¡ ¡ ¡For ¡all ¡ ¡i, ¡j ¡ ¡ ¡ ¡V ¡ ¡ ¡ ¡w(i,j) ¡= ¡0 ¡if ¡i=j. ¡ ¡ ¡ ¡ ¡w(i,j) ¡= ¡Cij ¡if ¡(i, ¡j) ¡ϵ ¡ ¡E ¡ ¡ ¡ ¡w(i,j) ¡= ¡+∞ ¡if ¡(i, ¡j) ¡ϵ ¡ ¡E. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

Running Time for G(V,E) = Θ(V3)

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-8
SLIDE 8

Fl Floyd-­‑

  • ­‑Wa

Warshall ¡ ¡Algorithm ¡ m ¡2

FLOYD-­‑WARSHALL2(W) ¡ D ¡ ¡← ¡W ¡ ¡ ¡ ¡ ∏ ¡← ¡0 ¡ ¡ ¡ ¡ ¡ ¡ for ¡k ¡← ¡1 ¡to ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡do ¡for ¡i ¡← ¡1 ¡to ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡do ¡for ¡j ¡← ¡1 ¡to ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(D[ ¡i, ¡j ¡] ¡> ¡D[ ¡i, ¡k ¡] ¡+ ¡D[ ¡k, ¡j ¡] ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡then ¡ ¡D[ ¡i, ¡j ¡] ¡← ¡D[ ¡i, ¡k ¡] ¡+ ¡D[ ¡k, ¡j ¡] ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡∏ij ¡← ¡∏kj ¡; ¡ ¡ ¡

¡Please ¡refer ¡to ¡Frank ¡Drews’s ¡ ¡slides ¡for ¡another ¡version ¡of ¡ ¡Floyd ¡Warshall ¡algorithm. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-9
SLIDE 9

Fl Floyd-­‑

  • ­‑Wa

Warshall ¡ ¡Algorithm ¡ m ¡-­‑

  • ­‑ ¡

¡Examp mple

2 4 5 1 3 3 4

  • 4
  • 5

6 7 1 8 2

0 3 8 ∞ -4 ∞ 0 ∞ 1 7 ∞ 4 0 ∞ ∞ 2 ∞ -5 0 ∞ ∞ ∞ ∞ 6 0

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-10
SLIDE 10

Fl Floyd-­‑

  • ­‑Wa

Warshall ¡ ¡Algorithm ¡ m ¡-­‑

  • ­‑ ¡

¡Examp mple

0 3 8 ∞ -4 ∞ 0 ∞ 1 7 ∞ 4 0 ∞ ∞ 2 ∞ -5 0 ∞ ∞ ∞ ∞ 6 0

D(0) Π(0)

nil 1 1 nil 1 nil nil nil 2 2 nil 3 nil nil nil 4 nil 4 nil nil nil nil nil 5 nil nil ¡ ¡ ¡ ¡ ¡ ¡if ¡i=j ¡or ¡ ¡wij ¡= ¡∞ ¡ ¡i ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡i ¡≠ ¡j ¡ ¡and ¡ ¡wij ¡< ¡∞ ¡ Π(0)

ij =

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-11
SLIDE 11

Fl Floyd-­‑

  • ­‑Wa

Warshall ¡ ¡Algorithm ¡ m ¡-­‑

  • ­‑ ¡

¡Examp mple

0 3 8 ∞ -4 ∞ 0 ∞ 1 7 ∞ 4 0 ∞ ∞ 2 5 -5 0 -2 ∞ ∞ ∞ 6 0

D(1) Π(1)

nil 1 1 nil 1 nil nil nil 2 2 nil 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­‑ ¡

¡Examp mple

0 3 -1 2 -4 3 0 -4 1 -1 7 4 0 5 3 2 -1 -5 0 -2 8 5 1 6 0

D(5) Π(5)

nil 1 4 2 1 4 nil 4 2 1 4 3 nil 2 1 4 3 4 nil 1 4 3 4 5 nil

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-16
SLIDE 16

Fl Floyd’s ¡ ¡Algorithm ¡ m ¡– ¡ – ¡Path ¡ ¡Construc@on

¡ ¡path ¡(int ¡i, ¡int ¡j) ¡{ ¡ ¡ ¡ ¡if ¡(i!=j) ¡ ¡ ¡ ¡ ¡path(i,p[i][j]); ¡ ¡ ¡ ¡ ¡print(j); ¡ ¡ ¡ ¡} ¡

¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

Path ¡Construc*on ¡: ¡(5 ¡,2) ¡ ¡ Path(5,2) ¡-­‑> ¡Path(5,3) ¡-­‑> ¡Path(5,4) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡-­‑> ¡Path(5,5) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡5 ¡à ¡2 ¡, ¡ ¡5 ¡à ¡3 ¡à ¡2 ¡, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡5 ¡à ¡4 ¡à ¡3 ¡à ¡2 ¡

¡

Please ¡refer ¡to ¡Frank ¡Drews’s ¡ ¡slides ¡for ¡path ¡construc*on ¡with ¡respect ¡to ¡ the ¡algorithm ¡given ¡in ¡his ¡slides ¡– ¡Slide ¡no.s ¡ ¡17 ¡– ¡18. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-17
SLIDE 17

Tr Transi@ve ¡ ¡Closure

G ¡= ¡(V,E) ¡: ¡a ¡directed ¡graph, ¡V ¡= ¡{1,. ¡. ¡. ¡,N} ¡ The ¡adjacency ¡matrix ¡A ¡= ¡(aij) ¡of ¡G ¡is ¡ ¡

¡ ¡

¡1 ¡ ¡ ¡ ¡if ¡ ¡(i,j) ¡ ¡ ¡ ¡ ¡ ¡E ¡or ¡i ¡= ¡j, ¡

¡aij ¡= ¡

¡0 ¡ ¡ ¡ ¡otherwise. ¡ ∈

{

The ¡transi*ve ¡closure ¡of ¡G ¡is ¡G* ¡= ¡(V,E*), ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡E* ¡= ¡{(i, ¡j) ¡| ¡j ¡is ¡reachable ¡from ¡i ¡in ¡G}. ¡ ¡

a b c d

a ¡ b ¡ c ¡ d ¡ a ¡ 0 ¡ 1 ¡ 0 ¡ 0 ¡ b ¡ 0 ¡ 0 ¡ 0 ¡ 1 ¡ c ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ d ¡ 1 ¡ 0 ¡ 1 ¡ 0 ¡ A ¡= ¡ ¡ T ¡= ¡ ¡ a ¡ b ¡ c ¡ d ¡ a ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ b ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ c ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ d ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ Figure ¡: ¡(a) ¡Digraph. ¡(b) ¡Its ¡adjacency ¡matrix. ¡(c) ¡Its ¡transi*ve ¡closure. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-18
SLIDE 18

Tr Transi@ve ¡ ¡Closure ¡ ¡

Main ¡idea: ¡a ¡path ¡exists ¡between ¡two ¡ver*ces ¡i, ¡j, ¡iff ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡-­‑ ¡ ¡there ¡is ¡an ¡edge ¡from ¡i ¡to ¡j; ¡or ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡-­‑ ¡ ¡there ¡is ¡a ¡path ¡ ¡from ¡i ¡to ¡j ¡going ¡through ¡any ¡of ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡the ¡other ¡ver*ces. ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-19
SLIDE 19

Tr Transi@ve ¡ ¡Closure ¡ ¡

  • Warshall ¡Algorithm ¡

¡ ¡ ¡ ¡Subs*tute ¡+ ¡with ¡˄ (logical ¡AND) ¡and ¡min ¡with ¡˅ (logical ¡OR) ¡ ¡ ¡ ¡ ¡ ¡ ¡in ¡Floyd-­‑Warshall ¡algorithm. ¡ Let ¡the ¡adjacency ¡for ¡the ¡matrix ¡of ¡graph ¡G ¡is ¡t(0), ¡ ¡ ¡ ¡ ¡t(0)

ij ¡= ¡ ¡ ¡ ¡ ¡ ¡1 ¡if ¡ ¡i ¡= ¡j ¡or ¡(i,j) ¡ϵ ¡E ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0 ¡ ¡if ¡ ¡i ¡≠ ¡j ¡and ¡(i, ¡j) ¡ϵ ¡E ¡ ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-20
SLIDE 20

Tr Transi@ve ¡ ¡Closure ¡ ¡

  • Let ¡n ¡be ¡the ¡number ¡of ¡ver*ces ¡in ¡G. ¡
  • ¡For ¡k ¡= ¡{1,2..n}, ¡let ¡t(k) ¡be ¡an ¡adjacency ¡matrix ¡such ¡that, ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡1 ¡ ¡ ¡ ¡ ¡ ¡if ¡there ¡is ¡a ¡path ¡in ¡G ¡from ¡any ¡vertex ¡i ¡to ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡any ¡other ¡vertex ¡j ¡going ¡only ¡through ¡ver*ces ¡ ¡t(k)

ij ¡= ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡in ¡{ ¡1, ¡2,..., ¡k ¡}, ¡then ¡ ¡ ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0 ¡ ¡ ¡ ¡ ¡ ¡otherwise. ¡

¡

  • Transi*ve ¡closure ¡G* ¡= ¡(V, ¡E*) ¡and ¡(i,j) ¡ϵ ¡E* ¡iff ¡ ¡t(k)

ij ¡=1. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-21
SLIDE 21

Tr Transi@ve ¡ ¡Closure ¡ ¡

for ¡k ¡>= ¡1: ¡ ¡ ¡ ¡ ¡t(k)

ij ¡= ¡t(k-­‑1) ij ¡˅ ¡(t(k-­‑1) ik ¡˄ t(k-­‑1) kj) ¡ ¡

j ¡ k ¡ k ¡

1 ¡

i ¡

0 ¡ 1 ¡

R ¡(k-­‑1) ¡= ¡ ¡ j ¡ k ¡ k ¡ 1 ¡ i ¡ 1 ¡ 1 ¡ R ¡(k) ¡= ¡ ¡

Figure ¡: ¡Rule ¡for ¡changing ¡zeros ¡in ¡Warshall ¡‘s ¡algorithm ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-22
SLIDE 22

Tr Transi@ve ¡ ¡Closure ¡ ¡

Transi*ve ¡Closure ¡(G) ¡

  • 1. N ¡ß ¡|V ¡[G]| ¡
  • 2. for ¡i ¡ß ¡1 ¡to ¡n ¡
  • 3. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡do ¡for ¡j ¡ß ¡1 ¡to ¡n ¡
  • 4. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡do ¡if ¡i ¡= ¡j ¡or ¡ ¡(i, ¡j) ¡ ¡ ¡ ¡ ¡ ¡E[G] ¡
  • 5. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡then ¡tij

(0) ¡ ¡ß1 ¡ ¡

  • 6. ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡tij

(0) ¡ ¡ß0 ¡

  • 7. for ¡k ¡ß ¡1 ¡to ¡n ¡
  • 8. ¡ ¡ ¡ ¡ ¡ ¡do ¡for ¡i ¡ß ¡1 ¡to ¡n ¡
  • 9. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡do ¡for ¡j ¡ß ¡1 ¡to ¡n ¡
  • 10. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tij

(k) ¡ß ¡ ¡ ¡tij (k-­‑1) ¡˅ ¡ ¡(tik (k-­‑1) ¡˄ tkj (k-­‑1) ¡) ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

Please ¡also ¡refer ¡to ¡Frank ¡Drews’s ¡ slide ¡-­‑19 ¡on ¡ ¡Dynamic ¡Programming ¡

slide-23
SLIDE 23

Tr Transi@ve ¡ ¡Closure ¡ ¡

2 4 1 3

1 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 1 ¡ 1 ¡ 1 ¡ 0 ¡ 1 ¡ 1 ¡ 0 ¡ 1 ¡ 0 ¡ 1 ¡ 1 ¡ T ¡(0) ¡= ¡ ¡ 1 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 1 ¡ 1 ¡ 1 ¡ 0 ¡ 1 ¡ 1 ¡ 0 ¡ 1 ¡ 0 ¡ 1 ¡ 1 ¡ T ¡(1) ¡= ¡ ¡ 1 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 1 ¡ 1 ¡ 1 ¡ 0 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 0 ¡ 1 ¡ 1 ¡ T ¡(2) ¡= ¡ ¡ 1 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 1 ¡ 1 ¡ 1 ¡ 0 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ T ¡(3) ¡= ¡ ¡ 1 ¡ 0 ¡ 0 ¡ 0 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ T ¡(4) ¡= ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡