1 / 69

COMP108 Algorithmic Foundations Introduction + Mathematical Induction

COMP108 Algorithmic Foundations Introduction + Mathematical Induction. Prudence Wong http://www.csc.liv.ac.uk/~pwong/teaching/comp108/200708. Module information …. Teaching Team. Dr Prudence Wong Demonstrator Mr Matt Webster. Rm 3.18 Ashton Building. pwong@liverpool.ac.uk.

astro
Download Presentation

COMP108 Algorithmic Foundations Introduction + Mathematical Induction

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. COMP108Algorithmic FoundationsIntroduction + Mathematical Induction Prudence Wong http://www.csc.liv.ac.uk/~pwong/teaching/comp108/200708

  2. Module information …

  3. Teaching Team Dr Prudence Wong Demonstrator Mr Matt Webster Rm 3.18 Ashton Building pwong@liverpool.ac.uk Office hours: Monday 3-5pm

  4. Module Structure Lectures Tutorials Help channels Examination Assignments& Class Tests

  5. ~36 lectures(Mon 2-3pm, Thu 10-11am, Fri 10-11am) 12 tutorials, 1 hr per week 2 Assignments & 2 Class Tests(continuous assessment) A written examination(80% of final mark) Office hours, email, appointment (by email)

  6. Main Reference Introduction to the Design and Analysis of Algorithms Anany V. Levitin Villanova University Addison Wesley Additional Reading Introduction to Algorithms Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest The MIT Press

  7. More about tutorials Tutorials start this week Goto http://www.csc.liv.ac.uk/~pwong/teaching/comp108/200708 to register for a tutorial session Tutorials are either held in Rm 310 Ashton Building and Lab 4 George Holt Building Check the website to see the location for each tutorial The first tutorial will be held in Rm 310 Ashton Building

  8. More about assignments • plagiarism is the practise of presenting the thoughts or writings of another or others as original • consequence? • What happen if you are ill when there is a deadline for assignment? • Get medical proof and ask for extension • This is not an excuse to commit plagiarism declaration form on Plagiarism and Collusion

  9. More about Class Tests There will be two class tests • Friday 15 February 10-11am • Friday 18 April 10-11am Scope will be announced closer to the time Incorrect dates in handout

  10. Why COMP108? Pre-requisite for: COMP202, COMP209(COMP202 is pre-requisite for COMP308, COMP309) Algorithm design is a foundation for efficient and effective programs "Year 1 modules do not count towards honour classification…" (Message from Career Services: Employers DO consider year 1 module results)

  11. Aims What do we mean by good? • To give an overview of the study of algorithms in terms of their efficiency. • To introduce the standard algorithmic design paradigms employed in the development of efficient algorithmic solutions. • To describe the analysis of algorithms in terms of the use of formal models of Time and Space. • To give a brief introduction to the subject of computational complexity theory and its use in classifying computational problems. How to achieve? Can we prove? Can all problems be solved efficiently?

  12. Ready to start …

  13. Learning outcomes • Able to tell what an algorithm is and have some understanding why we study algorithms • Able to use pseudo code to describe algorithm • Able to prove by Mathematical Induction

  14. Learning outcomes • Able to tell what an algorithm is and have some understanding why we study algorithms • Able to use pseudo code to describe algorithm • Able to prove by Mathematical Induction

  15. What is an algorithm? A sequence of precise and concise instructions that guide you (or a computer) to solve a specific problem Daily life examples: cooking recipe, furniture assembly manual(What are input / output in each case?) Algorithm Input Output

  16. 8 10 6 4 12 3 3 5 2 5 3 5 9 7 7 5 6 11 1 5 9 Why do we study algorithms? The obvious solution to a problem may not be efficient Example: We are given a map with n cities and the traveling cost between the cities. What is the cheapest way to go from city A to city B? B • Simple solution • Compute the cost of each route from A to B • Choose the cheapest one A

  17. Shortest path to go from A to B The obvious solution to a problem may not be efficient How many routes between A & B? involving 1 intermediate city? • Simple solution • Compute the cost of each route from A to B • Choose the cheapest one B 2 A

  18. Shortest path to go from A to B The obvious solution to a problem may not be efficient How many routes between A & B? involving 1 intermediate city? 3 cities? • Simple solution • Compute the cost of each route from A to B • Choose the cheapest one B 2 Number of routes = 2 A

  19. Shortest path to go from A to B The obvious solution to a problem may not be efficient How many routes between A & B? involving 1 intermediate city? 3 cities? • Simple solution • Compute the cost of each route from A to B • Choose the cheapest one B 3 Number of routes = 2 Number of routes = 2 + 3 A

  20. Shortest path to go from A to B The obvious solution to a problem may not be efficient How many routes between A & B? involving 1 intermediate city? 3 cities? • Simple solution • Compute the cost of each route from A to B • Choose the cheapest one B Number of routes = 2 + 3 A

  21. Shortest path to go from A to B The obvious solution to a problem may not be efficient How many routes between A & B? involving 1 intermediate city? 3 cities? • Simple solution • Compute the cost of each route from A to B • Choose the cheapest one B Number of routes = 2 + 3 + 6 = 11 Number of routes = 2 + 3 A 6

  22. Shortest path to go from A to B The obvious solution to a problem may not be efficient TOO MANY!! How many routes between A & B? involving 1 intermediate city? 3? 8? • Simple solution • Compute the cost of each route from A to B • Choose the cheapest one B For large n, it is impossible to check all routes! We need more sophisticated solutions A

  23. Shortest path to go from A to B There is an algorithm, called Dijkstra's algorithm, that can compute this shortest path efficiently. B A

  24. 8 10 6 4 12 3 3 5 2 5 3 5 9 7 7 5 6 11 1 5 9 Idea of Dijkstra's algorithm Go one step from A, label the visited city with the cost. B 10 0 A

  25. 8 10 6 4 12 3 3 5 2 5 3 5 9 7 7 5 6 11 1 5 9 Idea of Dijkstra's algorithm Choose city with the smallest cost and go one step from it similarly. B 10 0 5 A

  26. 8 10 6 4 12 3 3 5 2 5 3 5 9 7 7 5 6 11 1 5 9 Idea of Dijkstra's algorithm Again ... B 10 0 5 A 7

  27. 8 10 6 4 12 3 3 5 2 5 3 5 9 7 7 5 6 11 1 5 9 Idea of Dijkstra's algorithm Choose city with the smallest cost. All neighbours of A have labels already. Choose the next smallest one. B 10 0 5 A 7

  28. 8 10 6 4 12 3 3 5 2 5 3 5 9 7 7 5 6 11 1 5 9 Idea of Dijkstra's algorithm This path must NOT be used because we find a cheaper alternative path An alternative cheaper route is found! Record this route and erase the more expensive one. B 10 8 0 5 A 7

  29. 8 10 6 4 12 3 3 5 2 5 3 5 9 7 7 5 6 11 1 5 9 Idea of Dijkstra's algorithm Repeat and repeat ... 17 B 8 0 5 A 7

  30. Idea of Dijkstra's algorithm We will further discuss Dijkstra’s algorithm later. Finally, the cheapest route from A to every other city will be discovered. ? B 8 8 10 6 4 12 3 0 3 ? ? 5 2 5 5 3 5 9 ? A 7 7 5 6 11 1 ? 5 7 9 ?

  31. Lesson to learn The most straightforward (or brute force) algorithm for a problem may run slowly. We need more sophisticated solutions.

  32. Learning outcomes • Able to tell what an algorithm is and have some understanding why we study algorithms • Able to use pseudo code to describe algorithm • Able to prove by Mathematical Induction

  33. How to represent algorithms …

  34. Algorithm vs Program Still remember? An algorithm is a sequence of precise and concise instructions that guide a computer to solve a specific problem Algorithms are free from grammatical rules • Content is more important than form • Acceptable as long as it tells people how to perform a task Programs must follow some syntax rules • Form is important • Even if the idea is correct, it is still not acceptable if there is syntax error

  35. Computing the n-th power Input: a number x & a non-negative integer n Output: the n-th power of x Algorithm: • Set a temporary variable p to 1. • Repeat the multiplication p = p * x for n times. • Output the result p.

  36. Pseudo Code p = 1 i = 1 while i <= n do begin p = p * x i = i+1 end output p Another way to describe algorithm is by pseudo code p = 1 for i = 1 to n do p = p * x output p similar to programming language more like English Combination of both

  37. Pseudo Code: statement Assignment statement variable = expression e.g., assign the expression 3 * 4 to the variable result result = 3 * 4 compound statements begin statement1 statement2 end

  38. Pseudo Code: conditional if a < 0 then a = -a abs = a output abs Conditional statement if condition then statement if condition then statement else statement if a > 0 then abs = a else abs = -a output abs What do these two algorithms compute?

  39. Pseudo Code: iterative (loop) Iterative statement for var = start_value to end_value do statement while condition do statement repeat statement until condition condition to CONTINUE the loop condition to STOP the loop condition for while loop is NEGATION of condition for repeat-until loop

  40. for loop for var = start_value to end_value do statement Computing sum of the first n numbers: input n sum = 0 for i = 1 to n do begin sum = sum + i end output sum var is first assigned the value start_value. If var  end_value, execute the statement. var is incremented by 1 and then go back to step 2. the loop is executed for n times

  41. for loop for var = start_value to end_value do statement Computing sum of the first n numbers: input n sum = 0 for i = 1 to n do begin sum = sum + i end output sum i=1 No i <= n? Yes sum = sum+i the loop is executed for n times

  42. for loop for var = start_value to end_value do statement Computing sum of the first n numbers: input n sum = 0 for i = 1 to n do begin sum = sum + i end output sum suppose n = 4 the loop is executed for n times

  43. while loop condition to CONTINUE the loop if condition is true, execute the statement else stop go back to step 1 while condition do statement Computing sum of the first n numbers: input n sum = 0 i = 1 while i <= n do begin sum = sum + i i = i + 1 end output sum this while-loop performs the same task as the for-loop in the previous slides

  44. while loop – example 2 this loop is executed for undetermined number of times Computing sum of all input numbers: sum = 0 while (user wants to continue) do begin ask for a number sum = sum + number end output sum No continue? Yes ask for number sum = sum+number

  45. repeat-until condition to STOP the loop execute the statement if condition is true, stop go back to step 1 repeat statement until condition Computing sum of all input numbers: sum = 0 repeat ask for a number sum = sum + number until (user wants to stop) output sum • this loop is also executed for undetermined number of times • How it differs from while-loop?

  46. repeat-until condition to STOP the loop ask for number sum = sum+number repeat statement until condition Stop? No Yes Computing sum of all input numbers: sum = 0 repeat ask for a number sum = sum + number until (user wants to stop) output sum • this loop is also executed for undetermined number of times • How it differs from while-loop?

  47. Pseudo Code: exercise Write for-loops for the followings • Find the product of all integers in the interval [x, y], assuming that x and y are both integers • e.g., if x and y are 2 and 5, then the output should be 2*3*4*5 = 120 • List all factors of a given positive integer x • e.g., if x is 60, then the output should be 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60

  48. Hint (1) Find the product of all integers in the interval [x, y], assuming that x and y are both integers product = 1 for i = x to y do begin product = product * i end output product

  49. Hint (2) List all factors of a given positive integer x for i = 1 to x do begin if (x%i == 0) then output i end you may use the operator '%'. a%b means the remainder ofa divided b

  50. Convert to while loops Find the product of all integers in the interval [x, y], assuming that x and y are both integers product = 1 i = x while i <= y do begin product = product * i i = i+1 end output product

More Related