FACIT TILL OMTENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B kl. 14:15 19:15

Relevanta dokument
OMTENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B kl. 14:15 19:15

ORDINARIE TENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B kl. 08:15 13:15

OMTENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B kl. 08:15 13:15

ORDINARIE TENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B kl. 08:15 13:15

TENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B kl. 14:00-19:00

FACIT till ORDINARIE TENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B kl. 08:15 13:15

TENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B kl. 08:15 13:15

OMTENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B kl. 08:15-13:15

OMTENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B kl. 09:00 14:00

FACIT TILL TENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B03

FACIT TILL TENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B kl. 08:15 13:15

DVG C01 TENTAMEN I PROGRAMSPRÅK PROGRAMMING LANGUAGES EXAMINATION :15-13: 15

Tentamen MMG610 Diskret Matematik, GU

Graphs (chapter 14) 1

Isometries of the plane

Tentamen i Matematik 2: M0030M.

This exam consists of four problems. The maximum sum of points is 20. The marks 3, 4 and 5 require a minimum

Föreläsning 7 Datastrukturer (DAT037)

LUNDS TEKNISKA HÖGSKOLA Institutionen för Elektro- och Informationsteknik

Schenker Privpak AB Telefon VAT Nr. SE Schenker ABs ansvarsbestämmelser, identiska med Box 905 Faxnr Säte: Borås

Support Manual HoistLocatel Electronic Locks

Solutions to exam in SF1811 Optimization, June 3, 2014

Webbregistrering pa kurs och termin

Föreläsning Datastrukturer (DAT036)

Styrteknik: Binära tal, talsystem och koder D3:1

denna del en poäng. 1. (Dugga 1.1) och v = (a) Beräkna u (2u 2u v) om u = . (1p) och som är parallell

Föreläsning Datastrukturer (DAT037)

Lösningar Datastrukturer TDA

Föreläsning 8 Datastrukturer (DAT037)

Viktig information för transmittrar med option /A1 Gold-Plated Diaphragm

Pre-Test 1: M0030M - Linear Algebra.

1. Compute the following matrix: (2 p) 2. Compute the determinant of the following matrix: (2 p)

Webbreg öppen: 26/ /

Föreläsning Datastrukturer (DAT036)

Grafisk teknik IMCDP IMCDP IMCDP. IMCDP(filter) Sasan Gooran (HT 2006) Assumptions:

FÖRBERED UNDERLAG FÖR BEDÖMNING SÅ HÄR

Grafer, traversering. Koffman & Wolfgang kapitel 10, avsnitt 4

8 < x 1 + x 2 x 3 = 1, x 1 +2x 2 + x 4 = 0, x 1 +2x 3 + x 4 = 2. x 1 2x 12 1A är inverterbar, och bestäm i så fall dess invers.

Module 6: Integrals and applications

Föreläsning 8 Datastrukturer (DAT037)

Rastercell. Digital Rastrering. AM & FM Raster. Rastercell. AM & FM Raster. Sasan Gooran (VT 2007) Rastrering. Rastercell. Konventionellt, AM

Preschool Kindergarten

x 2 2(x + 2), f(x) = by utilizing the guidance given by asymptotes and stationary points. γ : 8xy x 2 y 3 = 12 x + 3

OMTENTAMEN I PROGRAMSPRÅK -- DVG C kl. 08:15-13: 15

(D1.1) 1. (3p) Bestäm ekvationer i ett xyz-koordinatsystem för planet som innehåller punkterna

Quicksort. Koffman & Wolfgang kapitel 8, avsnitt 9

Högskolan i Skövde (SK, JS) Svensk version Tentamen i matematik

Grafisk teknik IMCDP. Sasan Gooran (HT 2006) Assumptions:

GU / Chalmers Campus Lindholmen Tentamen Programutveckling LEU 482 / TIG167

TDDC74 Programmering: Abstraktion och modellering Tentamen, onsdag 9 juni 2016, kl 14 18

TENTAMEN I PROGRAMSPRÅK -- DVG C kl. 08:15-13:15

1. Varje bevissteg ska motiveras formellt (informella bevis ger 0 poang)

Övning 3 - Tillämpad datalogi 2012

Beijer Electronics AB 2000, MA00336A,

Självbalanserande träd AVL-träd. Koffman & Wolfgang kapitel 9, avsnitt 1 2

F11. Läsanvisning: kap 10 + dessa OH. Kruskals algoritm kortaste vägar en till alla

Föreläsning 9 Innehåll

Grafisk teknik. Sasan Gooran (HT 2006)

Föreläsning Datastrukturer (DAT036)

12.6 Heat equation, Wave equation

Materialplanering och styrning på grundnivå. 7,5 högskolepoäng

SVENSK STANDARD SS-EN ISO 19108:2005/AC:2015

Isolda Purchase - EDI

Algoritmer och Komplexitet ht 08. Övning 6. NP-problem

NP-fullständighetsbevis

E: 9p D: 10p C: 14p B: 18p A: 22p

Module 1: Functions, Limits, Continuity

Discrete Mathematics (English)

Tentamen del 2 SF1511, , kl , Numeriska metoder och grundläggande programmering

Föreläsning Datastrukturer (DAT036)

2(x + 1) x f(x) = 3. Find the area of the surface generated by rotating the curve. y = x 3, 0 x 1,

f(x) =, x 1 by utilizing the guidance given by asymptotes and stationary points. cos(x) sin 3 (x) e sin2 (x) dx,

F ξ (x) = f(y, x)dydx = 1. We say that a random variable ξ has a distribution F (x), if. F (x) =

Schenker Privpak AB Telefon VAT Nr. SE Schenker ABs ansvarsbestämmelser, identiska med Box 905 Faxnr Säte: Borås

Tentamen i kurserna Beräkningsmodeller (TDA181/INN110) och Grundläggande Datalogi (TDA180)

Exempel på uppgifter från 2010, 2011 och 2012 års ämnesprov i matematik för årskurs 3. Engelsk version

TDDC74 Programmering: Abstraktion och modellering Dugga 2, , kl 14-16

Två fall: q Tom sekvens: () q Sekvens av element: (a b c) ; (sum-rec '(2 4 6)) = 12. q Första elementet uppfyller vissa villkor: (2 a b c)

Tentamen i Matematik 2: M0030M.

Resultat av den utökade första planeringsövningen inför RRC september 2005

Tentamen Datastrukturer (DAT036/DAT037/DIT960)

S 1 11, S 2 9 and S 1 + 2S 2 32 E S 1 11, S 2 9 and 33 S 1 + 2S 2 41 D S 1 11, S 2 9 and 42 S 1 + 2S 2 51 C 52 S 1 + 2S 2 60 B 61 S 1 + 2S 2 A

Namn: (Ifylles av student) Personnummer: (Ifylles av student) Tentamensdatum: Tid: Hjälpmedel: Inga hjälpmedel

and u = och x + y z 2w = 3 (a) Finn alla lösningar till ekvationssystemet

Lösningsförslag till tentamen Datastrukturer, DAT037 (DAT036), Tiden det tar att utföra en iteration av loopen är oberoende av värdet på

Föreläsning Datastrukturer (DAT037)

Boiler with heatpump / Värmepumpsberedare

Calculate check digits according to the modulus-11 method

Föreläsning 6 Datastrukturer (DAT037)

2.1 Installation of driver using Internet Installation of driver from disk... 3


LUNDS TEKNISKA HÖGSKOLA Inst. for Elektro- och Informationsteknik. SIGNALBEHANDLING I MULTIMEDIA, ETI265 Inlämningsuppgift 1 (av 2), Task 1 (out of 2)

Writing with context. Att skriva med sammanhang

Accomodations at Anfasteröd Gårdsvik, Ljungskile

S 1 11, S 2 9 and S 1 + 2S 2 32 E S 1 11, S 2 9 and 33 S 1 + 2S 2 41 D S 1 11, S 2 9 and 42 S 1 + 2S 2 51 C 52 S 1 + 2S 2 60 B 61 S 1 + 2S 2 A

TDDC74 Programmering: Abstraktion och modellering Datortenta , kl 14-18

Adding active and blended learning to an introductory mechanics course

Algebra och Diskret Matematik (svenska)

TDDC74 Programmering: Abstraktion och modellering Dugga 2, , kl 17-19

Transkript:

Karlstads universitet DSA omtentamen 150609 - facit Datavetenskap FACIT TILL OMTENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B03 150609 kl. 14:15 19:15 Ansvarig Lärare: Donald F. Ross Hjälpmedel: Inga. Algoritmerna finns i de respektive uppgifterna eller i bilogarna. *** OBS *** Betygsgräns: Kurs: Max 60p, Med beröm godkänd 50p, Icke utan beröm godkänd 40p, Godkänd 30p (varav minimum 20p från tentan, 10p från labbarna) Tenta: Max 40p, Med beröm godkänd 34p, Icke utan beröm godkänd 27p, Godkänd 20p Labbarna: Max 20p, Med beröm godkänd 18p, Icke utan beröm godkänd 14p, Godkänd 10p SKRIV TYDLIGT LÄS UPPGIFTERNA NOGGRANT Ange alla antaganden. Studenter som har läst kursen from HT2014 ska svara på samtliga uppgifter. Studenter som har läst kursen from HT2006 tom HT2013 ska välja uppgifter värt 30 poäng. Studenter som har läst kursen tom HT2005 ska svara på samtliga uppgifter. DFR DSA tentamen 150609 FACIT Sidan 1 av 22

Karlstads universitet DSA omtentamen 150609 - facit Datavetenskap (1) Ge en kortfattad definition av följande begrepp ((a)-(j)). (a) Mängd a collection of elements in which no duplicate values are allowed and in which there is no ordering. (b) Sekvens a collection of elements in which duplicates may be allowed and in which the relationships predecessor and successor are defined; i.e. linear ordering is defined (c) Träd - a collection of elements in which duplicates may be allowed and in which a parent-child relationship (i.e.a hierarchy) is defined. Each parent may have n children except at the lowest level (leaf nodes) and each child has exactly one parent (except for the root node). May be onordered or ordered. (d) Binärt träd an ordered tree i.e. there is a left child and a right child, with the restriction that each parent may have at most 2 children (e) AVL-träd a binary search tree with the restriction that the tree is ordered i.e. there is a left child and a right child and that the heights of each left and right subtree may not differ by more that 1 (f) Graf a pair of sets G = (V, E) where E is a set of nodes and V a set of edges where each of the edges connects nodes a and b in V. (g) Enkel cykel i en graf a simple path where the start and end nodes are the same. A simple path is a path where the edges and vertices are distinct. Directed graph (A B A) undirected graph (A B C A) (h) Artikuleringspunkt (articulation point) a node, which when removed breaks the graph into 2 or more components. (i) Prestande (performance) the decription of the relationship between the number of entities handled (for example in a sort) and the time and/or space required (j) Big-oh (O(n)) an upper limit on the time required by a given algorithm to handle n entities Totalt 5p DFR DSA tentamen 150609 FACIT Sidan 2 av 22

Karlstads universitet DSA omtentamen 150609 - facit Datavetenskap (2) Rekursion Beskriv ingående varför rekursion är så pass viktigt inom datastrukturer och algoritmer. Använd gärna exempel. Ge flera exempel där man använder sig av rekursion. Importance Totalt 5p (i) (ii) (iii) (iv) Recursion is a very succinct (shorthand) way of defining an entity or writing programming language code Non-recursive code solutions tend in certain cases to be longer and more complicated than recursive solutions Recursion changes your mindset i.e. your way of thinking about programming constructs and writing code Recursion is also useful in defining data structures (eg a sequence or a tree) and definitions used in Computer Science (eg defining the production rules P in a formal grammar (G = (S, P, NT, T)) definition of a programming language Examples of definitions (v) (vi) (vii) Sequence (see above) Seq ::= Head Tail empty; Head ::= element; Tail ::= Seq Binary Tree: BT ::= LC Node RC empty; Node ::= element; LC ::= BT; RC ::= BT Grammar definition [id list] ::= id [id list], id i.e. the id list is id, id, id,., id Examples of programming operations on the above (viii) Sequence display Recursive version List: display_l (Reftype List) { if not is_empty (List) { display_el ( head (List) ); display_l ( tail (List) ); return List; Iterative version List: display_l (Reftype List) { Reftype ref; ref = first (List); (last) while (not_null (ref)) { display_el ( ref ); ref = next ( ref ); return List; DFR DSA tentamen 150609 FACIT Sidan 3 av 22

Karlstads universitet DSA tentamen 150112 - Facit Datavetenskap (ix) Binary tree number of elements Recursive Int: size (BT) { if is_empty(bt) return 0; return 1 + size(left(bt)) + size(right(bt)); Non-recursive would be more difficult! (x) Id list Non-recursive: id_list() { match(id); while (lookahead == ','){ match(','); match(id); Recursive id_list() { match(id); if (lookahead == ',') { match(','); id_list(); Marks also for good arguments and examples. DFR Datastrukturer och algoritmer, DAV B03, tentamen 150112 Facit Sidan 4 av 22

Karlstads universitet DSA tentamen 150112 - Facit Datavetenskap (3) Heap (a) Läs koden till heap operationer (se Bilaga A), Svara på följande uppgifter:- i. Vad är parameter i i Heapify(A, i)? i is the index in the array representation of the heap ii. Skriv (pseudo) kod till funktionen Right(i) Left(i) { return 2*i; // assumption index starts at 1 Right(i) { return 2*i + 1; // assumption index starts at 1 Left(i) { return 2*(i+1)-1; // assumption index starts at 0 Right(i) { return 2*(i+1); // assumption index starts at 0 iii. I Heapify, vad gör koden nedan? Vad betyder koden? if l <= A.size and A[l] > A[i] then largest = l else largest = i if r <= A.size and A[r] > A[largest] then largest = r if the left child exists and the value of the left child is greater than the value in the parent then set largest to the left child, otherwise set largest to the parent if the right child exists and the value of the right child is greater than the value of largest then set largest to r this code compares the value of the parent with the values in the left and right children (if they exist) iv. I Heapify, vad gör koden nedan? Vad betyder koden? if largest!= i then swap(a[i], A[largest]) Heapify(A, largest) end if If the value in the parent is not the largest (of P, LC, RC) i.e. largest!= i, then swap the values of largest and i Now heapify the (sub-)tree with parent at largest v. Varför skriver man A.size/2 i funktion Build? This finds the last parent in the heap with 1 child or 2 children. This works since the heap is a complete tree and children are always added at the next node position on the lowest level. DFR Datastrukturer och algoritmer, DAV B03, tentamen 150112 Facit Sidan 5 av 22

Karlstads universitet DSA tentamen 150112 - Facit Datavetenskap vi. Hur fungerar funktion Remove? Remove swaps the value of the node at index r with the last node in the array The size of the array is reduced by 1, hence removing the value that was at index r The (sub-)tree is then re-heapified 3p (b) Givit heapen: 72, 66, 18, 44, 46, 9, 11, 33, 27, 15 Förklara stegvis hur funktionen Add fungerar när man lägger till värdet 68 i heapen. Rita en bild av den trädrepresentationen av heapen vid varje steg. 2p The tree version of the heap and add 68 process is Totalt 5p 72 66 18 44 46 9 11 33 27 15 72 66 18 44 46 9 11 33 27 15 * 46 66 18 44 46 9 11 33 27 15 46 46 66 18 44 66 9 11 33 27 15 46 46 68 18 44 66 9 11 33 27 15 46 DFR Datastrukturer och algoritmer, DAV B03, tentamen 150112 Facit Sidan 6 av 22

Karlstads universitet DSA tentamen 150112 - Facit Datavetenskap The explanation is:- 1. Increase the size of the array by 1 2. Set i to be the index of the last node 3. Bottom up, if the value in the parent of i is less than the value to be added to the heap then copy this value to the node given by i (NB) 4. Set i to reference its parent (moving up the tree) 5. Repeat the process until either i refers to the root of the heap i.e. the first element in the heap and the value in the parent of i is less than the value to be added 6. Set the value of A[i] to the new values to be added this is the add stage not before! 1. In the example above, the size of the heap is 10 (10 elements) 2. The value to be added is 68 3. An extra node is added at position 11 4. Set i to position 11 5. The parent is position 5 6. Value 46 in position 5 is less than 68 copy 46 to position 11 7. Set i to position 5 8. The parent is position 2 9. Value 66 in position 2 is less than 68 copy 66 to position 5 10. Set i to position 2 11. The parent is position 1 12. 72 is NOT less than 68 exit the while loop 13. Copy the new value 68 to position 2 DFR Datastrukturer och algoritmer, DAV B03, tentamen 150112 Facit Sidan 7 av 22

Karlstads universitet DSA tentamen 150112 - Facit Datavetenskap (4) Binärt Träd (a) Ge en rekursiv definition av ett binärt träd. BT ::= LC Node RC empty Node ::= element LC ::= BT RC ::= BT 1p (b) Utifrån din definition i (a) skriv pseudokod till en rekursiv version av den läggatill funktionen (add) för ett binärt träd. Anta att dubbleter inte tillåtas. 2p If you assume a BST then the answer is as below. Alternatives: that a find finction is applied as a precondition (except that this would be redundant for the BST) static treeref b_add(treeref T, int v) { return is_empty(t)? create_node(v) : v < get_value(node(t))? cons(b_add(lc(t), v), node(t), RC(T)) : v > get_value(node(t))? cons(lc(t), node(t), b_add(rc(t), v)) : T; If you assume a BT then the problem becomes a little more difficult. You have to make assumptions 1. That there is a find function available as a prcondition 2. Where in the BT are you going to add? 3. Do you add to the smaller tree of the LC and RC? There is no general solution for the BT case in the same way as the BST above. DFR Datastrukturer och algoritmer, DAV B03, tentamen 150112 Facit Sidan 8 av 22

Karlstads universitet DSA tentamen 150112 - Facit Datavetenskap (c) Att ta bort ett element från ett binärt träd kan reduceras till ett annat problem, nämligen att ta bort roten från ett binärt träd. Då finns det fyra möjligheter. Vilka är dessa? Diskutera utförligt vad händer i varje fall. 1. The root consists of a node only. Removing the node (root) return nil 2p 2. The root consists of a node with a left child only Removing the node (root) return left sub-tree 3. The root consists of a node with a right child only Removing the node (root) return right sub-tree 4. The root consists of a node with both a left and right child This is the most interesting case see below Case 4: if you assume a BST then The value in the root may be replaced by (i) The biggesst value in the left child (ii) The smallest value in the right child This value must then be removed from the corresponding child. If You take the BT case then vou can add the LC to the RC or vice-versa with an add function, but again you have to choose where to add the sub-tree Totalt 5p DFR Datastrukturer och algoritmer, DAV B03, tentamen 150112 Facit Sidan 9 av 22

Karlstads universitet DSA tentamen 150112 - Facit Datavetenskap (5) AVL-Träd Visa hur Du skulle ta fram de 4 rotationsfunktionerna från första principer. 2p An AVL-tree is a BST (Binary Search Tree) where the balance difference (balance factor) between the height of the left child and the height of the right child may be at most 1. i.e. height(lc) height(rc) <= 1 The rotation functions are o SLR (Single Left Rotation) o SRR (Single Right Rotation) o DLR (Double Left Rotation) or Right-Left Rotation o DRR (Double Right Rotation) or Left-Right Rotation o The rotation functions may be derived using a few simple examples. (a) (b) (c) 10 10 10 12 12 12 14 11 14 11 (a) and (b) require a SLR to rebalance while (c) requires a DLR (right-left rotation) to give 12 12 10 11 10 14 10 14 11 10 12 11 12 And the code becomes (SRR and DRR are mirror images of SLR and DLR respectively) static treeref SLR(treeref T) { treeref RT = RC(T); set_rc(t, LC(RT)); set_lc(rt, T); return RT; static treeref SRR(treeref T) { treeref RT = LC(T); set_lc(t, RC(RT)); set_rc(rt, T); return RT; static treeref DLR(treeref T) { set_rc(t, SRR(RC(T))); return SLR(T); static treeref DRR(treeref T) { set_lc(t, SLR(LC(T))); return SRR(T); DFR Datastrukturer och algoritmer, DAV B03, tentamen 150112 Facit Sidan 10 av 22

Karlstads universitet DSA tentamen 150112 - Facit Datavetenskap DFR Datastrukturer och algoritmer, DAV B03, tentamen 150112 Facit Sidan 11 av 22

Karlstads universitet DSA tentamen 150112 - Facit Datavetenskap This is another way of writing what was in the notes Treeref RotateLeft(n2) n1 = n2.right n2.right = n1.left n1.left = n2 return n1 end RotateLeft Treeref RotateRight(n2) n1 = n2.left n2.left = n1.right n1.right = n2 return n1 end RotateRight Is implemented as (using the set and get functions) /* RotateLeft */ /* n2 = T and n1 = RT */ static treeref SLR(treeref T) { treeref RT = RC(T); set_rc(t, LC(RT)); set_lc(rt, T); return RT; /* RotateRight */ static treeref SRR(treeref T) { treeref RT = LC(T); set_lc(t, RC(RT)); set_rc(rt, T); return RT; Förklara vad balansfaktorn (balance factor) är och använd denna för att ta fram balansfunktionen från första principer. Skriv balansfunktionen i pseudokod. Define the balance factor br = height(lc(t) height(rc(t)) Now you can decide which sub-tree is the highest. This decides whether the rotation is left or right. Then look at that sub-tree to decide if the imbalance is on the inside ( double rotation) or outside ( single rotation). DFR Datastrukturer och algoritmer, DAV B03, tentamen 150112 Facit Sidan 12 av 22

Karlstads universitet DSA omtentamen 150609 - Facit Datavetenskap Förklara vad balansfaktorn (balance factor) är och använd denna för att ta fram balansfunktionen från första principer. Skriv balansfunktionen i pseudokod. Tillämpa Din pseudokod på trädet nedan 50 3p 38 55 30 40 45 From the diagram, the left child is clearly higher than the right child The right child of the left child is higher that the left child of the left child indicating a possible addition of 45 i.e. to the INSIDE of the left child of 50 hence a DRR is required. NB delete 60 from the above tree + 60 would give the same requirement. DRR = SLR (38) then a SRR(50) to give 50 40 55 38 45 30 40 38 50 30 45 55 static treeref DRR(treeref T) { set_lc(t, SLR(LC(T))); return SRR(T); static treeref SLR(treeref T) { treeref RT = RC(T); set_rc(t, LC(RT)); set_lc(rt, T); return RT; static treeref SRR(treeref T) { treeref RT = LC(T); set_lc(t, RC(RT)); set_rc(rt, T); return RT; SLR(T=38) RT = 40; RC(38) = LC(40) (null); LC(40) = 38; return 40; SRR(T=50) RT = 40; LC(50) = 45; RC(40) = 50; return 40; DFR Datastrukturer och algoritmer, DAV B03, omtentamen 150609 - Facit Sidan 13 av 22

Karlstads universitet DSA omtentamen 150609 - Facit Datavetenskap (6) Rekursiva funktioner Skriv pseudokod till två rekursiva funktioner (alltså en funktion plus en hjälpfunktion) för att räkna fram antalet kanter ( edges ) i en graf. Ange alla antagande. 5p Assumptions: Structure: typedef struct nodeelem * noderef; typedef struct nodeelem { char nname; int ninfo; noderef edges; noderef nodes; nodeelem; + corresponding get/set functions per attribute and head/tail operations for both the node list (nodes) and the edge list (edges). G is a reference to the graph, the is_empty(r) function is defined. static int b_nedges(noderef E) { return is_empty(e)? 0 : 1 + b_nedges(etail(e)); static int b_esize(noderef G) { return is_empty(g)? 0 : b_nedges(get_edges(nhead(g))) + b_esize(ntail(g)); DFR Datastrukturer och algoritmer, DAV B03, omtentamen 150609 - Facit Sidan 14 av 22

Karlstads universitet DSA omtentamen 150609 - Facit Datavetenskap (7) Prim Tillämpa den givna Prims algoritm (nedan) på den oriktade grafen, (a-20-b, a-36-c, a-34-d, b-22-c, b-24-e, c-28-d, c-30-e, c-38-f, d-26-f, e-36-f). Prim s Algoritm -- antagande: att det finns en kostnadsmatrix C Prim ( node v) -- v is the start node { U = {v; for i in (V-U) { low-cost[i] = C[v,i]; closest[i] = v; while (!is_empty (V-U) ) { i = first(v-u); min = low-cost[i]; k = i; for j in (V-U-k) if (low-cost[j] < min) { min = low-cost[j]; k = j; display(k, closest[k]); U = U + k; for j in (V-U) if ( C[k,j] < low-cost[j] ) ) { low-cost[j] = C[k,j]; closest[j] = k; Börja med nod a. Visa varje steg i dina beräkningar. Ange *alla* antaganden och visa *alla* beräkningar och mellanresultat Visa att man har kollat på varje kant under algoritmens exekvering. Använd bilder. I vilka ordning har algoritmen kollat på kanterna? Vilka kanter ersätter andra kanter under algoritmensexekvering? (3p) (2p) Totalt 5p The principle is that the MST grows from the one component (here a ) by connecting this component to any other component (a node) by the shortest EDGE SO FAR FOUND this last proviso reveals that Prim s is a GREEDY algorithm i.e. used a local best solution. See below for the calculations. Draw the graph (and possibly sketch the answer use Kruskalls for a quick check!): Cost 120 DFR Datastrukturer och algoritmer, DAV B03, omtentamen 150609 - Facit Sidan 15 av 22

Karlstads universitet DSA omtentamen 150609 - Facit Datavetenskap 20 a 34 20 a b e 24 36 22 c 28 30 38 36 d f 26 b e 24 22 28 c 26 d f DFR Datastrukturer och algoritmer, DAV B03, omtentamen 150609 - Facit Sidan 16 av 22

Karlstads universitet DSA omtentamen 150609 - Facit Datavetenskap Draw the cost matrix C and array D a b c d e f a 20 36 34 b 20 22 24 c 36 22 28 30 38 d 34 28 26 e 24 30 36 f 38 26 36 a b c d e f lowcost 20 36 34 closest a a a a a Minedge: lowcost: 20 36 34 closest: a a a a a U = {a,b V-U = {c,d,e,f min = 20; k = b Readjust costs: if C[k,j] < lowcost[j] then { lowcost[j] = C[k,j]; closest[j] = k j = c; if C[b,c] < lowcost[c] then { lowcost[c] = C[b,c]; closest[b] = b 22<36 b-22-c j = d; if C[b,d] < lowcost[d] then { lowcost[d] = C[b,d]; closest[d] = b <34 no change j = e; if C[b,e] < lowcost[e] then { lowcost[e] = C[b,e]; closest[e] = b 24< b-24-e j = f; if C[b,f] < lowcost[f] then { lowcost[f] = C[b,f]; closest[f] = b < no change Minedge: lowcost: 20 22 34 24 closest: a b a b a U = {a,b,c V-U = {d,e,f min = 22; k = c Readjust costs: if C[k,j] < lowcost[j] then { lowcost[j] = C[k,j]; closest[j] = k j = d; if C[c,d] < lowcost[d] then { lowcost[d] = C[c,d]; closest[d] = c 28<34 c-28-d j = e; if C[c,e] < lowcost[e] then { lowcost[e] = C[c,e]; closest[e] = c 30<24 no change j = f; if C[c,f] < lowcost[f] then { lowcost[f] = C[c,f]; closest[f] = c 38< c-38-f Minedge: lowcost: 20 22 28 24 38 closest: a b c b c U = {a,b,c,e V-U = {d,f min = 24; k = e j = d; if C[e,d] < lowcost[d] then { lowcost[d] = C[e,d]; closest[d] = e <28 no change j = f; if C[e,f] < lowcost[f] then { lowcost[f] = C[e,f]; closest[f] = e 36<38 e-36-f Minedge: lowcost: 20 22 28 24 36 closest: a b c b e U = {a,b,c,e,d V-U = {f min = 28; k = d j = f; if C[d,f] < lowcost[f] then { lowcost[f] = C[d,f]; closest[f] = d 26<36 d-26-f Min edge: lowcost: 20 22 28 24 26 --- closest: a b c b d --- U = {a,c,b,d,f e V-U = { QED MST edges a-20-b, b-22-c, c-28-d, b-24-e, d-26-f Total cost = 120 (Confirm using Kruskal s) The edges are examined in the following order see the steps above. Initialisation: (a,b,20), (a,c,36), (a,d,34) Iteration 1: (b,c,22) (b,e,24) (b,c,22) ersätter (a,c,36) Iteration 2: (c,d,28), (c,e,30), (c,f,38) (c,d,28) ersätter (a,d,34) Iteration 3: (e,f,36) (e,f,36) ersätter (c,f,38) Iteration 4: (d,f,26) (d,f,26) ersätter (e,f,36) DFR Datastrukturer och algoritmer, DAV B03, omtentamen 150609 - Facit Sidan 17 av 22

Karlstads universitet DSA omtentamen 150609 - Facit Datavetenskap The process in pictures 20 a 34 20 a b e 24 36 22 c 28 30 38 36 d f 26 b e 24 22 28 c solution 26 d f Step 1 Step 2 20 a 34 a 20 b 36 d b 22 c c 24 34 d e f e f Step 3 Step 4 20 a 20 a b e 24 22 28 c 38 d f b e 24 22 28 c 36 d f Then the final step (step 5) to the solution above (d,f,26) replaces (e,f,36) (cheaper!) DFR Datastrukturer och algoritmer, DAV B03, omtentamen 150609 - Facit Sidan 18 av 22

Karlstads universitet DSA omtentamen 150609 - Facit Datavetenskap (8) Dijkstra Algoritmen kan skrivas som nedan Dijkstra( a ) { S = {a for (i in V-S) D[i] = C[a, i]; while(!is_empty(v-s)) { choose w in V-S such that D[w] is a minimum S = S + {w foreach ( v in V-S ) D[v] = min(d[v], D[w] + C[w,v]); Visa hur du skulle utöka algoritmen för att bygga den Shortest Path Tree (kortaste väg trädet) samtidigt som man utför algoritmen. (2p) Dijkstra_SPT ( a ) { S = {a for (i in V-S) { D[i] = C[a, i] --- initialise D - (edge cost) E[i] = a --- initialise E - SPT (edge) L[i] = C[a, i] --- initialise L - SPT (cost) while(!is_empty(v-s)) { choose w in V-S such that D[w] is a minimum S = S + {w foreach ( v in V-S ) if (D[w] + C[w,v] < D[v]) { D[v] = D[w] + C[w,v] E[v] = w L[v] = C[w,v] DFR Datastrukturer och algoritmer, DAV B03, omtentamen 150609 - Facit Sidan 19 av 22

Karlstads universitet DSA omtentamen 150609 - Facit Datavetenskap Tillämpa den givna Dijkstra algoritmen (ovan) på den riktade grafen, (a, b, 12), (a, d, 11), (a, e, 9), (b, c, 7), (c, e, 5), (d, c, 3), (d, e, 1) Börja med nod a. Visa varje steg i Dina beräkningar. Ange *alla* antaganden och visa *alla* beräkningar och mellanresultat Cost Matrix a b c d e a 12 11 9 b 7 c 5 d 3 1 e (3p) Totalt 5p Initialise D D: 12 11 9 w is e (min value in D) S = {a,e V-S = {b,c,d v = b min (D[b], D[e]+C (e,b)) min(12, 9+ ) no change v = c min (D[c], D[e]+C (e,c)) min(, 9+ ) no change v = d min (D[d], D[e]+C (e,d)) min(11, 9+ ) no change D: 12 11 9 b 12 a 11 7 d 9 3 1 c 5 e ---------------------------------------------------------------------------------------------------------------- DFR Datastrukturer och algoritmer, DAV B03, omtentamen 150609 - Facit Sidan 20 av 22

Karlstads universitet DSA omtentamen 150609 - Facit Datavetenskap D: 12 11 9 w is d (min value in D) S = {a,d,e V-S = {b, c v = b min (D[b], D[d]+C (d,b)) min(12, 11+ ) no change v = c min (D[c], D[d]+C (d,c)) min(, 11+3) change a-d-c 14 D: 12 14 11 9 b 12 a 11 7 d 3 1 9 c 5 e ---------------------------------------------------------------------------------------------------------------- D: 12 14 11 9 w is b (min value in D) S = {a,d,b,e V-S = {c v = c min (D[c], D[b]+C (b,c)) min(14, 12+7) no change D: 12 14 11 9 b 12 a 11 7 d 9 3 1 c e 5 ---------------------------------------------------------------------------------------------------------------- This is the final result. Costs: a b (12), a d c (14), a d (11), a e (9) DFR Datastrukturer och algoritmer, DAV B03, omtentamen 150609 - Facit Sidan 21 av 22

Karlstads universitet DSA omtentamen 150609 - Facit Datavetenskap Bilaga A Heap Algoritmer Heapify(A, i) l = Left(i) r = Right(i) if l <= A.size and A[l] > A[i] then largest = l else largest = i if r <= A.size and A[r] > A[largest] then largest = r if largest!= i then swap(a[i], A[largest]) Heapify(A, largest) end if end Heapify Build(A) for i = [A.size / 2] downto 1 do Heapify(A, i) end Build Remove (H, r) let A = H.array A[r] = A[A.size] A.size-- Heapify(A, r) end Remove Add (H, v) let A = H.array A.size++ i = A.size while i > 1 and A[Parent(i)] < v do A[i] = A[Parent(i)] i = Parent(i) end while A[i] = v end Add DFR Datastrukturer och algoritmer, DAV B03, omtentamen 150609 - Facit Sidan 22 av 22