De presentatie wordt gedownload. Even geduld aub

De presentatie wordt gedownload. Even geduld aub

Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 117 October 2000 - 14:17 Basiscursus Informatica 98/991 Inleiding Informatica College 5 Brookshear.

Verwante presentaties


Presentatie over: "Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 117 October 2000 - 14:17 Basiscursus Informatica 98/991 Inleiding Informatica College 5 Brookshear."— Transcript van de presentatie:

1 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 117 October 2000 - 14:17 Basiscursus Informatica 98/991 Inleiding Informatica College 5 Brookshear hoofdstuk 4: Algoritmes

2 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 217 October 2000 - 14:17 Basiscursus Informatica 98/992 Overzicht college 5 n Notie van algoritme n Representatie van algoritmes n “Ontdekking” van algoritmes n Iteratieve structuren n Recursieve structuren n Efficientie en correctie

3 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 317 October 2000 - 14:17 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 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 417 October 2000 - 14:17 Basiscursus Informatica 98/994 Termen n Er is een verschil tussen: programma, algoritme en proces: ä Programma is de representatie van algoritme ä Proces is de aktiviteit van de executie van een programma

5 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 517 October 2000 - 14:17 Basiscursus Informatica 98/995 Overzicht college 5 n Notie van algoritme n Representatie van algoritmes n “Ontdekking” van algoritmes n Iteratieve structuren n Recursieve structuren n Efficientie en correctie

6 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 617 October 2000 - 14:17 Basiscursus Informatica 98/996 Representatie: de taal n Probleem van natuurlijke taal ä ambiguiteit ä mate van detail is niet uniform ä bv. ‘Visiting grandchildren can be nerve-racking’ ä bv. ‘Ik zag een grote hond en kat’ n Instructies in beeldvorm ä origami: syntax en semantiek ä Lego

7 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 717 October 2000 - 14:17 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 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 817 October 2000 - 14:17 Basiscursus Informatica 98/998 Pseudocode n Informele notatie tijdens programma- ontwikkeling, gemakkelijk om te zetten naar de programmeertaal ä programmeertal van belang? (Prolog, Java?) n Terugkerende patronen in primitieven vatten ä If/Then/Else en If/Then ä While/Do ä Assign

9 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 917 October 2000 - 14:17 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 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 1017 October 2000 - 14:17 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 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 1117 October 2000 - 14:17 Basiscursus Informatica 98/9911 Inspringen n If (item is belastbaar) Then [( If (prijs > limiet) Then (betaal x) Else (betaal y)] Else (betaal z)

12 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 1217 October 2000 - 14:17 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 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 1317 October 2000 - 14:17 Basiscursus Informatica 98/9913 Uniforme taal: OO n UML =Unified Modelling Language n object oriëntatie = zelfde concepten voor traject van analyse, ontwerp en implementatie: ä klassen, objecten, ä methoden, variabelen, ä verzoek, boodschap, ä overerving,

14 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 1417 October 2000 - 14:17 Basiscursus Informatica 98/9914 Overzicht college 5 n Notie van algoritme n Representatie van algoritmes n “Ontdekking” van algoritmes n Iteratieve structuren n Recursieve structuren n Efficientie en correctie

15 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 1517 October 2000 - 14:17 Basiscursus Informatica 98/9915 Programmaontwikkeling n Tot nu toe: representatie van een algoritme n Vraag: waar komt het algoritme vandaan? n Polya: theorie van probleemoplossen: ä begrijpen - ontwerpen - uitvoeren - evalueren n Creatief proces ä soms “weet” je de oplossing opeens als je al niet meer met probleem bezig bent ä “getting a foot in the door”

16 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 1617 October 2000 - 14:17 Basiscursus Informatica 98/9916 Waar beginnen? n Achterwaarts werken vanuit de gevraagde output (Warnier-Orr) n Gebruik eerdere oplossingen voor gelijkende problemen (Design Patterns) n Probeer een paar specifieke voorbeelden, en generaliseer (Case Based) n “Stepwise refinement”: deel probleem op in sub-problemen

17 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 1717 October 2000 - 14:17 Basiscursus Informatica 98/9917 Pas op! n Stepwise refinement lastig toepasbaar bij een sterke samenhang tussen elementen: 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 your hat?

18 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 1817 October 2000 - 14:17 Basiscursus Informatica 98/9918 Overzicht college 5 n Notie van algoritme n Representatie van algoritmes n “Ontdekking” van algoritmes n Iteratieve structuren n Recursieve structuren n Efficientie en correctie

19 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 1917 October 2000 - 14:17 Basiscursus Informatica 98/9919 Herhalingslussen 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)

20 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 2017 October 2000 - 14:17 Basiscursus Informatica 98/9920 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)

21 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 2117 October 2000 - 14:17 Basiscursus Informatica 98/9921 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

22 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 2217 October 2000 - 14:17 Basiscursus Informatica 98/9922 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) ]

23 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 2317 October 2000 - 14:17 Basiscursus Informatica 98/9923 Sorteren door invoegen n Efficiënt met de ruimte omspringen: daarom sorteren binnen de lijst zelf, + 1 hulpplaats n Uitgangspositie: alles ligt door elkaar n Aanpak: ä 1. Sorteer nr. 1 en 2 ä 2. Voeg nr. 3 in de gesorteerde lijst van 1 en 2 ä 3. Voeg nr. 4 in de lijst van 1, 2 en 3 ä 4. Ga zo door tot en met het laatste element

24 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 2417 October 2000 - 14:17 Basiscursus Informatica 98/9924 Pseudo code: Insertion Sort procedure Sorteer (lijst) { assign: N krijgt de waarde 2 while ( N niet groter dan de lengte van de lijst ) do { kies het N-de element als pivot, zet de pivot in het hulpvakje, waarbij een vakje open blijft, na while ( boven het open vakje een naam die na de pivot komt) do { verplaats de naam boven het open vakje 1 plaats naar beneden } Verplaats de pivot naar het open vakje, Hoog N met 1 op, } Figuur 4.11

25 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 2517 October 2000 - 14:17 Basiscursus Informatica 98/9925 Test de pseudo code n Lege lijst n Lijst met 1 element n Lijst met 2 elementen n Volledig gesorteerde lijst n ‘worst case’ scenario (fig 4.18)

26 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 2617 October 2000 - 14:17 Basiscursus Informatica 98/9926 Overzicht college 5 n Notie van algoritme n Representatie van algoritmes n “Ontdekking” van algoritmes n Iteratieve structuren n Recursieve structuren n Efficientie en correctie

27 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 2717 October 2000 - 14:17 Basiscursus Informatica 98/9927 Recursie n Andere manier van probleemopsplitsen ä herhaling van zelfde programma op simpelere input ä bv. Binair zoeken n Stopconditie belangrijk n Geheugenintensief n Equivalente power: iteratief - recursief

28 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 2817 October 2000 - 14:17 Basiscursus Informatica 98/9928 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)]

29 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 2917 October 2000 - 14:17 Basiscursus Informatica 98/9929 Recursie n Heeft ook ä initialisatie, test voor terminatie, modificatie n Test voor terminatie (base case) Als slaagt, dan is huidige activatie van programma klaar, en Terug naar vorige activatie Anders start nieuwe activatie met simpeler input n Initialisatie=begin input n modificatie=simpelere input ä bv. splits in tweeën, haal 1 element weg

30 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 3017 October 2000 - 14:17 Basiscursus Informatica 98/9930 Overzicht college 5 n Notie van algoritme n Representatie van algoritmes n “Ontdekking” van algoritmes n Iteratieve structuren n Recursieve structuren n Efficiëntie en correctheid

31 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 3117 October 2000 - 14:17 Basiscursus Informatica 98/9931 Efficiëntie n Sommige algoritmes zijn significant sneller of trager dan andere: ä bv. in een gesorteerde lijst: –traag: sequential search –snel: binary search n Lijst met 30000 elementen, zoek element ä sequential: gemiddeld 15000 vergelijkingen ä binary: 15 vergelijkingen

32 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 3217 October 2000 - 14:17 Basiscursus Informatica 98/9932 Correctheid n Intuïtief 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

33 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 3317 October 2000 - 14:17 Basiscursus Informatica 98/9933 Oplossing: 1 keer 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.

34 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 3417 October 2000 - 14:17 Basiscursus Informatica 98/9934 Formele verificatie n We nemen pré-condities 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

35 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 3517 October 2000 - 14:17 Basiscursus Informatica 98/9935 Beweringen in while loop test conditie initialiseerbodyverander Precondities Loop invariant Loop invariant en stopconditie Vind beweringen om te checken en te propageren door het programma false true

36 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 3617 October 2000 - 14:17 Basiscursus Informatica 98/9936 Insertion sort: beweringen n Loop invariant = aanname die waar is bij ingaan van de lus en bij de stop conditie ä voorgaande elementen vormen steeds een gesorteerde lijst n Stop conditie: ä de hele lijst is ontschaduwd n Eindig proces: ä steeds een naam ontschaduwen leidt uiteindelijk tot ‘unshaded list’

37 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 3717 October 2000 - 14:17 Basiscursus Informatica 98/9937 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

38 Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 3817 October 2000 - 14:17 Basiscursus Informatica 98/9938 Verificatie versus testen n Verificatie is fundamenteel, omslachtig en duur ä wie verifieert de verificatie? n Testen is goedkoper, maar garandeert geen correcte software (alfa en bèta testers) ä populairder in industriële wereld dan verificatie n Consumenten software versus bedrijfskritische applicaties!


Download ppt "Inleiding Informatica, 2000-2001 College 4, H 4 – sheet nr. 117 October 2000 - 14:17 Basiscursus Informatica 98/991 Inleiding Informatica College 5 Brookshear."

Verwante presentaties


Ads door Google