170 likes | 318 Views
Intro to Computer Algorithms Lecture 18. Phillip G. Bradford Computer Science University of Alabama. Announcements. Advisory Board’s Industrial Talk Series http://www.cs.ua.edu/9IndustrialSeries.shtm 2-Dec: Mike Thomas, CIO, Gulf States Paper Next Research Colloquia:
E N D
Intro to Computer Algorithms Lecture 18 Phillip G. Bradford Computer Science University of Alabama
Announcements • Advisory Board’s Industrial Talk Series • http://www.cs.ua.edu/9IndustrialSeries.shtm • 2-Dec: Mike Thomas, CIO, Gulf States Paper • 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
Outline • Complete Dynamic Programming • Principle of Optimality • Start the Greedy Design Paradigm
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
Knapsack Problem • Section 3.4 from the book
Knapsack Problem • Suppose the knapsack’s capacity is • W=10 • What subsets can we choose within the knapsack’s capacity while maximizing the subsets value? • Note: {3,4} is optimal • Does not include item 1 (of the highest value)
Apparent worst-case? • Try all subsets! • Can we find a better algorithm? • It is not clear!
Other than exhaustive search? • Dynamic programming • V[i,j] is the optimal solution for • w1,…,wi (having values v1,…,vi) • And capacity j: W > j > 1. • Want to find V[n,W] • Suppose W is a base-10 integer, then how large is this table???
DP Recurrence for Knapsack • Can we fit the ith item? • No, then optimal solution is V[i-1,j] • Yes, then optimal solution • Includes ith item: vi + V[i-1,j-wi] • Does not include the ith item: V[i-1,j] • If j-wi> 0 • V[i,j] max{ V[i-1,j], vi + V[i-1,j-wi] }
DP Recurrence for Knapsack • Base Cases • V[0,j] = 0 • No items in knapsack (with j capacity) has no value • V[i,0] = 0 • Zero capacity, putting items 1,…,i does no good
Computing this DP Recurrence Capacity 0, 1, j-wi W 0 0 0 0 0 1 0 Value n 0
The Greedy Paradigm • Greedy Principle: • Start with an (optimal) solution and adding locally optimal structure eventually gives a globally optimal solution
The Greedy Paradigm • Change Making Problem • Not necessarily optimal • d1>d2> … > dn • Give back correct change • Greedy Method • Start with largest denomination • Exhaust it, • On to next smaller denomination
Prim’s Algorithm • Spanning Tree of an undirected graph • Acyclic subgraph containing all nodes • Minimum weight spanning tree • Weighted edges • Among the least cost spanning trees • The algorithm • The inductive Proof of optimality