De presentatie wordt gedownload. Even geduld aub

De presentatie wordt gedownload. Even geduld aub

Basiscursus Informatica, 98-99 College 4, H 4 1 Basiscursus Informatica 98/991 Basiscursus Informatica College 4, Algoritmes Brookshear H4 Richard Benjamins.

Verwante presentaties


Presentatie over: "Basiscursus Informatica, 98-99 College 4, H 4 1 Basiscursus Informatica 98/991 Basiscursus Informatica College 4, Algoritmes Brookshear H4 Richard Benjamins."— Transcript van de presentatie:

1 Basiscursus Informatica, 98-99 College 4, H 4 1 Basiscursus Informatica 98/991 Basiscursus Informatica College 4, Algoritmes Brookshear H4 Richard Benjamins

2 Basiscursus Informatica, 98-99 College 4, H 4 2 Basiscursus Informatica 98/992 Overzicht college 4 n Notie van algoritme n Representatie van algoritmes n “Ontdekking” van algoritmes n Iteratieve structuren n Recursieve structuren n Efficientie en correctie

3 Basiscursus Informatica, 98-99 College 4, H 4 3 Basiscursus Informatica 98/993 Algoritme n Informeel ä Verzameling stappen die definieert hoe een taak wordt uitgevoerd n Formeel ä Verzameling geordende, niet-ambigue, uitvoerbare stappen, die een eindig proces definieren ä Is onderstaande een programma? - maak een lijst van alle positieve integers - zet lijst in afnemende volgorde (hoog naar laag) - neem eerste integer van lijst

4 Basiscursus Informatica, 98-99 College 4, H 4 4 Basiscursus Informatica 98/994 Termen n Programma, algoritme, proces n Programma is de representatie van algoritme n Process is de executie van een programma

5 Basiscursus Informatica, 98-99 College 4, H 4 5 Basiscursus Informatica 98/995 Overzicht college 4 n Notie van algoritme n Representatie van algoritmes n “Ontdekking” van algoritmes n Iteratieve structuren n Recursieve structuren n Efficientie en correctie

6 Basiscursus Informatica, 98-99 College 4, H 4 6 Basiscursus Informatica 98/996 Natuurlijke taal n Probleem van natuurlijke taal ä ambiguiteit ä mate van detail is niet uniform n Visiting grandchildren can be nerve-racking n Ik zag een grote hond en kat

7 Basiscursus Informatica, 98-99 College 4, H 4 7 Basiscursus Informatica 98/997 Programmeertaal n Goed gedefinieerde bouwstenen: primitieven ä syntax (symbool) ä semantiek (wat het betekent) ä formele regels hoe primitieven te combineren n Op het nivo van machine-instructies? n Abstract hulpmiddel ä onafhankelijk van specifieke computer ä denken op hoger nivo (minder detail)

8 Basiscursus Informatica, 98-99 College 4, H 4 8 Basiscursus Informatica 98/998 Pseudocode n Notatie om tijdens programma-ontwikkeling ideeen uit te drukken op een hoger nivo dan programmeertaal n Terugkerende patronen in primitieven vatten ä If/Then/Else en If/Then ä While/Do ä Assign

9 Basiscursus Informatica, 98-99 College 4, H 4 9 Basiscursus Informatica 98/999 If/Then/Else n Neem je jas mee als het regent, en anders je zonnebril: If (conditie) dan (actie) ä If (regent) Then (jas mee) Else (zonnebril mee) n Afhankelijk of het een schrikkeljaar is, deel het totaal door 366 of 365, respectievelijk ä If (schrikkeljaar) Then (deel totaal door 366) Else (deel totaal door 365) n Als ik geld heb, ga ik op reis ä If (geld) Then (ik op reis)

10 Basiscursus Informatica, 98-99 College 4, H 4 10 Basiscursus Informatica 98/9910 While/Do n Zolang er nog kaartjes zijn, ga door met de verkoop: While (conditie) Do (actie) ä While (er zijn kaartjes) Do (verkoop) Assignment n Assign name the value expression ä assign Prijs the value Kosten + Winstmarge

11 Basiscursus Informatica, 98-99 College 4, H 4 11 Basiscursus Informatica 98/9911 Inspringen n If (item is belastbaar) Then [( If (prijs > limiet) Then (betaal x) Else (betaal y)] Else (betaal z)

12 Basiscursus Informatica, 98-99 College 4, H 4 12 Basiscursus Informatica 98/9912 Procedures n Aanroepen van programma’s binnen een programma: ä subprogramma, module, functie, procedure n Procedure naam (argumentenlijst) n Procedure groet (Persoon) assign Teller the value 3; while Teller > 0 do (print bericht “hallo Persoon” en assign Teller the value Teller - 1)

13 Basiscursus Informatica, 98-99 College 4, H 4 13 Basiscursus Informatica 98/9913 Overzicht college 4 n Notie van algoritme n Representatie van algoritmes n “Ontdekking” van algoritmes n Iteratieve structuren n Recursieve structuren n Efficientie en correctie

14 Basiscursus Informatica, 98-99 College 4, H 4 14 Basiscursus Informatica 98/9914 Programmaontwikkeling n Tot nu toe: ä representatie van algoritme in programma n Waar komt algoritme vandaan? n Theory van probleemoplossen n Creatief proces ä soms “weet” je de oplossing opeens als je al niet meer met probleem bezig bent

15 Basiscursus Informatica, 98-99 College 4, H 4 15 Basiscursus Informatica 98/9915 Waar beginnen? n Achterwaarts werken vanuit de gevraagde output n Gebruik eerdere oplossingen voor gelijkende problemen n Probeer een paar specifieke voorbeelden, en generaliseer n “Stepwise refinement”: deel probleem op in sub-problemen

16 Basiscursus Informatica, 98-99 College 4, H 4 16 Basiscursus Informatica 98/9916 Pas op! n Stepwise refinement kan soms het probleem vermoeilijken n As you step from a pier into a boat, your hat falls into the water, unbeknownst to you. The river is flowing at 2.5 mph so your hat begins to float downstream. In the meantime, you begin traveling upstream at about the speed of 4.75 mph relative to the water. After 10 minutes you realize that your hat is missing,turn the boat around, and begin to chase your hat down the river. How long will it take to catch up with you hat?

17 Basiscursus Informatica, 98-99 College 4, H 4 17 Basiscursus Informatica 98/9917 Overzicht college 4 n Notie van algoritme n Representatie van algoritmes n “Ontdekking” van algoritmes n Iteratieve structuren n Recursieve structuren n Efficientie en correctie

18 Basiscursus Informatica, 98-99 College 4, H 4 18 Basiscursus Informatica 98/9918 Loops n Herhaaldelijk uitvoeren van een verzameling instructies ä initialisatie ä test voor terminatie ä modificatie (in richting terminatieconditie) n While (beker niet vol) Do (voeg iets toe) n Soorten: ä While/Do ä Repeat/Until (Do/While, in Java)

19 Basiscursus Informatica, 98-99 College 4, H 4 19 Basiscursus Informatica 98/9919 De “While” loop conditie waar conditie onwaar test conditie actie n Data flow diagram n Semantiek van de loop ä actie wordt 0 of meer keren uitgevoerd n while (munt in zak) do (haal ‘m eruit)

20 Basiscursus Informatica, 98-99 College 4, H 4 20 Basiscursus Informatica 98/9920 De “Repeat” loop conditie waar conditie onwaar test conditie actie n Semantiek van de loop ä actie wordt 1 of meer keren uitgevoerd n repeat (haal munt uit zak) until (geen munt in zak) n Actie moet tenminste 1x uitgevoerd kunnen worden

21 Basiscursus Informatica, 98-99 College 4, H 4 21 Basiscursus Informatica 98/9921 Sequential Search algoritme procedure Search (List, TargetValue) if (List empty) then (Declare search a failure) else [Select the first entry in list as the test entry; while (TargetValue > test entry and there remain entries to be considered) do(Select the next entry in list as the test entry); if (TargetValue = test entry) then (Declare search of success) else (Declare search a failure) ]

22 Basiscursus Informatica, 98-99 College 4, H 4 22 Basiscursus Informatica 98/9922 Insertion Sort algoritme procedure Sort (List) if (that are two or more entries in List) then [Shade the portion of List from the 2nd entry through the last entry; repeat (Remove the shading from the first name in the shaded portion of List and identify this name as to the pivot entry; Move the pivot entry to a temporary location leaving a hole in List; while (there is a name above the hole and that name is greater than the pivot) do (Move the name above the hole down into the hole leaving the hole above the name) Move the pivot entry into the hole in List) until (entire List is unshaded)] Figuur 4.11

23 Basiscursus Informatica, 98-99 College 4, H 4 23 Basiscursus Informatica 98/9923 Overzicht college 4 n Notie van algoritme n Representatie van algoritmes n “Ontdekking” van algoritmes n Iteratieve structuren n Recursieve structuren n Efficientie en correctie

24 Basiscursus Informatica, 98-99 College 4, H 4 24 Basiscursus Informatica 98/9924 Recursie n Alternatief voor loops ä cyclisch herhalen van set instructies n Recursie ä herhaling van zelfde programma op simpelere input

25 Basiscursus Informatica, 98-99 College 4, H 4 25 Basiscursus Informatica 98/9925 Binary Search Fig. 4.15 Procedure Search (List, TargetValue) if (List empty) then (Declare the search a failure) else [select the "middle" entry in List as the test entry; Execute one of the following blocks of instructions depending on whether TargetValue is equal to, less than, or greater than the test entry TargetValue = test entry; (Declare the search a success) TargetValue < test entry; [Apply the procedure Search to see whether TargetValue is in the portion of List preceding the test entry, and if (that search is successful) then (Declare this search a success) else (Declare this search a failure)] TargetValue > test entry; [Apply the procedure Search to see whether TargetValue is in the portion of List following the test entry, and if (that search is successful) then (Declare this search a success) else (Declare this search a failure)]

26 Basiscursus Informatica, 98-99 College 4, H 4 26 Basiscursus Informatica 98/9926 Recursie n Heeft ook ä initialisatie, test voor terminatie, modificatie n Test voor terminatie (base case) Als slaagt, dan huidige activatie van programma is klaar, en Terug naar vorige activatie Anders start nieuwe activatie met simpelere input n Initialisatie=begin input n modificatie=simpelere input ä bv. splits in tweeen, haal 1 element weg

27 Basiscursus Informatica, 98-99 College 4, H 4 27 Basiscursus Informatica 98/9927 Quick Sort (recursief) n Kies een naam -->pivot Zoek juiste plaats in lijst voor pivot Stop pivot op die plaats in lijst n Hoe kom je aan juiste plaats? ä Splits lijst in twee sub-lijsten Eerste lijst komt voor pivot (alphabetisch) Tweede lijst komt na pivot Juiste plaats pivot is tussen twee sub-lijsten in n Sorteer beide sub-lijsten

28 Basiscursus Informatica, 98-99 College 4, H 4 28 Basiscursus Informatica 98/9928 Quick Sort Procedure Sort (list) if (List contains fewer than two entries) then (Declare List to be sorted) else [Select the first entry in List as the pivot; Place pointers at the first and last entries of List; while (the pointers do not coincide) do (Move the bottom pointer up to the nearest entry less than or equal to the pivot but not beyond the top pointer; Move the top pointer down to the nearest entry greater than the pivot but not beyond the bottom pointer; if (the pointers do not coincide) then (Interchange the entries indicated by the pointers)) Interchange the pivot with the entry indicated by the common pointers; Apply the procedure Sort to the portion of List above the pivot Apply the procedure Sort to the portion of List below the pivot]

29 Basiscursus Informatica, 98-99 College 4, H 4 29 Basiscursus Informatica 98/9929 Quick Sort Jane Bob Alice Tom Carol Bill George Cheryl Sue John Jane (pivot entry) Bob Alice Tom Carol Bill George Cheryl Sue John Jane (pivot entry) Bob Alice Tom Carol Bill George Cheryl Sue John > pivot

30 Basiscursus Informatica, 98-99 College 4, H 4 30 Basiscursus Informatica 98/9930 Quick Sort Jane (pivot entry) Bob Alice Tom Carol Bill George Cheryl Sue John > pivot =< pivot Jane (pivot entry) Bob Alice Cheryl Carol Bill George Tom Sue John > pivot =< pivot

31 Basiscursus Informatica, 98-99 College 4, H 4 31 Basiscursus Informatica 98/9931 Quick Sort Jane (pivot entry) Bob Alice Cheryl Carol Bill George Tom Sue John > pivot =< pivot George Bob Alice Cheryl Carol Bill Jane (pivot entry) Tom Sue John > pivot =< pivot

32 Basiscursus Informatica, 98-99 College 4, H 4 32 Basiscursus Informatica 98/9932 Overzicht college 4 n Notie van algoritme n Representatie van algoritmes n “Ontdekking” van algoritmes n Iteratieve structuren n Recursieve structuren n Efficientie en correctie

33 Basiscursus Informatica, 98-99 College 4, H 4 33 Basiscursus Informatica 98/9933 Efficientie n Sommige algoritmes zijn significant sneller of trager dan andere: ä sequential search ä binary search n Lijst met 30000 elementen, zoek element ä sequential: gemiddeld 15000 vergelijkingen ä binary: 15 vergelijkingen – 2 log 30000: 2 x = 30000

34 Basiscursus Informatica, 98-99 College 4, H 4 34 Basiscursus Informatica 98/9934 Correctheid n Intuitief correct versus formeel correct A traveler with a gold chain of seven links must stay in an isolated hotel for seven nights. The rent each night consists of one link from the chain. What is the fewest number of links that must be cut so that the traveler can pay the hotel one link of the chain each morning without paying for lodging in advance? Fig 4.21, 4.22

35 Basiscursus Informatica, 98-99 College 4, H 4 35 Basiscursus Informatica 98/9935 Oplossing On the first morning, give the hotel the single link. On the second morning, retrieve the single link and give the hotel the two-link piece. On the third morning, give the hotel the single link. On the fourth morning, retrieve the three links held by the hotel and give the hotel the four-link piece. On the fifth morning, give the hotel the single link. On the sixth morning, retrieve the single link and give the hotel the double-link piece. On the seventh morning, give the hotel the single link.

36 Basiscursus Informatica, 98-99 College 4, H 4 36 Basiscursus Informatica 98/9936 Formele verificatie n We nemen precondities aan n Propageer gevolgen door het programma ä if (conditie) then (instructie 1) else (instructie 2) ä als voor uitvoering if/then een bewering waar is, dan is direct voor uitvoering van instructie 1 deze bewering waar + de conditie waar. In geval instructie 2 uitgevoerd moet worden is vlak daarvoor de bewering waar en de ontkenning van de conditie

37 Basiscursus Informatica, 98-99 College 4, H 4 37 Basiscursus Informatica 98/9937 Beweringen in loop test conditie initialiseerbodyverander Precondities Loop invariant Loop invariant en terminatie conditie Vind beweringen om te checken en te propageren door het programma

38 Basiscursus Informatica, 98-99 College 4, H 4 38 Basiscursus Informatica 98/9938 Insertion Sort algoritme procedure Sort (List) if (that are two or more entries in List) then [Shade the portion of List from the 2nd entry through the last entry; repeat (Remove the shading from the first name in the shaded portion of List and identify this name as the pivot entry; Move the pivot entry to a temporary location leaving a hole in List; while (there is a name above the hole and that name is greater than the pivot) do (Move the name above the hole down into the hole leaving the hole above the name) Move the pivot entry into the hole in List) until (entire List is unshaded)] Figuur 4.11

39 Basiscursus Informatica, 98-99 College 4, H 4 39 Basiscursus Informatica 98/9939 Beweringen n Loop invariant: ä each time the loop body is completed, the names in the unshaded portion of the list are sorted n Termination condition: ä the entire list is unshaded n Eindig proces: ä steeds een naam ontschaduwen leidt uiteindelijk tot unshaded list

40 Basiscursus Informatica, 98-99 College 4, H 4 40 Basiscursus Informatica 98/9940 Quick sort beweringen n 1. Namen boven de toppointer zijn kleiner of gelijk aan de pivot, en namen beneden de bottompointer zin groter dan de pivot n 2. Aan begin van sorteerproces en na elke wisseling, wijst de toppointer naar een naam minder of gelijk aan de pivot n 3. Als de pointers samenvallen, is de betreffende entry gelijk of kleiner dan de pivot

41 Basiscursus Informatica, 98-99 College 4, H 4 41 Basiscursus Informatica 98/9941 Verificatie versus testen n Verificatie is formeel, rigoreus, maar duur n Testen is goedkoper, maar garandeert geen correcte software ä populairder in industriele wereld dan verificatie n Critische applicaties!


Download ppt "Basiscursus Informatica, 98-99 College 4, H 4 1 Basiscursus Informatica 98/991 Basiscursus Informatica College 4, Algoritmes Brookshear H4 Richard Benjamins."

Verwante presentaties


Ads door Google