Algorithms and Programming ChinSung Lin Eleanor Roosevelt High
- Slides: 89
Algorithms and Programming Chin-Sung Lin Eleanor Roosevelt High School
Algorithms and Programming • Types of Algorithms – Brute-Force (or Exhaustive Search) – Divide and Conquer – The Greedy Method – Recursion • Algorithms of Skyscrapers Project – Review of Pseudo Code – Decimal-to-Binary Conversion Algorithm • Programming: A Crash Course of C • Sorting Algorithms and Implementations • Searching Algorithms and Implementations
Brute-Force Algorithms
Brute-Force (or Exhaustive Search) • Brute-force is a straightforward approach to solving a problem, usually directly based on the problem’s statement and definitions of the concepts involved. • Try out all possible values or combinations to solve a problem, or try every possible solution to see which is best.
Brute-Force (or Exhaustive Search) Example • Example: Try out all possible combinations to open a lock. • Example: Try out all possible password values to log into an account.
Brute-Force (or Exhaustive Search) Example • Example: Compute an for a given number a and a nonnegative integer n. By the definition of exponentiation, an = a a a. n times • Example: Build 250 -block skyscraper by adding one block each week.
Brute-Force (or Exhaustive Search) Though inefficient in general, the brute-force algorithm should not be overlooked as an important algorithm design strategy for the following reasons: • It is applicable to a very wide variety problems, and seems to be the only general approach for all problems. • The expense of designing a more efficient algorithm may be unjustifiable if only a few instances of a problem need to be solved and a brute-force algorithm can solve those instances with acceptable speed. • It is useful for solving small-size instances of a problem. • It can serve as an important theoretical or education purpose, e. g. , as a benchmark to judge more efficient alternatives algorithms.
Divide and Conquer Algorithms
Divide and Conquer • Repeatedly reduces an instance of a problem to one or more smaller instances of the same problem (usually recursively) until the instances are small enough to solve easily. The solutions to the subproblems are then combined to give a solution to the original problem.
Divide and Conquer The most well known algorithm design strategy: 1. Divide the problem into two or more smaller subproblems. 2. Conquer the subproblems by solving them recursively. 3. Combine the solutions to the subproblems into the solutions for the original problem.
Divide and Conquer Example: Large-integer multiplication (The Grade-School Algorithm) x a 1 a 2 … a n b 1 b 2 … b n (d 10) d 11 d 12 … d 1 n (d 20) d 21 d 22 … d 2 n ………………… (dn 0) dn 1 dn 2 … dnn This process requires n 2 single digit multiplications. For 1231 2012, this process requires 16 multiplications.
Divide and Conquer Example Large-integer multiplication (Divide and Conquer Algorithm) 1231 2012 = (12*102 + 31) * (20*102 + 12) = (12*20)*104 + c 1*102 + 31*12 where c 1 = (12+31)*(20+12) – 12*20 – 31*12 = 764 12*20 = (1*10 + 2) * (2*10 + 0) = (1*2)*102 + c 2*10 + 2*0 where c 2 = (1+2)*(2+0) – 1*2 – 2*0 = 4 31*12 = (3*10 + 1) * (1*10 + 2) = (3*1)*102 + c 3*10 + 1*2 where c 3 = (3+1)*(1+2) – 3*1 – 1*2 = 7 (12+31)*(20+12) = 43 * 32 = (4*10 + 3) * (3*10 + 2) = (4*3)*102 + c 4*10 + 3*2 where c 4 = (4+3)*(3+2) – 4*3 – 3*2 = 17 This process requires 9 digit multiplications as opposed to 16.
Greedy Algorithms
Greedy Algorithms • An optimization problem is one in which you want to find, not just a solution, but the best solution. • It is not exhaustive, and does not give an accurate answer to many problems. But when it works, it is the fastest method. • A greedy algorithm works in phases. At each phase: – You take the best you can get right now, without regard for future consequences – You hope that by choosing a local optimum at each step, you will end up at a global optimum 14
Greedy Algorithm Example • Example: Pay out the exact amount of money using US monetary system. • Example: The knapsack problem – Given a set of items, each with a mass and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.
Greedy Algorithm Example • Example: Dijkstra's algorithm (single-source shortest path problem) Works on both directed and undirected graphs. However, all edges must have nonnegative weights. Input: Weighted graph and source vertex (v) such that all edge weights are nonnegative Output: Lengths of shortest paths (or the shortest paths themselves) from a given source vertex (v) to all other vertices
Greedy Algorithm Example
Greedy Algorithm Example
Greedy Algorithm Example
Greedy Algorithm Example
Greedy Algorithm Example
Greedy Algorithm Example
Greedy Algorithm Example
Greedy Algorithm Example
Greedy Algorithm Example
Greedy Algorithm Example
Recursive Algorithms Recursive Algorithms
Recursion A recursive function is one which calls itself. To prevent infinite recursion, you need an if-else statement (of some sort) where one branch makes a recursive call, and the other branch does not.
Recursion Dream (problem) { If problems solved return solution else dream(problem) }
Recursion Example • Example: Calculate the factorial of N: N! = N x (N-1) x (N-2) x ……. x 3 x 2 x 1 Traditional algorithm using Loop: Factorial () 1. Let index = 1 2. Let product = 1 3. For index = 1 to N product = product x index 4. end-For 5. return product
Recursion Example • Example: Calculate the factorial of N: N! = N x (N-1) x (N-2) x ……. x 3 x 2 x 1 Recursive algorithm: Factorial(long n) { if (n==1) } return(n * return(n); else factorial(n-1));
Recursion Example A traditional algorithm for finding the Fibonacci Numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ……) Fibonacci () 1. Input N 2. Let result 0 = 0 3. Let result 1 = 1 4. If N = 1 or N = 2 5. result = N - 1 6. else 7. for loop counter = 2 to N – 1 8. result = result 1 + result 0 9. result 0 = result 1 10. result 1 = result 11. end-for loop 12. end-if 13. output result
Recursion Example A recursive algorithm for finding the Fibonacci Numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ……) Fibonacci (N) 1. If N = 1 or N = 2 2. return (N – 1) 3. else 4. return Fibonacci (N – 1) + Fibonacci (N – 2) 5. end-if
Algorithms of Skyscrapers Project
Algorithms of Skyscrapers Project Rules: • Using prefabricated-modular blocks of 100 meters long, 100 meters wide, and 5 meters tall. • The blocks interlock on top and bottom (like Legos), and they cannot be stacked sideways. • Using special lifters, putting stacks of blocks at the same height on ground or on top of another set of equal-height stacks takes one week regardless of how tall the stacks are or how many stacks are lifted. • The prefabrication time of the blocks doesn’t count since they are already in stock. • No resource/budget limitations (i. e. , you can have as many stacks as possible at the same time),
Algorithms of Skyscrapers Project Problem Part A: • If a client wants to build a 100 -meter long, 100 -meter wide, and 1250 -meter high tower as quickly as possible, what is the shortest amount of time that it will take to build the tower? • Show your algorithm in both flowchart and pseudocode forms. Problem Part B: • Develop a general algorithm for skyscrapers of 100 -meter long, 100 -meter wide, and N-meter high (where N is a multiple of 5) in pseudocode format.
Review of Pseudocode
Convention of Pseudocode Write pseudocode for: • calculating total number of blocks needed for N-meter high skyscraper. • calculating the remainder of number of stacks divided by 2. • creating a while loop to work as long as the number of stacks is not equal to 1. • creating a counter to increment the week count in a while loop.
Convention of Pseudocode Write pseudocode for: • calculating total number of blocks needed for N-meter high skyscraper. 1. number. Block = N/5
Convention of Pseudocode Write pseudocode for: • calculating the remainder of number of stacks divided by 2. 1. Remainder = number. Stack % 2
Convention of Pseudocode Write pseudocode for: • creating a while loop to work as long as the number of stacks is not equal to 1. While loop number. Stack ≠ 1 2. …… 3. end-loop
Convention of Pseudocode Write pseudocode for: • creating a counter to increment the week count in a while loop. 1. week. Counter = 0 2. While loop number. Stack ≠ 1 3. week. Counter = week. Counter + 1 4. end-loop
Group Presentation of Skyscraper Algorithms
Decimal-to-Binary Conversion Algorithm
Decimal-to-Binary Conversion 240 0 120 0 60 0 30 0 2 15 1 2 7 1 2 3 1 2 2 1 Decimal: 240 Binary: 11110000 No. of Division 7 No. of One’s: 4 No. of Weeks: 7 + 4 = 11
Programming: A Crash Course of C
C Example: Sum of 1+ 2+…+N 1. // Calculate the Sum of 1 + 2 + 3 + …. . + N 2. #include <stdio. h> 3. int main () 4. { 5. int n, i; 6. int sum = 0; 7. printf("Enter N: "); 8. scanf("%d", &n); 9. for (i = 1; i <= n; i++) 10. sum = sum + i; 11. printf("Sum = %dn", sum); 12. return 0; 13. }
C Example: Average of Numbers 1. // Calculate the average of three numbers 2. // average = (a + b + c)/3 3. #include <stdio. h> 4. int main() 5. { 6. float a, b, c; 7. float average; 8. printf ("Averaging three numbers n"); 9. printf ("Enter three numbers a, b, and c: "); 10. scanf ("%f %f %f", &a, &b, &c); 11. average = (a + b + c)/3. 0; 12. printf("Average = %. 2 fn", average); 13. return 0; 14. }
C Example: Linear Equation 1. // Solve Linear Equation 2. // ax + b = c, x = (c - b)/a 3. #include <stdio. h> 4. int main() 5. { 6. float a, b, c; 7. float x; 8. printf("Solve linear equation: ax + b = cn"); 9. printf("Enter coefficients: a, b, c: "); 10. scanf ("%f %f %f", &a, &b, &c); 11. x = (c - b)/a; 12. printf ("x = %. 2 fn", x); 13. return 0; 14. }
C Example: Skyscraper Algorithm 1. // Skyscraper Algorithm 2. #include <stdio. h> 3. int main() 4. { 5. int no. Week, building. Height, no. Stack, remainder; 6. no. Week = 1; 7. printf ("Hello! Please enter the building height: "); 8. scanf ("%d", &building. Height); 9. no. Stack = building. Height/5; 10. while (no. Stack != 1) 11. { 12. remainder = no. Stack%2; 13. no. Stack = no. Stack/2; 14. no. Week = no. Week + remainder + 1; 15. } 16. printf("Number of Week to Build the Skyscraper is: %dn", no. Week); 17. return 0; 18. }
C Example: Square Root 1. #include <stdio. h> 2. #include <math. h> 3. int main() 4. { 5. float a; 6. float x; 7. printf("Please enter the number: "); 8. scanf("%f", &a); 9. if (a < 0) 10. printf("No solution. n"); 11. else 12. { 13. x = sqrt(a); 14. printf("sqrt(%. 2 f) = %. 5 fn", a, x); 15. } 16. return 0; 17. }
C Example: Integer Array 1. // Integer Array 2. #include <stdio. h> 3. #define MAX 5 4. int main() 5. { 6. int array[MAX]; 7. float sum = 0. ; 8. int i; 9. printf("Enter 5 integers: "); 10. for (i =0; i < MAX; i++) 11. { 12. scanf("%d", &array[i]); 13. sum = sum + array[i]; 14. } 15. for (i =0; i < MAX; i++) 16. printf("array[%d] = %dn", i, array[i]); 17. printf("Average = %. 2 fn", sum/MAX); 18. return 0; 19. }
C Example: Integer Array Output: Enter 5 integers: 1 2 3 4 5 Enter 5 integers: 1 2 3 4 6 array[0] = 1 array[1] = 2 array[2] = 3 array[3] = 4 array[4] = 5 array[4] = 6 Average = 3. 00 Average = 3. 20
C Example: Character Strings 1. // Strings are Array of Characters. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. #include <stdio. h> #include <string. h> #define MAX_STRING_LENGTH 80 int main() { char str[MAX_STRING_LENGTH]; unsigned long length; int i; str[0] = 'H'; str[1] = 'e'; str[2] = 'l'; str[3] = 'l'; str[4] = 'o'; str[5] = '!'; str[6] = '