












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
These are the Lecture Slides of Introduction to Algorithms which includes Expensive Operations, Sort Edges, Running Time, Upshot, Union, Makeset, Disjoint Set, Disjoint Set Union, Naïve Implementation etc. Key important points are: Greedy Algorithms, Dynamic Programming, Strategy, Designing Algorithms, Small Subproblems, Breaks Down, Observation, Optimal Substructure, Overlapping Subproblems, Subproblem
Typology: Slides
1 / 20
This page cannot be seen from the preview
Don't miss anything!













Review: Dynamic Programming
Review: Structure of Subproblems
■ There are few subproblems in total ■ And many recurring instances of each (unlike divide & conquer, where subproblems unique)
Memoization
■ After computing the solution to a subproblem, store in a table ■ Subsequent calls just do a table lookup
■ There are mn subproblems ■ How many times is each subproblem wanted? ■ What will be the running time for this algorithm? The running space?
Review: Dynamic Programming
■ Optimal substructure: optimal solution to problem consists of optimal solutions to subproblems ■ Overlapping subproblems: few subproblems in total, many recurring instances of each ■ Solve bottom-up, building a table of solved subproblems that are used to solve larger ones
■ “Table” could be 3-dimensional, triangular, a tree, etc.
Greedy Algorithms
■ My everyday examples: ○ Walking to the Corner ○ Playing a bridge hand ■ The hope: a locally optimal choice will lead to a globally optimal solution ■ For some problems, it works
Activity-Selection
■ Given a set S of n activities s (^) i = start time of activity i f (^) i = finish time of activity i ■ Find max-size subset A of compatible activities
Assume (wlog) that f 1 ≤ f 2 ≤ … ≤ f (^) n
1
2
3 4 5
6
Activity Selection: Optimal Substructure
■ In words: once activity #1 is selected, the problem reduces to finding an optimal solution for activity- selection over activities in S compatible with # ■ Proof: if we could find optimal solution B’ to S’ with | B | > | A - { k }|, ○ Then B U { k } is compatible ○ And | B U { k }| > |A|
Greedy Choice Property
■ Locally optimal choice ⇒ globally optimal sol’n ■ Them 17.1: if S is an activity selection problem sorted by finish time, then ∃ optimal solution A ⊆ S such that {1} ∈ A ○ Sketch of proof: if ∃ optimal solution B that does not contain {1}, can always replace first activity in B with {1} ( Why? ). Same number of activities, thus optimal.
Activity Selection: A Greedy Algorithm
■ Sort the activities by finish time ■ Schedule the first activity ■ Then schedule the next activity in sorted list which starts after previous activity finishes ■ Repeat until no more activities
■ Always pick the shortest ride available at the time
Review: The Knapsack Problem
■ A thief breaks into a museum. Fabulous paintings, sculptures, and jewels are everywhere. The thief has a good eye for the value of these objects, and knows that each will fetch hundreds or thousands of dollars on the clandestine art collector’s market. But, the thief has only brought a single knapsack to the scene of the robbery, and can take away only what he can carry. What items should the thief take to maximize the haul?
Review: The Knapsack Problem
■ The thief must choose among n items, where the i th item worth vi dollars and weighs wi pounds ■ Carrying at most W pounds, maximize value ○ Note: assume v (^) i, w (^) i, and W are all integers ○ “0-1” b/c each item must be taken or left in entirety
■ Thief can take fractions of items ■ Think of items in 0-1 problem as gold ingots, in fractional problem as buckets of gold dust
Solving The Knapsack Problem
■ How?
■ Greedy strategy: take in order of dollars/pound ■ Example: 3 items weighing 10, 20, and 30 pounds, knapsack can hold 50 pounds ○ Suppose item 2 is worth $100. Assign values to the other items so that the greedy strategy will fail
The Knapsack Problem: Greedy Vs. Dynamic
■ As you have seen, however, it can be solved with dynamic programming