Alg-Animation (ppt) [updated]

advertisement
Animation of Algorithm
Goal:
To understand an algorithm by animating its
execution, step-by-step.
Algorithm: Sum (find sum of n numbers)
Observe carefully:
sequential instruction
conditional statements,
repetitive statements,
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 1
Algorithm Sum
Let’s animate the execution of
this algorithm on the input:
Sum(A, n);
begin
Sum_SF  0;
k  1;
Input to Algorithm Sum:
while (k <= n) do
n=6
Array A = [2, 5, 10, 3, 12, 24]
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 2
Model of Computer: Initial State
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
?
Sum_SF  0;
A[3]
10
k  1;
A[4]
3
A[5]
12
A[6]
24
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF
CPU
?
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
We assume that the values of
n and the array A[1..6] has
been read into the computer.
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 3
Start of execution…
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
?
Sum_SF  0;
A[3]
10
k  1;
A[4]
3
A[5]
12
A[6]
24
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF
CPU
?
Sum
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 4
?
executing…
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
?
Sum_SF  0;
A[3]
10
k  1;
A[4]
3
A[5]
12
A[6]
24
Sum_SF  0;
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF
CPU
0
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
Assignment statement;
The value of 0 is stored in the
storage box called Sum_sf.
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 5
executing…
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
1
Sum_SF  0;
A[3]
10
k  1;
A[4]
3
A[5]
12
A[6]
24
k  1;
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF
CPU
0
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
Assignment statement;
The value of 1 is stored in the
storage box called k.
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 6
executing beginning of loop
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
1
A[3]
10
Sum_SF  0;
k  1;
A[4]
3
A[5]
12
A[6]
24
Is (k <= n)?
Is (1 <= 6)?
True
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF
CPU
0
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
Conditional: (k <= n)
Reads value of k and n;
Check outcome of (k <= n),
Condition is True, execute loop;
Value(UIT2201:
of k, nAlgorithms)
are unchanged.
Page 7
inside body of loop
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
1
A[3]
10
A[4]
3
A[5]
12
A[6]
24
Sum_SF  0;
k  1;
(k = 1)
(A[k] = 2)
RHS = 0 + 2 = 2
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF
CPU
0
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
Calculate value of expression
on the right-hand-side;
Note: In A[k], k is used as an
index (subscript) to the array A
(UIT2201: Algorithms) Page 8
inside body of loop
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
1
A[3]
10
A[4]
3
A[5]
12
(k = 1)
(A[k] = 2)
RHS = 0 + 2 = 2
Sum_SF  2
A[6]
24
CPU
Sum_SF  0;
k  1;
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF
2
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
Calculate value of expression
on the right-hand-side;
Assign result to variable on left;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 9
inside body of loop
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
2
1
Sum_SF  0;
A[3]
10
k  1;
A[4]
3
A[5]
12
A[6]
24
k  k + 1;
k  1 + 1;
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF
CPU
2
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
This increments value of k;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 10
End of body of loop, loop back...
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
2
Sum_SF  0;
A[3]
10
k  1;
A[4]
3
A[5]
12
A[6]
24
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF
CPU
2
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
Reached end of the loop body.
Must loop back and
re-test the loop condition.
(We will skip this in future loops)
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 11
Test loop condition again…
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
2
A[3]
10
Sum_SF  0;
k  1;
A[4]
3
A[5]
12
A[6]
24
Is (k <= n)?
Is (2 <= 6)?
True
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF
CPU
2
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
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.)
(UIT2201: Algorithms) Page 12
inside body of loop
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
2
A[3]
10
A[4]
3
A[5]
12
A[6]
24
Sum_SF  0;
k  1;
(k = 2)
(A[k] = 5)
RHS = 2 + 5 = 7
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF
CPU
2
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
Notice that A[k] now gets the
second number in the list;
end;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 13
inside body of loop
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
2
A[3]
10
A[4]
3
A[5]
12
(k = 2)
(A[k] = 5)
RHS = 2 + 5 = 7
Sum_sf  7
A[6]
24
CPU
Sum_SF  0;
k  1;
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF
7
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
Notice that A[k] now gets the
second number in the list;
This number (5) is added to
Sum_SF
end;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 14
inside body of loop
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
3
2
Sum_SF  0;
A[3]
10
k  1;
A[4]
3
A[5]
12
A[6]
24
k  k + 1;
k  2 + 1;
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF
CPU
7
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
This increments value of k;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 15
Test loop condition again…
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
3
A[3]
10
Sum_SF  0;
Is (3 <= 6)?
k  1;
A[4]
3
True
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
A[5]
12
A[6]
24
Sum_SF
CPU
7
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
Testing the loop condition again
with new value of k.
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 16
inside body of loop
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
3
A[3]
10
A[4]
3
A[5]
12
(k = 3)
(A[k] = 10)
Rhs = 7 + 10
Sum_SF  17
A[6]
24
CPU
Sum_SF  0;
k  1;
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
7
Sum_SF 17
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
Now A[k] gets the third element
Add A[3] to Sum_sf;
end;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 17
inside body of loop
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
4
3
Sum_SF  0;
A[3]
10
k  1;
A[4]
3
A[5]
12
A[6]
24
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF 17
k  k + 1;
k  3 + 1;
CPU
Sum
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
increment k;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 18
?
Test loop condition again…
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
4
A[3]
10
Sum_SF  0;
Is (4 <= 6)?
k  1;
A[4]
3
True
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
A[5]
12
A[6]
24
Sum_SF 17
CPU
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
Testing the while loop condition
again with new value of k.
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 19
inside body of loop
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
4
A[3]
10
A[4]
3
A[5]
12
(k = 4)
(A[k] = 3)
Rhs = 17 + 3
Sum_SF  20
A[6]
24
CPU
Sum_SF  0;
k  1;
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
17
Sum_SF 20
Sum
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
Add A[4] to Sum_SF;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 20
?
inside body of loop
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
5
4
Sum_SF  0;
A[3]
10
k  1;
A[4]
3
A[5]
12
A[6]
24
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF 20
k  4 + 1;
CPU
Sum
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
increment k;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 21
?
Test loop condition again…
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
5
A[3]
10
Sum_SF  0;
Is (5 <= 6)?
k  1;
A[4]
3
True
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
A[5]
12
A[6]
24
Sum_SF 20
CPU
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
Testing the while loop condition
again with new value of k.
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 22
inside body of loop
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
5
A[3]
10
A[4]
3
A[5]
12
(k = 5)
(A[k] = 12)
Rhs = 20 + 12
Sum_SF  32
A[6]
24
CPU
Sum_SF  0;
k  1;
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
20
Sum_SF 32
Sum
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
Add A[5] to Sum_SF;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 23
?
inside body of loop
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
6
5
Sum_SF  0;
A[3]
10
k  1;
A[4]
3
A[5]
12
A[6]
24
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF 32
k  5 + 1;
CPU
Sum
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
increment k;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 24
?
Test loop condition again…
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
6
A[3]
10
Sum_SF  0;
Is (6 <= 6)?
k  1;
A[4]
3
True
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
A[5]
12
A[6]
24
Sum_SF 32
CPU
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
Testing the while loop condition
again with new value of k.
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 25
inside body of loop
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
6
A[3]
10
A[4]
3
A[5]
12
(k = 6)
(A[k] = 24)
Rhs = 32 + 24
Sum_SF  56
A[6]
24
CPU
Sum_SF  0;
k  1;
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
32
Sum_SF 56
Sum
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
Add A[6] to Sum_SF;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 26
?
inside body of loop
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
7
6
Sum_SF  0;
A[3]
10
k  1;
A[4]
3
A[5]
12
A[6]
24
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF 56
k  6 + 1;
CPU
Sum
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
increment k;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 27
?
Test loop condition again…
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
7
A[3]
10
Sum_SF  0;
Is (7 <= 6)?
k  1;
A[4]
3
False
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
A[5]
12
A[6]
24
Sum_SF 56
CPU
Sum
?
Sum  Sum_SF;
Print “Sum is”, Sum;
The loop condition fails (is false);
Skip the body of loop.
Go to the statement after
the while loop;
end;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 28
Jump out of loop, to next stmt…
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
7
Sum_SF  0;
A[3]
10
k  1;
A[4]
3
A[5]
12
A[6]
24
Sum  Sum_SF
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF 56
CPU
Sum
56
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
Copy value of Sum_SF to Sum
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 29
Print statement…
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
7
Sum_SF  0;
A[3]
10
k  1;
A[4]
3
A[5]
12
A[6]
24
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_SF 56
CPU
Sum
56
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
Output of Algrithm Sum:
Sum is 56
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 30
End of Algorithm…
Algorithm Sum
Sum(A, n);
A[1]
2
n
6
begin
A[2]
5
k
7
Sum_SF  0;
A[3]
10
k  1;
A[4]
3
A[5]
12
A[6]
24
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
endwhile
Sum_sf
CPU
56
Sum
56
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
Output of Algrithm Sum:
End of the Algorithm
Sum is 56
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 31
Algorithm Sum
Your Homework:
Sum(A, n);
On your own, execute
algorithm Sum on this input.
begin
Sum_SF  0;
k  1;
Input to Algorithm Sum:
while (k <= n) do
Sum_SF  Sum_SF + A[k];
k  k + 1;
n=4
Array A = [4, 7, 3, 9, 2]
endwhile
Sum  Sum_SF;
Print “Sum is”, Sum;
end;
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 32
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?
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 33
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.
© Leong Hon Wai, 2003-2009
LeongHW, SoC, NUS
(UIT2201: Algorithms) Page 34
Download