420 likes | 513 Views
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.
E N D
Overzicht Informatica College 6 - Oktober 11 Computer Science an overview EDITION7 J. Glenn Brookshear
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
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)
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…
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’)
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
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
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
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
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)
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 …
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)
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)
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 (
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)
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
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…!
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
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
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! )
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:
4.4: Components of Loop Control • Note: termination condition is negation of condition in: ‘ while (condition) ‘
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, …
4.4: While loop structure vs. Repeat loop structure while (condition) do (activity) repeat (activity) until (condition) • Many programming languages provide both constructs
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; )
4.4: Another Example: Insertion Sort • Consider the problem of sorting a list of names into alphabetical order (‘within itself’)
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;
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
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
(TestEntry) 4.5: Find ‘Bill’ in List Alice Bill Carol Report : SUCCES! Report : SUCCES!
(TestEntry) empty 4.5: Find ‘David’… (1) Alice Carol
4.5: Find ‘David’… (2) empty Report : FAILED!
4.5: Find ‘David’… (3) Alice Carol (TestEntry) empty list Report : FAILED! Report : FAILED!
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
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
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
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)
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!