cs324fig8.cdr Wednesday, October 22, 2014 8:12:41 AM Color profile: Disabled Transitive Closure Composite Default screen ➪ Transitive relation Recall, digraphs are useful to model dependence such as dependent tasks. They can be used to model relations in general. ➪ Modeling relations ➪ Adjacency matrix Quiz Define formally the transitive closure of digraph. Quiz Give an example for a relation that is not transitive. ➪ Transitive closure of digraph a b c d KAU • CPCS-324 © 2014 Dr. Muhammad Al-Hashimi 1 cs324fig8.cdr Wednesday, October 22, 2014 8:12:41 AM Color profile: Disabled Transitive Closure of Digraph Applications Composite Default screen An important reachability question in al these case is: given two elements, are they related? ➪ It’s about reachability ➪ Cells in spreadsheet ➪ Lineage (family) trees ➪ OOP: class hierarchy analysis ➪ Connecting flights KAU • CPCS-324 © 2014 Dr. Muhammad Al-Hashimi 2 cs324fig8.cdr Wednesday, October 22, 2014 8:12:42 AM Color profile: Disabled Transitive Closure of Digraph Computation Composite Default screen ➪ DFS-based TC Notice how the code template relates to pictorial representation of graph. d ... 0 0 1 2 3 Is there a starting point which answers the question for paths of length 1? a c a b c d ... Notice how “phantom” nodes simulate positions in the adjacency matrix. b 2 b a a a 3 4 c d d b ➪ Warshall’s algorithm: idea Quiz What does the number in () mean? How does it relate to path length? KAU • CPCS-324 Can it be used to generate answer for path of length 2? © 2014 Dr. Muhammad Al-Hashimi 3 Warshall’s Algorithm cs324fig8.cdr Wednesday, October 22, 2014 8:12:42 AM Color profile: Disabled Principle Composite Default screen Rules Look at element k in my row and column. a 1 0 (next) (previous) b c 1 d Main insight 1 2 3 4 a b c d 0 0 0 1 1 0 0 0 0 0 0 1 0 1 0 0 b d 0 0 0 1 1 0 0 1 0 0 0 1 0 1 0 0 Exercise Verify that there is a path from d to b that passes through a. (Answer next slide) KAU • CPCS-324 © 2014 Dr. Muhammad Al-Hashimi 4 cs324fig8.cdr Wednesday, October 22, 2014 8:12:42 AM Color profile: Disabled Warshall’s Algorithm Example Composite Default screen a b c d Exercise Verify the 1 circled green. Check b (vertex 2) in its row and column in R(1). a b c d KAU • CPCS-324 © 2014 Dr. Muhammad Al-Hashimi 5 cs324fig8.cdr Wednesday, October 22, 2014 8:12:42 AM Color profile: Disabled Warshall’s Algorithm Composite Default screen can we add an edge to find a path between i,j of length k+1? inherit previous path of length k ➪ Performance © 2014 Dr. Muhammad Al-Hashimi 6 check vertex k in my row (i) Exercise Compare to performance of DFS-based TC. check vertex k in my column (j) KAU • CPCS-324 While We Are At It ... cs324fig8.cdr Wednesday, October 22, 2014 8:12:42 AM Color profile: Disabled Composite Default screen ➪ Distance matrix Weight can be interpreted as distance or cost, something to be minimized. a 2 7 3 6 c b 1 d Quiz What does infinity means? KAU • CPCS-324 © 2014 Dr. Muhammad Al-Hashimi 7 Shortest Distance cs324fig8.cdr Wednesday, October 22, 2014 8:12:42 AM Color profile: Disabled Look at vertex k=1 (vertex a) in my row and column, if sum smaller replace. Composite Default screen a 2 2 3 4 b 7 3 6 c 1 1 d 2 Exercise Verify in the graph the length of a path from b to c that passes through a. 1 3 5 2 3 4 Exercise Verify the 7 in green. (Answer next slide). KAU • CPCS-324 © 2014 Dr. Muhammad Al-Hashimi 8 Shortest Paths cs324fig8.cdr Wednesday, October 22, 2014 8:12:42 AM Color profile: Disabled Composite Default screen Exercise What’s the path? Verify its length. KAU • CPCS-324 © 2014 Dr. Muhammad Al-Hashimi 9 All-Pairs Shortest-Paths Problem cs324fig8.cdr Wednesday, October 22, 2014 8:12:42 AM Color profile: Disabled Composite Default screen Floyd’s Algorithm ➪ Performance KAU • CPCS-324 © 2014 Dr. Muhammad Al-Hashimi 10 Dynamic “Programming” cs324fig8.cdr Wednesday, October 22, 2014 8:12:42 AM Color profile: Disabled Composite Default screen Quiz State the principle as required by dynamic programming. Quiz What’s the simpler subproblem solved by each stage? ➪ Principle of optimality ➪ Review Warshall-Floyd idea ➪ Originally a math method ➪ In algorithms Solve simpler subproblems in each stage, not necessarily smaller in terms of input size. © 2014 Dr. Muhammad Al-Hashimi ➪ Solve a problem in stages ➪ Space efficiency concern 11 KAU • CPCS-324 cs324fig8.cdr Wednesday, October 22, 2014 8:12:42 AM Color profile: Disabled Graph Implementation TC Package Composite Default screen Checkpoint 8 expectations KAU • CPCS-324 © 2014 Dr. Muhammad Al-Hashimi 12 Class Exercise cs324fig8.cdr Wednesday, October 22, 2014 8:12:42 AM Color profile: Disabled Composite Default screen Do it by hand! a a b 2 7 3 6 c c d For all pairs, is there a path? b 1 d For all pairs, what’s the shortest distance? What’s the cheapest way to connect the vertices? © 2014 Dr. Muhammad Al-Hashimi 13 KAU • CPCS-324 Check Your Answers cs324fig8.cdr Wednesday, October 22, 2014 8:12:43 AM Color profile: Disabled Composite Default screen a a b 2 7 3 6 c c d b 1 d KAU • CPCS-324 © 2014 Dr. Muhammad Al-Hashimi 14
© Copyright 2024