Title: Animation of Algorithm
1Animation 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,
2Simulating an Algorithm
011 123 336 6410 10515
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Lets animate the execution of this simple
algorithm.
3Simulating an Algorithm
011 123 336 6410 10515
Initial state of the algorithm
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Our abstract model of the computer
4Simulating an Algorithm
011 123 336 6410 10515
Start of execution, at Step 1.
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Assignment statement The value of 0 is stored in
thestorage box called sum.
5Simulating an Algorithm
011 123 336 6410 10515
Executing Step 2.
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Assignment statement The value of 1 is stored in
thestorage box called k.
6Simulating an Algorithm
011 123 336 6410 10515
Executing Step 3. start of body-of-loop
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Assignment statement The new value of sum is
stored the old value is gone.
7Simulating an Algorithm
011 123 336 6410 10515
Executing Step 4. inside body-of-loop
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Assignment statement The new value of k is
stored the old value is gone.
8Simulating an Algorithm
011 123 336 6410 10515
Executing Step 5. loop-test
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Condition check evaluate (k gt 5)? NO ?
execute Step 6 next.
9Simulating an Algorithm
011 123 336 6410 10515
Executing Step 6.
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
goto repeat means to get algorithm to continue
at the step labelled repeat.
10Simulating an Algorithm
011 123 336 6410 10515
Executing Step 3. 2nd round of loop-body
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Add 2 to sum The new value of sum is stored
the old value is gone.
11Simulating an Algorithm
011 123 336 6410 10515
Executing Step 4. 2nd round of loop-body
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Increment k The new value of k is stored the
old value is gone.
12Simulating an Algorithm
011 123 336 6410 10515
Executing Step 5. 2nd loop-test
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Condition check evaluate (k gt 5)? NO ?
execute Step 6 next.
13Simulating an Algorithm
011 123 336 6410 10515
Executing Step 6. 2nd round
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Goto Step 3 and Execute the loop-body again.
14Simulating an Algorithm
011 123 336 6410 10515
Executing Step 3. 3rd round of loop-body
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Add 3 to sum The new value of sum is stored
the old value is gone.
15Simulating an Algorithm
011 123 336 6410 10515
Executing Step 4. 3rd round of loop-body
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Increment k The new value of k is stored the
old value is gone.
16Simulating an Algorithm
011 123 336 6410 10515
Executing Step 5. 3rd loop-test
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Condition check evaluate (k gt 5)? NO ?
execute Step 6 next.
17Simulating an Algorithm
011 123 336 6410 10515
Executing Step 6. 3rd round
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Goto Step 3 and Execute the loop-body again.
18Simulating an Algorithm
011 123 336 6410 10515
Executing Step 3. 4th round of loop-body
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Add 4 to sum The new value of sum is stored
the old value is gone.
19Simulating an Algorithm
011 123 336 6410 10515
Executing Step 4. 4th round of loop-body
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Increment k The new value of k is stored the
old value is gone.
20Simulating an Algorithm
011 123 336 6410 10515
Executing Step 5. 4th loop-test
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Condition check evaluate (k gt 5)? NO ?
execute Step 6 next.
21Simulating an Algorithm
011 123 336 6410 10515
Executing Step 6. 4th round
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Goto Step 3 and Execute the loop-body again.
22Simulating an Algorithm
011 123 336 6410 10515
Executing Step 3. 5th round of loop-body
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Add 5 to sum The new value of sum is stored
the old value is gone.
23Simulating an Algorithm
011 123 336 6410 10515
Executing Step 4. 5th round of loop-body
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Increment k The new value of k is stored the
old value is gone.
24Simulating an Algorithm
011 123 336 6410 10515
Executing Step 5. 5th loop-test
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Condition check evaluate (k gt 5)? YES ?
execute Step 7 next.
25Simulating an Algorithm
011 123 336 6410 10515
Executing Step 7. exit the iterative loop
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- finish print out the value of sum
Goto finish (Step 8) (exit the iterative
loop!)
26Simulating an Algorithm
011 123 336 6410 10515
Executing Step 8. print output and END
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- finish print out the value of sum
Print statement print to output the value of
sum
27Summary
- 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?
28Explore 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.)
29Simulating an Algorithm
011 123 336 6410 10515
Executing Step 5. 99th loop-test
- ALGORITHM Sum-1-to-100
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 100)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Condition check evaluate (k gt 100)? NO ?
execute Step 6 next.
30Simulating an Algorithm
011 123 336 6410 10515
Executing Step 6. 99th round
- ALGORITHM Sum-1-to-5
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 5)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Goto Step 3 and Execute the loop-body again.
31Simulating an Algorithm
011 123 336 6410 10515
Executing Step 3. 100th round of loop-body
- ALGORITHM Sum-1-to-100
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 100)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Add 100 to sum The new value of sum is stored
the old value is gone.
32Simulating an Algorithm
011 123 336 6410 10515
Executing Step 4. 100th round of loop-body
- ALGORITHM Sum-1-to-100
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 100)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Increment k The new value of k is stored the
old value is gone.
33Simulating an Algorithm
011 123 336 6410 10515
Executing Step 5. 100th loop-test
- ALGORITHM Sum-1-to-100
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 100)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- Finish print out the value of sum
Condition check evaluate (k gt 100)? YES ?
execute Step 7 next.
34Simulating an Algorithm
011 123 336 6410 10515
Executing Step 7. exit the iterative loop
- ALGORITHM Sum-1-to-100
- 1. sum ? 0
- 2. k ? 1
- repeat add k to sum
- 4. add 1 to k
- 5. Is (k gt 100)?
- 6. no ? goto repeat
- 7. yes ? goto finish
- finish print out the value of sum
Goto finish (Step 8) (exit the iterative
loop!)
35(No Transcript)