Greedy Algorithms (brief review) Divide and Conquer (Example) Dynamic Programming Backtracking Algorithms Another Puzzler 12 A: Algorithm Design Techniques II CS1102S: Data Structures and Algorithms Martin Henz April 7, 2010 Generated on Tuesday 6 th April, 2010, 14:42 CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 1
Greedy Algorithms (brief review) Divide and Conquer (Example) Dynamic Programming Backtracking Algorithms Another Puzzler 1 Greedy Algorithms (brief review) 2 Divide and Conquer (Example) 3 Dynamic Programming 4 Backtracking Algorithms 5 Another Puzzler CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 2
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler 1 Greedy Algorithms (brief review) Scheduling Huffman Codes 2 Divide and Conquer (Example) 3 Dynamic Programming 4 Backtracking Algorithms 5 Another Puzzler CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 3
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler Nonpreemptive Scheduling Input A set of jobs with a running time for each Desired output A sequence for the jobs to execute on on single machine, minimizing the average completion time CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 4
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler Example Some schedule: The optimal schedule: CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 5
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler The Multiprocessor Case N processors Now we can run the jobs on N identical machines. What is a schedule that minimizes the average completion time? CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 6
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler Example CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 7
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler A “Slight” Variant Miniming final completion time If we want to minimize the final completion time (completion time of the last task), the problem becomes NP-complete! CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 8
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler Optimal Prefix Code in Table Form CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 9
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler Optimal Prefix Code in Tree Form CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 10
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler Huffman’s Algorithm: An Example CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 11
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler Huffman’s Algorithm: An Example CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 12
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler Huffman’s Algorithm: An Example CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 13
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler Huffman’s Algorithm: An Example CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 14
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler Huffman’s Algorithm: An Example CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 15
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler Huffman’s Algorithm: An Example CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 16
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler Huffman’s Algorithm: An Example CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 17
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler Correctness of Huffman’s Algorithm Observation 1 An optimal tree must be full; no node has only one child. Observation 2 The two least frequent characters α and β must be the two deepest nodes. Observation 3 Characters at the same level can be swapped without affecting optimality. CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 18
Greedy Algorithms (brief review) Divide and Conquer (Example) Scheduling Dynamic Programming Huffman Codes Backtracking Algorithms Another Puzzler Correctness of Huffman’s Algorithm Observation 3 Characters at the same level can be swapped without affecting optimality. Initial Step of Huffman’s Algorithm An optimal tree can be found that contains the two least frequent symbols as siblings; the first step in Huffman’s algorithm is not a mistake. Observation 4 Every step of Huffman’s algorithm produces a simplified problem, resulting from treating two characters as indistinguishable. CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 19 Each Step of Huffman’s Algorithm
Greedy Algorithms (brief review) Divide and Conquer (Example) Dynamic Programming Backtracking Algorithms Another Puzzler 1 Greedy Algorithms (brief review) 2 Divide and Conquer (Example) 3 Dynamic Programming 4 Backtracking Algorithms 5 Another Puzzler CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 20
Greedy Algorithms (brief review) Divide and Conquer (Example) Dynamic Programming Backtracking Algorithms Another Puzzler Closest-Points Problem Input Set of points in a plane Euclidean distance between p 1 and p 2 [( x 1 − x 2 ) 2 + ( y 1 − y 2 ) 2 ] 1 / 2 Required output Find the closest pair of points CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 21
Greedy Algorithms (brief review) Divide and Conquer (Example) Dynamic Programming Backtracking Algorithms Another Puzzler Example CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 22
Greedy Algorithms (brief review) Divide and Conquer (Example) Dynamic Programming Backtracking Algorithms Another Puzzler Naive Algorithm Exhaustive search Compute the distance between each two points and keep the smallest Run time There are N 2 pairs to check, thus O ( N 2 ) CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 23
Greedy Algorithms (brief review) Divide and Conquer (Example) Dynamic Programming Backtracking Algorithms Another Puzzler Idea Preparation Sort points by x coordinate; O ( N log N ) Divide and Conquer Split point set into two halves, P L and P R . Recursively find the smallest distance in each half. Find the smallest distance of pairs that cross the separation line. CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 24
Greedy Algorithms (brief review) Divide and Conquer (Example) Dynamic Programming Backtracking Algorithms Another Puzzler Partitioning with Shortest Distances Shown CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 25
Greedy Algorithms (brief review) Divide and Conquer (Example) Dynamic Programming Backtracking Algorithms Another Puzzler Two-lane Strip CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 26
Greedy Algorithms (brief review) Divide and Conquer (Example) Dynamic Programming Backtracking Algorithms Another Puzzler Brute Force Calculation of min ( δ, d C ) CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 27
Greedy Algorithms (brief review) Divide and Conquer (Example) Dynamic Programming Backtracking Algorithms Another Puzzler Better Idea Sort points by y coordinate This allows a scan of the strip. Sort points by y coordinate This allows a scan of the strip. CS1102S: Data Structures and Algorithms 12 A: Algorithm Design Techniques II 28
Recommend
More recommend