Animation of Algorithm Goal: To understand an algorithm by animating its execution, step-by-step. Algorithm: Sum 1-to-5 (find sum from 1 to 5) (Note: Similar to Sum 1-to-100, but shorter!!) Observe carefully: sequential operations/statements conditional statements, iterative statements, © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Let’s animate the execution of this simple algorithm. ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 2 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Initial state of the algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k ??? sum ??? Step 0. CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Our abstract model of the computer (UIT2201: Algorithms) Page 3 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Start of execution, at Step 1. ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) k (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish sum ??? 0 Step 1. Sum 0; CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Assignment statement; The value of 0 is stored in the (UIT2201: Algorithms) Page 4 storage box called sum. Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 2. ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 1 sum 0 Step 2. k 1; CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Assignment statement; The value of 1 is stored in the (UIT2201: Algorithms) storage box called k. Page 5 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. start of “body-of-loop” ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 1 sum 1 Step 3. sum + k =0+1 CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Assignment statement; The new value of sum is stored; (UIT2201: the old valueAlgorithms) is gone. Page 6 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. inside “body-of-loop” ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 2 sum 1 Step 4. k+1 =1+1 CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Assignment statement; The new value of k is stored; (UIT2201: the old valueAlgorithms) is gone. Page 7 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. loop-test ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 2 sum 1 Step 5. (k <= 5)? (2 <= 5)? = TRUE CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Condition check: evaluate (k <= 5)? (UIT2201: Algorithms) Page68 next. TRUE execute Step Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 2 sum 1 Step 6. Goto Repeat (Step 3) CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS goto “Repeat” means to get algorithm to continue at (UIT2201: Algorithms) Page 9 the step labelled “repeat”. Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 2nd round of loop-body ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 2 sum 3 Step 3. sum + k =1+2 CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Add 2 to sum; The new value of sum is stored; (UIT2201: the old valueAlgorithms) is gone. Page 10 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 2nd round of loop-body ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 3 sum 3 Step 4. k+1 =2+1 CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Increment k; The new value of k is stored; (UIT2201: the old valueAlgorithms) is gone. Page 11 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 2nd loop-test ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 3 sum 3 Step 5. (k <= 5)? (3 <= 5)? = TRUE CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Condition check: evaluate (k <= 5)? (UIT2201: Algorithms) Page612next. TRUE execute Step Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. 2nd round ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 3 sum 3 Step 6. Goto Repeat (Step 3) CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Goto Step 3 and Execute the loop-body again. (UIT2201: Algorithms) Page 13 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 3rd round of loop-body ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 3 sum 6 Step 3. sum + k =3+3 CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Add 3 to sum; The new value of sum is stored; (UIT2201: the old valueAlgorithms) is gone. Page 14 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 3rd round of loop-body ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 4 sum 6 Step 4. k+1 =3+1 CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Increment k; The new value of k is stored; (UIT2201: the old valueAlgorithms) is gone. Page 15 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 3rd loop-test ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 4 sum 6 Step 5. (k <= 5)? (4 <= 5)? = TRUE CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Condition check: evaluate (k <= 5)? (UIT2201: Algorithms) Page616next. TRUE execute Step Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. 3rd round ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 4 sum 6 Step 6. Goto Repeat (Step 3) CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Goto Step 3 and Execute the loop-body again. (UIT2201: Algorithms) Page 17 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 4th round of loop-body ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 4 sum 10 Step 3. sum + k =6+4 CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Add 4 to sum; The new value of sum is stored; (UIT2201: the old valueAlgorithms) is gone. Page 18 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 4th round of loop-body ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 5 sum 10 Step 4. k+1 =4+1 CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Increment k; The new value of k is stored; (UIT2201: the old valueAlgorithms) is gone. Page 19 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 4th loop-test ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 5 sum 10 Step 5. (k <= 5)? (5 <= 5)? = TRUE CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Condition check: evaluate (k <= 5)? (UIT2201: Algorithms) Page620next. TRUE execute Step Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. 4th round ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 5 sum 10 Step 6. goto repeat (Step 3) CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Goto Step 3 and Execute the loop-body again. (UIT2201: Algorithms) Page 21 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 5th round of loop-body ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 5 sum 15 Step 3. sum + k = 10 + 5 CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Add 5 to sum; The new value of sum is stored; (UIT2201: the old valueAlgorithms) is gone. Page 22 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 5th round of loop-body ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 6 sum 15 Step 4. k+1 =5+1 CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Increment k; The new value of k is stored; (UIT2201: the old valueAlgorithms) is gone. Page 23 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 5th loop-test ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish k 6 sum 15 Step 5. (k <= 5)? (6 <= 5)? = FALSE CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Condition check: evaluate (k <= 5)? (UIT2201: Algorithms) Page 24 FALSE execute Step 7 next. Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 7. & exit the iterative loop ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish finish: k 6 sum 15 Step 7. goto finish (Step 8) CPU print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Goto finish (Step 8) (exit(UIT2201: the iterative loop!) Algorithms) Page 25 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 8. print output and END ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish finish: k 6 sum 15 Step 8. Print to output CPU print out the value of sum Output of Algorithm: 15 LeongHW, SoC, NUS © Leong Hon Wai, 2003-2008 Print statement; print(UIT2201: to output the value of sum Algorithms) Page 26 Summary Summary of Steps: 1, 2, (3,4,5,6), (3,4,5,6), (3,4,5,6), (3,4,5,6), (3,4,5,7), 8 Note the sequential execution, except for Conditional statements Goto statements iterative statements Questions: Where is the “loop-body”? How many iteration of the loop-body? How many times is the loop-test done? © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 27 Explore further (DIY) We did Sum 1-to-5 (instead of Sum 1-to-100) DIY: Simulate the execution for the original algorithm for Sum 1-to-100? (Use the following “ending”-slides to help you.) © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 28 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 99th loop-test ALGORITHM Sum-1-to-100; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 100) 6. then Goto Step 3. Repeat 7. else Goto Finish k 100 sum 5050 Step 5. (k <= 100)? (100 <= 100)? = TRUE CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Condition check: evaluate (k <= 100)? (UIT2201: Algorithms) Page629next. TRUE execute Step Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. 99th round ALGORITHM Sum-1-to-5; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 100) 6. then Goto Step 3. Repeat 7. else Goto Finish k 100 sum 4950 Step 6. Goto Repeat (Step 3) CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Goto Step 3 and Execute the loop-body again. (UIT2201: Algorithms) Page 30 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 100th round of loop-body ALGORITHM Sum-1-to-100; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 100) 6. then Goto Step 3. Repeat 7. else Goto Finish k 100 sum 5050 Step 3. sum + k = 4950 + 100 CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Add 100 to sum; The new value of sum is stored; (UIT2201: the old valueAlgorithms) is gone. Page 31 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 100th round of loop-body ALGORITHM Sum-1-to-100; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 100) 6. then Goto Step 3. Repeat 7. else Goto Finish k 101 sum 5050 Step 4. k+1 = 100 + 1 CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Increment k; The new value of k is stored; (UIT2201: the old valueAlgorithms) is gone. Page 32 Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 100th loop-test ALGORITHM Sum-1-to-100; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 100) 6. then Goto Step 3. Repeat 7. else Goto Finish k 101 sum 5050 Step 5. (k <= 100)? (101 <= 100)? = FALSE CPU Finish: print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Condition check: evaluate (k <= 100)? (UIT2201: Algorithms) Page 33 FALSE execute Step 7 next. Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 7. & exit the iterative loop ALGORITHM Sum-1-to-100; 1. sum 0 2. k1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 100) 6. then Goto Step 3. Repeat 7. else Goto Finish finish: k 101 sum 5050 Step 7. Goto finish (Step 8) CPU print out the value of sum © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS Goto finish (Step 8) (exit(UIT2201: the iterative loop!) Algorithms) Page 34 © Leong Hon Wai, 2003-2008 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 35