Algo-Animation (ppt) [no-change]

advertisement
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,
(UIT2201: Algorithms) Page 1
LeongHW, SoC, NUS
Simulating an Algorithm
0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;
ALGORITHM Sum-1-to-5;
1.
sum  0
2.
k1
Let’s animate the execution of
this simple algorithm.
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
Finish: print out the value of sum
(UIT2201: Algorithms) Page 2
LeongHW, SoC, NUS
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
???
sum
???
Step 0.
CPU
Finish: print out the value of sum
Our abstract model
of the computer
(UIT2201: Algorithms) Page 3
LeongHW, SoC, NUS
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
sum
???
0
Step 1.
Sum  0;
CPU
Finish: print out the value of sum
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
1
sum
0
Step 2.
k  1;
CPU
Finish: print out the value of sum
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
1
sum
1
Step 3.
sum + k
=0+1
CPU
Finish: print out the value of sum
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
2
sum
1
Step 4.
k+1
=1+1
CPU
Finish: print out the value of sum
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
2
sum
1
Step 5.
(k > 5)?
(2 > 5)?
= NO
CPU
Finish: print out the value of sum
LeongHW, SoC, NUS
Condition check:
evaluate (k > 5)?
Algorithms)
8
NO (UIT2201:
 execute
Step 6Page
next.
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
2
sum
1
Step 6.
goto repeat
(Step 3)
CPU
Finish: print out the value of sum
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
2
sum
3
Step 3.
sum + k
=1+2
CPU
Finish: print out the value of sum
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
3
sum
3
Step 4.
k+1
=2+1
CPU
Finish: print out the value of sum
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
3
sum
3
Step 5.
(k > 5)?
(3 > 5)?
= NO
CPU
Finish: print out the value of sum
LeongHW, SoC, NUS
Condition check:
evaluate (k > 5)?
Algorithms)
12
NO (UIT2201:
 execute
Step 6Page
next.
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
3
sum
3
Step 6.
goto repeat
(Step 3)
CPU
Finish: print out the value of sum
Goto Step 3 and
Execute
the loop-body again.
(UIT2201: Algorithms) Page 13
LeongHW, SoC, NUS
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
3
sum
6
Step 3.
sum + k
=3+3
CPU
Finish: print out the value of sum
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
4
sum
6
Step 4.
k+1
=3+1
CPU
Finish: print out the value of sum
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
4
sum
6
Step 5.
(k > 5)?
(4 > 5)?
= NO
CPU
Finish: print out the value of sum
LeongHW, SoC, NUS
Condition check:
evaluate (k > 5)?
Algorithms)
16
NO (UIT2201:
 execute
Step 6Page
next.
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
4
sum
6
Step 6.
goto repeat
(Step 3)
CPU
Finish: print out the value of sum
Goto Step 3 and
Execute
the loop-body again.
(UIT2201: Algorithms) Page 17
LeongHW, SoC, NUS
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
4
sum
10
Step 3.
sum + k
=6+4
CPU
Finish: print out the value of sum
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
5
sum
10
Step 4.
k+1
=4+1
CPU
Finish: print out the value of sum
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
5
sum
10
Step 5.
(k > 5)?
(5 > 5)?
= NO
CPU
Finish: print out the value of sum
LeongHW, SoC, NUS
Condition check:
evaluate (k > 5)?
Algorithms)
20
NO (UIT2201:
 execute
Step 6Page
next.
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
5
sum
10
Step 6.
goto repeat
(Step 3)
CPU
Finish: print out the value of sum
Goto Step 3 and
Execute
the loop-body again.
(UIT2201: Algorithms) Page 21
LeongHW, SoC, NUS
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
5
sum
15
Step 3.
sum + k
= 10 + 5
CPU
Finish: print out the value of sum
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
6
sum
15
Step 4.
k+1
=5+1
CPU
Finish: print out the value of sum
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
6
sum
15
Step 5.
(k > 5)?
(6 > 5)?
= YES
CPU
Finish: print out the value of sum
LeongHW, SoC, NUS
Condition check:
evaluate (k > 5)?
Algorithms)
24
YES(UIT2201:
 execute
StepPage
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
finish:
print out the value of sum
k
6
sum
15
Step 7.
goto finish
(Step 8)
CPU
Goto finish (Step 8)
(exit(UIT2201:
the iterative
loop!)
Algorithms) Page 25
LeongHW, SoC, NUS
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
finish:
print out the value of sum
k
6
sum
15
Step 8.
Print to output
CPU
Output of Algorithm:
15
LeongHW, SoC, NUS
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?
(UIT2201: Algorithms) Page 27
LeongHW, SoC, NUS
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.)
(UIT2201: Algorithms) Page 28
LeongHW, SoC, NUS
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 100)?
6.
no  goto repeat
7.
yes  goto finish
k
100
sum
5050
Step 5.
(k > 100)?
(100 > 100)?
= NO
CPU
Finish: print out the value of sum
LeongHW, SoC, NUS
Condition check:
evaluate (k > 100)?
Algorithms)
29
NO (UIT2201:
 execute
Step 6Page
next.
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 5)?
6.
no  goto repeat
7.
yes  goto finish
k
100
sum
4950
Step 6.
goto repeat
(Step 3)
CPU
Finish: print out the value of sum
Goto Step 3 and
Execute
the loop-body again.
(UIT2201: Algorithms) Page 30
LeongHW, SoC, NUS
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 100)?
6.
no  goto repeat
7.
yes  goto finish
k
100
sum
5050
Step 3.
sum + k
= 4950 + 100
CPU
Finish: print out the value of sum
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 100)?
6.
no  goto repeat
7.
yes  goto finish
k
101
sum
5050
Step 4.
k+1
= 100 + 1
CPU
Finish: print out the value of sum
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 100)?
6.
no  goto repeat
7.
yes  goto finish
k
101
sum
5050
Step 5.
(k > 100)?
(101 > 100)?
= YES
CPU
Finish: print out the value of sum
LeongHW, SoC, NUS
Condition check:
evaluate (k > 100)?
Algorithms)
33
YES(UIT2201:
 execute
StepPage
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.
k1
repeat: add k to sum
4.
add 1 to k
5.
Is (k > 100)?
6.
no  goto repeat
7.
yes  goto finish
finish:
print out the value of sum
k
101
sum
5050
Step 7.
goto finish
(Step 8)
CPU
Goto finish (Step 8)
(exit(UIT2201:
the iterative
loop!)
Algorithms) Page 34
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 35
LeongHW, SoC, NUS
Download