1 / 42

Overzicht Informatica College 6 - Oktober 11

Overzicht Informatica College 6 - Oktober 11. Computer Science an overview EDITION 7. J. Glenn Brookshear. C H A P T E R 4 (now chap. 5). Algorithm Program Process all related, but distinct entities.

xenia
Download Presentation

Overzicht Informatica College 6 - Oktober 11

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. Overzicht Informatica College 6 - Oktober 11 Computer Science an overview EDITION7 J. Glenn Brookshear

  2. C H A P T E R4 (now chap. 5) • Algorithm Program Process • all related, but distinct entities • How to tell computer precisely what to do? • algorithm • machine-compatible representation: program Algorithms

  3. The central role of algorithms in computer science

  4. As long as HALT not executed do: - fetch an instruction - decode the instruction - execute the instruction 4.1: The Definition of an Algorithm (1) • Informally: • set of steps that defines how a task is performed (we saw many examples)

  5. Formally: Note: mistake in book! 4.1: The Definition of an Algorithm (2) • (1) must have well-defined order in which steps are executed • not necessarily one after another (remember parallel processing!) • (2) each step must have unique & complete interpretation • (3) each step must be ‘doable’ • e.g.: ‘make a list of all positive integers’ is not doable • (4) (execution of algorithm must lead to an end) • well… e.g.: ‘divide 1.0 by 3.0’ is not a terminating process…

  6. Opdracht: 4.1.4 • Waarom is de volgende set instructies in stricte zin geen algorithme? • Stap 1: neem een munt uit je zak en leg ‘m op tafel • Stap 2: ga naar stap 1 • Probleem 1: de set van instructies termineert niet • Probleem 2: geen stap die aangeeft wat te doen als je geen munt (meer) in je zak hebt (dus: niet altijd ‘doable’)

  7. 4.1: Algorithm vs. Representation • Algorithm is abstract, conceptual • Representation is concrete, real • Compare: • Algorithm …………………….… Story • Representation …………………. Book • translation into another language changes representation, but not the story ! • So: algorithm can be represented in many different ways • e.g.: think of the many programming languages that exist

  8. Algorithm representation requires some form of language • e.g. natural language: English, Russian, Japanese, … • e.g. pictorial form: • … 4.2: Algorithm Representation • But: ‘natural’ communication often ambiguous • e.g.: “Visiting grandchildren can be nerve-racking” • So: well-defined building-blocks are required

  9. Primitives consist of: • syntax: symbolic representation • semantics: meaning (e.g.: “no smoking!”) 4.2: Building Blocks • Building blocks for algorithm construction • called: ‘primitives’ • if well-defined: primitives can remove ambiguity problems • Set of primitives + set of ‘rules for combining’ • constitutes a programming language

  10. syntax semantics LOAD R5, 6C LOAD R5, 6D ADD R0, R5, R6 STORE R0, 6E HALT set of primitives known as: assembly language 4.2: Syntax & Semantics of Machine Language

  11. 4.2: Obtaining Languages by Abstraction • Assembly language • slightly higher level of abstraction than machine language • slightly easier to use - but still very difficult • Better: much higher-level primitives • where each primitive is an abstraction of lower-level primitives provided by the machine language • Many choices possible => many languagesexist (see chapter 5) • for now we use ‘pseudo-code’ (less formal notation)

  12. 4.2: Pseudo-code • Pseudo-code: • intuitive/informal notational system • good starting point for representing algorithms in any high-level programming language • And now some definitions of primitives …

  13. e.g.: TrafficLight green 4.2: Pseudo-code Primitives (1) • Assignment of values to descriptive names (’variables’): • name expression • Choice between two possible activities: • if (condition) then (activity1) else (activity2) • e.g.: if (TrafficLight is green) then (drive) else (stop) • 1 conditional activity: if (TrafficLight is red) then (stop) • Repetition of one or more activities: • while (condition) do (activity) • e.g.: while (TrafficLight is green) do (hit the pedal)

  14. e.g.: 4.2: Pseudo-code Primitives (2) • Reusable, encapsulated code: • procedure name • e.g.: if (ApproachingPerson is Friend) then (Greetings) • Parameterized procedures: • procedurename(parameterlist) • e.g.: procedureSort(list)

  15. X het grootste getal; Y het kleinste getal; while ((X not 0) and (Y not 0)) do ( rest rest van X/Y; X Y; Y rest; ) GrootsteGemDeler X; X het grootste getal; Y het kleinste getal; Opdracht: 4.2.3 - grootste gemeenschappleijke deler van X & Y • Schrijf in pseudo-code: • Zo lang de getallen X en Y niet 0 zijn (met X >= Y), deel X doorY, geef X de waarde van Y, en geef Y de rest-waarde. X het grootste getal; Y het kleinste getal; while ((X not 0) and (Y not 0)) do (

  16. 4.2: Use of Indentation • For better readability: if (item is taxable) then ( if (price > limit) then ( pay x ) else ( pay y ) ) else ( pay z ) • Instead of: if (item is taxable) then (if (price > limit) then (pay x) else (pay y)) else (pay z)

  17. 4.3: Algorithm Discovery • The art of problem solving: • phase 1: • understand the problem • phase 2: • think of how an algorithmic procedure might solve the problem • phase 3: • formulate the algorithm and represent it as a program • phase 4: • evaluate the program for accuracy and for its potential to use it as a tool for solving other problems

  18. 4.3: Reconsidering the Problem Solving Phases • The phases are not steps to be followed one after another • e.g.: a deeper understanding of the problem often is gained by trial and error • A good (often used) approach: • ‘step-wise refinement’ • break the problem into smaller pieces, each of which is easier to solve • But: • the art of algorithm discovery can only be mastered over a period of time • so: just try… and don’t be afraid to fail initially…!

  19. 4.4: Describing Algorithmic Processes • Several ‘tools’ exist that you will often use in the design of algorithms: • (1) iterative structures • repeating a set of instructions in a looping manner while (condition) do (activity) • (2) recursive structures • repeating a set of instructions as a subtask of itself, e.g.: 4! = 4 x (3!) = 4 x (3 x (2!)) = 4 x (3 x (2 x (1!))) = 4 x (3 x (2 x (1))) = 4 x (3 x (2)) = 4 x (6) 24

  20. start start success! failure! 4.4: Iterative Structures (sequential search) • Consider the problem of searching an ordered list for a particular target value: 17 24 25 36 44 59 71 83 90 • TargetValue = 44 • TargetValue = 39

  21. TestEntry first entry in List while (TargetValue > TestEntry ANDentries remaining) do ( TestEntry next entry in List ) 4.4: Sequential Search Pseudo-code • This can be written in pseudo-code as follows: procedure Search (List, TargetValue) if (List is empty)then ( failure! ) else ( ) if (TargetValue = TestEntry)then ( succes! ) else ( failure! )

  22. Number 0 while (Number < 10) do ( Number Number + 1 doSomethingUseful ) • can be used for unknown number of iterations: while (roomTemperature < 16) do ( let heatingSystem run roomTemperature = measureTemperature(room) ) 4.4: Loop Control • Repetition by loop structure flexible: • can be used for fixed number of iterations:

  23. 4.4: Components of Loop Control • Note: termination condition is negation of condition in: ‘ while (condition) ‘

  24. Number 8 while (Number =75) do ( Number Number + 3 doSomethingUseful ) 4.4: Opdracht • What’s wrong here? • Loop never terminates as Number never hits ‘75’ • 8, 11, 14, …, 68, 71, 74, 77, …

  25. 4.4: While loop structure vs. Repeat loop structure while (condition) do (activity) repeat (activity) until (condition) • Many programming languages provide both constructs

  26. Z 0 X 1 repeat ( Z Z + X; X X + 1; ) until (X = 6) Opdracht 4.4.2 • Convert into loop using repeat statement: Z 0 X 1 while (X < 6) do ( Z Z + X; X X + 1; )

  27. 4.4: Another Example: Insertion Sort • Consider the problem of sorting a list of names into alphabetical order (‘within itself’)

  28. 4.4: Insertion Sort (continued)

  29. procedure Sort (List) N 2; while (N <= length of List) do ( PivotEntryN-th entry in List; N N + 1; ) 4.4: Insertion Sort in Pseudo-code Move PivotEntry to temporary location leaving hole in List; while (there is a name above holeANDname > PivotEntry) do ( Move name above hole down into hole leaving hole above name; ) Move pivotEntry into hole in List;

  30. 4.5: Recursive Structures (binary search) • Again, consider the problem of searching an ordered list for a particular target value: • but apply procedure as in dictionary search: • TargetValue = John

  31. references itself! 4.5: Binary Search in Pseudo-code • So: divide the list into smaller segments until the target is found, or an empty segment is reached

  32. (TestEntry) 4.5: Find ‘Bill’ in List Alice Bill Carol Report : SUCCES! Report : SUCCES!

  33. (TestEntry) empty 4.5: Find ‘David’… (1) Alice Carol

  34. 4.5: Find ‘David’… (2) empty Report : FAILED!

  35. 4.5: Find ‘David’… (3) Alice Carol (TestEntry) empty list Report : FAILED! Report : FAILED!

  36. 4.4: Factorials: n! = n x (n-1) x (n-2) x … x 1 procedure factorial (n) if (n = 1) then ( report: 1; ) else ( report: n x (report of factorial(n -1)); ) For example: factorial(4) 4 x (factorial(3)) 4 x (3 x (factorial(2))) 4 x (3 x (2 x (factorial(1)))) 4 x (3 x (2 x (1))) 4 x (3 x (2)) 4 x (6) 24

  37. binary search: • every comparison removes half of search-space • so, worst case: 15 comparisons per search (note: 2 = 32.768) 15 4.6: Algorithm Efficiency (1) • Although today’s computers may be fast, algorithm efficiency is major issue !!! • Consider university registry of 30.000 students • sequential search / binary search - any difference?? • sequential search: • average depth of search is halfway through the list • so, on average: 15.000 comparisons per search

  38. 4.6: Algorithm Efficiency (2) • Generalized: • for list of n entries: • sequential search: n/2 comparisons per search • binary search: log n comparisons per search • Significance: • number of comparisons gives approximation of the amount of time required for algorithm execution • Especially: • when number of entries (n) gets large

  39. 4.6: Worst-case Analysis of Binary Search

  40. 4.6: Worst-case Analysis of Insertion Sort

  41. Parabola-shape: • Θ(n ) 2 4.6: Complexity Analysis: Big-Theta Notation • Graph reflects efficiency characteristics of an algorithm • So, it is possible to classify algorithms based on graph-shape (esp.: worst-case) • notation used is called ‘big-theta notation’ • Log-shape • Θ(log n)

  42. Chapter 4 - Algorithms: Conclusions • Algorithm: • ordered, unambiguous, executable (& terminating) • Can be represented in many different ways • requires well-defined ‘primitives’ (syntax/semantics): • assignment, choice, repetition, encapsulation, … • Problem solving is an art - not an exact science • But: ‘tools’ exist for the design of algorithms • iterative structures, recursive structures • Always note: algorithm efficiency is important!

More Related