Lee oe gi at con dei beth an aay (La) coins 2 sj element, an iso Ree
— es rogour agit and show the eu sing ender soto (18 pots)
4. What isan tinal Htfnan code forthe following st equence, esd on te fat 8 booze nembes?
ast bit oid d:3 2S #28 ge13 Bs2t
omit your answer od he opin cod wen the fue te isin bons nters plas)WEF,
a Fee OB tr a
as |cem em
BLkPARKE SERB a BRPRRAR ®
BRERA BML RE Tee gue:
AR RR EB Oseeane
(19%) Given an algorithm that determines whether or not & given undirecsed graph G =
(V,B) contains @ cycle. Your algorithm should run in O(V} time, independent of [21
(155%) Show thet a graph G = (V, 2) has a unique minimum spanning tree if, for every cut
| __-__ ‘of the graph, there Is 2 unique light edge crossing the cut. Show thet the converse is not
true by giving & counterexample
3. (20%) (A problem about Dijkstra’s algorithm)
(a) Deseribe Dikstra's algorithm ia words,
oe (B) Applying Dilkstra's elgoitira to the graph chown In Figure 1(a), find all shortast
‘paths from source node s to all nodes.
(6) Applying Dikstes's algorithm to the graph shown in Figure 1(b), find all shortest
paths from source nede » to all nodes.
{€) Discuss the running time of Dijkstr's elgorithm in verms of big-O notation.
Figure 1: Graphs forthe problem related to Dijkstre's algorithm.
ae OF |Puy FPF «®
Fee mle a
ani cem pee a
BLdeARAF RERE hh ARAL a RR OE
BRAS
TTT TT
SRR RR— BIER :
Puatregaatan knee
“Solve the flowing ecurence exactly. (15 polit)
nel? ifn =0,1,2003
tL gates otherwise
Pease give precise defn forthe © notation. The egress Your ane assimply as posiby
sing the © station.
vac acay Teortangn lems, You wan to find he mamas whee ms mh smaller han
fn Would you
(srt T an pick the fst m element oF plats)
(@) use some other method? (10 potas)
“analyze te rong ies of be agit in tems of #84
Sappoee thatch ow ofanmxcnarayA cons sand Se8 a pe of Ayall1's
Saree et rom, Amung A ea a marorys essa eneion AE
sr age) ame for counting te numberof V's 4. 20 pln)
enFal aE ee
elas oe aa
HLePARAS Sane 90 SRORAR oe RF
SEAR RMR RBS EL gael:
“ste TEARERB
oe
&
x
ea
EHP PrEEEHE erro eee
25%)
1. (a) Beifly deseribe the principle of dynamic programming. (0) What is the al
Stil way te compute A, A, Ay A, where the dimensions of the mates ae
TAs 20%, ys 1540, AMOS, Acs 5x30? (Using dynamic programing
approach, Please show the table.)
(5%)
2. Find all the articulation points and the biconnested components inthe following
raph (how the nanviow ofall the vertices, andthe content of stack, (Starting
with node A
a“oer is
# Fl cam) eae a
BL eBanre 74 BERK | BAG ARON KR OTR
Ouse
aesgapa : (avERee A) Chee fatal:
Graeme
MITT
Answer the following.
(a) Please givea precise detition for he big-O notation S points)
(@) Explain wy quicksor is O(7?) ints worst ease, end Ofnogn in average. ( pals)
‘suppore you ae given an aay A of sorted muibers hat hasbeen circularly shied postions
pest, For example, {35,42,5,15,27,29} isa sorted array that has boen circalarly shifted
(ces) pontons, while {27,29,35,42,5, 5} hasbeen cireulely shified k= 4 poston
(@) Suppose you know what Kis. Give an O(1)algocthm to find the largest number A. 0
points)
(6) Suppote you do not know what kis, Give an O(log) sigh to find the largest number
‘4-5 points)
. Suppose that we are given a sequence 5 of m elements, each of which is eolred red or be
aereng Sis represented a5 an array, give ai-place method for ordering So hat all he bloe
‘ements are listed before all here elements. (15 points)
WLwal A AEE
Fee lem
a} (en em
RLEMARAE GU SER | PHY ROMA
Cm a
SRA eR RIB Bags aa:
r Ozeenes
{Close Books and Notes)
est)
1. Hind all he strong components in the following graph (show the dfs number of
all the vertices) (starting with node A.)
5%)
2. (@) Give the definitions of P and NP.
{@) Give the definition of NP-complete.
{0} Which problem asthe historical honor of being the first problems ever shown
to be NP-complete? Deserbe this probiem briefly.
{ho How to prove a given problem to be NP-complete (by the methodology of
reductions)?
{@) Why isi “herd to solve NP-complete problems? Hint: Prom the definition
aa cr a
* Siam jem em
Bi kReere > Sea% > Seis sRoaAK a RRO
SRE UBB Re AK gees ARAL
(Closed Books) i
1. (20%) Given e graph G and 2 minimum spanning tree T, suppose that we decrease the
weight of one of the edges in T. Show that T's still a minimum spanning trve for C.
More formally, lot T be a minimum spanning tro for C with edge weights given by weight
function w. Choose one edge (z,y) € 7’ snd a positive number k, and define the weight
function & by
Joya) ={ 2%) Ea) AG,
vou {ES 2 BODE ES tH
Stow that is «minimum spanning tes for G with edge weights given byw
2, (20%) Give an algoritam that determines whether or not a given unidrected graph @ =
(V,E) contains a cycle. Your algorithm should run in O(V) time, independent of [El
(Hint: By theorem B.2, in an acyclic undirected forest [BS |V|~1. )
3. (10%) Explain how a vertex u of adirected graph can end up in a depth-first tree containing
only u even though u has both incoming and outgoing edges in G.oa (%01) “wety Jo sexxe;duico aoe
(%01) “way Jo sanixajduiod at
(%01) Ww
eee pue yosdeay :wejqoid Bupios om,
:SUU9} BUIMOTIOJ €
oewreso
heey es
ewe WUROREY bl We < EXwe
ae we0 OP ]w &
fe oely wes
ate are |eval 7 eer alo a =
| cam) &
BAERHRAS ® eeRR | BHP SoA me
Fane PEAR E ZAG Gigs | Asal:
Bintuan
(Closed Books)
1. (20%)
{a) Define the following terms: (1) DAG (directed acyclic graph), (2) topologies! sort.
(b) Deseribe an approach to topologically sort a dag by using DFS (depth-first search)
algorithm, You do not need to describe the DES slgorithm in deval, just use it.
(0) What isthe running time of your approach ? Represent it in terms of big-O notation.
(20%) Assume that you heve an algorithm forthe single-source shortest-peths problem.
(6) Show that the following problems can be solved by the single-source shortest-paths
algerithen: (1) Singlo- destination shorvest-paths problem, (2) Single-pair shortest-patlt
problem, (3) All-peireshortest-pathe problema. You do not need to run the algorithm
fn detail, just give how you use the elgrithma.
(0) If Dipkotra's algorithm is used as the algorithm for solving the single source shortest-
paths problem. Give the time complexity for each problem listed in (a). The time
complexity of Djkstra’s algorithm is O(V log V +B).
(10%) Let (u,») be & minimum-weight edges in a graph G, Show thet (ux) belongs to
some minimum spanning tree of G.
(7HL SRARAKE
sae: a hg F
Ose
8% Gee Aaa:
Biasues
Pale eae ra
valance tes
> ReRR | SB GRARGMMR nome OF
Part B
1. To sort a sequence of numbers in increasing order we
can use heapsort.
A. What is a heap? (5%)
B. Show how heapsort works for sorting the following
numbers
5,4, 1,8, 3, 2, 7 (10%)
C. What kind of data structure you would use for the
heap in the heapsort program? Why? (5%)
2. Order the following 5 growth rates in increasing order.
(1.001)", n°, 2°", nl, nlogn (10%)
3. Show that any algorithm which sorts by comparisons
only must have a worst case computing time of
O(nlogn).(15%)9
. = 7
HAkRARAE Ge BRE ie SME Sho e RRO
vans yh of BebEnen S80:
(1) 20% Design an efficient greedy algorithm to solve the traveling on
salesman problem. What is the time complexity of your algorithm?
|= Compared it to the optimal solution.
(2) 20% Show that the complete graph of order p 2 3 contains (p-1)!/2
hamiltonian cycles (order: the suber of vertexes).
—— (3) 10%Fow to use the depth-first-search algorithm to find the cut vertex ———~
of a connected graph?
1: a ere =m
. biel ree fle &|
ss] irm em ae
HiePaers Te BERK a Snes FRewA BORA
kin
HE Re Aaa
SRE Ewe Brednas ©"
| — ae
25%) Sno
1. Find he optima binary search ie forthe fllowing keys: (The Sequences
sppear nthe parentheses afer ech Key. (Please show the vals in the marx) oo
| ‘A(10), BQI2). CC), DUS)
5m)
2. (@) Give the definitions of P and NP,
(@) Give the definition of NP-complete
(© What is Cook's theorem? Describe its proof brill.
{@) How to prove a given problem to be NP-complete?
{@) Why is t hard” to solve NP-complete problems? Hint: From the definition.BRERA RAE
SRB
tk wth ake Brtznay 68%
a a 7
alan ee 7 * an
1 FERE — SWE aROMM I ke
1. (19%) One the following functions by growth rate (ie, Big-Oh). indicate
‘whieh foetions grow a the same rate
NWS, NY, NlogN, Nloglog2, Nlog! M, Nlog(N), 201, 2", 2*°, 37, 0?
2, (13% Briefty desert the principle of dynamic programming. Describe how to
consuctthe opimatbiuay search ee by dynamic programming.
as
3. _{@) Deseibe the propeties oF an AVL tee (b) Draw the AVL tree built when the
eys 3.2, 5, 6,9, 4,7, and B ae inserted ini av inally empty Wee. ease
show the AVL wees flr each insertion) (e) Draw the AVL re bull whe tl
keys 6 and 5 ae deleted from the resultant AVL tee of pt (0) (Please Show the
AVL toes afer cach deletion)
15ee
7 ane =
oon THR BR Ml
saloon ee
=
— Hin PRGA
We ot
Cone
: ees 3h
FURS SG bah BE Shenae ©
(Closed Books)
1. (10%) Banks often record transactions on an aecount in order of the times of the trans
actions, but many people tke to roceive their bonk statements with checks listed in order
by check mmber. Peoplo usually write checks in order by ceck anime, and merchants
‘usally casi them wit reasonable dispatch. ‘The problem of converting time-of-transaction
‘ordering to ceck-numbor ordering is thorefore the problem of sorting almest-sorte input.
Argue tat the procedure Insertion Sort would tend to beat the procedure Quick sort on
this problem,
2, (1056) We say that a langiage £5 is pelysomil-time redasible to language La, denoted
8 by Sp Ly, if there oxsts © polynomial-time computable function f+ (0,1}* + {0, 1)"
sich that forall 2 € {0,1}*, © € Ly if and only if f(z) € La. Show that the Odo
Hanoi, 65,3);
end
{hint time complexity em) 1 ifm 1)
2. (1) Show that itis possible to multiply two men matices i atime O(a). (15%) ao
@ What do you do about matrices whose size isnot «power of 2? (10%)aa) 8 Te wie # fate
a cam am us
HREM ARAE SRE | Om RERSMRAK kone
sean: ob aig et ! TEA, Ane
1. The procedure MinMax below finds the minimam and maximum in an array X(1.2)
cfm numbers with at most n-2 comparisons of real numbers
minsx(}; mace
for i= 2 t0nd0
XG] max then max = XTi]
end
() Weite your ow version of Miniax that uses significantly fewer comparisons of
«eal numbers. For simplicity, assume that >=2, (15%)
(©) Exactly how many compatisons of real numbers does your new algorithm make
inthe wore eae? (10%)
2. Consider two sorting algorithms: Mergesot and Heapsort
fee (@) Deseribe them, (10%)
(@) Show that the runing time for each of them is O(ntogn. (10%)
(©) Show that the Mergesor is faster that Heapsort by a constant factor. (5%)
iyWane
at Fen ele a
ws] com am
Dike eka we SRERE — BM TH GER IM
aH
8 3R3a pel:
eRe 44 343m we eee ag OOH
as%)
1G) Show that for alt the nonempty bieary trees whose nonterminal nodes have
tracy two nonempty childen, the number of leaves ais greater than the
umber of nonterminal nodes kd mrt
(6) Let Tio be te ime complexity oF the felling fneton F. Express Tin) in
terms of Fy).
int Fata)
itfec=t) return ts
return lol HE(O2},
)
(2%)
2. (@)Athiof robbing a safe finds it filled wih W types of tems of varying size and
valu, buts only a small Krapsac of eapacity Mo use to carry the goods. The
knapsack problem i to find the combination of ems which the thet shoal
-choese for his knapsack in order to maximize the total value ofall the items he
lakes, Suppose thatthe sizes of items and AY are integec. Can you derive an
algorithm forthe knapssck problem with time complexity O(HD07 Ifyou ean,
‘write down your algort.
(6) Suppose that the sizes of items and M are real numbers. Can you derive an
algorithm forthe knapsack problem with polyromisl time complexity? Explain
your aner
*
weFee nie a
tM] came) er an
HASM AR AS So BRE, = BAAN | me |
sane: ALAD of eo an a
fi: reg a
1. Find the time complexity ofthe fllowing functions.
(10%)
fori=010nd0
begin
ink,
while j#0 do =5/25
oe end
was,
furetion Euclid (m,n)
while m>0 do
begin
t= mod my;
mat
end eee
retumn
2. Consider the following internal sorting slgoithms:
(selection sort
(insertion sort eee
(Obubbte sort
(@aquick sort
Answer the following questions with brief explanation: (25%)
frESEEEEEaG (@) Which method run fastest for file which ie already sorted?
(b) Which method run fastest fra file in reverse order?
(©) Which method run fastest fora file whose keys are all equal?
é (© Which method implies the fewest exchanges?
(©) Whiet method uses “Divide & Conquer" technique?
23. Please answer the following question briefly with “yes” or “no”
{IF any one NP-Complete problem can be solved in polynomial tne, then ll the
[NP problems car be solved in polynomial time? (10%)a are 2
cee FFh at |
mM kamal
Se at WRI RE FH Bes ae /
-16,30,8,28.4.10,20,6,18) at the end of each
[-— phase of the following algorithms:
(0) QuickSore C105)
8) MergeSort (10 9)
2 Derive jhe time complexity ef the Play ¢_aXbarithm for Lincling the minimum
enning. tree of a groph Gis I EF, |,
|__car_ he graph is_ represent by aay)
by) the. gsaph is _topresent by adjacency list” C/T #)
{Assume that G has at least one vertex)
T:=Q; TV:= {1}; {start with vertex f and no edges)
}— dione := false;
while T contains fewer than n—1 edges and not done do
begin
Let (u, v) be a least cost edge such that we TV and v ¢ TV;
if there is no such edge then done := true
— else add v to TV and (u, v) 10 T;
end;
if contains fewer than 1 edges then writeln (‘no spanning tree’);
Prim’s minimum spanning tree algorithmHLfRHRAF
Fa, Fe eee] =
Fee ele ot
+ m| cen en eo
YEAR > SHR ASME
saan UtRetle pe Biba Om
3. Write a context free grammar that generates all words of an equal
number ofa's and b’s. 2576
4 Design a data structure that may access sparse matrices efficiently. Use
an example to demonstrate your design. 25%
% pRR I Hhel ee *
S| Cam) ee at
Ri eBARKE ap eeae | BR GHARSRAK 8
sua: pee A Gee Aa:
1a D
(CLOSE BOOKS & NOTES)
20%
int reeursivetint n)
t
inti, sum-0;
if(n- Steet RoMMe 2 ne
OR
ata: Hea ot |
sata: ERT} Biaduay ©
.Peonve._the following. Chaotew.s _ ae eee
Any. decision tree. that_sorts 1 distinct. NL has_a height
Of ok Beast. dogg 80th CBI
algoritten that sorts only by comparisons must have a
f= Case. computing. hime _of OCs). C15 2)
2. The following. is 0 Sametion ‘that ul idan Caleran. walk tr a
connected, undirected groph that Ras wo vertices with odd edges.
Path Graph::euler (vertex v)
eee
oe 2 Path path = { }5
3 for (all vertices w adjacent to v) && (edge (v,w) not yet used)) {
4 mark edge (v,w) as used;
5S path = {(»,w)) VU euler (w) U path;
6
eet 7 return path;
8)
shoo that_if_o graph rie represented aby tts. adjacency mubtilists
Ard." path” hy. a finlerd Uiat, the. Suine-tion euler’ wortes ta
€_.vepresents she
of vestices
|____dnd. edge « 4 G, respectively. Clo)eal A ake aa
Fee mle
em] (am) a jae
on
BLERIRAMER CL PERB > MBA FREMAK % AR
ARAL
Oars
+ Baty Bs a :
Lees Besduas 9
(CLOSE BOOKS & NOTES)
1 (25%)
(@) Describe P and NP.
(b) Describe NP-hard and NP-complete.
(©) Describe Cook’s theorem,
(@)_ What is the first NP-complete problem?
(©) Give a method for proving that a problem is NP-complete (ic. list the steps)
Explain your answer,
2. (5%)
Tong int caleu(int n)
l
Tong int sum = 0;
int is
ifn <= 1) return 15
for (i= 1; i<= 10; 144)
sum +2 calcu(n-1);
return sum;
(Find the value of calou(n). (write down the recurrence equation and solve it)
(©) Find the time complexity of “calcu” function. (write down the recurrence equation and solve it)bk
ar a em
Fee Ble
tm] (am) oem jae
Rieereewgem Seeee | #a HRA Fy We
aaans Ut Ere a BEY cay ABA:
Groene:
1. 5%)
Functions d ands map set of postive integers into set of postive integers. You are given
dQ) =5(2) = 13 dQ) = 53) = 2
ny = dna) +2 tne;
sto) = sti) +n—1 if nis odd and n>
s(n) = s(n2) +n —2. if mis even andn> 4.
For integer n 2 2, prove
ln) + s(n) =2n-2
and
(a) = 2Llogy nl 1 ifgicn<3x24 and i2 ls
(ni) =2 Logg nl if3x2H! Sn< 2H and i2 1.
2. (25%)
(@) You are given an input file of characters S, A, O,R,T, I,N, G, A,N, D. Use a heap of
size three to show how the replacement selection can be used to produce initial runs in the
first par of the sort-merge process.
(b) What are the meri
of the replacement selection when compared with internal sorting
algorithms (such as quicksort)?& a) A ake fe
Fee mle tt
of
ay (em) ae ae
asewctawem wegee | ewHeReMRM og, ge
ae nem .
saa 1A IEe. Besdeas 98
+ The following procedure Dijkstra is used to solve Single Source Shortest Paths problems. C{ij] stores the
length of the a from vertex i to vertex j. If tore is no are from vertex ito vertex j Cf] = 0. S isa set
}- _of vertices to which the shortest paths from the source are known. Di] is an array and stores the length of
the known shortest path from source to vertex i, We have a digraph G with 10 vertices and vertex 1 is the
source. I the original data C[5,4] is damaged, then which part of the answer setD is unaffected?
|___ procedure Dijeiro;
{ Dijkstra computes the cost of the shortest paths
from veriex | to every vertex of a ditected graph }
Desi
wo
eee ata
Q iniitize Dy
|__ «a for 1:= 1 t9 n—1 do begin
o choose a vertex ib in V—S such that
_ lw] is minimums
o add w 0S;
oO for each vertex» in VS do
@ Div = min(Dlr}, Diw] + Cw. wD
od
anne end; (Dijkstra }