130 likes | 267 Views
COMPSCI 330 Design and Analysis of Algorithms. Midterm 1 Review. Basic Algorithm Design Techniques. Analyzing running time. Divide and conquer Dynamic Programming Greedy Common Theme: To solve a large, complicated problem, break it into many smaller sub-problems. Design algorithm.
E N D
COMPSCI 330 Design and Analysis of Algorithms Midterm 1 Review
Basic Algorithm Design Techniques Analyzing running time • Divide and conquer • Dynamic Programming • Greedy • Common Theme: To solve a large, complicated problem, break it into many smaller sub-problems. Design algorithm Proof of correctness
How to know the right technique? • In exam: it will be fairly obvious. • In reality: you need experience. Worst case can try all three.
Divide and conquer • Use when you can partition problems into unrelatedsubproblems. • Key step: How to merge the solutions. • Analysis: • Use recursion trees for running time (or guess and do induction) • Usually use induction to prove correctness.
Dynamic Programming • Use if all subproblems are highly related, and there are not many subproblems you need to solve. • Define a table for your subproblems. • Consider all possibilities in the last step, write the recursion function for the table. • Usually use induction to prove correctness (correctness is often straightforward and you only need to go through the formalities) • Try implement the basic algorithms we covered in class.
Greedy • Use when you can choose an obvious action to reduce the problem size. • Proof is crucial --- you will lose many points if you just give a greedy algorithm, even if it’s correct. • Analysis: • Try to find counterexamples • If you cannot find counterexamples, try prove the correctness by “proof by contradiction” • Try to argue OPTIMAL must look like your solution.
Dynamic Programming and Greedy • Both require treating the problem as making a sequence of decisions. • A greedy problem can often be solved using DP as well, but the DP algorithm might be slower. • Once you tweak a greedy problem (by adding weights/costs etc.) often the original greedy algorithm fails and you need to do DP. • Get used to finding counter-examples – if you succeed, it means the algorithm is wrong; if you fail, it often gives intuition for proof.
Recursions • (a) • (You will not see this kind of problem as it is difficult, but just wanted to use it as an example)
Recursions • (b)
Dynamic Programming • Alice is playing a game where she controls the in-game character to catch pancakes. The character moves on a stage of length n. The character's location can be described by a number x in {1,…,n}. Let xt be the position of the character at time t. At each time step, Alice can move the character to the left (xt+1=xt-1), right (xt+1=xt+1) or do nothing (xt+1=xt). • Alice already knows the game very well, so she knows a sequence of pairs (ti; pi) - This means at time ti there will be a pancake at location pi. • The character starts at location 1 at time 0, and the game goes until time m. • Please design an algorithm to find out how many pancakes Alice can get.
Example • n = 5, m = 5 • Pancakes ((1; 1); (2; 2); (3; 4); (3; 5); (4; 3); (4; 4); (5; 2)), • Optimal solution: 4 • Can get (1,1), (2,2), (4, 3) and (5,2) simultaneously
Greedy • After scheduling for classrooms, let's schedule meeting rooms. • Suppose there are n groups of people requesting for meeting rooms at the same time, and there are currently m rooms available. Group i has ni people, and room j has cj capacity. • For group i, they are satisfied with any meeting room j whose capacity cj is at least ni, the number of people in the group. • Design an algorithm that tries to find a meeting room for as many groups as possible.
Exam • Format is very similar to the practice exam • It would be quite obvious what technique you should use. • There will be an algorithm design question for divide and conquer but it is not difficult. • Problems are not necessarily sorted in level of difficulty.