You are on page 1of 10

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
Inary Search Trees
Search trees are data structures that support many dynamIcset
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 bInarysearchtree 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 NDF0EFTFEE
WALK (root[T]).
1. If x NlL
2. then lNDR0ER - TREE - WALK (le]t[x])
J. prInt key [x]
4. lNDR0ER - TREE - WALK (ryht [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 bInarysearchtree property.
t takes tIme to walk an nnode 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, redblack 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 NL. We
shall regard these NL's as beIng poInters to external nodes (leaves) of the bInary search tree and
the normal, keybearIng nodes as beIng Internal nodes of the tree.
A bInary search tree Is a redblack 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 (NL) 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
FIgure J.7 (a) shows an example of a redblack tree.
As a matter of convenIence In dealIng wIth boundary condItIons In redblack tree code, we use a
sIngle sentInel to represent NL. For a redblack tree T, the sentInel nl [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 NL are
replaced by poInters to the sentInel nl [T].
We use the sentInel so that we can treat a NL chIld of a node x as an ordInary node whose parent
Is x. Although we Instead could add a dIstInct sentInel node for each NL In the tree, so that the
parent of each NL Is well defIned, that approach would waste space. nstead, we use the one
sentInel nl [T] to represent all the NL'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 redblack tree, sInce they hold the
key values. n the remaInder of thIs chapter, we omIt the leaves when we draw redblack 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 blackheIght Is well
defIned, sInce all descendIng paths from the node have the same number of black nodes. We
defIne the blackheIght of a redblack tree to be the blackheIght 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

TransItIon 0Iagrams
n some sItuatIons, graphIcal representatIon of the nextmove (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 nextmove 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.

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)
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.


4 If L1 and L2 are context free |anguages then L1 z L2 |s a context free |anguage

Lxp|a|n pr|m's A|gor|thm
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 bullLup lLeraLlvely by addlng an edge LhaL has mlnlmum
welghL among Lhe remalnlng edges (le 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 semlformal deflnlLlon of Lhe

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
v1 (a)

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 13 1he edge de wlLh welghL
13 ls selecLed Pence afLer Lhlrd lLeraLlon we have Lhe glven graph wlLh chosen edges and v1 and L1 as
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
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|ngr|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 mlnlmumwelghL 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?
1here are n lLems ln a sLore lor l 12 n lLem l has welghL w
0 and worLh v
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
of ob[ecL l where 0 x
1 lLem l
conLrlbuLes x
Lo Lhe LoLal welghL ln Lhe knapsack and x
Lo Lhe value of Lhe load
ln Symbol Lhe fracLlon knapsack problem can be sLaLed as follows
sub[ecL Lo consLralnL
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


Greedyfract|ona|knapsack (w v W)
lC8 l 1 Lo n
do xl 0
welghL 0
whlle welghL W
do l besL remalnlng lLem
ll welghL + wl W
Lhen xl 1
welghL welghL + wl
xl (w welghL) / wl
welghL W
reLurn x

lf Lhe lLems are already sorLed lnLo decreaslng order of v
/ w
Lhen Lhe whlleloop 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
aL Lhe rooL 1hen
creaLlng Lhe heap Lakes C(n) Llme
whlleloop 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 worsLcase lL may be fasLer lf only a small number of
lLems are need Lo flll Lhe knapsack
Cne varlanL of Lhe 01 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)

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`

@heorem 1he fracLlonal knapsack problem has Lhe greedycholce 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