General comment the exam was not well done at all. The usual faults were to blame

Relevanta dokument
FACIT TILL OMTENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B kl. 08:15 13:15

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

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

FACIT TILL 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 OMTENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B kl. 08:15 13:15

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

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

Föreläsning 11: Grafer, isomorfi, konnektivitet

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

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

Algoritmer och datastrukturer, föreläsning 11

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

4 Example exam questions

v v v v 5 v v v 4 (V,E ) (V,E)

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

Laboration 1a: En Trie-modul

Tillståndsmaskiner. Moore-automat. Mealy-automat. William Sandqvist

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

Nordic Light Roulett. Aluminiumpersienn. Nordic Light Roulett Installation - Manövrering - Rengöring. Aluminiumpersienn

VATEK Multifix kopplingar för alla rörtyper

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

FACIT TILL TENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B03

Magnus Nielsen, IDA, Linköpings universitet

Tentamen MMG610 Diskret Matematik, GU

Innehåll. Träd. Träd. Traversering av träd. Binära träd. Datastrukturer och algoritmer Datastrukturer. Algoritmer.

The Next Generation platform Snabbguide

Datastrukturer och algoritmer. Modell/tillämpningar för träd. Innehåll. Organisation och terminologi (2) Organisation och terminologi (1)

Collections och annat nyttigt

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

Support Manual HoistLocatel Electronic Locks

Tentamen i Matematik 2: M0030M.

Webbregistrering pa kurs och termin

Innehåll. Föreläsning 7. Modeller/tillämpningar för träd. Organisation och terminologi (1) Organisation och terminologi (2)

Ulefos Multifi x Rörkopplingar för alla rörtyper

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

Pre-Test 1: M0030M - Linear Algebra.

Kurskod: TAMS11 Provkod: TENB 12 June 2014, 14:00-18:00. English Version

Making room for tomorrow

Webbreg öppen: 26/ /

Preschool Kindergarten

Workplan Food. Spring term 2016 Year 7. Name:

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

where β R. Find the numbers β for which the operator är diagonalizable, and state a basis of eigenvectors for each of these β.

Problem som kan uppkomma vid registrering av ansökan

Trädstrukturer. Definitioner och terminologi. Informationsteknologi Tom Smedsaas 21 augusti 2016

FÄRGLAGD A STENSUNDSVÄGEN BOSTÄDER BILPLATSER GARAGE 86 ST

Preliminär version 2 juni 2014, reservation för fel. Tentamen i matematik. Kurs: MA152G Matematisk Analys MA123G Matematisk analys för ingenjörer

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

Boiler with heatpump / Värmepumpsberedare

24 Integraler av masstyp

F8: Logiska komponenter. Introduktion. Koder. Avkodare. Logiska komponenter

H1009, Introduktionskurs i matematik Armin Halilovic. Definition. Mängden av alla lösningar till en ekvation kallas ekvationens lösningsmängd.

Accomodations at Anfasteröd Gårdsvik, Ljungskile

Module 6: Integrals and applications

24 poäng. betyget Fx. framgår av. av papperet. varje blad.

st tt r s s ss r t r r r t rs r st ä r st r

Discovering!!!!! Swedish ÅÄÖ. EPISODE 6 Norrlänningar and numbers Misi.se

Tentamen TMV210 Inledande Diskret Matematik, D1/DI2

1. lösa differentialekvationer (DE) och system av DE med konstanta koefficienter

Föreläsning 7 Datastrukturer (DAT037)

Finaltävling den 20 november 2010

EXPERT SURVEY OF THE NEWS MEDIA

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

som gör formeln (*) om vi flyttar första integralen till vänsterledet.

Questionnaire for visa applicants Appendix A

Prov i DAT 312: Algoritmer och datastrukturer för systemvetare

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

Listor = generaliserade strängar. Introduktion till programmering SMD180. Föreläsning 8: Listor. Fler listor. Listindexering.

Solutions to exam in SF1811 Optimization, June 3, 2014

ASSEMBLY INSTRUCTIONS SCALE SQUARE - STANDARD

Starla juldekoration / christmas decoration

Datastrukturer. föreläsning 8. Lecture 6 1

SPEL OM PENGAR FÖR - EN FRÅGA FÖR SKOLAN? VERKTYG, ÖVNINGAR OCH KUNSKAPSBANK FÖR ARBETE MED SPEL OM PENGAR I SKOLAN

============================================================ V1. Intervallet [a,b] är ändligt, dvs gränserna a, b är reella tal och INTE ±.

Kombinerad pump och Sugkopp Small

Föreläsning 7. Splay-träd. Prioritetsköer och heapar. Union/Find TDDC70/91: DALG. Innehåll. Innehåll. 1 Splay-träd

C100-LED Duschhörn med LED-Belysning

Associativa lagen för multiplikation: (ab)c = a(bc). Kommutativa lagen för multiplikation: ab = ba.

Inlämningsuppgift 2 i Digital signalbehandling ESS040, HT 2010 Måndagen den 22 november 2010 i E:B.

Lösning av Tentamen i Numerisk Analys V3, FMN020, Carmen Arévalo

Föreläsning Datastrukturer (DAT036)

Företagens synpunkter på skattesystemet, skattefusket och Skatteverkets kontroll

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

Affärsmodellernas förändring inom handeln

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

Chapter 1 : Who do you think you are?

Vila vid denna källa (epistel nr 82)

12.6 Heat equation, Wave equation

In Bloom CAL # 8, sista varv och ihopsättning / last rows and assemble

..c( ~J ()f;..~c4-- l)o1/\jk) -=t~ AG 7, iv"/--'. e E" .LeA. --'-( ~ /', I AD AD AD AD H H H. AD ' AD H H 0 0 V V. o DOH H H o V V H.

TentamensKod:

Insamlingsforumbilden. Den nya givaren

INSTALLATION INSTRUCTIONS

Föreläsning 7: Trigonometri

The Academic Career Path - choices and chances ULRIKKE VOSS

Programinformation Teknikcollege Allhamra. Kinda Lärcentrum Kontakt. Teknisk utbildning, för framtida anställning

Arvika 2019_243 Stömne Bertil Persson Betongteknik AB DECIBEL - Huvudresultat Beräkning: VKV SWE99TM VKV typ Ljuddata

Module 1: Functions, Limits, Continuity

Transkript:

Commnts on th xm n qustions. Gnrl commnt th xm ws not wll on t ll. Th usul fults wr to blm 1. NOT READING THE QUESTION CAREFULLY TO START WITH - xmpls. 1() - rcursiv finition not function!!! b. 1() tr not binry tr! c. Principls not wht th lgorithm os but th gnrl is bhin HOW it works. 7 () unirct grph this is stt in th qustion 2. Not knowing th subjct 3. Lck of til in th nswr 4. Nor giving ch stp in clcultion 5. Prim not using th lgorithm by inspction nswrs no stps in th clcultion 6. Tim mngmnt uring th xm running out of tim Qustion 1: finitions/scriptions short nswrs Problms not knowing th subjct / not ring th qustion crfully Qustion 2: Problms 2() - principls not wht th lgorithm os but th gnrl is bhin HOW it works. 2() you MUST b bl to giv ths finitions us th forml nottion S ::= H T ; H ::= lmnt; T ::= S; - mpty BT ::= LC N RC ; N ::= lmnt; LC ::= BT; RC ::= BT; - mpty Qustion 3: Problms lck of til s th fcit nswr blow collision hnling H(ky) + f(i) whr f(i) shoul b fin for ch cs linr probing, qurtic probing & oubl hshing Qustion 4: Problms inbility to construct n rgumnt n iscussion qustion thr is n xmpl on th wb http://www.cs.ku.s/~onl/inx.php?dsaxmiss=1 DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 1 v 18

Qustion 5: Problms this ws th hrst qustion! Th rmovl of vlu from BST Th givn co (7 stps) is sris of tsts to xclu crtin conitions you lso n to r btwn th lins s to wht ths tsts imply 1. Th mpty tr you cnnot lt vlu which os not xist by finition! 2. Th vlu is in th LC rconstruct th tr from th LC with vlu rmov, no n RC 3. Th vlu is in th RC rconstruct th tr from th LC, no n RC with vlu rmov 4. Th vlu to b rmov is t th (locl) root no thr r 4 possibilitis hr for th tr ( = mpty) N is th root no to b rmov (i) (, N, ), (ii) (LC, N, ), (iii) (, N, RC) (iv) (LC, N, RC) Cs 4 covrs (i) n (iii) - i.. LC is mpty rturn RC ithr cs (i) mpty or cs (iii) non-mpty 5. Covrs cs (ii) mpty RC hnc rturn th non-mpty LC 6. Is cs (iv) bov i.. (LC, N, RC) - n cis on th tr blnc whthr to tk th mximum vlu of th LC to rplc th root no (N) to b rmov LCMsAsRoot 7. Us th minimum vlu of th RC s th vlu to rplc th root no N In css (6) & (7) LCMxAsRoot/RCMinAsRoot both functions will crt nw tr with mxlc / minrc rmov from th pproprit tr (hnc using b_rm( ) rcursivly) i.. S th fcit nswr blow. Qustion 6: Problms 6() not using n xmpl; th simplst xplntion uss th jcncy list s th fcit nswr blow. Qustion 7: Problms Prim/Kruskl unirct grph! It woul hv bn bttr to o 7(c) Kurskl first sinc it tks lss tim n givs th nswr to 7() which mns tht you cn cross-chck your clcultions s you go. Too mny by inspction nswrs no til, stpwis clcultions for Prim!!! DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 2 v 18

FACIT TILL OMTENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B03 160402 kl. 09:00 14:00 Ansvrig Lärr: Donl F. Ross Hjälpml: Ing. Algoritmrn finns i rspktiv uppgiftrn llr i bilogrn. *** OBS *** Btygsgräns: Kurs: Mx 60p, M bröm gokän 50p, Ick utn bröm gokän 40p, Gokän 30p (vrv minimum 20p från tntn, p från lbbrn) Tnt: Mx 40p, M bröm gokän 34p, Ick utn bröm gokän 27p, Gokän 20p Lbbrn: Mx 20p, M bröm gokän 18p, Ick utn bröm gokän 14p, Gokän p SKRIV TYDLIGT LÄS UPPGIFTERNA NOGGRANT Ang ll ntgnn. (1) G tt kortfttt svr till följn uppgiftr (()-(j)). () V skull tt ltrntiv för Floys lgoritm vr? Apply Dijkstr s lgorithm to ch no in th grph. (b) V är n rkursiv finition? A finition which is PARTLY fin in trms of itslf.g. squnc, BT (c) V btyr collction bstrction? Trting th st, squnc, tr n grph s collction of ntitis n fining common oprtions s oprtions on collction.g., rmov, fin lmnt, count th numbr of lmnts (crinlity), is_mpty oprtion. DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 3 v 18

() G n finition v tt trä? A collction of ntitis (nos) fin by prnt/chil rltionship (hirrchicl), whr ch no (xcpt th lvs) my hv numbr of chilrn n ch no (xcpt th root) hs xctly 1 prnt. If th collction of chilrn is consir st thn th tr is unorr; if consir s squnc, th tr is orr. () G tt xmpl v n llmän jupt-först trvrsring v tt BST? A gnrl pth-first trvrsl of th bov BST woul visit ch no 3 tims n prouc th following squnc: 1 1 2 3 2 1 2 3 3 whr th x 1 rprsnt prorr trvrsl, th x 2 n in-orr trvrsl n th x 3 post-orr trvrsl. (f) V är mningn m sortring? To mk srching mor fficint. (g) V är hntrlig ( trctbl ) problm i komplxittstori? Ths r problms whos Big-oh solutions my b xprss s polynomil tim..g. n, n 2, n vn logn, n logn. (h) Nämn två mtor för tt upptäck cyklr i grfr. Apply Wrshll s lgorithm n chck th igonl (top-lft to bottom right) for 1s Construct th pth-first spnning forst n chck for bck gs. (i) V är tt miniml spnningträ ( miniml spnning tr ) (MST)? A Fr Tr riv from n unirct grph whr for n nos thr r (n-1) gs which rprsnt th chpst wy of conncting th nos in singl componnt. (j) Nämn två xmpl v lgoritmr är jupt-först-sökning tillämps? Topologicl sort n crting pth-first spnning forst. Totlt 5p DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 4 v 18

(2) G tt kortfttt svr till följn uppgiftr (()-()). () Bskriv principn bkom Dijkstrs lgoritmn. Mrks for goo nswr. 2p (b) Bskriv n bstrkt t typ (ADT) som n virtull mskin. Vilk förlr finns m n sån bskrivning? 2p Mrks for goo nswr. (c) Skriv bstrkt rkursiv psuoko till n funktion för tt omblnsr tt AVL-trä. Ang ll ntgn och förklr ll hjälpfunktionr som bhövs. 2p (1) Tst th tr (T) to s whthr lft or right rottion is rquir. (2) Tst th RC/LC to ci whthr singl or oubl rottion is rquir. NOTE: th is not th nswr rquir but sinc this is prt of th lbs for th cours, I o not intn to provi fr co. Th ssumption is tht you hv on th lbs! () Skriv bstrkt rkursiv psuoko till funktionn T2Q från lbb 1. T2Q förvnlr tt binärt trä till n rry. 2p NOTE: th is not th nswr rquir but sinc this is prt of th lbs for th cours, I o not intn to provi fr co. Th ssumption is tht you hv on th lbs! () Skriv n rkursiv finitionn v n skvns och tt binärt-trä. 2p Sq ::= H Til mpty H ::= lmnt non-rcursiv prt Til : ::= Sq rcursiv prt BT ::= LC N RC mpty N ::= lmnt (no) non-rcursiv prt LC ::= BT rcursiv prt RC ::= BT rcursiv prt Totlt p DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 5 v 18

(3) Hshning Diskutr ingån hur hshning fungrr. Vilkt är t störst problmt? Hur lösr mn tt? Vilk spktr skull mn t hänsyn till? G xmpl för vrj fll Du bskrivr. 5p Points for iscussion (1) Hsh function H(ky) inx in th hsh spc. (2) Mntion th possibilitis for th typ of hsh function. (3) Th min problm is collision hnling w look t 4 mthos (4) Collisions r hnl by H(ky) + f(i) whr i is th i th collision (5) Linr probing f(i) = i; qurtic probing f(i) = i*i; oubl hshing f(i) = i*h2(ky) (6) Mtho 1 sprt chining scrib & iscuss th vntgs n isvntgs. (7) Mtho 2 linr probing scrib & iscuss th vntgs n isvntgs. (8) Mtho 3 qurtic probing scrib & iscuss th vntgs n isvntgs. (9) Wht r th min problms with mthos 1 to 3? () Wht is th min problm with mtho 3? (11) Mtho 4 - oubl hshing scrib & iscuss th vntgs n isvntgs. (12) Dscrib r-hshing whn is this us? (13) Mntion lo fctors (14) Mntion physicl implmnttion fctors giv xmpls. + mrks for goo xmpls n xplntions. (4) Diskussionsuppgift Mn kn påstå tt skvnsn är n viktigst bstrkt tstrukturn (ADT). Skriv n utförlig bskrivning v n skvns (2p) och tt tljr iskussion som stöjr tt påstån (3p). Svr ingån. Ang ll ntgn. Totl 5p Mrks for goo iscussion Points to not (1) A squnc is n orr collction of non-uniqu ntitis i.. ch lmnt hs position n vlu lmnt. (2) A squnc my b sort but this is n xtr proprty n not prt of th finition. (3) A squnc my b fin ithr itrtivly or rcursivly scrib both. (4) Dscrib th oprtions on squnc is_mpty, crinlity,, fin & rmov. (5) A squnc my b us to implmnt st n grph s wll s st. DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 6 v 18

(5) Rkursion Titt på kon för tt t bort tt vär från tt BST nn. Dnn ko sk tillämps på tt binärt sök trä som skpts gnom tt lägg till följn värn i nn orning:-, 5, 30, 20, 15, 25, 13, 17, 16 Värt 15 sk sn ts bort. Ang ll ntgn. 1. Skriv (psuo)kon till funktionn HDiff(trrf T) (1p) 2. Skriv (psuo)kon till funktionrn LCmxAsRoot(trrf T) smt RCminAsRoot(trrf T) (1p) Använ rkursion och smm progrmmringsstil för Din vrsionr v HDiff, LCmxAsRoot och RCminAsRoot. 3. Förklr stgvis och utförligt hur kon, inklusiv Din vrsionr v (i) HDiff (ii) LCmxAsRoot och (iii) RCminAsRoot fungrr när n tilllämps på tt trä. Vis vrj rkursivt nrop till b_rm. Dt först nropt är b_rm([], 15) är [] står för trät m rotvär. Dvs [x] står för trät m rotvär x. Rit trät som rturnrs ftr vrj nrop i rr 2-7. (3p) Totl 5p sttic trrf b_rm(trrf T, int v) { 1. rturn is_mpty(t)? T 2. : v < gt_vlu(no(t))? cons(b_rm(lc(t), v), no(t), RC(T)) 3. : v > gt_vlu(no(t))? cons(lc(t), no(t), b_rm(rc(t), v)) 4. : is_mpty(lc(t))? RC(T) 5. : is_mpty(rc(t))? LC(T) 6. : HDiff(T) > 0? LCmxAsRoot(T) 7. : RCminAsRoot(T); DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 7 v 18

1. sttic int HDiff(trrf T) { rturn b_hight(lc(t)) - b_hight(rc(t)); 2. sttic trrf fin_mxlc(trrf T) { rturn is_mpty(rc(t))? T : fin_mxlc(rc(t)); 3. sttic trrf fin_minrc(trrf T) { rturn is_mpty(lc(t))? T : fin_minrc(lc(t)); 4. sttic trrf LCmxAsRoot(trrf T) { trrf mxno = fin_mxlc(lc(t)); rturn cons(b_rm(lc(t), gt_vlu(mxno)), mxno, RC(T)); 5. sttic trrf RCminAsRoot(trrf T) { trrf minno = fin_minrc(rc(t)); rturn cons(lc(t), minno, b_rm(rc(t), gt_vlu(minno))); sttic trrf b_rm(trrf T, int v) { 1. rturn is_mpty(t)? T 2. : v < gt_vlu(no(t))? cons(b_rm(lc(t), v), no(t), RC(T)) 3. : v > gt_vlu(no(t))? cons(lc(t), no(t), b_rm(rc(t), v)) 4. : is_mpty(lc(t))? RC(T) 5. : is_mpty(rc(t))? LC(T) 6. : HDiff(T) > 0? LCmxAsRoot(T) 7. : RCminAsRoot(T); DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 8 v 18

Th strt tr 5 30 20 15 25 13 17 16 1. Initil cll b_rm([], 15) 2. Lin 3: rcursiv cll 1 cons([5], [], b_rm([30], 15)) 3. Lin 2: rcursiv cll 2 cons(b_rm([20], 15), [30], [ ]) 4. Lin 2: rcursiv cll 3 cons(b_rm([15], 15), [20], [25]) 5. Lin 6: HDiff([15] 1 2-1 i.. th RC is pr 6. Lin 8; RCminAtRoot([15]). Clls fin_minrc([15]) which rturns [16] b. Rcursiv cll 4 cons([13], [16], b_rm([17], 16); Turning point in th rcursion c. Lin 2: rcursiv cll 5 cons(b_rm([16], 16), [17], [ ]). Lin 4: rturns [ ] to th cons bov in (c). Th cons in (c) rturns [17] to th cll in (b) f. Th cons in (b) thn rturns th tr ([13], [16], [17]) to (6) bov g. This cons rturns ( ([13], [16], [17]), [20], [25]) from (4) to (3) h. (3) rturns ( (([13], [16], [17]), [20], [25])), [30], [ ] ) to (2) i. (2) rturns ( [5], [], ([13], [16], [17]), [20], [25])), [30], [ ] )) to (1) (LC, N, RC) is us to not trs with nst lmnts for LC n RC 5 30 20 16 25 13 17 DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 9 v 18

Picturs for th bov:- () rturns (f) rturns 17 16 13 17 (g) rturns 20 16 25 13 17 30 (h) rturns 20 16 25 13 17 5 30 (i) rturns 20 16 25 13 17 DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin v 18

(6) Lbbko () I grflbbn hr n stunt skrivit följn ko för tt t bort n knt (g) från n jcncy list. Förklr ingån hur kon fungrr. Använ gärn xmpl. Ang ll ntgn. Vilk är förutsättningrn för tt kon sk fungr? voi rm(chr cs, chr c) { st_gs(b_finn(cs, G), b_rm(c, gt_gs(b_finn(cs, G)))); 2p (b) I trälbbn hr n stunt skrivit ko för tt sök ftr tt vär i tt BST (binärt sökträ). Sn hr stuntn kommit på tt nn ko kun lätt npsss för tt sök ftr tt vär i tt kompltt trä. Dss funktionr finns nn. V hr stuntn skrivit för xxx och yyy? Ang ll ntgn. sttic int b_finb(trrf T, int v) { rturn is_mpty(t)? 0 : v < gt_vlu(no(t))? b_finb(lc(t), v) : v > gt_vlu(no(t))? b_finb(rc(t), v) : 1; sttic int b_finc(trrf T, int v) { rturn is_mpty(t)? 0 : xxx? 1 : yyy; 1p (c) Skriv bstrkt rkursiv (psuo)ko för tt lägg till tt lmnt i tt binärt trä. Ang ll ntgn. 2p Totlt 5p DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 11 v 18

() I grflbbn hr n stunt skrivit följn ko för tt t bort n knt (g) från n jcncy list. Förklr ingån hur kon fungrr. Använ gärn xmpl. Ang ll ntgn. Vilk är förutsättningrn för tt kon sk fungr? voi rm(chr cs, chr c) { st_gs(b_finn(cs, G), b_rm(c, gt_gs(b_finn(cs, G)))); 2p Assumptions: (i) G is rfrnc to th grph, (ii) th grph is rprsnt s n jcncy list (AL) (iii) (cs, c) fin th g. Working from th insi out (functionl thinking) b_finn(cs, G) givs rfrnc to th no in th AL; gt_gs(n) thn givs rfrnc to th g list for this no n b_rm(, Elist) rmovs c from this g list n rturns (nw) rfrnc to th g list which is rconnct to th g list of th no cs by st_gs(n, Elist) Drw n xmpl of grph s wll s th corrsponing jcncy mtrix.g. unirct grph nos:, b, c gs (,b), (,c), (b,c) this is G jcncy mtrix rmov g (,b) rsult : b, c b:, c c:, b : c b: c c:, b now nlys th co b_finn(cs, G) gt_gs(n) b_rm(, L) st_gs(n, L) rm(cs, c) fin cs (sourc no) in th no list gt th g list for no N rmov from th g list L st th g list of no N to g list L cs is th sourc no in th no list c is th stintion no in th g list for n unirct grph, th front n must issu 2 clls rm(,b) n rm(b,) AFTER hving chck tht nos n b xist n tht g (,b) xists. DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 12 v 18

(b) I trälbbn hr n stunt skrivit ko för tt sök ftr tt vär i tt BST (binärt sökträ). Sn hr stuntn kommit på tt nn ko kun lätt npsss för tt sök ftr tt vär i tt kompltt trä. Dss funktionr finns nn. V hr stuntn skrivit för xxx och yyy? Ang ll ntgn. sttic int b_finb(trrf T, int v) { rturn is_mpty(t)? 0 : v < gt_vlu(no(t))? b_finb(lc(t), v) : v > gt_vlu(no(t))? b_finb(rc(t), v) : 1; sttic int b_finc(trrf T, int v) { rturn is_mpty(t)? 0 : xxx? 1 // xxx v == gt_vlu(no(t)) : yyy; // yyy b_finc(lc(t), v) b_finc(rc(t), v); 1p (c) Skriv (psuo)ko för tt lägg till tt lmnt i tt binärt trä. Ang ll ntgn. 2p TrRf: A(TrRf T, intgr v) { if IsEmpty(T) thn rturn crt_l(v); if v < vlu(t) thn rturn cons(a(lft(t), v), T, right(t)); if v > vlu(t) thn rturn cons(lft(t), T, A(right(T), v)); rturn T; // no uplicts. DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 13 v 18

(7) Prims ) Tillämp Prims lgoritm (nn) på n orikt grfn: (-7-b, -1-c, --, b-3-c, b-2-, c-15-, c-4-, c-9-f, -4-f, -8-f). Börj m no. Ang ll ntgnn och vis ll bräkningr och mllnrsultt. V rprsntrr rsulttt? Ang vrj stg i in bräkning. Rit lrsulttt ftr vrj itrtion. (2p) b) Förklr principrn bkom Prims lgoritm. (2p) c) Vis hur Du skull nvän principrn bkom Kruskls lgoritm för tt bkräft rsulttt från Prims lgoritm. (1p) Totlt 5p Ang *ll* ntgnn och vis *ll* bräkningr och mllnrsultt Prim ( no v) -- v is th strt no { U = {v; for i in (V-U) { low-cost[i] = C[v,i]; closst[i] = v; whil (!is_mpty (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; isply(k, closst[k]); U = U + k; for j in (V-U) if ( C[k,j] < low-cost[j] ) ) { low-cost[j] = C[k,j]; closst[j] = k; DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 14 v 18

Th principl is tht th MST grows from th on componnt (hr ) by conncting this componnt to ny othr componnt ( no) by th chpst g SO FAR foun this lst proviso rvls tht Prim s is GREEDY lgorithm i.. us locl bst solution. 1. Choos strt no (usully ) n mrk s visit U = { U is componnt 2. Clcult th istncs btwn n th rmining nos V-U n rw th MST clcult so fr 3. Choos th closst no x to n mrk s visit U = {,x 4. If th istnc x to ny no y in V-U is shortr thn rjust costs by rmoving th prvious g n ing th g (x y) now w hv nw MST 5. U rprsnts th visit nos n V-U th unvisit nos choos th shortst g from th visit nos to no in V-U n this to th MST 6. Rpt 4 & 5 until th MST hs bn foun n V-U = (mpty) Brifly, Prim s grows componnt from strt no n th shortst g from tht strt no. If thr is shortr g from no in th componnt (visit nos) to non-componnt no (non-visit nos), rplc th currnt g with th nw (shortr) g. S blow for th clcultions. Drw th grph (n possibly sktch th nswr us Kruskl s for quick chck!): Cost 18 7 b 2 1 3 c 15 4 9 8 f 4 b 2 3 c 8 1 4 f DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 15 v 18

Drw th cost mtrix C n rry D b c f 7 1 b 7 3 2 c 1 3 15 4 9 15 4 2 4 8 f 9 4 8 b c f lowcost 7 1 closst Ming: lowcost: 7 1 closst: U = {,c V-U = {c,b,,,f min = 1; k = c Rjust costs: if C[k,j] < lowcost[j] thn { lowcost[j] = C[k,j]; closst[j] = k j = b; if C[c,b] < lowcost[b] thn { lowcost[b] = C[c,b]; closst[b] = c 3<7 c-3-b j = ; if C[c,] < lowcost[] thn { lowcost[] = C[c,]; closst[] = c 15< no chng j = ; if C[c,] < lowcost[] thn { lowcost[] = C[c,]; closst[] = c 4< c-4- j = f; if C[c,f] < lowcost[f] thn { lowcost[f] = C[c,f]; closst[f] = c 9< c-9- Ming: lowcost: 3 1 4 9 closst: c c c U = {,c,b V-U = {,,f min = 3; k = b Rjust costs: if C[k,j] < lowcost[j] thn { lowcost[j] = C[k,j]; closst[j] = k j = ; if C[b,] < lowcost[] thn { lowcost[] = C[b,]; closst[] = b < no chng j = ; if C[b,] < lowcost[] thn { lowcost[] = C[b,]; closst[] = b 2<4 b-2- j = f; if C[b,f] < lowcost[f] thn { lowcost[f] = C[b,f]; closst[f] = b <9 no chng Ming: lowcost: 3 1 2 9 closst: c b c U = {,b,c, V-U = {,f min = 2; k = j = ; if C[,] < lowcost[] thn { lowcost[] = C[,]; closst[] = < no chng j = f; if C[,f] < lowcost[f] thn { lowcost[f] = C[,f]; closst[f] = 8<9-8-f Ming: lowcost: 3 1 2 8 closst: c b U = {,b,c,, V-U = {f min = 8; k = f j = f; if C[,f] < lowcost[f] thn { lowcost[f] = C[,f]; closst[f] = 4< -4-f Min g: lowcost: 3 1 4 2 8 --- closst: c f b --- U = {,c,b,,f V-U = { QED MST gs -1-c, c-3-b, b-2-, -8-f, f-4- Totl cost = 18 (Confirm using Kruskl s) DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 16 v 18

Förklr hur Prims lgoritm fungrr gnom tt rit bilr som rprsntrr vrj mllnrsultt unr lgoritmnsxkvring. Använ xmplt ovn. V är principn bkom Prims lgoritm? S bov for th principl. Digrms:- (2p) 7 from 7 b 2 1 3 c 15 4 9 8 f 4 b c 1 f from c from b b 3 c 1 b 2 3 c 1 4 9 f 9 f DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 17 v 18

from from b 2 3 c 1 b 2 3 c 1 4 8 f 8 f Chck this rsult using Kruskll s Priority Quu -1-c b-2- b-3-c c-4- woul prouc cycl -4-f -7-b - woul prouc cycl -8-f th rst! b 2 7 1 3 c 15 4 9 8 f 4 DFR Dtstrukturr och lgoritmr, DAV B03, Rsit 160402 Sin 18 v 18