Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Animation of Algorithm Goal: To understand an algorithm by animating its execution,

Similar presentations


Presentation on theme: "© Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Animation of Algorithm Goal: To understand an algorithm by animating its execution,"— Presentation transcript:

1 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Animation of Algorithm Goal: To understand an algorithm by animating its execution, step-by-step. Algorithm: Array-Sum (find sum of n numbers) Observe carefully: sequential instruction conditional statements, repetitive statements,

2 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 2 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum Let’s animate the execution of this algorithm on the input: Input to Algo. Array-Sum: n = 6 Array A = [2, 5, 10, 3, 12, 24]

3 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 3 Model of Computer: Initial State We assume that the values of n and the array A[1..6] has been read into the computer. We assume that the values of n and the array A[1..6] has been read into the computer. A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 CPU Sum_SF ? Sum ? k ? Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

4 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 4 Start of execution… A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 CPU Sum_SF ? Sum ? k ? Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

5 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 5 executing… A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 Sum_SF  0; CPU Sum_SF 0 Sum ? k ? Assignment statement; The value of 0 is stored in the storage box called Sum_sf. Assignment statement; The value of 0 is stored in the storage box called Sum_sf. Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

6 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 6 executing… A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 k  1; CPU Sum_SF 0 Sum ? k 1 Assignment statement; The value of 1 is stored in the storage box called k. Assignment statement; The value of 1 is stored in the storage box called k. Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

7 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 7 executing beginning of loop A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 CPU Sum_SF 0 Sum ? k 1 Conditional: (k <= n) Reads value of k and n; Check outcome of (k <= n), Condition is True, execute loop; Value of k, n are unchanged. Conditional: (k <= n) Reads value of k and n; Check outcome of (k <= n), Condition is True, execute loop; Value of k, n are unchanged. Is (k <= n)? Is (1 <= 6)? True Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

8 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 8 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 (k = 1) (A[k] = 2) RHS = 0 + 2 = 2 CPU Sum_SF 0 Sum ? k 1 Calculate value of expression on the right-hand-side; Note: In A[k], k is used as an index (subscript) to the array A Calculate value of expression on the right-hand-side; Note: In A[k], k is used as an index (subscript) to the array A Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

9 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 9 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 (k = 1) (A[k] = 2) RHS = 0 + 2 = 2 Sum_SF  2 CPU Sum_SF 2 Sum ? k 1 Calculate value of expression on the right-hand-side; Assign result to variable on left; Calculate value of expression on the right-hand-side; Assign result to variable on left; Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

10 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 10 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 k  k + 1; k  1 + 1; CPU Sum_SF 2 Sum ? k 1 This increments value of k; 2 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

11 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 11 End of body of loop, loop back... A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 CPU Sum_SF 2 Sum ? k 2 Reached end of the loop body. Must loop back and re-test the loop condition. (We will skip this in future loops) Reached end of the loop body. Must loop back and re-test the loop condition. (We will skip this in future loops) Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

12 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 12 Test loop condition again… A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 CPU Sum_SF 2 Sum ? k 2 Testing the loop condition again with new value of k. (This is call a pre-test loop, where testing is done before entering the loop.) Testing the loop condition again with new value of k. (This is call a pre-test loop, where testing is done before entering the loop.) Is (k <= n)? Is (2 <= 6)? True Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

13 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 13 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 (k = 2) (A[k] = 5) RHS = 2 + 5 = 7 CPU Sum_SF 2 Sum ? k 2 Notice that A[k] now gets the second number in the list; Notice that A[k] now gets the second number in the list; Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

14 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 14 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 (k = 2) (A[k] = 5) RHS = 2 + 5 = 7 Sum_sf  7 CPU Sum_SF 7 Sum ? k 2 Notice that A[k] now gets the second number in the list; This number (5) is added to Sum_SF Notice that A[k] now gets the second number in the list; This number (5) is added to Sum_SF Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

15 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 15 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 k  k + 1; k  2 + 1; CPU Sum_SF 7 Sum ? k 2 This increments value of k; 3 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

16 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 16 Test loop condition again… A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 CPU Sum_SF 7 Sum ? k 3 Testing the loop condition again with new value of k. Testing the loop condition again with new value of k. Is (3 <= 6)? True Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

17 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 17 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 (k = 3) (A[k] = 10) Rhs = 7 + 10 Sum_SF  17 CPU Sum_SF 7 Sum ? k 3 Now A[k] gets the third element Add A[3] to Sum_sf; Now A[k] gets the third element Add A[3] to Sum_sf; 17 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

18 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 18 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 k  k + 1; k  3 + 1; CPU Sum_SF 17 Sum ? k 3 increment k; 4 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

19 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 19 Test loop condition again… A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 CPU Sum_SF 17 Sum ? k 4 Testing the while loop condition again with new value of k. Testing the while loop condition again with new value of k. Is (4 <= 6)? True Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

20 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 20 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 (k = 4) (A[k] = 3) Rhs = 17 + 3 Sum_SF  20 CPU Sum_SF 17 Sum ? k 4 Add A[4] to Sum_SF; 20 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

21 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 21 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 k  4 + 1; CPU Sum_SF 20 Sum ? k 4 increment k; 5 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

22 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 22 Test loop condition again… A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 CPU Sum_SF 20 Sum ? k 5 Testing the while loop condition again with new value of k. Testing the while loop condition again with new value of k. Is (5 <= 6)? True Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

23 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 23 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 (k = 5) (A[k] = 12) Rhs = 20 + 12 Sum_SF  32 CPU Sum_SF 20 Sum ? k 5 Add A[5] to Sum_SF; 32 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

24 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 24 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 k  5 + 1; CPU Sum_SF 32 Sum ? k 5 increment k; 6 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

25 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 25 Test loop condition again… A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 CPU Sum_SF 32 Sum ? k 6 Testing the while loop condition again with new value of k. Testing the while loop condition again with new value of k. Is (6 <= 6)? True Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

26 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 26 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 (k = 6) (A[k] = 24) Rhs = 32 + 24 Sum_SF  56 CPU Sum_SF 32 Sum ? k 6 Add A[6] to Sum_SF; 56 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

27 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 27 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 k  6 + 1; CPU Sum_SF 56 Sum ? k 6 increment k; 7 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

28 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 28 Test loop condition again… A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 CPU Sum_SF 56 Sum ? k 7 The loop condition fails (is false); Skip the body of loop. Go to the statement after the while loop; The loop condition fails (is false); Skip the body of loop. Go to the statement after the while loop; Is (7 <= 6)? False Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

29 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 29 Jump out of loop, to next stmt… A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 CPU Sum_SF 56 Sum 56 k 7 Copy value of Sum_SF to Sum Sum  Sum_SF Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

30 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 30 Print statement… A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 CPU Sum_SF 56 Sum 56 k 7 Output of Algrithm Sum: Sum is 56 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

31 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 31 End of Algorithm… A[1] A[2] A[3] A[4] A[5] A[6] 2 5 10 3 12 24 n 6 CPU Sum_sf 56 Sum 56 k 7 Output of Algrithm Sum: Sum is 56 End of the Algorithm Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

32 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 32 Your Homework: On your own, execute algorithm Sum on this input. Input to Algorithm Sum: n = 4 Array A = [4, 7, 3, 9, 2, 20, 10] Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

33 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 33 Summary Review: A. Did you understand the following concepts: 1. variables (storage boxes) 2. arrays (a collection of contiguous variables) 3. array index or subscript B. Can you follow the execution of sequential instruction, the loop pre-test, repetitive statements?

34 © Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 34 Self Assessment Self Test 1: (based on the example) 1. How many times was the loop executed? 2. How many times was the pre-loop test executed? 3. What is the value of k at the end of the algorithm? Self Test 2: (for a general value of n) 1. How many times was the loop executed? 2. How many times was the pre-loop test executed? 3. What is the value of k at the end of the algorithm? Self Test 3: (Modifying the algorithm slightly to …) 1. compute the average of the n numbers in array A. 2. the “sum-of-squares” of the n numbers.


Download ppt "© Leong Hon Wai, 2003-2009 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Animation of Algorithm Goal: To understand an algorithm by animating its execution,"

Similar presentations


Ads by Google