dynami mic programmi mming
play

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)


  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 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  2. All-Pairs Shortest Paths Problem (APSP) with SSSP Dijkstra would take time Θ ( V × V 2 ) = Θ ( V 3 ) (standard version) Θ ( V × E lgV ) = Θ ( VE lgV ) ( Binary Min Heap) Θ ( V × ( V lg V + E )) = Θ ( V 2 lg V + VE )) (Fibonacci heaps), but doesn’t work with negative-weight edges. Bellman-Ford would take Θ ( V × VE ) = Θ ( V 2 E ). If the graph is dense O(v 4 ) Jeevani Goonetillake (University of Colombo - Sri Lanka) 2 Lecture 6

  3. All-­‑ -­‑Pairs ¡ ¡Shortest ¡ ¡Paths ¡ ¡Problem ¡ m ¡(APSP) The Floyd Warshall dynamic programming algorithm solves this problem in O(V 3 ) 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 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  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 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  5. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Fl Floyd-­‑ -­‑Wa Warshall ¡Alg ¡Algorit orithm • 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: ¡ i ¡ j ¡ P2 ¡ P1 ¡ k ¡ ¡ ¡ ¡ ¡ ¡ ¡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). ¡ ¡ Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  6. Fl Floyd-­‑ -­‑Wa Warshall ¡ ¡Algorithm: m: ¡ ¡Recu ecursiv sive ¡solu e ¡solu@on on • Thus ¡if ¡ ¡d ij (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 ¡ ¡ ¡ ¡d ij (k) = ¡ ¡ ¡ ¡ ¡ ¡w ij ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡k=0, ¡ { ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡min(d ij (k-­‑1) , ¡d ik (k-­‑1)+ d kj (k-­‑1) ) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡k ¡ ¡ ¡ ¡ ¡ ¡1. ¡ ≥ • The ¡matrix ¡D(n)=(d ij (n) ) ¡gives ¡the ¡final ¡answer ¡ ( i , j ) ∈ • d ij (n) = ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡all ¡i,j ¡ ¡ ¡ ¡ ¡ ¡ ¡V ¡ ¡since ¡all ¡intermediate ¡ver*ces ¡are ¡in ¡ ¡ δ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡the ¡set ¡{1,2,…,n}. ¡ Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  7. ¡Fl Floyd-­‑ -­‑Wa Warshall ¡ ¡Algorithm ¡ m ¡1 FLOYD-­‑WARSHALL(W) ¡ ¡Base ¡Cases: ¡ ¡ ¡ ¡ ¡n ¡ ¡ ¡ ß ¡ ¡rows[W] ¡ ¡ ¡ ¡For ¡all ¡ ¡i, ¡j ¡ ¡ ¡ ¡V ¡ ¡ ¡ ¡ ¡D (0) ¡ ¡ ß ¡ ¡ W ¡ ¡ ¡ ¡w(i,j) ¡= ¡0 ¡if ¡i=j. ¡ ¡ ¡ ¡ ¡ ¡for ¡ ¡k ¡ ß ¡ ¡ 1 ¡to ¡n ¡ ¡ ¡ ¡w(i,j) ¡= ¡Cij ¡if ¡(i, ¡j) ¡ϵ ¡ ¡E ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡do ¡ ¡for ¡i ¡ ¡ ß ¡ ¡1 ¡to ¡n ¡ ¡ ¡ ¡w(i,j) ¡= ¡+∞ ¡if ¡(i, ¡j) ¡ϵ ¡ ¡E. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡do ¡for ¡j ¡ ß ¡ ¡1 ¡to ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡d ij (k) ¡ ß ¡min(d ij (k-­‑1) , ¡d ik (k-­‑1) +d kj (k-­‑1) ) ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡D (n) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Running Time for G(V,E) = Θ (V 3 ) Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  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 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend