1 / 69

CSI 1306

CSI 1306. ALGORITHMS - PART 5 SUB ALGORITHMS. Solving More Complicated Problems. So far we have developed algorithms that are limited in complexity How would we develop algorithms for larger and more complex programs? ie. Word, PowerPoint, Excel. Four Stages of Solving a Problem.

zan
Download Presentation

CSI 1306

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSI 1306 ALGORITHMS - PART 5 SUB ALGORITHMS

  2. Solving More Complicated Problems • So far we have developed algorithms that are limited in complexity • How would we develop algorithms for larger and more complex programs? • ie. Word, PowerPoint, Excel

  3. Four Stages of Solving a Problem • 1 - Understand the Problem • Define the givens (the input) • Define the expected results (the output) • 2 – Develop a General Solution • How do we get from givens to results? • Perhaps, divide the problem into smaller problems/tasks (sub problems), a technique known as divide and conquer • Develop the outline of an algorithm (sub algorithm) for each of these sub-problems/tasks, indicating how each relates to the overall algorithm

  4. Four Stages of Solving a Problem • 3 – Develop a Detailed Solution • Define the steps of each sub algorithm in more detail in a succession of iterations • Develop an overall algorithm to link the sub algorithms • 4 - Paper Test the Algorithms • Verify correctness • Trace each algorithm with some givens for which you know the expected results • Compare the results obtained with the expected results • If they are the same, the algorithm works fine • If there is a difference, review and revise the algorithm

  5. Why Develop Sub-Algorithms? • There are advantages to breaking down a problem into smaller problems or tasks • Smaller problems are less complicated and easier to solve • We promote efficient task management • John does problem 1 and Mary does problem 2 • We are not completely stopped if one of the smaller problems cannot be solved immediately. • Solve the other problems • Then research and solve the difficult problem. • We may be able to reuse the solution to the smaller problem in another large problem

  6. Calling Algorithms • Let’s examine the Definition from our Algorithm Description Result := Name(Givens) • We can use the definition as a call statement in another algorithm. Then, it means • Run (execute) the algorithm Name • Using the values of the Given variables • And place the result in the variable Result • Consider the algorithm AVGL that we developed previously

  7. Calling Algorithms • Write the algorithm for finding the average of a list • Name: AVGL • Givens: L, N • Change: None • Results: Avg • Intermediates: Sum, I • Definition • Avg := AVGL(L, N) Method Get L, N Let Sum = 0 Let I = 1 Loop When (I <= N) Let Sum = Sum + LI Let I = I + 1 Finish Loop Let Avg = Sum/N Give Avg

  8. Calling Algorithms • If we use the following call statement in any algorithm Avg := AVGL(L, N) • When the statement executes • AVGL executes • Using the values of the Givens L and N • And places the result in the variable Avg • But what if we want to compute the average of the values in another list (L2) of another size (N2) and place the results in another variable (Avg2)?

  9. Calling Algorithms • We could rewrite the algorithm using the same logic in our method but changing the Givens and Result variables. • But that is a lot of work and would have to be repeated each time we wanted to find the average of the values in a different list • Instead, we use the following call statement Avg2 := AVGL(L2, N2) • When the statement executes • AVGL executes • Using the value of L2 which is passed to L and the value of N2 which is passed to N • And places the result in the variable Avg2

  10. Calling Algorithms • The Givens are called parameters or arguments • The ones whose values we actually use when we run the algorithm (L2, N2) are the actual parameters • They appear in the calling algorithm • The ones whose values we used when we developed the algorithm (L, N) are the formal parameters • They appear in the called algorithm

  11. Calling Algorithms • When the algorithm is run by a call statement, the values of the actual parameters are passed to the called algorithm and become the values of the formal parameters. So naturally • The number of actual parameters in the calling algorithm must equal the number of formal parameters in the called algorithm • The one to one correspondence between parameters is determined by sequence • The first actual parameter corresponds to the first formal parameter; the second actual parameter corresponds to the second formal parameter, etc.

  12. An Analogy • The Pythagorean theorem (where c represents the length of the hypotenuse, and a and b represent the lengths of the other 2 sides of a right-angled triangle) states that C = (a2 + b2)0.5 • a and b can be thought of as formal parameters • When you want to use the formula to compute a value for c, you substitute values for the formal parameters, such as a = 3 and b = 4. 3 and 4 can be thought of as the actual parameters

  13. Calling Algorithms • Now, let’s see how this fits with our desire to divide a large problem into a number of smaller sub problems

  14. Algorithm 5.1 • Given a list of class marks, determine how many of the marks exceed the class average • Step 1 - Understand the Problem) • Givens • Marks - list of all marks • NStu - number of students in class (size of the list) • Result • Ngood - number of students with a mark above the average mark

  15. Algorithm 5.1 • Step 2 – Develop a General Solution • Find the average of the marks • Already have an algorithm to calculate the average value in a list • Count the number of students whose mark is above the average • Already have an algorithm to find the number of values in a list that exceed a given value • Set up the two solutions to work together • Step 3 – Develop a Detailed Solution

  16. Algorithm 5.1 • The algorithm for finding the average of a list • Name: AVGL • Givens: L, N • Change: None • Results: Avg • Intermediates: Sum, I • Definition • Avg := AVGL(L, N) Method Get L, N Let Sum = 0 Let I = 1 Loop When (I <= N) Let Sum = Sum + LI Let I = I + 1 Finish Loop Let Avg = Sum/N Give Avg

  17. Algorithm 5.1 • The algorithm for finding how many elements exceed a given value • Name: COUNTL • Givens: L, N, V • Change: None • Results: Count • Intermediate: I • Definition • Count := COUNTL(L, N, V) Method Get L, N, V Let Count = 0 Let I = 1 Loop When (I <=N) If (LI > V) Let Count = Count + 1 I = I + 1 Finish Loop Give Count

  18. Algorithm 5.1 • AVGL calculates an average (Avg) from a list L of size N • Avg := AVGL(L,N) • However, we want to calculate an average mark (AMark) from a list Marks of size NStu • AMark := AVGL(Marks,NStu) • This notation means run algorithm AVGL, with list Marks (instead of L) of size NStu (instead of N) and assign the result to AMark (instead of Avg)

  19. Algorithm 5.1 • As you will see on the next slide, we are passing the parameters Marks and NStu of the algorithm Main to the algorithm AVGL to be used as Givens instead of the parameters L and N • Use the same logic for calculation of the number of students who exceed the average mark using COUNTL

  20. Algorithm 5.1 • Write the algorithm for the third task Name: MAIN Givens: Marks, NStu Change: None Results: NGood Intermediate: AMark Definition NGood:= MAIN(Marks, NStu) Method Get Marks, NStu AMark := AVGL(Marks, NStu) NGood := COUNTL(Marks, NStu, AMark) Give NGood

  21. Algorithm 5.1 • Putting it all together • Name: AVGL • Givens :L, N • Change: None • Results : Avg • Intermediate: Sum, I • Definition • Avg := AVGL(L, N) • Get L, N • Let Sum = 0 • Let I = 1 • Loop When (I <= N) • Let Sum = Sum + LI • Let I = I + 1 • Finish Loop • Let Avg = Sum/N • Give Avg • Name: COUNTL • Givens: L, N, V • Change: None • Results: Count • Intermediate: I • Definition • Count := COUNTL(L, N, V) • Get L, N, V • Let Count = 0 • Let I = 1 • Loop When (I <=N) • If (LI > V) • Let Count = Count + 1 • Let I = I + 1 • Finish Loop • Give Count • Name: MAIN • Givens: Marks, NStu • Change: None • Results: NGood • Intermediate: AMark • Definition • Ngood:= • MAIN(Marks,Nstu) • Get Marks, NStu • AMark := AVGL(Marks,NStu) • NGood := COUNTL(Marks, • NStu, AMark) • Give Ngood

  22. Update on Tracing • If the algorithm being traced calls another algorithm, we must also produce a trace for the second algorithm each time it is called. • A separate table should be prepared for each algorithm • Use the GET and GIVE statements to track the transfer of information from one algorithm to another • (optional) Use arrows to show the transfer of information from one algorithm to another

  23. Trace 5.1 LN L N Sum I Avg Test 11 (76,98,55,73) 4 12 0 13 1 14 (1<=4) 15 76 16 2 14 (2<=4) 15 174 16 3 14 (3<=4) 15 229 16 4 14 (4<=4) 15 302 16 5 14 (5<=4) 18 75.5 19 Output 75.5 • Trace the algorithm for the first task of 5.1 with the list (76,98,55,73) (11) Get L, N (12) Let Sum = 0 (13) Let I = 1 (14) Loop When (I <= N) (15) Let Sum = Sum + LI (16) Let I = I + 1 (17) Finish Loop (18) Let Avg = Sum/N (19) Give Avg

  24. Trace 5.1 LN L N V I Count Test 21 (76,98,55,73) 4 75.5 22 0 23 1 24 (1<=4) 25 (76>75.5) 26 1 27 2 24 (2<=4) 25 (98>75.5) 26 2 27 3 24 (3<=4) 25 (55>75.5) 27 4 24 (4<=4) 25 (73>75.5) 27 5 24 (5<=4) 29 Output 2 • Trace the algorithm for the second task of 5.1 with the list (76,98,55,73) (21) Get L, N, V (22) Let Count = 0 (23) Let I = 1 (24) Loop When (I <=N) (25) If (LI > V) (26) Let Count = Count + 1 (27) Let I = I + 1 (28) Finish Loop (29) Give Count

  25. Trace 5.1 • Trace the algorithm for the third task of 5.1 with the list (76,98,55,73)

  26. (01) Get Marks, NStu (02) AMark := AVGL(Marks, NStu) (03) NGood := COUNTL(Marks,NStu, AMark) (04) Give AMark ------------------------ (11) Get L, N (12) Let Sum = 0 (13) Let I = 1 (14) Loop When (I <= N) (15) Let Sum = Sum + LI (16) Let I = I + 1 (17) Finish Loop (18) Let Avg = Sum/N (19) Give Avg --------------------------------- (21) Get L, N, V (22) Let Count = 0 (23) Let I = 1 (24) Loop When (I <=N) (25) If (LI > V) (26) Let Count = Count + 1 (27) Let I = I + 1 (28) Finish Loop (29) Give Count MAIN AVGL COUNTL LN M NS NG AM L N I Sum Avg L N V I C 01 (76..) 4 02 11 (76,..) 4 12 0 13 1 15 76 16 2 15 174 16 3 15 229 16 4 15 302 16 5 18 75.5 19 Output 75.5 02 75.5 03 (76..) 4 21 (76..) 4 75.5 22 0 23 1 26 1 27 2 26 2 27 3 27 4 27 5 29 Output 2 03 2 04 Output 2

  27. Question • Write an algorithm to calculate the number of parts with a cost above the average cost of a part, given a list Costs of size Parts. The list Costs contains the cost of each part in inventory. • You will develop this algorithm during your next tutorial • The algorithm for this question and for example 5.1 show us • The value of dividing a problem into subproblems • The reuse of a solution to a problem in another problem • How to pass parameters • How algorithms for larger and more complex programs are developed

  28. Algorithm 5.2 • Write an algorithm to find the number of occurrences of K in a list L of size N • Name: COUNTK • Givens: L, N, K • Change: None • Result: C • Intermediate: I • Definition • C := COUNTK(L,N,K) Method Get L, N, K Let C = 0 Let I = 1 Loop When (I <= N) If (LI = K) Let C = C + 1 Let I = I + 1 Finish Loop Give C

  29. Trace 5.2 LN L N K I C Test 1 (3,1,4,1) 4 1 2 0 3 1 4 (1<=4) 5 (3=1) 7 2 4 (2<=4) 5 (1=1) 6 1 7 3 4 (3<=4) 5 (4=1) 7 4 4 (4<=4) 5 (1=1) 6 2 7 5 4 (5<=4) 9 Output 2 Trace algorithm 5.2 with the list (3,1,4,1) when K is 1 (1) Get L, N, K (2) Let C = 0 (3) Let I = 1 (4) Loop When (I <= N) (5) If (LI = K) (6) Let C = C + 1 (7) Let I = I + 1 (8) Finish Loop (9) Give C

  30. Algorithm 5.3 • Write an algorithm to find the maximum value in a list L of size N • Name: MAXL • Given: L, N • Change: None • Results: Max • Intermediate: I • Definition • Max := MAXL(L, N) Method Get L, N Let Max = -9999 Let I = 1 Loop When (I <= N) If (LI > Max) Let Max = LI Let I = I + 1 Finish Loop Give Max

  31. Trace 5.3 LN L N I Max Test 1 (2,9,3,4) 4 2 -9999 3 1 4 (1<=4) 5 (2>-9999) 6 2 7 2 4 (2<=4) 5 (8>2) 6 8 7 3 4 (3<=4) 5 (3>8) 7 4 4 (4<=4) 5 (4>8) 7 5 4 (5<=4) 9 Output 8 • Trace algorithm 5.3 with the list (2,8,3,4) Method (1) Get L, N (2) Let Max = -9999 (3) Let I = 1 (4) Loop When (I <= N) (5) If (LI > Max) (6) Let Max = LI (7) Let I = I + 1 (8) Finish Loop (9) Give Max

  32. Algorithm 5.4 • Write an algorithm to count the number of occurrences of the maximum value in a list L of size N • Name: CMAX • Givens: L, N • Change: None • Result: NMax • Intermediate: Max • Definition: • NMax := CMAX(L, N) Method Get L, N Max := MAXL(L,N) NMax := COUNTK(L,N,Max) Give NMax

  33. Trace 5.4 • Trace algorithm 5.4 with the list (5, 2, 5, 5)

  34. CMAX (01) Get L, N (02) Max := MAXL(L,N) (03) Nmax := COUNTK(L,N,Max) (04) Give NMax *** = (5,2,5,5) LN L N Max Nmax L N I Max L N K C I Test 01 *** 4 02 11 *** 4 12 -9999 13 1 14 (1<=4) 15 (5>-9999) 16 5 17 2 14 (2<=4) 15 (2>5) 17 3 14 (3<=4) 15 (5>5) 17 4 14 (4<=4) 15 (5>5) 17 5 14 (5<=4) 19 Output 5 02 5 03 MAXL (11) Get L, N (12) Let Max = -9999 (13) Let I = 1 (14) Loop When (I <= N) (15) If (LI > Max) (16) Let Max = LI (17) Let I = I + 1 (18) Finish Loop (19) Give Max COUNTK (21) Get L, N, K (22) Let C = 0 (23) Let I = 1 (24) Loop When (I <= N) (25) If (LI = K) (26) Let C = C + 1 (27) Let I = I + 1 (28) Finish Loop (29) Give C

  35. LN L N Max Nmax L N I Max L N K C I Test 01 *** 4 02 5 03 21 *** 4 5 22 0 23 1 24 (1<=4) 25 (5=5) 26 1 27 2 24 (2<=4) 25 (2=5) 27 3 24 (3<=4) 25 (5=5) 26 2 27 4 24 (4<=4) 25 (5=5) 26 3 27 5 24 (5<=4) 29 Output 3 03 3 04 Output 3 CMAX (01) Get L, N (02) Max := MAXL(L,N) (03) Nmax := COUNTK(L,N,Max) (04) Give NMax MAXL (11) Get L, N (12) Let Max = -9999 (13) Let I = 1 (14) Loop When (I <= N) (15) If (LI > Max) (16) Let Max = LI (17) Let I = I + 1 (18) Finish Loop (19) Give Max COUNTK (21) Get L, N, K (22) Let C = 0 (23) Let I = 1 (24) Loop When (I <= N) (25) If (LI = K) (26) Let C = C + 1 (27) Let I = I + 1 (28) Finish Loop (29) Give C

  36. Additional Material

  37. Flow Charts

  38. Flow Charts • To Call another algorithm, use the "Predefined Process" box. • Usually you do not show the off page references or Gives/Gets, but for these two examples they have been shown using dotted lines

  39. Algorithm 5.1a Name: AVGL Givens: L, N Change: None Results: Avg Intermediates: Sum, I Definition Avg := AVGL(L, N)

  40. Algorithm 5.1b Name: COUNTL Givens: L, N, V Change: None Results: Count Intermediate: I Definition Count := COUNTL(L, N, V)

  41. Algorithm 5.1c Name: MAIN Givens: Marks, NStu Change: None Results: NGood Intermediate: AMark Definition NGood:= MAIN(Marks, NStu)

  42. Algorithm 5.2 Name: COUNTK Givens: L, N, K Change: None Result: C Intermediate: I Definition C := COUNTK(L,N,K)

  43. Algorithm 5.3 Name: MAXL Given: L, N Change: None Results: Max Intermediate: I Definition Max := MAXL(L, N)

  44. Algorithm 5.4 Name: CMAX Givens: L, N Change: None Result: NMax Intermediate: Max Definition: NMax := CMAX(L, N)

  45. NSD

  46. NSD • To Call another algorithm, the standard call that is seen in the pseudo code can be used. • For these examples the Gives and Gets will be shown, like flowcharts, they are not required • Note • A Get in a called algorithm is a Give in the calling algorithm • A Give in a called algorithm is a Get in a calling algorithm

  47. Algorithm 5.1a Name: AVGL Givens: L, N Change: None Results: Avg Intermediates: Sum, I Definition Avg := AVGL(L, N)

  48. Algorithm 5.1b Name: COUNTL Givens: L, N, V Change: None Results: Count Intermediate: I Definition Count := COUNTL(L, N, V)

  49. Algorithm 5.1c Name: MAIN Givens: Marks, NStu Change: None Results: NGood Intermediate: AMark Definition NGood:= MAIN(Marks, NStu)

  50. Algorithm 5.2 Name: COUNTK Givens: L, N, K Change: None Result: C Intermediate: I Definition C := COUNTK(L,N,K)

More Related