FACIT TILL OMTENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B0 140610 kl. 08:15 1:15 Ansvrig Lärr: Donl F. Ross Hjälpml: Ing. Algoritmrn finns i rspktiv uppgiftrn. Btygsgräns: *** OBS *** Kurs: Tntmn: Lrn: Mx 60p, M röm gokän 50p, Ik utn röm gokän 40p, Gokän 0p (vrv minimum 15p från tntmn, 15p från lrn) Mx 0p, tyg 5: 26p-0p, tyg 4: 21p-25p, tyg : 15p-20p Mx 0p, tyg 5: 26p-0p, tyg 4: 21p-25p, tyg : 15p-20p SKRIV TYDLIGT LÄS UPPGIFTERNA NOGGRANT Ang ll ntgnn. (1) G tt kortfttt svr till följn uppgiftr (()-(j)). () En finition v n mäng Dfinition: An unorr olltion of uniqu ntitis hving ommon proprty (ttriut). Exmpl: St of olours: {r, grn, lu, yllow,..., lk () En finition v n skvns Dfinition: An orr olltion of ntitis (not nssrily uniqu) hving ommon proprty (ttriut) with sussor rltionship fin twn ntitis. Exmpl: Dys of th wk: (Mony, Tusy,..., Suny) () En finition v tt trä Dfinition: An unorr/orr olltion of ntitis (not nssrily uniqu) hving ommon proprty (ttriut) with snnt rltionship fin twn ntitis. Exmpl: Fil irtoris in omputing systms () En finition v n grf Dfinition: An unorr olltion of ntitis (not nssrily uniqu) hving ommon proprty (ttriut) with (gnrl) rltionship fin twn ntitis. Exmpl: A Tlphon / Computr ntwork (in ft ny ntwork) () En finition v n smling Dfinition: An unorr/orr numr of ntitis (not nssrily uniqu) hving ommon proprty (ttriut) possily with rltionship fin twn ntitis. Exmpl: st, squn, tr, grph DFR Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin 1 v 17
Förklr Din xmpl ((f)-(j)) m någr mningr FULL: PERFECT: BT hight h with xtly 2 h -1 lmnts COMPLETE: prft on th nxt lowst lvl AND th lowst lvl is fill from th lft vry no hs xtly 2 or 0 hilrn (f) Ett xmpl v tt fullt trä som int är kompltt (g) Ett xmpl v tt kompltt trä som int är fullt (h) Ett xmpl v tt trä som är vrkn fullt llr kompltt llr prfkt (i) Ett xmpl v implmnttionsstrktion Gt n st funtions on th ttriuts of n ADT (j) Ett xmpl v svnsrkursion (til rursion) int: siz (List) { if is_mpty (List) rturn 0; rturn 1 + siz(til(list)); Totlt 5p DFR Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin 2 v 17
(2) G tt kortfttt svr till följn uppgiftr. () En finition v rkursion Dfinition 1: An ntity prtilly fin in trms of itslf is rursivly fin Dfinition 2: A funtion whih lls itslf unr rtin onitions is rursivly fin. () En förklring v strktion Dfinition 1: MODELLING ABSTRACTION Th pross of slting rtin proprtis (ttriuts) of n ntity to rprsnt tht ntity in givn sitution. Exmpl 1: In rlity informtion out stunt ( rl worl ntity) my inlu proprtis (ttriuts) suh s STUDENT (nm, t of irth, gnr, rss, pnumr, tlphon numr, moil numr, hir olour, y olour, fthr's nm, mothr's nm, t....) Som of this informtion n "strt" s STUDENT (nm, t of irth, gnr, rss, pnumr) in ts for xmpl to "rprsnt" (or mol) stunt Dfinition 2: COLLECTION ABSTRACTION Th pross of gnrlising ommon proprtis n oprtions of th st, squn, tr n grph. Eh is olltion with th squn, tr n grph lso hving rltionships fin - squn (sussor/prssor), tr (lft hil, right hil), grph ( gnrl rltionship rprsnting istn, ost, onntion). Exmpl 2: A olltion n fin s omprising ntitis n rltionships, oth with possil ttriuts n oprtions suh s is_mpty(), rinlity(), (), rmov(), fin(), isply(). Dfinition : IMPLEMENTATION ABSTRACTION Th pross of slting rtin proprtis of Dt Typ inpnnt of th implmnttion of tht Dt Typ - hn th xprssion Astrt Dt Typ Exmpl : A squn my implmnt using n rry of vlus n n inx to tht rry rrys (vlu, nxt) n n inx rors/struturs n pointrs ( link list) In ll ss, t mor strt lvl, rtin proprtis n oprtions pply in oth implmnttions.g. first, nxt, numr of lmnts In ll ss w hv strutur (rry(s)/list) whih rprsnts olltion of lmnts n rfrn (inx/pointr) to n iniviul lmnt in th olltion. DFR Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin v 17
() Priniprn kom hshning Dfinition: Th mpping of vlu (oftn ky vlu) to position in linr sp (hsh sp) y mns of hsh funtion h. h: ky => position ( mpping from ky sp to hsh sp) Issus: min vntg: srh tim is O(1) (i.. onstnt) ollision hnling (i.. if h(x) mps to th sm position s h(y)) th istriution of vlus in th ky sp if this is known, som form of optimistion my ppli th lo ftor in th hsh sp (s th nimtion low) th numr of pros rquir (inrss s th lo ftor inrss) () Priniprn kom Kruskls lgoritm Divi th grph into omponnts (G ) with on no in h omponnt initilly n priority quu (PQ) for h g (lowst ost first). Work through th PQ ing n g to th grph (G ) of nos iff 2 istint omponnts r onnt until th grph ontins on omponnt th Miniml Spnning Tr (MST). () Om mn skull lägg till skvnsn (1, 4, 22, 14, 4, 8, 44, 7) i tt hshutrymm (hsh sp) m 10 pltsr, vilkn kollisionshntringsmto v linjär proning (linr proing) oh kvrtisk proning (qurti proing) är säkrr? Förklr vrför. Choos linr proing sin qurti proing nnot gurnt suss if th lo ftor is ov 50%. Hr thr r 8 lmnts n 10 pls giving lo of 80%. Totlt 5p DFR Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin 4 v 17
() Rkursion Skriv (psuo)ko till n rkursiv funktion (llr två rkursiv funktionr) för tt räkn frm ntlt kntr (gs) i n grf. Ang ll ntgn. 5p Assumptions: Strutur: typf strut nolm * norf; typf strut nolm { hr nnm; int ninfo; norf gs; norf nos; nolm; + orrsponing gt/st funtions pr ttriut n h/til oprtions for oth th no list (nos) n th g list (gs). G is rfrn to th grph, th is_mpty(r) funtion is fin. stti int _ngs(norf E) { rturn is_mpty(e)? 0 : 1 + _ngs(til(e)); stti int _siz(norf G) { rturn is_mpty(g)? 0 : _ngs(gt_gs(nh(g))) + _siz(ntil(g)); DFR Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin 5 v 17
(4) Grfoprtionr En grf kn skrivs som n virtull (llr strkt) mskin. I grflortionn, filn grph.h är fktiskt n skrivning v nn virtull mskin. Innhållt v grph.h gs nn. /**************************************************************************/ /* funtion prototyps - oprtions on th Grph ( virtul mhin) */ /**************************************************************************/ /* Grph = (V, E) whr V is st of vrtis/nos n E st of gs */ /* Thr r limit numr of oprtions (9) whih n ppli to G */ /**************************************************************************/ voi _isply_jlist(); /* isply G s n jny list */ voi _isply_jmtrix(); /* isply G s n jny mtrix*/ voi _no(hr ); /* vrtx (no) to G */ voi _rmno(hr ); /* rmov vrtx (no) from G */ voi _g(hr s, hr, int v);/* n g (with wight) to G*/ voi _rmg(hr s, hr ); /* rmov n g from G */ int _is_nmmr(hr ); /* is no mmr of G? */ int _is_mmr(hr s, hr ); /* is n g mmr of G? */ int _siz(); /* th numr of nos in G */ /**************************************************************************/ I front-n:n kn mn spifir förvillkor (pronitions) till vrj oprtion ovn m hjälp v n llr flr v ss 9 funktionr. Ang förvillkort/förvillkorn till vrj v först ått oprtionrn ovn. Dvs i (i) f_isply_jlist(), (ii) f_isply_jmtrix(), (iii) f_no(), (iv) f_rmno(), (v) f_g(), (vi) f_rmg(), (vii) f_is_nmmr(), (viii) f_is_mmr(). Kom ihåg tt ifrån front-n:n kn mn utför tt smtl m nvänrn för tt gär olik värn smt skriv flmln. (5p) (i) f_isply_jlist() voi f_isply_jlist() { if (_siz()==0) ui_putgrphempty(); ls { ui_puttitllist(); _isply_jlist(); (ii) f_isply_jmtrix() voi f_isply_jmtrix() { if (_siz()==0) ui_putgrphempty(); ls { ui_puttitlmtrix(); _isply_jmtrix(); DFR Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin 6 v 17
(iii) f_no() voi f_no() { hr ; = ui_gtno(); if (_is_nmmr()) ui_putnoerror(); ls _no(); (iv) f_rmno() voi f_rmno() { hr ; if (_siz()==0) ui_putgrphempty(); ls { = ui_gtno(); if(!_is_nmmr()) ui_putnonoerror(); ls _rmno(); (v) f_g() voi f_g() { hr s, ; int w; if (_siz()==0) ui_putgrphempty(); ls { s = ui_gtno(); if (!_is_nmmr(s)) ui_putnoerror(s); ls { = ui_gtno(); if (!_is_nmmr()) ui_putnoerror(); ls if (_is_mmr(s, )) ui_putegerror(s, ); ls { w = ui_gtwight(); _g(s,, w); if (ui_ismou() && (!= s)) _g(, s, w); DFR Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin 7 v 17
(vi) f_rmg() voi f_rmg() { hr s, ; if (_siz()==0) ui_putgrphempty(); ls { s = ui_gtno(); if (!_is_nmmr(s)) ui_putnonoerror(s); ls { = ui_gtno(); if (!_is_nmmr()) ui_putnonoerror(); ls if (!_is_mmr(s, )) ui_putnoegerror(s, ); ls { _rmg(s, ); if (ui_ismou()) { if (!_is_mmr(, s)) ui_putnoegerror(s, ); ls _rmg(, s); (vii) f_is_nmmr() voi f_is_nmmr() { hr ; if (_siz()==0) ui_putgrphempty(); ls { = ui_gtno(); if (_is_nmmr()) ui_putnofoun(); ls ui_putnonotfoun(); DFR Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin 8 v 17
(viii) f_is_mmr() voi f_is_mmr() { hr s, ; if (_siz()==0) ui_putgrphempty(); ls { s = ui_gtno(); if (!_is_nmmr(s)) ui_putnonoerror(s); ls { = ui_gtno(); if (!_is_nmmr()) ui_putnonoerror(); ls if (_is_mmr(s, )) ui_putegfoun(s, ); ls ui_putegnotfoun(s, ); DFR Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin 9 v 17
(5) Prims lgoritm ) Tillämp Prims lgoritm (nn) på n orikt grfn: (--, --, --, --, --, --, --, --f, --f, --f). Börj m no. Ang ll ntgnn oh vis ll räkningr oh mllnrsultt. V rprsntrr rsulttt? Ang vrj stg i in räkning. Ant tt norn lgrs i närlistn (jny list) i lftisk orning smt tt mn sökr ftr t miniml värt i lftisk orning. Rit lrsulttt ftr vrj itrtion. ) Förklr priniprn kom Prims lgoritm. (p) (2p) Totlt 5p Ang *ll* ntgnn oh vis *ll* räkningr oh mllnrsultt Prim ( no v) -- v is th strt no { U = {v; for i in (V-U) { low-ost[i] = C[v,i]; losst[i] = v; whil (!is_mpty (V-U) ) { i = first(v-u); min = low-ost[i]; k = i; for j in (V-U-k) if (low-ost[j] < min) { min = low-ost[j]; k = j; isply(k, losst[k]); U = U + k; for j in (V-U) if ( C[k,j] < low-ost[j] ) ) { low-ost[j] = C[k,j]; losst[j] = k; DFR Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin 10 v 17
Th prinipl is tht th MST grows from th on omponnt (hr ) y onnting this omponnt to ny othr omponnt ( no) y th hpst g SO FAR foun this lst proviso rvls tht Prim s is GREEDY lgorithm i.. us lol st solution. S low for th lultions. Drw th grph (n possily skth th nswr us Krusklls for quik hk!): Cost 15 f f DFR Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin 11 v 17
Drw th ost mtrix C n rry D f f f lowost losst Min g: lowost: --- losst: --- U = {, V-U = {,,,f min = ; k = Rjust osts: if C[k,j] < lowost[j] thn { lowost[j] = C[k,j]; losst[j] = k j = ; if C[,] < lowost[] thn { lowost[] = C[,]; losst[] = < no hng j = ; if C[,] < lowost[] thn { lowost[] = C[,]; losst[] = < no hng j = ; if C[,] < lowost[] thn { lowost[] = C[,]; losst[] = < -- j = f; if C[,f] < lowost[f] thn { lowost[f] = C[,f]; losst[f] = < no hng f f f lowost losst DFR Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin 12 v 17
Min g: lowost: 1 --- losst: --- U = {,, V-U = {,,f min = ; k = Rjust osts: if C[k,j] < lowost[j] thn { lowost[j] = C[k,j]; losst[j] = k j = ; if C[,] < lowost[] thn { lowost[] = C[,]; losst[] = < no hng j = ; if C[,] < lowost[] thn { lowost[] = C[,]; losst[] = < no hng j = f; if C[,f] < lowost[f] thn { lowost[f] = C[,f]; losst[] = < --f f f f lowost losst Min g: lowost: --- losst: --- U = {,,, V-U = {,f min = ; k = Rjust osts: if C[k,j] < lowost[j] thn { lowost[j] = C[k,j]; losst[j] = k j = ; if C[,] < lowost[] thn { lowost[] = C[,]; losst[] = < no hng j = f; if C[,f] < lowost[f] thn { lowost[f] = C[,f]; losst[f] = < no hng Min g: lowost: --- losst: --- U = {,,,, V-U = {f min = ; k = Rjust osts: if C[k,j] < lowost[j] thn { lowost[j] = C[k,j]; losst[j] = k j = f; if C[,f] < lowost[f] thn { lowost[f] = C[,f]; losst[f] = < no hng Finlly th rmining no no (thr r no furthr lultions) Min g: lowost: --- losst: --- U = {,,,,f V-U = { QED MST gs --, --, --, --, --f Totl ost = 15 (Confirm using Kruskl s) DFR Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin 1 v 17
Prinipl: to uil th MST from singl omponnt y hoosing th hpst g to nonomponnt nos from th lst no. Aov strt with, g istns (infinit if no g), hoos th hpst (--) n this to th omponnt. Now rhk if thr r hpr gs from to th non-omponnt nos. Rpt until ll th nos r onnt. So th omponnt vlops s (), (--), (--, --1, -4-, -5-, --f) (s ov). Strt Grph Solution f f f lowost losst DFR Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin 14 v 17
(6) Dijkstr + SPT (Shortst Pth Tr) Tillämp n givn Dijkstr_SPT lgoritmn (nn) på n rikt grfn, (,, 1), (,, 12), (,, 20), (,, 60), (,, 50), (,, 0), (,, 40) SPT = Shortst Pth Tr - vs kortst väg trät (KVT) från n no till ll nr. Börj m no. Vis vrj stg i in räkningr. Ang *ll* ntgnn oh vis *ll* räkningr oh mllnrsultt Rit vrj stg i konstruktionn v SPT:t vs vis till oh m nor oh kntr som läggs till mn sn ts ort. (p) Förklr priniprn kom Dijkstrs_SPT lgoritm. (2p) Totlt 5p Dijkstrs lgoritm m n utökning för SPT Dijkstr_SPT ( ) { S = { for (i in V-S) { D[i] = C[, i] --- initilis D - (g ost) E[i] = --- initilis E - SPT (g) L[i] = C[, i] --- initilis L - SPT (ost) for (i in 1..( V -1)) { hoos w in V-S suh tht D[w] is minimum S = S + {w forh ( 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 Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin 15 v 17
Initilis D, E, L D: 1 12 20 E: L: 1 12 20 w is (min vlu in D) S = {, V-S = {,, v = min (D[], D[]+C (,)) min(1, 12+ ) no hng v = min (D[], D[]+C (,)) min(, 12+0) -- 42 v = min (D[], D[]+C (,)) min(20, 12+40) no hng D: 1 42 12 20 E: L: 1 0 12 20 1 12 60 0 40 20 50 ---------------------------------------------------------------------------------------------------------------- D: 1 42 12 20 E: L: 1 0 12 20 v = min (D[], D[]+C (,)) v = min (D[], D[]+C (,)) w is (min vlu in D) S = {,, V-S = {, min(42, 1+60) no hng min(20, 1+ ) no hng D: 1 42 12 20 E: L: 1 0 12 20 1 12 60 0 40 20 50 DFR Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin 16 v 17
---------------------------------------------------------------------------------------------------------------- D: 1 42 12 20 E: L: 1 0 12 20 v = min (D[], D[]+C (,)) w is (min vlu in D) S = {,,, V-S = { min(42, 20+ ) no hng D: 1 42 12 20 E: L: 1 0 12 20 1 12 60 0 40 20 50 ---------------------------------------------------------------------------------------------------------------- This is th finl rsult. Costs: (1), (42), (12), (20) SPT gs: (1), (12), (0), (20) Prinipl similr to Prims i. uil omponnt stp y stp SPT 1 1 1 1 12 12 12 12 60 20 60 20 60 20 20 0 40 0 40 0 40 0 DFR Dtstrukturr oh lgoritmr, DAV B0, omtntmn 140610 - Fit Sin 17 v 17