JuIy 2011

Master of Computer AppIication (MCA) - Semester 4
MC0080 - AnaIysis and Design of AIgorithms - 4 Credits
(Book ID: B0891)
Ass|gnment Set Ŷ 2

1. Describe the foIIowing with suitabIe exampIes for each:
4Binary Search Trees
4Red BIack Trees
Ansť
ßInary Search Trees
Search trees are data structures that support many dynamIc·set
operatIons, IncludIng SEAFCH, |ÌNÌ|U|, |AXÌ|U|, PFE0ECESSDF, SUCCESSDF, ÌNSEFT, and
0ELETE. Thus, a search tree can be used both as a dIctIonary and as a prIorIty queue.
8asIc operatIons on a bInary search tree take tIme proportIonal to the heIght of the tree.
J.6.1 What Is a bInary search tree:
A bInary search tree Is organIzed, as the name suggests, In a bInary tree, as shown In FIgure J.5.
Such a tree can be represented by a lInked data structure In whIch each node Is an object. Ìn
addItIon to a key fIeld and satellIte data, each node contaIns fIelds

For any node the keys In the Ieft subtree of are at most key ( and the keys In the rIght
subtree of are at Ieast key (. 0Ifferent bInary search trees can represent the same set of
vaIues. The worst-case runnIng tIme for most search-tree operatIons Is proportIonaI to the
heIght of the tree. (a) A bInary search tree on 6 nodes wIth heIght 2. (b) A Iess effIcIent bInary
search tree wIth heIght 4 that contaIns the same keys. Ieft rIght and p that poInt to the
nodes correspondIng to Its Ieft chIId Its rIght chIId and Its parent respectIveIy. If a chIId or
the parent Is mIssIng the approprIate fIeId contaIns the vaIue NIL. The root node Is the onIy
node In the tree whose parent fIeId Is NIL.
The keys In a bInary search tree are always stored In such a way as to satIsfy the bInary-search-
tree property:
Let x be a node In a bInary search tree. Ìf y Is a node In the left subtree of x, then
key [y] · key [x]. Ìf y Is a node In the rIght subtree of x, then
key [x] · key [y].
Thus, In FIgure J.5(a), the key of the root Is 5, the keys 2, J, and 5 In Its left subtree are no larger
than 5, and the keys 7 and 8 In Its rIght subtree are no smaller than 5. The same property holds
for every node In the tree. For example, the key J In FIgure J.5(a) Is no smaller than the key 2 In
Its left subtree and no larger than the key 5 In Its rIght subtree.
The bInary·search·tree property allows us to prInt out all the keys In a bInary search tree In sorted
order by a sImple recursIve algorIthm, called anInorder tree waIk. ThIs algorIthm Is so named
because the key of the root of a subtree Is prInted between the values In Its left subtree and those
In Its rIght subtree. (SImIlarly, a preorder tree waIk prInts the root before the values In eIther
subtree, and a postorder tree waIk prInts the root after the values In Its subtrees.) To use the
followIng procedure to prInt all the elements In a bInary search tree T, we call ÌNDF0EF·TFEE·
WALK (root[T]).
lNDR0ER - TREE - WALK (x)
1. If x NlL
2. then lNDR0ER - TREE - WALK (le]t[x])
J. prInt key [x]
4. lNDR0ER - TREE - WALK (rìyht [x])
As an example, the Inorder tree walk prInts the keys In each of the two bInary search trees from
FIgure J.5 In the order 2, J, 5, 5, Z, 8. The correctness of the algorIthm follows by InductIon
dIrectly from the bInary·search·tree property.
Ìt takes tIme to walk an n·node bInary search tree, sInce after the InItIal call, the
procedure Is called recursIvely exactly twIce for each node In the tree-once for Its left chIld and
once for Its rIght chIld. The followIng theorem gIves a more formal proof that It takes lInear tIme
to perform an Inorder tree walk.
#ed-ßIack Trees
A red-bIack tree Is a bInary search tree wIth one extra bIt of storage per node: Its coIor, whIch
can be eIther FE0 or 8LACK. 8y constraInIng the way nodes can be colored on any path from the
root to a leaf, red·black trees ensure that no such path Is more than twIce as long as any other, so
that the tree Is approxImately baIanced.
Each node of the tree now contaIns the fIelds color, key, left, rIght, and 5. Ìf a chIld or the parent
of a node does not exIst, the correspondIng poInter fIeld of the node contaIns the value NÌL. We
shall regard these NÌL's as beIng poInters to external nodes (leaves) of the bInary search tree and
the normal, key·bearIng nodes as beIng Internal nodes of the tree.
A bInary search tree Is a red·black tree If It satIsfIes the followIng red-bIack propertIes:
1. Every node Is eIther red or black.
2. The root Is black.
J. Every leaf (NÌL) Is black.
4. Ìf a node Is red, then both Its chIldren are black.
5. For each node, all paths from the node to descendant leaves contaIn the same number of black
nodes.
FIgure J.7 (a) shows an example of a red·black tree.
As a matter of convenIence In dealIng wIth boundary condItIons In red·black tree code, we use a
sIngle sentInel to represent NÌL. For a red·black tree T, the sentInel nìl [T] Is an object wIth the
same fIelds as an ordInary node In the tree. Ìts color fIeld Is 8LACK, and Its other fIelds - 5, left,
rIght, and key-can be set to arbItrary values. As FIgure J.7(b) show, all poInters to NÌL are
replaced by poInters to the sentInel nìl [T].
We use the sentInel so that we can treat a NÌL chIld of a node x as an ordInary node whose parent
Is x. Although we Instead could add a dIstInct sentInel node for each NÌL In the tree, so that the
parent of each NÌL Is well defIned, that approach would waste space. Ìnstead, we use the one
sentInel nìl [T] to represent all the NÌL's - all leaves and the root's parent. The values of the
fIelds 5, left, rIght, and key of the sentInel are ImmaterIal, although we may set them durIng the
course of a procedure for our convenIence.
We generally confIne our Interest to the Internal nodes of a red·black tree, sInce they hold the
key values. Ìn the remaInder of thIs chapter, we omIt the leaves when we draw red·black trees, as
shown In FIgure J.7 (c).
We call the number of black nodes on any path from, but not IncludIng, a node x down to a leaf
the bIack-heIght of the node, denoted -h (x). 8y property 5, the notIon of black·heIght Is well
defIned, sInce all descendIng paths from the node have the same number of black nodes. We
defIne the black·heIght of a red·black tree to be the black·heIght of Its root.


F+FS*S = F + FS*S (property 6)
= F ( + S*S) (property
= F ( + SS*) (property 12)
= FS* (property 12)
= (b+aa*b)S* (defInItIon of F)
= ( + aa*)bS* (propertIes 6 and
= a*bS*. (property 12)
As we already know the concept of language and regular expressIons, we have an Important type
of language derIved from the regular expressIon, called regular language.

Ŧ Lxp|a|n the concept of trans|t|on d|agrams w|th examp|es wherever necessary
Ansť

TransItIon 0Iagrams
Ìn some sItuatIons, graphIcal representatIon of the next·move (partIal) functIon of a TurIng
|achIne may gIve better Idea of the behavIour of a T| In comparIson to the tabular
representatIon of .
A TransItIon 0Iagram of the next·move functIons d of a T| Is a graphIcal representatIon consIstIng
of a fInIte number of nodes and (dIrected) labelled arcs between the nodes. Each node represents
a state of the T| and a label on an arc from one state (say p) to a state (say q) represents the
InformatIon about the requIred Input symbol say x for the transItIon from p to q to take place and
the actIon on the part of the control of T|. The actIon part consIsts of (I) the symbol say y to be
wrItten In the current cell and (II) the movement of the tape Head.
Then the label of an arc Is generally wrItten as x/(y, |) where | Is L, F or N.
ExampIe 10.4.2.1

Where Q = [q0, q1, q2, hJ]
= [0, 1]
= [0, 1, #]
and d be gIven by the followIng table.

0 1 #
q0 · · (q2, #, F)
q1 (q2, 0, F) (q1, #, F) (h, #, N)
q2 (q2, 0, L) (q1, 1, F) (h, #, N)
hJ · · ·
Then, the above TurIng |achIne may be denoted by the TransItIon 0Iagram shown below, where
we assume that q0 Is the InItIal state and h Is the fInal state.

FIgure 10.4.2.1

4Ŧ If L1 and L2 are contextŴ free |anguagesţ then L1 z L2 |s a context free |anguageŦ
Ansť


Ŧ Lxp|a|n pr|m's A|gor|thm
Ansť
Þr|m's A|gor|thm
1he algorlLhm due Lo Þr|m bullds up a mlnlmum spannlng Lree by addlng edges Lo form a sequence of
expandlng subLreesŦ 1he sequence of subLrees ls represenLed by Lhe palr (v1ţ L1)ţ where v1 and L1
respecLlvely represenL Lhe seL of verLlces and Lhe seL of edges of a subLree ln Lhe sequenceŦ lnlLlallyţ Lhe
subLreeţ ln Lhe sequenceţ conslsLs of [usL a slngle verLex whlch ls selecLed arblLrarlly from Lhe seL v of
verLlces of Lhe glven graphŦ 1he subLree ls bullLŴup lLeraLlvely by addlng an edge LhaL has mlnlmum
welghL among Lhe remalnlng edges (lŦeŦţ edge selecLed greedlly) andţ whlch aL Lhe same Llmeţ does noL
form a cycle wlLh Lhe earller selecLed edgesŦ
We lllusLraLe Lhe Þrlms algorlLhm Lhrough an example before glvlng a semlŴformal deflnlLlon of Lhe
algorlLhmŦ

Lxamp|e (of Þrlms AlgorlLhm)ť
LeL us explaln Lhrough Lhe followlng example how Þrlmes algorlLhm flnds a mlnlmal spannlng Lree of a
glven graphŦ LeL us conslder Lhe followlng graphť
In|t|a||y
v1 ƹ (a)
L1 ƹ ú

ln Lhe flrsL lLeraLlonţ Lhe edge havlng welghL whlch ls Lhe mlnlmum of Lhe welghLs of Lhe edges havlng a
as one of lLs verLlcesţ ls chosenŦ ln Lhls caseţ Lhe edge ab wlLh welghL 1 ls chosen ouL of Lhe edges abţ ac
and ad of welghLs respecLlvely 1ţ 3 and 2Ŧ 1husţ afLer llrsL lLeraLlonţ we have Lhe glven graph wlLh
chosen edges ln bold and v1 and L1 as followsť
v1 ƹ (aţ b)
L1 ƹ ( (aţ b))

ln Lhe nexL lLeraLlonţ ouL of Lhe edgesţ noL chosen earller and noL maklng a cycle wlLh earller chosen
edge and havlng elLher a or b as one of lLs verLlcesţ Lhe edge wlLh mlnlmum welghL ls chosenŦ ln Lhls case
Lhe verLex b does noL have any edge orlglnaLlng ouL of lLŦ ln such casesţ lf requlredţ welghL of a nonŴ
exlsLenL edge may be Laken as ~Ŧ 1hus cholce ls resLrlcLed Lo Lwo edges vlzŦţ ad and ac respecLlvely of
welghLs 2 and 3Ŧ Penceţ ln Lhe nexL lLeraLlon Lhe edge ad ls chosenŦ Penceţ afLer second lLeraLlonţ we
have Lhe glven graph wlLh chosen edges and v1 and L1 as followsť
v1 ƹ (aţ bţ d)
L1 ƹ ((aţ b)ţ (aţ d))


ln Lhe nexL lLeraLlonţ ouL of Lhe edgesţ noL chosen earller and noL maklng a cycle wlLh earller chosen
edges and havlng elLher aţ b or d as one of lLs verLlcesţ Lhe edge wlLh mlnlmum welghL ls chosenŦ 1hus
cholce ls resLrlcLed Lo edges acţ dc and de wlLh welghLs respecLlvely 3ţ 3ţ 1Ŧ3Ŧ 1he edge de wlLh welghL
1Ŧ3 ls selecLedŦ Penceţ afLer Lhlrd lLeraLlon we have Lhe glven graph wlLh chosen edges and v1 and L1 as
followsť
v1 ƹ (aţ bţ dţ e)
L1 ƹ ((aţ b)ţ (aţ d)Ť (dţ e))

ln Lhe nexL lLeraLlonţ ouL of Lhe edgesţ noL chosen earller and noL maklng a cycle wlLh earller chosen
edge and havlng elLher aţ bţ d or e as one of lLs verLlcesţ Lhe edge wlLh mlnlmum welghL ls chosenŦ 1husţ
cholce ls resLrlcLed Lo edges dc and ac wlLh welghLs respecLlvely 3 and 3Ŧ Pence Lhe edge dc wlLh welghL
3 ls chosenŦ 1husţ afLer fourLh lLeraLlonţ we have Lhe glven graph wlLh chosen edges and v1 and L1 as
followsť
v1 ƹ (aţ bţ dţ eţ c)
L1 ƹ ((aţ b)ţ (aţ d) (dţ e) (dţ c))

AL Lhls sLageţ lL can be easlly seen LhaL each of Lhe verLlcesţ ls on some chosen edge and Lhe chosen
edges form a LreeŦ
Clven below ls Lhe semlformal deflnlLlon of Þrlms AlgorlLhm

A|gor|thm Spann|ngŴÞr|m (G)
// Lhe algorlLhm consLrucLs a mlnlmum spannlng Lree
// for whlch Lhe lnpuL ls a welghLed connecLed graph C ƹ (vţ L)
// Lhe ouLpuL ls Lhe seL of edgesţ Lo be denoLed by L1ţ whlch LogeLher consLlLuLe a mlnlmum spannlng
Lree of Lhe glven graph C
// for Lhe palr of verLlces LhaL are noL ad[acenL ln Lhe graph Lo each oLherţ can be glven
// Lhe label ~ lndlcaLlng ºlnflnlLe" dlsLance beLween Lhe palr of verLlcesŦ
// Lhe seL of verLlces of Lhe requlred Lree ls lnlLlallzed wlLh Lhe verLex v0
v1 r žv0ſ
L1 r ú // lnlLlally L1 ls empLy
// leL n ƹ number of verLlces ln v

or l ƹ 1 to “n“Ŷ 1 do
flnd a mlnlmumŴwelghL edge ƹ (v1ţ u1) among all Lhe edges such LhaL v1 ls
ln v1 and u1 ls ln v Ŷ v1Ŧ
v1 r v1 Ģ ž u1ſ
L1 ƹ L1 Ģ ž ſ
eturn L1

6Ŧ G|ve an a|gor|thm for Greedy knapsack prob|emŦ Ana|yse your a|gor|thm?
Ansť
1here are n lLems ln a sLoreŦ lor l ƹ1ţ2ţ Ŧ Ŧ Ŧ ţ nţ lLem l has welghL w
l
ƽ 0 and worLh v
l
ƽ 0Ŧ 1hlef can carry a
maxlmum welghL of W pounds ln a knapsackŦ ln Lhls verslon of a problem Lhe lLems can be broken lnLo
smaller pleceţ so Lhe Lhlef may declde Lo carry only a fracLlon x
l
of ob[ecL lţ where 0 ƿ x
l
ƿ 1Ŧ lLem l
conLrlbuLes x
l
w
l
Lo Lhe LoLal welghL ln Lhe knapsackţ and x
l
v
l
Lo Lhe value of Lhe loadŦ
ln Symbolţ Lhe fracLlon knapsack problem can be sLaLed as followsŦ
maxlmlze
n
S
lƹ1
x
l
v
l
sub[ecL Lo consLralnL
n
S
lƹ1
x
l
w
l
ƿ W
lL ls clear LhaL an opLlmal soluLlon musL flll Lhe knapsack exacLlyţ for oLherwlse we could add a fracLlon of
one of Lhe remalnlng ob[ecLs and lncrease Lhe value of Lhe loadŦ 1hus ln an opLlmal soluLlon
n
S
lƹ1
x
l
w
l
ƹ


GreedyŴfract|ona|Ŵknapsack (wţ vţ W)
lC8 l ƹ1 Lo n
do xżlŽ ƹ0
welghL ƹ 0
whlle welghL Ƽ W
do l ƹ besL remalnlng lLem
ll welghL + wżlŽ ƿ W
Lhen xżlŽ ƹ 1
welghL ƹ welghL + wżlŽ
else
xżlŽ ƹ (w Ŵ welghL) / wżlŽ
welghL ƹ W
reLurn x

Ana|ys|s
lf Lhe lLems are already sorLed lnLo decreaslng order of v
l
/ w

Lhen Lhe whlleŴloop Lakes a Llme ln C(n)Ť
1hereforeţ Lhe LoLal Llme lncludlng Lhe sorL ls ln C(n log n)Ŧ
lf we keep Lhe lLems ln heap wlLh largesL v
l
/w
l
aL Lhe rooLŦ 1hen
creaLlng Lhe heap Lakes C(n) Llme
whlleŴloop now Lakes C(log n) Llme (slnce heap properLy musL be resLored afLer Lhe removal of rooL)
AlLhough Lhls daLa sLrucLure does noL alLer Lhe worsLŴcaseţ lL may be fasLer lf only a small number of
lLems are need Lo flll Lhe knapsackŦ
Cne varlanL of Lhe 0Ŵ1 knapsack problem ls when order of lLems are sorLed by lncreaslng welghL ls Lhe
same as Lhelr order when sorLed by decreaslng valueŦ
1he opLlmal soluLlon Lo Lhls problem ls Lo sorL by Lhe value of Lhe lLem ln decreaslng orderŦ 1hen plck up
Lhe mosL valuable lLem whlch also has a leasL welghLŦ llrsLţ lf lLs welghL ls less Lhan Lhe LoLal welghL LhaL
can be carrledŦ 1hen deducL Lhe LoLal welghL LhaL can be carrled by Lhe welghL of Lhe lLem [usL plckŦ 1he
second lLem Lo plck ls Lhe mosL valuable lLem among Lhose remalnlngŦ keep follow Lhe same sLraLegy
unLll Lhlef cannoL carry more lLem (due Lo welghL)Ŧ

Þroof
Cne way Lo proof Lhe correcLness of Lhe above algorlLhm ls Lo prove Lhe greedy cholce properLy and
opLlmal subsLrucLure properLyŦ lL conslsL of Lwo sLepsŦ llrsLţ prove LhaL Lhere exlsLs an opLlmal soluLlon
beglns wlLh Lhe greedy cholce glven aboveŦ 1he second parL prove LhaL lf A ls an opLlmal soluLlon Lo Lhe
orlglnal problem Sţ Lhen A Ŵ a ls also an opLlmal soluLlon Lo Lhe problem S Ŵ s where a ls Lhe lLem Lhlef
plcked as ln Lhe greedy cholce and S Ŵ s ls Lhe subproblem afLer Lhe flrsL greedy cholce has been madeŦ
1he second parL ls easy Lo prove slnce Lhe more valuable lLems have less welghLŦ
noLe LhaL lf v` / w`

ţ ls noL lL can replace any oLher because w` Ƽ wţ buL lL lncreases Lhe value because v`
ƽ vŦ ¿

@heorem 1he fracLlonal knapsack problem has Lhe greedyŴcholce properLyŦ
Þroof LeL Lhe raLlo v`/w` ls maxlmalŦ 1hls supposlLlon lmplles LhaL v`/w` ǀ v/w for any palr (vţ w)ţ so
v`v / w ƽ v for any (vţ w)Ŧ now Suppose a soluLlon does noL conLaln Lhe full w` welghL of Lhe besL raLloŦ
1hen by replaclng an amounL of any oLher w wlLh more w` wlll lmprove Lhe valueŦ ¿

Sign up to vote on this title
UsefulNot useful