1 / 35

Animation of Algorithm

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,

Download Presentation

Animation of Algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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,

  2. 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 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.

  3. 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 Simulating an Algorithm Step 0. k ??? ??? sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Initial state of the algorithm Our abstract model of the computer

  4. 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 Simulating an Algorithm Step 1. Sum  0; k ??? 0 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Start of execution, at Step 1. Assignment statement; The value of 0 is stored in thestorage box called sum.

  5. 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 Simulating an Algorithm Step 2. k 1; k 1 0 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 2. Assignment statement; The value of 1 is stored in thestorage box called k.

  6. 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 Simulating an Algorithm Step 3. sum + k = 0 + 1 k 1 1 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. start of “body-of-loop” Assignment statement; The new value of sum is stored; the old value is gone.

  7. 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 Simulating an Algorithm Step 4. k + 1 = 1 + 1 k 2 1 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. inside “body-of-loop” Assignment statement; The new value of k is stored; the old value is gone.

  8. 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 Simulating an Algorithm Step 5. (k <= 5)? (2 <= 5)? = TRUE k 2 1 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. loop-test Condition check: evaluate (k <= 5)? TRUE  execute Step 6 next.

  9. 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 Simulating an Algorithm Step 6. Goto Repeat(Step 3) k 2 1 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. goto“Repeat” means to get algorithm to continue at the step labelled “repeat”.

  10. 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 Simulating an Algorithm Step 3. sum + k = 1 + 2 k 2 3 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 2nd round of loop-body Add 2 to sum; The new value of sum is stored; the old value is gone.

  11. 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 Simulating an Algorithm Step 4. k + 1 = 2 + 1 k 3 3 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 2nd round of loop-body Increment k; The new value of k is stored; the old value is gone.

  12. 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 Simulating an Algorithm Step 5. (k <= 5)? (3 <= 5)? =TRUE k 3 3 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 2nd loop-test Condition check: evaluate (k <= 5)? TRUE  execute Step 6 next.

  13. 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 Simulating an Algorithm Step 6. Goto Repeat(Step 3) k 3 3 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. 2nd round Goto Step 3 and Execute the loop-body again.

  14. 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 Simulating an Algorithm Step 3. sum + k = 3 + 3 k 3 6 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 3rd round of loop-body Add 3 to sum; The new value of sum is stored; the old value is gone.

  15. 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 Simulating an Algorithm Step 4. k + 1 = 3 + 1 k 4 6 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 3rd round of loop-body Increment k; The new value of k is stored; the old value is gone.

  16. 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 Simulating an Algorithm Step 5. (k <= 5)? (4 <= 5)? = TRUE k 4 6 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 3rd loop-test Condition check: evaluate (k <= 5)? TRUE  execute Step 6 next.

  17. 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 Simulating an Algorithm Step 6. Goto Repeat(Step 3) k 4 6 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. 3rd round Goto Step 3 and Execute the loop-body again.

  18. 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 Simulating an Algorithm Step 3. sum + k = 6 + 4 k 4 10 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 4th round of loop-body Add 4 to sum; The new value of sum is stored; the old value is gone.

  19. 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 Simulating an Algorithm Step 4. k + 1 = 4 + 1 k 5 10 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 4th round of loop-body Increment k; The new value of k is stored; the old value is gone.

  20. 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 Simulating an Algorithm Step 5. (k <= 5)? (5 <= 5)? = TRUE k 5 10 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 4th loop-test Condition check: evaluate (k <= 5)? TRUE  execute Step 6 next.

  21. 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 Simulating an Algorithm Step 6. goto repeat(Step 3) k 5 10 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. 4th round Goto Step 3 and Execute the loop-body again.

  22. 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 Simulating an Algorithm Step 3. sum + k = 10 + 5 k 5 15 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 5th round of loop-body Add 5 to sum; The new value of sum is stored; the old value is gone.

  23. 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 Simulating an Algorithm Step 4. k + 1 = 5 + 1 k 6 15 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 5th round of loop-body Increment k; The new value of k is stored; the old value is gone.

  24. 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 Simulating an Algorithm Step 5. (k <= 5)? (6 <= 5)? = FALSE k 6 15 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 5th loop-test Condition check: evaluate (k <= 5)? FALSE  execute Step 7 next.

  25. 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 Simulating an Algorithm Step 7. goto finish(Step 8) k 6 15 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 7. & exit the iterative loop Goto finish (Step 8) (exit the iterative loop!)

  26. 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 Simulating an Algorithm Output of Algorithm: 15 Step 8. Print to output k 6 15 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 8. print output and END Print statement; print to output the value of sum

  27. 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?

  28. 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. 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 Simulating an Algorithm Step 5. (k <= 100)? (100 <= 100)? = TRUE k 100 5050 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 99th loop-test Condition check: evaluate (k <= 100)? TRUE  execute Step 6 next.

  30. 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 Simulating an Algorithm Step 6. Goto Repeat(Step 3) k 100 4950 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. 99th round Goto Step 3 and Execute the loop-body again.

  31. 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 Simulating an Algorithm Step 3. sum + k = 4950 + 100 k 100 5050 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 100th round of loop-body Add 100 to sum; The new value of sum is stored; the old value is gone.

  32. 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 Simulating an Algorithm Step 4. k + 1 = 100 + 1 k 101 5050 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 100th round of loop-body Increment k; The new value of k is stored; the old value is gone.

  33. 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 Simulating an Algorithm Step 5. (k <= 100)? (101 <= 100)? = FALSE k 101 5050 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 100th loop-test Condition check: evaluate (k <= 100)? FALSE  execute Step 7 next.

  34. 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 Simulating an Algorithm Step 7. Gotofinish(Step 8) k 101 5050 sum CPU 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 7. & exit the iterative loop Goto finish (Step 8) (exit the iterative loop!)

More Related