FACIT TILL ORDINARIE TENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B03 1501 kl. 08:15 13:15 Ansvrig Lärr: Donl F. Ross Hjälpml: Ing. Algoritmrn finns i rspktiv uppgiftrn llr i ilogrn. *** OBS *** Btygsgräns: Kurs: Mx 60p, M röm gokän 50p, Ick utn röm gokän 40p, Gokän 30p (vrv minimum 20p från tntn, 10p från lrn) Tnt: Mx 40p, M röm gokän 34p, Ick utn röm gokän 27p, Gokän 20p Lrn: Mx 20p, M röm gokän 18p, Ick utn röm gokän 14p, Gokän 10p SKRIV TYDLIGT LÄS UPPGIFTERNA NOGGRANT Ang ll ntgnn. DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin 1 v 17
(1) G tt kortfttt svr till följn uppgiftr (()-(j)). () V är ig-o för n funktion som skrivr ut n jcncy mtrix? Vrför? O(n 2 ) mtrix is 2D which implis 2 nst for loops to isply th contnt. () V gör Dijkstrs lgorithm? Clcults th lngth of th shortst PATH twn givn no (th strt no) n th rmining nos in th grph. (c) V gör Floys lgorithm? All pirs shortst pth lgorithm. Clcults th lngth of th shortst PATH twn ch pir of nos ((, )!= )) in th grph. () V gör Wrshlls lgorithm? Clcults th trnsitiv closur of th grph, i.. if thr is PATH twn ny pir of nos (, ). () V gör Topologisk sortring? Givn DAG s input, proucs squnc which rprsnts prtil orring of th nos in th DAG (Dirct Acyclic Grph). (f) V är n hp? A t structur, which my rprsnt s n rry or s (inry) tr with th proprty tht th prnt no hs vlu which is grtr thn (or lss thn) its chilrn. Is us to implmnt priority quu (PQ) (g) V är förln m hshning? Th n fin oprtions r O(1). (h) V är n rkursiv funktion? A function which clls itslf usully in conitionl cll othrwis th function will isppr in n nlss squnc of rcursiv clls. (i) V är tt AVL-trä? A BST, Binry Srch Tr, with n constrint tht th hight of th lft n right su-trs my not iffr y mor thn 1. (j) V är ul hshning? A conflict rsolution tchniqu whr th f(i) function is scon hsh function. Giv n xmpl. Totlt 5p DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin 2 v 17
(2) G tt kortfttt svr till följn uppgiftr (()-()). () Kn non m t mximl värt i tt vänstrrn i tt BST (Binärt SökTrä) h tt högrrn? Förklr vrför! NO sinc th originl no (6) woul not mximum vlu y finition which givs contriction. Drw n xmpl to show this! 10 6 4 8 () Skriv rkursiv psuoko till n funktion för tt hitt t miniml värt i tt högrrn i tt BST (Binärt SökTrä). sttic int fin_min(trrf T) { rturn is_mpty(lc(t))? gt_vlu(t) : fin_min(lc(t)); (c) G n rkursiv finition v tt BT (Binärt Trä) BT ::= LC N RC mpty N ::= lmnt LC ::= BT RC ::= BT () Skriv n rkursiv sök ( fin ) funktion för tt BT (Binärt Trä) OBS j BST! sttic int t_fin(trrf T, int v) { rturn is_mpty(t)? 0 : v == gt_vlu(no(t))? 1 : t_fin(lc(t), v) t_fin(rc(t), v); () Förklr hur Du skull rprsntr tt BT (Binärt Trä) m hjälp v n rry. V är förhållnt mlln trät och rryn? A inry tr my rprsnt s n rry with th root in position 1, th lft chil in position 2 n th right chil in position 3. If th inxing strts t 1: In gnrl th lft chil is foun t inx i whr i = 2 * inx(prnt) n th right chil is foun t inx j whr j = 2 * inx(prnt)+1. Th tr is stor in th rry in rth-first orr. Totlt 5p DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin 3 v 17
(3) Hp Diskutr ingån hur kon till hp oprtionr (s Bilg A) fungrr? Använ skvnsn, 13, 18, 33, 72, 9,, 44, 27, 15, 66 som tt xmpl. Ant tt t störst värt hmnr i rotn. Vis vrj stg i Din räkningr. Apply hpify to th ov squnc of vlus. Solution 1 clcult th vlus using th lgorithm Input:, 13, 18, 33, 72, 9,, 44, 27, 15, 66 Arry siz = NB: i, l, r n lrgst r positions in th rry n not vlus Exrcis: rw th corrsponing trs for ch instnc of th rry. 5p stp 1: for i = 5 ownto 1 o Hpify(A, i) th cll to Hpify(A, 5) i = 5 A =, 13, 18, 33, 72, 9,, 44, 27, 15, 66 i = 5; (vlu 72) l = 10; (vlu 15) r = ; (vlu 66) lrgst = 5; (vlu 72) lrgst = 5 (vlu 72) lrgst = i hnc no swp giving, 13, 18, 33, 72, 9,, 44, 27, 15, 66 th cll to Hpify(A, 4) i = 4 A =, 13, 18, 33, 72, 9,, 44, 27, 15, 66 i = 4; (vlu 33) l = 8; (vlu 44) r = 9; (vlu 27) lrgst = 8; (vlu 44) lrgst = 8 (vlu 44) lrgst!= i hnc swp A[4] n A[8] giving, 13, 18, 44, 72, 9,, 33, 27, 15, 66 Hpify(A, 8) hs no ffct on A (A[8] is lf no) th cll to Hpify(A, 3) i = 3 A =, 13, 18, 44, 72, 9,, 33, 27, 15, 66 i = 3; (vlu 18) l = 6; (vlu 9) r = 7; (vlu ) lrgst = 3; (vlu 18) lrgst = 3 (vlu 44) lrgst = i hnc no swp giving, 13, 18, 44, 72, 9,, 33, 27, 15, 66 th cll to Hpify(A, 2) i = 2 A =, 13, 18, 44, 72, 9,, 33, 27, 15, 66 i = 2; (vlu 13) l = 4; (vlu 44) r = 5; (vlu 72) lrgst = 5; (vlu 72) lrgst = 5 (vlu 72) lrgst!= i hnc swp A[2] n A[5] giving, 72, 18, 44, 13, 9,, 33, 27, 15, 66 DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin 4 v 17
Hpify(A, 5) is rcursiv cll rorgniz th su-tr th cll to Hpify(A, 1) i = 5 A =, 72, 18, 44, 13, 9,, 33, 27, 15, 66 i = 5; (vlu 13) l = 10; (vlu 15) r = ; (vlu 66) lrgst = ; (vlu 66) lrgst = (vlu 66) lrgst!= i hnc swp A[] n A[5] giving, 72, 18, 44, 66, 9,, 33, 27, 15, 13 Hpify(A, ) hs no ffct on A (A[] is lf no) i = 1 A =, 72, 18, 44, 66, 9,, 33, 27, 15, 13 i = 1; (vlu ) l = 2; (vlu 72) r = 3; (vlu 18) lrgst = 2; (vlu 72) lrgst = 2 (vlu 72) lrgst!= i hnc swp A[1] n A[2] giving 72,, 18, 44, 66, 9,, 33, 27, 15, 13 Hpify(A, 2) is rcursiv cll rorgniz th su-tr i = 2 A = 72,, 18, 44, 66, 9,, 33, 27, 15, 13 i = 2; (vlu ) l = 4; (vlu 44) r = 5; (vlu 66) lrgst = 5; (vlu 66) lrgst = 5 (vlu 66) lrgst!= i hnc swp A[2] n A[5] giving 72, 66, 18, 44,, 9,, 33, 27, 15, 13 Hpify(A, 5) is rcursiv cll rorgniz th NEXT su-tr i = 5 A = 72, 66, 18, 44,, 9,, 33, 27, 15, 13 i = 5; (vlu ) l = 10; (vlu 15) r =; (vlu 13) lrgst = 5; (vlu ) lrgst = 5 (vlu ) lrgst = i hnc no swp giving 72, 66, 18, 44,, 9,, 33, 27, 15, 13 Rturn from th 2 lvls of rcursion n th lgorithm is finish. Explin th sic principls hin hpify Itrt ovr ll th PARENTS i.. lowr(n/2) Compr th prnt LC n RC n mov th lrgst vlu to th prnt Rpt th procss rcursivly for th LC/RC if swp took plc DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin 5 v 17
Pictoril xplntion 13 18 33 72 9 44 27 15 66 13 18 33 72 9 44 27 15 66 13 18 33 72 9 44 27 15 66 13 18 44 72 9 33 27 15 66 13 18 44 72 9 33 27 15 66 13 18 44 72 9 33 27 15 66 13 18 44 72 9 33 27 15 66 72 18 44 66 9 33 27 15 13 72 18 44 66 9 33 27 15 13 72 66 18 44 9 33 27 15 13 DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin 6 v 17
Rmov: Swp th lmnt to rmov with th lst lmnt in th hp n thn rmov th lmnt. R-hpify th rsultnt hp. (S ov). You my us n xmpl. Exmpl: 72, 66, 18, 44,, 9,, 33, 27, 15, 13 lt 72 hpify 13, 66, 18, 44,, 9,, 33, 27, 15 (s ov for hpify) DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin 7 v 17
(4) Rkursion Skriv psuoko till två rkursiv funktionr (lltså n funktion plus n hjälpfunktion) för tt räkn frm ntlt kntr ( gs ) i n grf. Ang ll ntgn. 5p Assumptions: Structur: typf struct nolm * norf; typf struct nolm { chr nnm; int ninfo; norf gs; norf nos; nolm; + corrsponing gt/st functions pr ttriut n h/til oprtions for oth th no list (nos) n th g list (gs). G is rfrnc to th grph, th is_mpty(r) function is fin. sttic int _ngs(norf E) { rturn is_mpty(e)? 0 : 1 + _ngs(til(e)); sttic int _siz(norf G) { rturn is_mpty(g)? 0 : _ngs(gt_gs(nh(g))) + _siz(ntil(g)); DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin 8 v 17
(5) Diskussionsuppgift V mns m implnttionsstrktion? Prsntr för- och ncklr till implmnttionsstrktion. Lr t frm till tt mn proucrr ättr ko? Diskutr ingån. G ll ntgn. Dfinition 3: IMPLEMENTATION ABSTRACTION Th procss of slcting crtin proprtis of Dt Typ inpnnt of th implmnttion of tht Dt Typ - hnc th xprssion Astrct Dt Typ Exmpl 3: A squnc my implmnt using 5p n rry of vlus n n inx to tht rry rrys (vlu, nxt) n n inx rcors/structurs n pointrs ( link list) Avntgs Th implmnttion is hin from most of th co th xcption is o th finition of th NULLREF vlu (-1 for rrys, NULL for pointrs) o th gt/st functions, on pr ttriut o th crt_lmnt function th implmnnttion cn mor sily chng (rry link lists or vic vrs) th rst of th co coms mor strct slf-ocumnting function nms my thn chosn th co coms shortr (spcilly whn comin with rcursion n mor functionl styl of progrmming) o.g. sttic voi pos(int fvl, int fpos) { movtoposition(fpos); link_in(crt_(fvl)); o.g. Disvntgs sttic listrf _fin_vl(listrf L, int v) { rturn (is_mpty(l) (v==gt_vlu(h(l))))? L : _fin_vl(til(l), v); Th tchniqu rquirs mor thought in th ginning Bttr co? Is topic for iscussion n rgumnt Mrks for goo rgumnts. DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin 9 v 17
(6) AVL-Trä Vis hur Du skull t frm 4 rottionsfunktionrn från först principr. 2p An AVL-tr is BST (Binry Srch Tr) whr th lnc iffrnc (lnc fctor) twn th hight of th lft chil n th hight of th right chil my t most 1. i.. hight(lc) hight(rc) <= 1 Th rottion functions r o SLR (Singl Lft Rottion) o SRR (Singl Right Rottion) o DLR (Doul Lft Rottion) or Right-Lft Rottion o DRR (Doul Right Rottion) or Lft-Right Rottion o Th rottion functions my riv using fw simpl xmpls. () () (c) 10 10 10 14 14 () n () rquir SLR to rlnc whil (c) rquirs DLR (right-lft rottion) to giv 10 10 14 10 14 10 An th co coms (SRR n DRR r mirror imgs of SLR n DLR rspctivly) sttic trrf SLR(trrf T) { trrf RT = RC(T); st_rc(t, LC(RT)); st_lc(rt, T); rturn RT; sttic trrf SRR(trrf T) { trrf RT = LC(T); st_lc(t, RC(RT)); st_rc(rt, T); rturn RT; sttic trrf DLR(trrf T) { st_rc(t, SRR(RC(T))); rturn SLR(T); sttic trrf DRR(trrf T) { st_lc(t, SLR(LC(T))); rturn SRR(T); DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin 10 v 17
This is nothr wy of writing wht ws in th nots Trrf RottLft(n2) n1 = n2.right n2.right = n1.lft n1.lft = n2 rturn n1 n RottLft Trrf RottRight(n2) n1 = n2.lft n2.lft = n1.right n1.right = n2 rturn n1 n RottRight Is implmnt s (using th st n gt functions) /* RottLft */ /* n2 = T n n1 = RT */ sttic trrf SLR(trrf T) { trrf RT = RC(T); st_rc(t, LC(RT)); st_lc(rt, T); rturn RT; /* RottRight */ sttic trrf SRR(trrf T) { trrf RT = LC(T); st_lc(t, RC(RT)); st_rc(rt, T); rturn RT; Förklr v lnsfktorn (lnc fctor) är och nvän nn för tt t frm lnsfunktionn från först principr. Skriv lnsfunktionn i psuoko. Dfin th lnc fctor r = hight(lc(t) hight(rc(t)) Now you cn ci which su-tr is th highst. This cis whthr th rottion is lft or right. Thn look t tht su-tr to ci if th imlnc is on th insi ( oul rottion) or outsi ( singl rottion). DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin v 17
Tillämp Din psuoko på trät nn 3p 50 38 55 30 40 45 From th igrm, th lft chil is clrly highr thn th right chil Th right chil of th lft chil is highr tht th lft chil of th lft chil inicting possil ition of 45 i.. to th INSIDE of th lft chil of 50 hnc DRR is rquir. NB lt 60 from th ov tr + 60 woul giv th sm rquirmnt. DRR = SLR (38) thn SRR(50) to giv 50 40 55 38 45 30 40 38 50 30 45 55 sttic trrf DRR(trrf T) { st_lc(t, SLR(LC(T))); rturn SRR(T); sttic trrf SLR(trrf T) { trrf RT = RC(T); st_rc(t, LC(RT)); st_lc(rt, T); rturn RT; sttic trrf SRR(trrf T) { trrf RT = LC(T); st_lc(t, RC(RT)); st_rc(rt, T); rturn RT; SLR(T=38) RT = 40; RC(38) = LC(40) (null); LC(40) = 38; rturn 40; SRR(T=50) RT = 40; LC(50) = 45; RC(40) = 50; rturn 40; DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin v 17
(7) Kruskls lgoritm Totlt 5p Bskriv principrn kom Kruskls lgoritm. V lir rsulttt v n tillämpning v lgoritmn? Använ grfn nn (orikt) som xmpl. (,20,), (,36,c), (,34,), (,22,c), (,24,), (c,28,), (c,30,), (c,38,f), (,26,f), (,36,f) Us n xmpl to show how Kruskl s works 3p 20 34 20 24 36 22 c 28 30 38 36 f 26 24 22 28 c 26 f 1. Construct priority quu (PQ) with th gs lowst vlu first -20-; -22-c; -24-; -26-f; c-28-; c-30-; -34-; -36-c; -36-f; c-38-f; 2. Rmov ll gs from th grph n consir ch no s componnt of th grph 3. Choos n g from th PQ which conncts 2 istinct componnts. -20- conncts componnt to giving componnt -20-. -22-c conncts -20- to c giving componnt -20-, -22-c c. -24- conncts -20-, -22-c to giving componnt -20-, -22-c, 24-. -26-f conncts componnt to f giving -26-f. c-28- conncts componnt -20-, -22-c, 24- to -26-f f. th MST (Miniml Spnning Tr) hs now n foun (s pictur ov) Hur skull mn kunn npss iér från Kruslls lgoritmn för tt t frm n huristik för tt g n lösning till t rsn försäljr-prolmt (Trvlling Slsmn Prolm)? Us vrint of Kruskl y ing n xtr conition tht no no my hv gr grtr thn 2 n tht no gs xcpt th lst my rsult in cycl. 2p Totlt 5p DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin 13 v 17
(8) Dijkstr + SPT (Shortst Pth Tr) Tillämp n givn Dijkstr_SPT lgoritmn (nn) på n rikt grfn, (,, ), (,, ), (,, 9), (, c, 7), (c,, 5), (, c, 3), (,, 1) 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 Din räkningr. Ang *ll* ntgnn och vis *ll* räkningr och mllnrsultt Rit vrj stg i konstruktionn v SPT:t vs vis till och m nor och kntr som läggs till mn sn ts ort. (3p) Förklr principrn kom Dijkstrs_SPT lgoritm. Dijkstrs lgoritm m n utökning för SPT (2p) Totlt 5p Dijkstr_SPT ( ) { S = { for (i in V-S) { D[i] = C[, i] --- initilis D - (g cost) E[i] = --- initilis E - SPT (g) L[i] = C[, i] --- initilis L - SPT (cost) for (i in 1..( V -1)) { choos w in V-S such tht D[w] is minimum S = S + {w forch ( 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] Cost Mtrix c 9 7 c 5 3 1 DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin 14 v 17
Initilis D, E, L D: 9 E: L: 9 w is (min vlu in D) S = {, V-S = {,c, v = min (D[], D[]+C (,)) min(, 9+ ) no chng v = c min (D[c], D[]+C (,c)) min(, 9+ ) no chng v = min (D[], D[]+C (,)) min(, 9+ ) no chng D: 9 E: L: 9 7 3 1 9 c 5 ---------------------------------------------------------------------------------------------------------------- D: 9 E: L: 9 w is (min vlu in D) S = {,, V-S = {, c v = min (D[], D[]+C (,)) min(, + ) no chng v = c min (D[c], D[]+C (,c)) min(, +3) chng --c 14 D: 14 9 E: L: 3 9 7 3 1 9 c 5 ---------------------------------------------------------------------------------------------------------------- DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin 15 v 17
D: 14 9 E: L: 3 9 w is (min vlu in D) S = {,,, V-S = {c v = c min (D[c], D[]+C (,c)) min(14, +7) no chng D: 14 9 E: L: 3 9 7 9 3 1 c 5 ---------------------------------------------------------------------------------------------------------------- This is th finl rsult. Costs: (), c (14), (), (9) SPT gs: (), c (3), (), (9) Principl mrks for goo xplntion! 7 9 7 9 7 9 9 3 1 3 1 3 1 7 3 1 c c 5 5 5 5 c c DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin 16 v 17
Bilg A Hp Algoritmr Hpify(A, i) l = Lft(i) r = Right(i) if l <= A.siz n A[l] > A[i] thn lrgst = l ls lrgst = i if r <= A.siz n A[r] > A[lrgst] thn lrgst = r if lrgst!= i thn swp(a[i], A[lrgst]) Hpify(A, lrgst) n if n Hpify Buil(A) for i = [A.siz / 2] ownto 1 o Hpify(A, i) n Buil Rmov (H, r) lt A = H.rry A[r] = A[A.siz] A.siz-- Hpify(A, r) n Rmov A (H, v) lt A = H.rry A.siz++ i = A.siz whil i > 1 n A[Prnt(i)] < v o A[i] = A[Prnt(i)] i = Prnt(i) n whil A[i] = v n A DFR Dtstrukturr och lgoritmr, DAV B03, tntmn 1501 Fcit Sin 17 v 17