180 likes | 325 Views
Intro to Computer Algorithms Lecture 17. Phillip G. Bradford Computer Science University of Alabama. Announcements. Advisory Board’s Industrial Talk Series http://www.cs.ua.edu/9IndustrialSeries.shtm 4-Nov: Bill Engelke, Mercedes-Benz, USA. Next Research Colloquia:
E N D
Intro to Computer Algorithms Lecture 17 Phillip G. Bradford Computer Science University of Alabama
Announcements • Advisory Board’s Industrial Talk Series • http://www.cs.ua.edu/9IndustrialSeries.shtm • 4-Nov: Bill Engelke, Mercedes-Benz, USA. • Next Research Colloquia: • Prof. Nael Abu-Ghazaleh • 10-Nov @ 11:00am • “Active Routing in Mobile Ad Hoc Networks”
Computer Security Research Group • Meets every Friday from 11:00 to 12:00 • In 112 Houser • Computer Security, etc. • Email me to be on the mailing list!
CS Story Time • Prof. Jones’ research group • See http://cs.ua.edu/StoryHourSlide.pdf
Next Midterm • Tuesday before Thanksgiving ! • 25-November
Dynamic Programming • Principle of optimality • Consider any well-formed substructure s of an optimal structure S • Then s is optimal itself. • This allows the combination of well-formed substructures into optimal superstructures
Dynamic ProgrammingComputing Binomial Coefficients • For i1 to n do • For j 0 to min(i,k) do • If j=0 or j=k, then • C[i,j] 1 • Else • C[i,j] C[i-1,j-1] + C[i-1,j] • Endif • Endfor • Endfor • Return C[n,k]
What does this cost? • O(nk) • How about (corrected) exercise #6 in the book? Measure rec-calls instead • Omega(nk) for k constant, n varying • For general analyses, Beware of memoization! • Analyses may not reflect running times
Warshall’s and Floyd’s Algs. • Adjacency Matrices Columns (to) 1 n 2 1 2 3 Rows (from) n
Transitive Closure • When We work with directed graphs • Call them digraphs • Transitive closure T of a digraph is an nxn boolean matrix: • T[i,j]=1 iff there is a non-trivial path between nodes i and j. • T[i,j]=0 iff there is No path between nodes i and j (and i != j).
Transitive Closure • All Pairs Shortest Path Problems • Different from Single Source Shortest Paths
How to get a Transitive Closure? • How about DFS and BFS? • What about the principle of optimality? • The matrix R(k) is all paths with up to k intermediary nodes • R(0) is the adjacency matrix • R(1) is the adjacency matrix “along with” any paths that are extended by one using node 1. • OR…
Transitive Closure via Warshall • R(2) is all shortest paths using nodes 1 and 2 as intermediaries • R(3) is all shortest paths using nodes 1,2, and 3 as intermediaries • … • R(n) is all shortest paths using nodes 1,2,…,n as intermediaries
Basis for Warshall’s Algorithm • R(k)i,j = • = R(k-1)i,j OR [ R(k-1)i,k AND R(k-1)k,j ] • If R(k-1)i,j = 1, then R(k)i,j = 1 • Say R(k-1)i,j = 0, so R(k)i,j = 1 • iff R(k-1)i,k = 1 and R(k-1)k,j = 1 • Why?
Warshall’s Algorithm • R(0) Adjacency Matrix • For k 1 to n do • For i 1 to n do • For j 1 do n do • R(k)i,j R(k-1)i,j OR [ R(k-1)i,k AND R(k-1)k,j ] • Endfor • Endfor • Endfor • Return R(n)
Floyd’s Algorithm • Weighted (non-directed) graph • No cycles of negative length • Looks like (Min,+) “matrix multiplication”
Floyd’s Algorithm • DWeighted Adj. Matrix, augmented by Infinity • For k 1 to n do • For i 1 do n do • For j 1 to n do • D[i,j] min{ D[i,j], D[i,k] + D[k,j] } • Endfor • Endfor • Endfor • Return D