Download presentation

Presentation is loading. Please wait.

Published byMekhi Freeman Modified over 3 years ago

1
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, 2003-2008 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: osequential operations/statements oconditional statements, oiterative statements,

2
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 2 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Lets animate the execution of this simple algorithm.

3
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 3 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k ??? Step 0. CPU sum ??? Our abstract model of the computer Initial state of the algorithm

4
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 4 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k ??? Step 1. Sum 0; CPU sum 0 Start of execution, at Step 1. Assignment statement; The value of 0 is stored in the storage box called sum. Assignment statement; The value of 0 is stored in the storage box called sum.

5
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 5 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 1 Step 2. k 1; CPU sum 0 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. Executing Step 2.

6
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 6 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 1 Step 3. sum + k = 0 + 1 CPU sum 1 Assignment statement; The new value of sum is stored; the old value is gone. Assignment statement; The new value of sum is stored; the old value is gone. Executing Step 3. start of body-of-loop

7
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 7 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 2 Step 4. k + 1 = 1 + 1 CPU sum 1 Assignment statement; The new value of k is stored; the old value is gone. Assignment statement; The new value of k is stored; the old value is gone. Executing Step 4. inside body-of-loop

8
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 8 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 2 Step 5. (k <= 5)? (2 <= 5)? = TRUE CPU sum 1 Condition check: evaluate (k <= 5)? TRUE execute Step 6 next. Condition check: evaluate (k <= 5)? TRUE execute Step 6 next. Executing Step 5. loop-test

9
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 9 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 2 Step 6. Goto Repeat (Step 3) CPU sum 1 goto Repeat means to get algorithm to continue at the step labelled repeat. goto Repeat means to get algorithm to continue at the step labelled repeat. Executing Step 6.

10
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 10 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 2 Step 3. sum + k = 1 + 2 CPU sum 3 Add 2 to sum; The new value of sum is stored; the old value is gone. Add 2 to sum; The new value of sum is stored; the old value is gone. Executing Step 3. 2 nd round of loop-body

11
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 11 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 3 Step 4. k + 1 = 2 + 1 CPU sum 3 Increment k; The new value of k is stored; the old value is gone. Increment k; The new value of k is stored; the old value is gone. Executing Step 4. 2 nd round of loop-body

12
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 12 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 k 3 Step 5. (k <= 5)? (3 <= 5)? = TRUE CPU sum 3 Condition check: evaluate (k <= 5)? TRUE execute Step 6 next. Condition check: evaluate (k <= 5)? TRUE execute Step 6 next. Executing Step 5. 2 nd loop-test 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

13
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 13 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 k 3 Step 6. Goto Repeat (Step 3) CPU sum 3 Goto Step 3 and Execute the loop-body again. Goto Step 3 and Execute the loop-body again. Executing Step 6. 2 nd round 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

14
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 14 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 k 3 Step 3. sum + k = 3 + 3 CPU sum 6 Add 3 to sum; The new value of sum is stored; the old value is gone. Add 3 to sum; The new value of sum is stored; the old value is gone. Executing Step 3. 3 rd round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

15
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 15 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 k 4 Step 4. k + 1 = 3 + 1 CPU sum 6 Increment k; The new value of k is stored; the old value is gone. Increment k; The new value of k is stored; the old value is gone. Executing Step 4. 3 rd round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

16
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 16 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 k 4 Step 5. (k <= 5)? (4 <= 5)? = TRUE CPU sum 6 Condition check: evaluate (k <= 5)? TRUE execute Step 6 next. Condition check: evaluate (k <= 5)? TRUE execute Step 6 next. Executing Step 5. 3 rd loop-test 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

17
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 17 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 k 4 Step 6. Goto Repeat (Step 3) CPU sum 6 Goto Step 3 and Execute the loop-body again. Goto Step 3 and Execute the loop-body again. Executing Step 6. 3 rd round 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

18
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 18 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 k 4 Step 3. sum + k = 6 + 4 CPU sum 10 Add 4 to sum; The new value of sum is stored; the old value is gone. Add 4 to sum; The new value of sum is stored; the old value is gone. Executing Step 3. 4 th round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

19
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 19 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 k 5 Step 4. k + 1 = 4 + 1 CPU sum 10 Increment k; The new value of k is stored; the old value is gone. Increment k; The new value of k is stored; the old value is gone. Executing Step 4. 4 th round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

20
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 20 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 k 5 Step 5. (k <= 5)? (5 <= 5)? = TRUE CPU sum 10 Condition check: evaluate (k <= 5)? TRUE execute Step 6 next. Condition check: evaluate (k <= 5)? TRUE execute Step 6 next. Executing Step 5. 4 th loop-test 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

21
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 21 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 k 5 Step 6. goto repeat (Step 3) CPU sum 10 Goto Step 3 and Execute the loop-body again. Goto Step 3 and Execute the loop-body again. Executing Step 6. 4 th round 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

22
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 22 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 k 5 Step 3. sum + k = 10 + 5 CPU sum 15 Add 5 to sum; The new value of sum is stored; the old value is gone. Add 5 to sum; The new value of sum is stored; the old value is gone. Executing Step 3. 5 th round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

23
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 23 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 k 6 Step 4. k + 1 = 5 + 1 CPU sum 15 Increment k; The new value of k is stored; the old value is gone. Increment k; The new value of k is stored; the old value is gone. Executing Step 4. 5 th round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

24
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 24 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 k 6 Step 5. (k <= 5)? (6 <= 5)? = FALSE CPU sum 15 Condition check: evaluate (k <= 5)? FALSE execute Step 7 next. Condition check: evaluate (k <= 5)? FALSE execute Step 7 next. Executing Step 5. 5 th loop-test 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

25
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 25 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 k 6 Step 7. goto finish (Step 8) CPU sum 15 Goto finish (Step 8) (exit the iterative loop!) Goto finish (Step 8) (exit the iterative loop!) Executing Step 7. & exit the iterative loop 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

26
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 26 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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 k 6 Step 8. Print to output CPU sum 15 Print statement; print to output the value of sum Print statement; print to output the value of sum Executing Step 8. print output and END 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Output of Algorithm: 15

27
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 27 © Leong Hon Wai, 2003-2008 Summary Summary of Steps: o1, 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 oConditional statements oGoto statements oiterative statements Questions: oWhere is the loop-body? oHow many iteration of the loop-body? oHow many times is the loop-test done?

28
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 28 © Leong Hon Wai, 2003-2008 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.)

29
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 29 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-100; 1. sum 0 2. k 1 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: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 100 Step 5. (k <= 100)? (100 <= 100)? = TRUE CPU sum 5050 Condition check: evaluate (k <= 100)? TRUE execute Step 6 next. Condition check: evaluate (k <= 100)? TRUE execute Step 6 next. Executing Step 5. 99 th loop-test

30
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 30 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 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: print out the value of sum k 100 Step 6. Goto Repeat (Step 3) CPU sum 4950 Goto Step 3 and Execute the loop-body again. Goto Step 3 and Execute the loop-body again. Executing Step 6. 99 th round 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

31
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 31 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-100; 1. sum 0 2. k 1 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: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 100 Step 3. sum + k = 4950 + 100 CPU sum 5050 Add 100 to sum; The new value of sum is stored; the old value is gone. Add 100 to sum; The new value of sum is stored; the old value is gone. Executing Step 3. 100 th round of loop-body

32
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 32 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-100; 1. sum 0 2. k 1 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: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 101 Step 4. k + 1 = 100 + 1 CPU sum 5050 Increment k; The new value of k is stored; the old value is gone. Increment k; The new value of k is stored; the old value is gone. Executing Step 4. 100 th round of loop-body

33
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 33 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-100; 1. sum 0 2. k 1 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: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 101 Step 5. (k <= 100)? (101 <= 100)? = FALSE CPU sum 5050 Condition check: evaluate (k <= 100)? FALSE execute Step 7 next. Condition check: evaluate (k <= 100)? FALSE execute Step 7 next. Executing Step 5. 100 th loop-test

34
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 34 © Leong Hon Wai, 2003-2008 Simulating an Algorithm ALGORITHM Sum-1-to-100; 1. sum 0 2. k 1 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: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 101 Step 7. Goto finish (Step 8) CPU sum 5050 Goto finish (Step 8) (exit the iterative loop!) Goto finish (Step 8) (exit the iterative loop!) Executing Step 7. & exit the iterative loop

35
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 35 © Leong Hon Wai, 2003-2008

Similar presentations

Presentation is loading. Please wait....

OK

A Multiplication Algorithm

A Multiplication Algorithm

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Brochure ppt on architectural firm Ppt on product specification software Ppt on surface water treatment Mp ppt online form 2016 Free ppt on american war of independence Ppt on ar 25-50 Ppt on column chromatography lab Ppt on mobile tv Ppt on world diabetes day 2015 Ppt on sea level rise toolbox