You are on page 1of 11

i hc Quc Gia TP.

H Ch Minh Trng i hc Bch Khoa Khoa: Khoa hc & K thut My tnh B mn: Khoa hc My tnh

THI CUI HC K 1 Nm hc: 2011 2012 Mn: Cu trc d liu & Gii thut MSMH: 503001 Ngy thi: 07/01/2012 - Thi gian: 120 pht (c s dng ti liu)

Lu : thi gm 4 cu vi thang im 11/10. Cu 1: (1.5 im) Cho mt dy gm 11 s nh sau: 6, 12, 4, 19, 22, 3, 2, 15, 13, 17, 30. a. (1 im) Cy AVL c xy dng da trn chui s ny, gi s cc s c nhp vo khi xy dng cy ng theo th t trn. Hy v cy AVL ti cc trng thi chn (insert) thnh cng ln lt cc phn t: 22, 2, 13, 17 v 30. +22 6 4 12 19 22 2 4 3 12 +2 6 19 22

+13 6 3 2 4 12 13 15 +30 15 6 3 2 4 12 13 17 19 22

+17 6 3 2 4 12 13 17 15 19 22

19 22 30

b. (0.5 im) Gi s cy AVL c xy dng cu a ln lt b xa cc phn t sau: 17, 13, 22, 19. V li cc cy AVL sau khi tng phn t trn b xa khi cy.

-17 15 6 3 2 4 12 13 19 22 30 2 3 4

-13 15 6 12 19 22 30

Trng hp using largest node in the left -subtree: -22 15 6 3 2 4 12 19 30 2 3 4 12 -19 6 15 30

Trng hp using smallest node in the right -subtree -22 15 6 3 2 4 12 19 30 2 3 4 12 -19

6 15 30

Cu 2: (2.5 im) Ta k hiu L(H) l dng trnh by ca max-heap H di dng dy.

V d: Mt dy s ban u nh sau: (1, 2, 3), sau khi xy dng thnh heap HT, th L(HT) = (3, 1, 2). a. (1 im) Vi dy s ban u gm 11 s nh sau: (6, 12, 4, 19, 22, 3, 2, 15, 13, 17, 30), ta xy dng thnh heap H1. Gi s cc s c a vo H1 theo ng th t trn. Cho bit L(H1). Solution: L(H) = (30,22,4,15,19,3,2,6,13,12,17) b. (0.5 im) Sau khi loi b 3 phn t khi H1, ta c H2. Cho bit L(H2). Solution: L(H_2) = (17,15,4,13,12,3,2,6) c. (1 im) Cho cu trc d liu heap vi cc phng thc sn c nh hnh 1. Hin thc thm phng thc convert2MaxHeap chuyn i heap t min-heap thnh max-heap. Lu : Do s lng phn t n ca heap l kh ln, gii thut sp xp ny khng c khai bo thm bin ph c dung lng lu tr tng ng vi n (v d nh khai bo mt dy tm c ln l n/10 v n/10 v n l tng ng). Class heap Private: data: array<elementstype> count: int protected: reMaxHeapUp(position) reMaxHeapDown(position, lastPosition) reMinHeapUp(position) reMinHeapDown(position, lastPosition) Public: insertMaxHeap(element) insertMinHeap(element) deleteMaxHeap() deleteMinHeap() End class Hnh 1: Cu trc d liu heap Solution: void convert2MaxHeap() { for(int position=count/2-1; position>=0; position--){ reMaxHeapDown(position, count-1); } } Cu 3: (2 im) Xt dy s: 6, 12, 4, 19, 22, 3, 2, 15, 13, 17, 30. Dnh cho lp thng (khng cn lm cu b v cu c): a. (2 im) V bng biu din cc bc thc thi da theo cc thut ton sau: straight selection sort, bubble sort v merge sort. (Sinh vin xem v d bng biu din cc bc thc thi trong hnh 2). 3

6 6 4 4 4 3 2 2 2 2 2

12 12 6 6 6 4 3 3 3 3 3

4 4 12 12 12 6 4 4 4 4 4

19 19 19 19 19 12 6 6 6 6 6

22 22 22 22 22 19 12 12 12 12 12

3 3 3 3 3 22 19 15 13 13 13

2 2 2 2 2 2 22 19 15 15 15

15 15 15 15 15 15 15 22 19 17 17

13 13 13 13 13 13 13 13 22 19 19

17 17 17 17 17 17 17 17 17 22 22

30 30 30 30 30 30 30 30 30 30 30

Hnh 2. Bng lit k cc bc thc thi ca gii thut Straight Insertion Sort Solution: Straight selection sort: 6 12 4 19 22 3 2 15 13 17 30 2 12 4 19 22 3 6 15 13 17 30 2 3 4 19 22 12 6 15 13 17 30 2 3 4 19 22 12 6 15 13 17 30 2 3 4 6 22 12 19 15 13 17 30 2 3 4 6 12 22 19 15 13 17 30 2 3 4 6 12 13 19 15 22 17 30 2 3 4 6 12 13 15 19 22 17 30 2 3 4 6 12 13 15 17 19 22 30 2 3 4 6 12 13 15 17 19 22 30 2 3 4 6 12 13 15 17 19 22 30 Bubble sort: 6 12 4 19 2 6 12 4 2 3 6 12 2 3 4 6 2 3 4 6 2 3 4 6 2 3 4 6 2 3 4 6 2 3 4 6 2 3 4 6 2 3 4 6 Merge sort: 6 12 4 4 6 12 3 4 6 3 4 6 3 4 6

22 19 4 12 12 12 12 12 12 12 12

3 22 19 13 13 13 13 13 13 13 13

2 3 22 19 15 15 15 15 15 15 15

15 13 13 22 17 17 17 17 17 17 17

13 15 15 15 19 19 19 19 19 19 19

17 17 17 17 22 22 22 22 22 22 22

30 30 30 30 30 30 30 30 30 30 30

19 19 12 12 12

22 22 19 19 19

3 3 22 22 22

2 2 2 2 2

15 15 15 13 13

13 13 13 15 15

17 17 17 17 17 4

30 30 30 30 30

2 3

12 13 15 17 19 22 30

Dnh cho lp KSTN (khng cn lm cu a): b. (1 im) V bng biu din cc bc thc thi da theo hai trong cc thut gii c lit k trong cu a thc hin vic sp xp. c. (1 im) Da trn thut gii quicksort c trnh by trong b slide l thuyt ca mn hc, vit thut gii tr v phn t ln th k trong mt danh sch n phn t phn bit (c lu tr trong mt mng cho trc). phc tp trong trng hp xu nht c phi l O(n log n) ? Nu khng, hy xut gii php ci tin c c phc tp ny. Solution: Algorithm FindKth(k<int>) If(k > count) return ; recursiveFindKth(0,count-1,k) End FindKth Algorithm recursiveFindKth(int lowIndex, int highIndex, int k) if(lowIndex >= highIndex) return lowIndex ; // Divide data into 2 parts and return index of pivot pivot_position= Partition(lowIndex, highIndex) if(pivot_position == k-1) return data[pivot_position] ; // Pivot is the kth maximum value if(pivot_position > k-1) // just run recursive on the first division return recursiveFindKth (lowIndex, pivot_position-1,k) else return recursiveFindKth (pivot_position+1, highIndex,k) End recursiveFindKth Trong trng hp xu nht : k = n v phn t pivot c chn lun l phn t cc i hoc cc tiu trong mi partition. Khi s nh trng hp xu nht ca gii thut Quicksort l 0(n^2), khng phi 0(nlogn). Ci tin : Gii thut Quicksort trong slide bi ging chn gi tr pivot v tr gia trong dy d liu. Trong trng hp xu nht, nu phn t pivot lun c gi tr cc i hoc cc tiu bn trong dy d liu th gii thut Quicksort s phi thc hin xp x n ln hm Partition. Trong khi chi ph mi ln gi Partition phn tch d liu xp x n. Do phc tp l O(n^2). ci tin ta phi c gng gim s ln thc thi hm Partition => chn phn t pivot c gi tr gia t c xp x logn ln gi Partition.

=> Thm vo bn trong hm Partition ca gii thut Quicksort phn tin x l, xc nh phn t c chn l pivot chnh l phn t c gi tr gn vi gi tr trung bnh ca cc gi tr c trong danh sch. Int findPivotPosition(int low, int high) { int sum =0 ; int count = high low + 1 ; fore(i from low to hight) { sum+=data<i> ; } Int position = low; For(i from low to high) { if(|data<position> * count sum| > |data<i> * count - sum|) { position = i ; } } } int Partition(int low, int high) { // last_smallis the position of the last key less than pivot swap (low, findPivotPosition(low,hight)) // First entry is now pivot. pivot = entrylow last_small= low i= low+ 1 loop(i<= high) if(datai< pivot) { last_small= last_small+ 1 swap(last_small, i) // Move large entry to right and small to left. } swap(low, last_small) // Put the pivot into its proper position. return last_small } phc tp ca hm tm findPivotPosition l O(n) v vy phc tp ca c hm partition l O(n). Cu 4: (3 im) Xt bi ton v vic cp nht thng tin gia cc chi nhnh ng cp trong mt t chc. D liu c th v mt mng li truyn tin nh sau: AB(6), AC(3), AE(9), AJ(6), BD(4), CD(2), CI(1), DE(4), DF(3), DH(7), EG(3), FG(6), FH(8), GK(4), HJ(1), HK(3), IJ(5), JK(2). Trong XY(n) c ngha l thi gian truyn tin t X n Y mt n n v thi gian v ngc li. a. (1 im) Biu din ma trn lin k ca th cho. Da trn th cho, xy dng v biu din ma trn lin k ca th con tha mn iu kin sau: - Cha tt c cc nh ca th ban u - C duy nht mt thnh phn lin thng

- Tp cnh l tp con ca tp cnh ban u v tng thi gian ca tp cnh ny l nh nht. S cnh ca th con ny l bao nhiu ? Chng minh th ny khng bao gi xut hin chu trnh. Solution: Biu din ma trn lin k ca th cho: A 0 6 3 0 9 0 0 0 0 6 0 B 6 0 0 4 0 0 0 0 0 0 0 C 3 0 0 2 0 0 0 0 1 0 0 D 0 4 2 0 4 3 0 7 0 0 0 E 9 0 0 4 0 0 3 0 0 0 0 F 0 0 0 3 0 0 6 8 0 0 0 G 0 0 0 0 3 6 0 0 0 0 4 H 0 0 0 7 0 8 0 0 0 1 3 I 0 0 1 0 0 0 0 0 0 5 0 J 6 0 0 0 0 0 0 1 5 0 2 K 0 0 0 0 0 0 4 3 0 2 0

A B C D E F G H I J K

Vi nhng yu cu ca th con th th con cn tm l cy ph ti thiu ca th ban u. p dng gii thut Prim t m cy ph ti thiu trn th v hng. Ta c c th cn tm (theo ma trn lin k bn di). A 0 0 3 0 0 0 0 0 0 6 0 B 0 0 0 4 0 0 0 0 0 0 0 C 3 0 0 2 0 0 0 0 1 0 0 D 0 4 2 0 4 3 0 0 0 0 0 E 0 0 0 0 0 0 3 0 0 0 0 F 0 0 0 3 0 0 0 0 0 0 0 G 0 0 0 0 3 0 0 0 0 0 4 H 0 0 0 0 0 0 0 0 0 1 0 I 0 0 1 0 0 0 0 0 0 0 0 J 0 0 0 0 0 0 0 1 0 0 2 K 0 0 0 0 0 0 4 0 0 2 0

A B C D E F G H I J K

I F B

Vi cy ph ti thiu ca th v hng, s cnh bng |V| -1 vi V l tp cc nh ca th. Nn trong trng hp ny, s cnh bng 10. V theo nh ngha cy ph, th thu c khng c chu trnh.

b. (0.5 im) Cho bit th t duyt cc nh ca th trong cu a tng ng vi cc phng php depth-first traveral v breadth-first traveral. Lu rng trong trng hp nhiu nh c th c chn, cc nh s c chn theo th t trong bng ch ci. Solution: Theo th t bng ch ci, root trong qu trnh duyt s bt u t A Depth-first ordering ACDBEGKJHF-I Breadth-first ordering ACDIBEFGKJH Dnh cho lp thng (khng cn lm cu d): c. (1.5 im) ng vi th trong cu a, vit gii thut xc nh thi gian cn thit truyn thnh cng mt thng ip t chi nhnh X n tt c cc chi nhnh cn li (vi X c cho trc). Solution: th thu c t cu a l cy ph ti thiu nn khng c chu trnh hay gia 2 nh bt k s c duy nht mt ng i.Nu l thi gian cn thit truyn thnh cng thng ip t X ti mi chi nhnh khc s bng: max( thi gian truyn t X n cc node l). Ti mi nh ta lu tr thng tin: max: cha gi tr thi gian cn thit truyn t nh ti tt c cc nh con. maxChild: cha nh con tng ng vi ng i c gi tr thi gian max. int findTime(X) { int max = 0; recursiveFindTime1(X); return X.max; } Void recursiveFindTime1(Vertex v) { Foreach(Vertex w adjacent to v) { recursiveFindTime1(w); int newVal = w.max + weight(w,v); if(v.max < newVal) { v.max = newVal; v.maxChild = w; } } 8

} Dnh cho lp KSTN (khng cn lm cu c): d. (1.5 im) ng vi th con thu c cu a, xc nh thi gian t cn thit truyn thnh cng mt thng ip t mt chi nhnh bt k n tt c cc chi nhnh cn li. d1. (0.5 im) Vit gii thut tnh s t ny. d2. (1 im) Do hn ch v thi gian tnh ton, yu cu thut gii c phc tp ti a l O(n) vi n l s lng thnh ph (nu thut gii xut cu d1 cha t c O(n) th cn xut gii thut ci tin). Solution: V th cu a l cy ph ti thiu (khng c chu trnh) nn gia 2 nh bt k s c duy nht mt ng i. V vy thi gian cn thit truyn t nhnh bt k n tt c cc nhnh cn li l Max(ng ni gia 2 nh bt k). int findTime() { Int max = 0; Vertex X in Graph; // X is any Vertex in Graph recursiveFindTime1(X); recursiveFindTime2(X); return X.tmpMax; } // recursiveFindTime1(v) : dng xc nh thi gian cn thit truyn tin t chi nhnh v n cc nh con ca n (hay ni cch khc xc nh n cc node l con) Void recursiveFindTime1(Vertex v) { /* Duyt DFS bt ngun t v Pre: Subroot trong qu trnh duyt DFS Post: Mi nh s cha cc gi tr max v childMax nh li gii cu c. max: cha gi tr thi gian cn thit truyn t nh ti tt c cc nh con. maxChild: cha nh con tng ng vi ng i c gi tr thi gian max. */ Foreach(Vertex w adjacent to v) { recursiveFindTime1(w); int newVal = w.max + weight(w,v); if(v.max < newVal) { v.max = newVal; v.maxChild = w; } } } // recursiveFindTime2 dng xc nh thi gian cn thit truyn tin t 1 node l con bt k ca v n mt node l con bt k khc ca v (v l t tin chung u tin ca chng). Thut gii ny s cn tha k kt qu ca thut ton recursiveFindTime1. 9

hin thc thut gii ny, ti mt node ch cn xc inh hai nhnh con c thi gian truyn tin xa nht ri tnh tng ca chng. Sau mi ln duyt thut ton ny ti mi node trong th, ta cn cp nht xc inh thi gian cn thit truyn tin t 1 node (l con) bt k n mt node l con bt k khc. Void recursiveFindTime2(Vertex v) { Foreach(Vertex w adjacent to v) { recursiveFindTime2(w); w.tmpMax = w.max; Foreach(Vertex u adjacent to w and u != w.maxChild) { If(w.tmpMax < w.maxChild.max + u.max) { w.tmpMax = w.maxChild.max + u.max; } } If(v.tmpMax < tmpMax) v.tmpMax = w.tmpMax; } } Cu 5: (2 im) Cho mt dy gm 11 s nh sau: 6, 12, 4, 19, 22, 3, 2, 15, 13, 17, 30 a. (1 im) Xy dng hai cy B-tree tng ng vi dy s trn, vi s bc ca hai cy ny l 3 v 5. Gi s th t cc phn t c a vo cy tng ng vi th t trn dy. Solution: B-Tree vi bc 3: Root hoc l l hoc c t nht 2 cy con Mi inner node c t nht 1 entry Cc l c cng level 6 3 13 19

12

15

17

22

30

B-Tree vi bc 5 Root hoc l l hoc c t nht 2 cy con Mi inner node c t nht 2 entry Cc l c cng level 12 17 19 22 30

13

15

10

b. (1 im) V li hai cy B-Tree c xy dng cu a sau khi xa phn t 15 ra khi hai cy ny. 6 3 13 19

12

17

22

30

12 19 22 30

13

17

-- Ht --

11