Krlsts univrsitt Fit till DSA omtntmn 08 Dtvtnskp FACIT TILL OMTENTAMEN I DATASTRUKTURER OCH ALGORITMER DVG B0 08 kl. 08: : Ansvrig Lärr: Donl F. Ross Hjälpml: Ing. Algoritmrn inns i rspktiv uppgitrn llr i ilogrn. *** OBS *** Btygsgräns: Kurs: Mx 0p, M röm gokän 0p, Ik utn röm gokän 0p, Gokän 0p (vrv minimum 0p rån tntn, 0p rån lrn) Tnt: Mx 0p, M röm gokän p, Ik utn röm gokän 7p, Gokän 0p Lrn: Mx 0p, M röm gokän 8p, Ik utn röm gokän p, Gokän 0p SKRIV TYDLIGT LÄS UPPGIFTERNA NOGGRANT Ang ll ntgnn. () G tt kortttt svr till öljn uppgitr (()-(j)). () V skull tt ltrntiv ör Floys lgoritm vr? Apply Dijkstr s lgorithm to h no in th grph. () V är n rkursiv inition? A inition whih is PARTIALLY in in trms o itsl. Exmpl squn: Squn ::= H Til mpty H ::= lmnt Til ::= Squn () V är n rkursiv unktion? A untion whih CONDITIONALLY lls itsl Exmpl int siz(sq) { rturn is_mpty(sq) 0 : + siz(til(sq)); () G n inition v tt st (n mäng)? An unorr olltion o uniqu lmnts. DFR Dtstrukturr oh lgoritmr, DAV B0, Rsit 08 FACIT Sin v
Krlsts univrsitt Fit till DSA omtntmn 08 Dtvtnskp () V är Big-Oh? A prormn initor usully o th lngth o running tim n lgoritm rquirs s untion o th numr o lmnts to pross. Exmpl: Dijkstr O(n ) () Prims oh Dijkstrs lgoritmr är girig lgoritmr. V tyr girig? Girig = gry - mning tht th lgorithm pplis th LOCALLY st solution. (g) V är skillnn mlln ornt oh sortrt? Ornt orr mns tht th olltion is viw s squn n h lmnt hs position s n ttriut. Exmpl: n orr tr th hilrn o th prnt r viw s squn. (th ltrntiv is to viw th hilrn s st in whih s th tr is unorr) Sortrt sort th lmnts o th olltion r orr oring to thir vlu- Exmpl squn: 9 7 position: (h) V gör topologisk sortring? Givn DAG (Dirr Ayli Grph), rturns PARTIAL ORDER o th grph (whih is squn). (i) V är n hp? A t strutur whih i implmnt s inry tr, pls th lrgst/smllst vlu o th prnt n hilrn (i thy xist) in th prnt. Th lrgst/smllst vlu in th olltion is thus in th root. I implmnt s n rry, th lrgst/smllst vlu is in th irst lmnt. (j) Hur ungrr oprtion hpiy på n hp? Hpiy(A, i) l = Lt(i) // l = *i r = Right(i) // l = *i + i l <= A.siz n A[l] > A[i] thn lrgst = l ls lrgst = i i r <= A.siz n A[r] > A[lrgst] thn lrgst = r i lrgst!= i thn swp(a[i], A[lrgst]) Hpiy(A, lrgst) n i n Hpiy Or txtul xplntion. Totlt p DFR Dtstrukturr oh lgoritmr, DAV B0, Rsit 08 FACIT Sin v
Krlsts univrsitt Fit till DSA omtntmn 08 Dtvtnskp () ADT Squn Oprtionr på n skvns kn implmntrs ntign på tt itrtivt sätt llr på tt rkursivt sätt. Om mn implmntrr oprtionrn på tt itrtivt sätt nväns otst två pkr, nämlign pprvious oh pcurrnt. pcurrnt pkr på t ktull lmntt i skvnsn oh pprvious pkr på t örgån lmntt till pcurrnt (om sånt xistrr). liststrt pkr på t örst lmntt i skvnsn. Ant tt skvnsn är stortr i stign orning oh implmntr som n nkl länk list. listr är n pkrtyp som är n rrns till tt lmnt i skvnsn. () Skriv strkt itrtiv (psuo)ko till oprtionr voi link_in(listr pnw) smt voi unlink() är pnw är n pkr till t ny lmntt som mn sk sätt in i skvnsn mlln pprvious oh pcurrnt. voi unlink() tr ort pcurrnt rån skvnsn. (p) () Skriv strkt itrtiv (psuo)ko till (lägg till tt lmnt) oprtion. G xmpl v hur in ko ungrr (i) när mn läggr till tt vär i örjn v skvnsn, (ii) när mn läggr till tt vär i mittn v skvnsn oh när mn läggr till tt vär i slutt v skvnsn. Använ unktionn voi link_in(listr pnw). Skriv strkt itrtiv (psuo)ko till ll hjälp unktionr som u hövr. (p) () Skriv strkt itrtiv (psuo)ko till oprtionr in (hitt tt lmnt i skvnsn) smt rmov (t ort tt lmnt rån skvnsn). Vis hur u kn inkorporr oprtion in i oprtion rmov. Använ unktionn voi unlink() i rmov. Skriv strkt itrtiv (psuo)ko till ll hjälp unktionr som u hövr. (p) Th ollowing hlp untions r rquir (you my ssum ths xist) stti int gt_vlu (listr E) { rturn E->vlu; stti listr gt_nxt (listr E) { rturn E->nxt; stti voi st_vlu (listr E, int v) { E->vlu = v; stti voi st_nxt (listr E, listr n) { E->nxt = n; stti listr rt_ (int vl) { listr pnw = (listr) mllo(sizo(listlm)); st_vlu(pnw, vl); st_nxt (pnw, NULLREF); rturn pnw; stti int is_mpty(listr E) { rturn E == NULLREF; Totlt 0p DFR Dtstrukturr oh lgoritmr, DAV B0, Rsit 08 FACIT Sin v
Krlsts univrsitt Fit till DSA omtntmn 08 Dtvtnskp For link_in, unlink,, in n rmov, th ollowing hlp untions r rquir stti listr gt_urr_r() { rturn pcurrnt; stti int is_sq_mpty() { rturn is_mpty(pcurrnt); stti int gt_lmnt_vlu() { rturn gt_vlu(pcurrnt); stti voi gt_sq_irst() { pprvious = NULLREF; pcurrnt = liststrt; stti voi gt_sq_nxt () { i (!is_sq_mpty()) { pprvious = pcurrnt; pcurrnt = gt_nxt(pcurrnt); /* link_in n unlink oprtions */ stti voi link_in(listr pnw) { i (!is_mpty(pnw)) { st_nxt(pnw, pcurrnt); i (is_mpty(pprvious)) liststrt = pnw; ls st_nxt(pprvious, pnw); stti voi unlink() { i (!is_mpty(pcurrnt)) { i (is_mpty(pprvious)) liststrt = gt_nxt(pcurrnt); ls st_nxt(pprvious, gt_nxt(pcurrnt)); /* oprtion */ stti voi vl(int vl) { gt_sq_irst(); whil ((!is_sq_mpty()) && (vl > gt_lmnt_vlu())) gt_sq_nxt(); link_in(rt_(vl)); /* in oprtion */ stti listr _in_vl(int vl) { gt_sq_irst(); whil ((!is_sq_mpty()) && (vl!= gt_lmnt_vlu())) gt_sq_nxt(); rturn gt_urr_r(); /* rmov oprtion */ stti voi _rm_vl(int vl) { _in_vl(vl); unlink(); DFR Dtstrukturr oh lgoritmr, DAV B0, Rsit 08 FACIT Sin v
Krlsts univrsitt Fit till DSA omtntmn 08 Dtvtnskp () Algoritmr - Prinipr. Använ gärn ilr i Ditt svr till (), () oh () nn. Använ n orikt grn: ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ) () Bskriv priniprn kom Dijkstrs lgoritm (OBS: int Dijkstr_SPT). (p) Givn strt no (x), hoos th shortst g rom tht no to no y. Mrk nos x n y s visit. Chk to s i thr is shortr pth to th rmining (unvisit) nos in th grph rom x vi y. I so, upt th pth lngths so r lult. Rpt th pross until ll nos hv n visit. y Th prinipl is:- x z Grph SPT Explntion - == ininity i.. no g. strt no hpst ( ) initil osts [,,,, ] visit {, unvisit {,,, ( ) not hpr; ( ) not hpr; ( 7) hpr; ( ) hpr [,,, 7, ] hpst ( ) visit {,, unvisit {,, ( ) not hpr; ( ) not hpr; ( 7) not hpr; no hng [,,, 7, ] hpst ( ) visit {,,, unvisit {, ( ) not hpr; ( ) not hpr; no hng [,,, 7, ] hpst ( ) visit {,,,, unvisit { ( 9) not hpr; no hng [,,, 7, ] hpst ( 7) visit {,,,,, unvisit { - mpty STOP! SPT ( ) ( ) ( ) ( + 7) ( + ) DFR Dtstrukturr oh lgoritmr, DAV B0, Rsit 08 FACIT Sin v
Krlsts univrsitt Fit till DSA omtntmn 08 Dtvtnskp In piturs (s ov) this oms Initil onigurtion, strt no [,,,, ] whr = ininit ost -- is th hpst g hk to s i thr is hpr PATH rom vi to nos {,,, i.. th unvisit nos rom vi to {,,, givs [,,, 7, ] i.. pth lngths rom to {,,,, th pths r:- --, --, --, ----, ---- From [,,, 7, ] visit = {,, unvisit = {,,,. In orr -- is th hpst pth hn try to in hpr pths rom -- to {,, --- - ( ) is NOT hpr thn -- () sin thr is no g - --- - ( ) is NOT hpr thn ---- (7) sin thr is no g - ---- (7) is NOT hpr thn ---- () Hn thr is no hng rom this itrtion n visit = {,,, unvisit = {,, A similr rgumnt pplis to th nxt itrtion: ---- () is th nxt hpst pth. Visit = {,,, unvisit = {, Tst pths to {, vi ----- - ( ) is NOT hpr thn -- () sin thr is no g - ------ () is NOT hpr thn ---- (7) No hng -- () is th nxt hpst pth visit = {,,,,, unvisit = { ---- (9) is NOT hpr thn ---- (7) No hng ---- (7) is th nxt hpst pth visit ={,,,,, unvisit = { i.. mpty. STOP! No hng n now th rsult is ry. Th pths r:- -- (); -- (); -- (); ---- (7); ---- (); i.. [,,, 7, ] DFR Dtstrukturr oh lgoritmr, DAV B0, Rsit 08 FACIT Sin v
Krlsts univrsitt Fit till DSA omtntmn 08 Dtvtnskp () Bskriv priniprn kom Prims lgoritm. (p) Grph MST Th prinipls: givn strt no x mrk s visit; not th g vlus rom x to th rmining nos; this uss rrys L or th g lngths n C or th no nm; in th shortst g rom x to y; mrk y s visit; uil COMPONENT (x y) i.. y is thn to th omponnt (i.. th visit nos); now xmin th g osts rom y to th rmining nos; i this g is hpr, rpl th urrnt g with this g. Rpt or th unvisit nos. Th omponnt grows no y no n hpr gs rpl thos gs prviously oun s hpr. For th ov grph - == ininity Strt no visit { unvisit {,,,, L = [,,,, ] C = [,,,, ] Shortst g ( ) visit {, unvisit {,,, ( ) is hpr L = [,,,, ] C = [,,,, ] ( ) not hpr no hng ( ) is hpr L = [,,,, ] C = [,,,, ] ( ) is hpr L = [,,,, ] C = [,,,, ] Shortst g ( ) visit {,, unvisit {,, ( ) not hpr no hng ( ) is hpr L = [,,,, ] C = [,,,, ] ( ) not hpr no hng Shortst g ( ) visit {,,, unvisit {, ( ) not hpr no hng ( ) not hpr no hng L = [,,,, ] C = [,,,, ] Shortst g ( ) visit {,,,, unvisit { ( ) is hpr L = [,,,, ] C = [,,,, ] Shortst g ( ) visit {,,,,, unvisit { mpty STOP Rsult L = [,,,, ] C = [,,,, ] DFR Dtstrukturr oh lgoritmr, DAV B0, Rsit 08 FACIT Sin 7 v
Krlsts univrsitt Fit till DSA omtntmn 08 Dtvtnskp DFR Dtstrukturr oh lgoritmr, DAV B0, Rsit 08 FACIT Sin 8 v In piturs, this oms:- Grph Visit = {,, unvisit = {,,, MST Visit = {,,, unvisit = {,, Visit = {,,, unvisit = {, No hng rom ; hng rom Visit = {,,,,, unvisit = {
Krlsts univrsitt Fit till DSA omtntmn 08 Dtvtnskp () Bskriv priniprn kom Kruskls lgoritm. (p) Grph MST Th prinipls: uil priority quu (PQ) with th gs, shortst gs irst ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ) Eh no in th grph oms omponnt [], [], [], [], [], [] Choos n g rom th PQ suh tht th g onnts istint omponnts until thr is only on omponnt this is th MST ( ) [-], [], [], [], [] - omponnts ( ) [-], [], [-], [] - omponnts ( ) [-], [-], [, ] - omponnts ( ) [-, -, -], [-] - omponnts ( ) not hosn - & r in th sm omponnt ( ) [-, -, -, -, -] - omponnt (MST) () V är i. tt SPT (Shortst Pth Tr KVT kortst väg trä) oh ii. tt MST (Miniml Spnning Tr MST - Minimum Spänning Trä). (p) An SPT is tr giving th shortst PATHS rom givn no to th rmining nos in th grph. Th tr my onsir s irt or unirt grph. An MST is tr whih onnts ll th nos in grph in th hpst wy possil. This is FREE TREE n is n unirt grph. Totlt 0p DFR Dtstrukturr oh lgoritmr, DAV B0, Rsit 08 FACIT Sin 9 v
Krlsts univrsitt Fit till DSA omtntmn 08 Dtvtnskp DFR Dtstrukturr oh lgoritmr, DAV B0, Rsit 08 FACIT Sin 0 v In Piturs, this oms
Krlsts univrsitt Fit till DSA omtntmn 08 Dtvtnskp () Hshning Tillämp å linjär proning smt kvrtiskt proning på öljn skvnsn:, 7,, 87, 7, 7, 8, 7, 7, 7 Vilk prolm kn uppstå? Hur lös mn ss prolm? Vilk spktr ör mn t hänsyn till? Ant tt H(ky) är ky mo 0. Svr ingån. Ang ll ntgn. p For th solution s:- http://www.s.ku.s/s/ution/ourss/vg0/rvision/inx.php?hshingxs= () ADT Trä. Bskriv utörligt ll spktr v ADT:n trä som hr prsntrts unr kursns lopp. Points to mntion:- p. Gnrl tr inition + onvrsion gnrt tr to inry tr. Tr proprtis orr n unorr. Binry tr (BT); proprtis; trvrsls: gnrl, pth-irst: in-, pr-, postorr; rth irst, oprtions (, in, rmov); pplitions.g. rithmti xprssions. BT rursiv inition. BT proprtis: ull, prt, omplt. BT & rry rprsnttion 7. Binry Srh Tr (BST); proprtis; 8. AVL Tr; proprtis; lning oprtions; tr rottions 9. B-trs; Dtss; ln ushy trs (srh pth minimis) () Diskussionsuppgit - Astrktion Diskutr utörligt grppt strktion oh hur t hr nvänts unr nn kurs. Vilk sortrs strktion inns? Vrör är strktion så viktigt? Hur nvänr mn strktion när mn skrivr ko till oprtionr på ADT:r? G gärn xmpl. Mrks or goo isussion. p DFR Dtstrukturr oh lgoritmr, DAV B0, Rsit 08 FACIT Sin v