Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures by R.S. Chang, Dept. CSIE, NDHU1 Chapter 2 Algorithm Design Human Problems Result Input Data Structures Processing Output Data Structures.

Similar presentations


Presentation on theme: "Data Structures by R.S. Chang, Dept. CSIE, NDHU1 Chapter 2 Algorithm Design Human Problems Result Input Data Structures Processing Output Data Structures."— Presentation transcript:

1

2 Data Structures by R.S. Chang, Dept. CSIE, NDHU1 Chapter 2 Algorithm Design Human Problems Result Input Data Structures Processing Output Data Structures Computer Algorithms

3 Data Structures by R.S. Chang, Dept. CSIE, NDHU2 Chapter 2 Algorithm Design For a problem? What is an Optimal Solution? 用最少的 (CPU) 時間 ( 通常是指這點 ) 用最少的記憶體 Example: Given 4 numbers, sort it to nonincreasing order. Method 1: Sequential comparison 1. Find the largest (3 comparisons) 2. Find the second largest (2 comparisons) 3. Find the third largest (1 comparisons) 4. Find the fourth largest A total of 6 comparisons

4 Data Structures by R.S. Chang, Dept. CSIE, NDHU3 Chapter 2 Algorithm Design For a problem? What is an Optimal Solution? 用最少的 (CPU) 時間 ( 通常是指這點 ) 用最少的記憶體 Example: Given 4 numbers, sort it to nonincreasing order. Method 2: Somewhat clever method a1 a2 a3 a4 a2a4 a2a3 a2a3 a2 a1a3 a3 or a1 (4 comparisons) (5 comparisons)

5 Data Structures by R.S. Chang, Dept. CSIE, NDHU4 Chapter 2 Algorithm Design 2.1 Greedy Algorithms A greedy algorithm takes an action that seems the best at the given time, without consideration of future actions. Example: Coin changing problem Make change for any amount from $0.01 to $0.99 using the fewest number of coins. The available coins are $0.5, $0.25, $0.1, $0.05, and $0.01. $0.94=$0.5+$0.25+$0.1+$0.05+4*$0.01 A total of 8 coins Greedy algorithm works for U.S. coins. But not necessarily so for others. See Exercise 1 in Page 27.

6 Data Structures by R.S. Chang, Dept. CSIE, NDHU5 Chapter 2 Algorithm Design 2.1 Greedy Algorithms Hill climbing concept Local optimumGlobal optimum

7 Data Structures by R.S. Chang, Dept. CSIE, NDHU6 Chapter 2 Algorithm Design 2.2 Divide-and-Conquer Algorithms Example: Finding the minimum number in a list of numbers minimum(a,lower,upper) { if (upper-lower) == 1 return min(a[upper],a[lower]); else return min(minimum(a,lower,(lower+upper)/2), minimum(a,(lower+upper)/2+1,upper)); } Merge sort Quick sort

8 Data Structures by R.S. Chang, Dept. CSIE, NDHU7 Chapter 2 Algorithm Design 2.2 Divide-and-Conquer Algorithms The closest pair problem d d For each point near the border, there can be at most six candidates for the closest neighbor across the border. Why?

9 Data Structures by R.S. Chang, Dept. CSIE, NDHU8 Chapter 2 Algorithm Design 2.3 Dynamic Programming Algorithms Often a problem can be solved by divide-and- conquer, but it may turn out to be an inefficient algorithm because much work is duplicated when solving the subproblems. (or because the conquer (merge) step is too complicated)

10 Data Structures by R.S. Chang, Dept. CSIE, NDHU9 Chapter 2 Algorithm Design 2.3 Dynamic Programming Algorithms The Fibonacci numbers Fibonacci(n) { if (n<2) return 1; else return Fibonacci(n-1)+Fibonacci(n-2); } F(5) F(3) F(4) F(1) F(2) F(3) F(1) F(2) Many repetitive calculations

11 Data Structures by R.S. Chang, Dept. CSIE, NDHU10 Chapter 2 Algorithm Design 2.3 Dynamic Programming Algorithms The Fibonacci numbers Fibonacci(n) { int data[n]; data[0]=1; data[1]=1; for (j=2; j<n; j++) data[j]=data[j-2]+data[j-1]; return data[n-1]; } A simpler implementation: Example of one-dimensional dynamic programming

12 Data Structures by R.S. Chang, Dept. CSIE, NDHU11 Chapter 2 Algorithm Design 2.3 Dynamic Programming Algorithms The number of combinations

13 Data Structures by R.S. Chang, Dept. CSIE, NDHU12 Chapter 2 Algorithm Design 2.3 Dynamic Programming Algorithms The number of combinations The Pascal’s triangle 0 1 2 3 4... 0 1 2 3 4... 1... 1 2 3 4... 1 3 6... 1 4... 1 n r

14 Data Structures by R.S. Chang, Dept. CSIE, NDHU13 Chapter 2 Algorithm Design 2.4 Backtracking Algorithms The tic-tac-toe game xx How can a computer play the game? Remember Deep Blue?

15 Data Structures by R.S. Chang, Dept. CSIE, NDHU14 Chapter 2 Algorithm Design 2.4 Backtracking Algorithms The tic-tac-toe game x x 0 1 2 0 1 2 (1,1)C (0,0)H (0,1)H (0,2)H (1,0)H... (0,0)C, (0,1)C, (1,0)C... (0,1)H, (1,0)H, …, (2,2)H (0,1)C, (1,0)C, (1,2)C, (2,0)C... : Computer x : Human

16 Data Structures by R.S. Chang, Dept. CSIE, NDHU15 Chapter 2 Algorithm Design 2.4 Backtracking Algorithms 3 missionaries and 2 cannibals want to cross the river Condition: 1. A boat can take one or two (must include a missionary) 2. At any time, on either bank, the number of missionaries must not be less than the number of cannibals.


Download ppt "Data Structures by R.S. Chang, Dept. CSIE, NDHU1 Chapter 2 Algorithm Design Human Problems Result Input Data Structures Processing Output Data Structures."

Similar presentations


Ads by Google