TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. Page 1 J. Maluszynski, IDA, Linköpings Universitet, 2004. TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. Minimum Spanning Trees 1 5 b 3 1 1 e f a 1 c 2 1 2 1 d 5 b 3 1 1 e Page 2 f a 1 c 2 1 2 1 d 5 1 1 c 2 1 f J. Maluszynski, IDA, Linköpings Universitet, 2004. b 3 1 e with costs assigned to edges. Which edges with total minimal cost enable all connections? This subgraph must be a tree!! Why? a 2 d b 3 1 b 3 1 1 1 e e f a 1 c 2 1 2 f 1 1 d d a 1 c 2 1 2 b 3 e 1 1 b 3 1 1 5 5 TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. 1 d 1 5 5 Kruskal’s Algorithm a 2 a 2 d Page 4 f a 1 c 2 1 c 2 1 2 1 e f 1 d 5 b 3 1 1 e 2 c 1 1 f J. Maluszynski, IDA, Linköpings Universitet, 2004. Example application: purchasing communication lines in a network. Overview [L/D 12.3] motivated by important applications Minimum Spanning Trees, Kruskal’s Algorithm [L/D 12.3] Graph Algorithms: Single Source Least Cost Paths Program Construction Techniques: . J. Maluszynski, IDA, Linköpings Universitet, 2004. Exhaustive Search p.59 Page 3 Greedy Algorithms p.60-61 and Examples . is acyclic; Dynamic Programming p.61-65 such that Divide and Conquer p.29 and Examples TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. Kruskal’s Algorithm , times: of connects two distinct connected components of Connected graph Repeat find At each step Return ! L ' ' + = / = P K -B B K E B > K D D J = -B B " F I >C ' M , > >C . " F . C B L H < B B ! - C A - 9 N B < >C :; 123 0 / K = < -B D . B >C C @ :; > < 456 F > = 7 u’ e’ v’ 8 ! J. Maluszynski, IDA, Linköpings Universitet, 2004. J. Maluszynski, IDA, Linköpings Universitet, 2004. " Page 6 9 u v min-cost connected extension F This is also a min-cost connected extension F’ u’ > @ ? Page 8 v’ c(e) <- c(e’) c(F’) <- c(F) u 9 e B v - u e F v A F -B # TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. J. Maluszynski, IDA, Linköpings Universitet, 2004. Page 5 - Correctness of Kruskal’s algorithm < I " ( ): of the edges of a MST on The set of edges in the tree The number of components make a priority queue containing the edges of M -B L L M A TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. D Correctness of Kruskal’s algorithm / G Given " , ! ' , s.that disconnected. of wrt if: is connected. / and is a ) TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. ' J. Maluszynski, IDA, Linköpings Universitet, 2004. Page 7 If is min-cost edge between connected components of then some min-cost connected extension of includes . Kruskal’s algorithm [Algorithm 12.5] I O A ,- TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. * " find a minimal cost edge in check whether it connects distinct connected components of & % " add it to Implementation of Kruskal’s Algorithm " At every step we have to: How to do this efficiently? for edges in for connected components of The time complexity is . Explain why! ! $ ( " <W Q / " C K Q C J K > B : L : P 7 A I I V L I L 8 B : B C C ' " / , > A C P K > K B ! 9 C L > Q A Q " P U : Q > " ! L B < E Q A Q S C J 1 2 1 3 2 1 1 0 1 2 4 1 2 1 2 Q Q F [ Q J \ \ F Q \ F I J Q J J ] ] Q Q D Q J Page 10 3 3 3 2 2 1 1 5 4 3 5 1 3 2 0 1 1 0 1 2 5 4 1 1 3 2 5 6 2 3 2 J. Maluszynski, IDA, Linköpings Universitet, 2004. 1 2 1 2 4 3 5 1 0 1 ] 5 _ 2 F 4 3 5 J 1 A I J 2 F 2 Q 4 3 5 2 [ 5 4 2 5 4 5 4 F 5 4 J are J. Maluszynski, IDA, Linköpings Universitet, 2004. 5 4 TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. Page 12 J 4 3 5 . then Q J 1 2 F 1 ] F 4 3 5 Y 2 Q F 4 ] F 2 Q 0 1 to : J F " 7 5 4 A J. Maluszynski, IDA, Linköpings Universitet, 2004. J > = Dijkstra’s Algorithm Q TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. ] J > Correctness of Dijkstra Algorithm ] - contradiction ] _ :; : ; R distance computed by DA for vertex F D @ ? computed before X Z Z Z E If J is the least cost of a path from Assume not: ^ There exists with cost such that the least costs for the paths from , since is the least cost; since was computed before J > F hence _ T X ! J Discuss complexity of Dijkstra’s algorithm! D # " to each vertex. Page 9 I < B J. Maluszynski, IDA, Linköpings Universitet, 2004. A TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. " Q Page 11 Least-Cost Paths Problem J ) Given directed graph with costs on edges. Find the cost of min-cost path from a vertex > " A 1 2 : 1 K ! Minimal costs from 1 B 3 > 1 2 3 4 5 1 2 3 4 2 C 8 Q 4 3 2 J 2 K 1 C 5 4 TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. ! / J ( I " with minimum Distance K 9 J O J / any member of X " Dijkstra’s Algorithm 8 a TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. # f $ " 8 g V b # " B V _ l K B h i h E 8 # subsets of the set. h " X K + B ' Page 14 Page 16 K X h J. Maluszynski, IDA, Linköpings Universitet, 2004. $ B B Page 13 B There are TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. Exhaustive search is expensive tours !! % a 2 1 d b 3 1 4 1 e 2 c J. Maluszynski, IDA, Linköpings Universitet, 2004. 5 1 2 J. Maluszynski, IDA, Linköpings Universitet, 2004. partial results instead of recomputing them. Exhaustive Search E Given all distances between cities find a minimum length tour " TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. U ! A brute-force strategy for search problems: check systematically object in the set. ` 7 There are ) Given: (a) objects with and (b) a knapsack with fixed fill the knapsack so that the total value is maximal. de Often very expensive (e.g. exponential). J. Maluszynski, IDA, Linköpings Universitet, 2004. Page 15 f " c TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. Dynamic Programming Strategy Greedy Search Strategy k Fibonacci function: X U G To obtain sequence of minimal cost: choose minimal element at every step. B Often efficient if applicable: E Recursive procedure has exponential complexity (p.26) K ' Dynamic programming solution: to compute store computed in time. 1. Kruskal’s minimal spanning tree algorithm / 2. Dijkstra’s algorithm for least-cost paths B 3. Job-shop scheduling (p.61) B m j ` ] t [ _ U B D [ ^ B ^ B x : Page 18 r Page 20 x x p Y ; o Y K X p C ] \ \ \ ; i i Q n C s s t u r t h s s X J. Maluszynski, IDA, Linköpings Universitet, 2004. J. Maluszynski, IDA, Linköpings Universitet, 2004. u r B TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. ; J. Maluszynski, IDA, Linköpings Universitet, 2004. B Q Page 17 h solved directly Y TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. X 0-1 Knapsack Problem Example { p [ Dynamic Programming for 0-1 Knapsack A Q (size , value ) z (capacity) o Objects: Knapsack: B C K p first objects and knapsack W solution for B . y o We want to find C Dynamic Programming Solution: compute all p 4 5 0 0 1 1 6 6 8 8 9 9 9 9 9 9 9 11 | 3 0 1 6 8 8 8 8 . B :q :q :q w w < < < u u 2 0 1 6 6 6 6 6 . h [ 1 0 1 5 5 5 5 5 5 B < k/A 0 1 2 3 4 5 6 7 D :q time. A : Needs } o TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. B J. Maluszynski, IDA, Linköpings Universitet, 2004. X n Page 19 x if if B H B B o Complexity of D&C algorithms B t t t u u u u u u X X r r :q :q :q < < < { [ TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. X Y Divide and Conquer subproblems smaller than W B ] Q Divide the problem into subproblems of smaller size. t : no. of subproblems solved at each step z ; Solve the subproblems (recursively or directly). max size of subproblems Combine the solutions to obtain the result. X u U w r cost of splitting into subproblems and combining solutions Assume: | ; r r v Some examples: B v v Binary search p.11 y B : + Merge Sort p.29 B p p Block multiplication algorithm p.53 B ; ... D TDDB57 DALG – Lecture 14: Selected Graph Algorithms, Algorithm paradigms. B : W . ,- . . ] ] : . B ; H ^ s h U W ,- ,- . ,- : : : B B B + + + ; B . ; h ; ,- | : . ,- B + B ; h + ,- Complexity of D&C algorithms (solution) B if if if t B Binary search: W Merge sort: ~ Multiplication (p.52) : Page 21 [ W x B B B B h + + + + ; | | | | Clever multiplication (p.54): B ; B B B : J. Maluszynski, IDA, Linköpings Universitet, 2004.