Sei sulla pagina 1di 334

www.alljntuworld.

in JNTU World

ADVANCED DAT A-
STRUCTURES

NTU
J
W
Lecture Notes

orld
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.2

ROLE OF DAT A-STRUCTURES Makes • Three

J
Program: • • Total Question: External Faster use

N
Algorithm − − Data − Input Efficient Time Basic
The Also, hence Supports Computations parallel structures is
basic = Algorithm (1) (2) (3) better. Computation the Computation

T
the Time Algorithm or efficient Locate/Access

Compute Store order order method; distributed to (Another (+ the

U
IN access/store Faster: of in + Good =
read/write new = DataStructure a read/write

COMPUTATION which it Good value Sequence Steps:


determines computation.) value way data-values Implementation)

W
(output those method data to of of data-access

make Computation data-items data-items of + the + (inputs + Time


a Implementation. step) Good data-items computations
o
to to operations). data-structures used/computed.

r
compute a are Steps step) computed computed.
ld
faster data. External Output is (and to

•? What is an efficient program?


•? What determines the speed of an Algorithm?
•? A program must also solve a "problem". Which of the three
parts
algorithm, data-structure, and implementation embodies this?
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.3

ALGORITHM OR METHOD Problem: Tw • • • Data (a)


J
(b) A Poor o Method They Any both Methods:

N
structures: Three An − − − Implementation

methods. improvement access array This more nums[i],


addr(nums[i]) involves When vidual Use varying they Compute
T
(1) variables of are is superior nums[0..2]. data time

data-items there individually inferior number used 1 (1) (2) the

U
the vs. addition in than x, are due the as
executable average to DAT aver aver y, to = of parameters of
Method large same accessing z. to is (a) (1): addr(nums[0]) and

data-items A = = named not data-structure because (x (x/3)

STRUCTURE of Using number order: 1

W
practical. + three (2); code multiplication.) y

data-items + to a + two 3 (y/3) 〈x, accessing a simple numbers.


are z)/3. additions has of function). y, less involved data-items, +
o
+ applies z, to (z/3). div-operations. aver〉. is

r
variable. i*sizeof(int), compute not an and array-item

equally (in suitable 1 naming division. particular,


ld
its (To well address when access which

takes
indi- to
if a
a = x + y; //uses 2 additional assignments
b = a + z; aver = b/3;
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.4

LIMITS OF EFFICIENCY
Hardware • Software • •
J O(n. Physical Limitless

circuits). From improvement One 100 That A1 N is

ns/instruction. log nano-second is, an 5 limit: limit: the mips n)


improvement limits in Sorting Sorting This is limit
T
a by the way, (millions limit of n. (n the is

efficiency-limit Algorithm Algorithm log n= time

U
problem except = 2 is factor 10number n
=
computation over −9 (speed of (one log for of instructions A2

n
W
dependent. A1: A2: 2. of the n of by

= →
billionth items for electrons) inherent the problem O(n.

∞ as
o
O(nsorting factor sorted) per 2 of ) log time
n →
nature sec) a independent. and Algorithms. n) second); time

∞.
r ld
to space of 10 the mips 10

(layout problem. mips is of an


=
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.5

MEASURING PERFORMANCE
Analytic Method:
• Theoretical analysis Empirical Methods:
• Count the number executing an instrumented • Measure
directly Example of Instrumentation:
Original code: if else Instrumentd code: countComparisons++; if
else Question:
J
•? •? •? What Instrument Show if else if the

N
(x (x loop:
T
is small the < < wrong y) 3) new the { = y code

code with countComparisons++; y;

U
= the (x (x of the of small small actual <

< times the following y) y) Algorithm’s version program-


execution small = small = specific y; y;

W
instrumentation: = = of x; x; operations the

o
//initialized small time program. complexity. =
r ld
time x; are } in performed

elsewhere a run.
below for readCount and writeCount of x:
x + 5;
when updates to loopCount is moved outside for (i=j; i<max;
i++) {
loopCount++;
by
if (x[i] < 0) break; }
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.6
J
EXERCISE 1. 2. 3. void { } Instrument

(numExchanges) of numComparisons input For maximum


mum? Give ments it Make metic int for (a) (b) is

N
the crazySort(int numItems i, (i=0; for a items[]
a and sure of array If in Will Is tion-sort from right-angled, j,
pseudocode (j=i+1; if } lengths we the the comparisons small,

T
i<numItems; numExchanges. that the (items[i] small

items[i]it that? data-items. = use algorithm outer affect [3, =


you code or j<numItems; x, and "i 6, *items, = after 2,
U
for-loop, to y, items[j]; < the minimize find

for = 4, obtuse-angled, below number > and numItems selection-


sort? i++) small; of each 5, items[j]) execution in deciding
Show int an 2, data-items)? z the //put can 0]. When iteration

W
j++) to do input numItems) items[j] of code
the the count we form − comparisons ith { time? total whether
1" for will values //exchange still more or of smallest a in In the
= which triangle, number an get the numExchanges items[i];

o
place what closely number of acute-angled three the

outer numExchanges (numComparisons) crazySort item of way


r
same operations and related "i given for-loop of in does

ld
< if final numItems"

items[i] so Exchanges line be will to triangle. whether it for


(arith- result?
inser- differ
mini-
seg- give and the
4. Given an array lengths[1..n] of the lengths of n line segments,
find a method for testing if they can form a polygon
(quadrilateral for n = 4, pentagon for n = 5, etc).
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.7

SOLUTION TO SELECTED EXERCISES:


1. void crazySort(int *items, int numItems)
{ int i, j, small,
numComparisons=0, //for two elements numExchanges=0; //of elements for
(i=0; i<numItems; i++) {//put for (j=i+1; j<numItems; j++)
numComparisons++; //keep if (items[i] > items[j]) numExchanges++;
small = items[j]; items[j] items[i] = small; }
}printf("numComparisons = %d, numExchanges numComparisons, } } After the
comparison and i=0, i=0, i=0, i=0, i=0,

numComparisons i=1, i=1, i=1, i=1,

numComparisons i=2, i=2, i=2, numComparisons

i=3, i=3, numComparisons i=4, numComparisons


J
i=5, numComparisons j=1, j=2, j=3,

N
j=4, j=5, j=2, j=3, j=4, j=5, j=3,

j=4, j=5, j=4, j=5, j=5, j=6, items[]:


items[]: items[]: items[]: items[]: items[]:

items[]: items[]: items[]: items[]: items[]:

items[]: items[]: items[]: items[]: items[]:

T U
= = = = = = 5, 9, 12,

14, 15, 15, exchanges 2 2 2 2 0 0 0 0 0 0 0 0 0 0

0 0 numExchanges numExchanges 3 3 3 3 3 3 3 2

2 2 2 2 numExchanges 2 2 numExchanges 2

numExchanges 2 numExchanges numExchanges); 4 4 4 4 4

4 4 4 4 4 3 2 2 2 2 2 (if 5 5 5 5 5 5 5 5 5 5 5
W
5 4 3 3 3 any) 2 2 2 2 2 2 2 3 3 3

4 4 5 5 4 4 { { in ith for it //exchange items[]

o
here input smallest = = in items[i]; %d\n",
r ld
items[] items[] item = [3, in 2,

items[i] 4, 5, 2, 0].
0 0 0 0 2 2 2 2 2 2 2 3 3 4 5 5 = 2 = 3 = 5 =

7 = 8 = 8
This exchange is more for each closely iteration related of outer-loop.

to selection-sort, #(Comparisons) which involves is still Cat 2n. most one


2. Triangle classification pseudocode; assume that 0 < x ≤ y ≤ z.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.8

if (z < x + y) { zSquare if (zSquare = z*z; ==

xySquareSum xySquareSum) = x*x + right-angled

triangle; else if (zSquare obtuse-angled >


xySquareSum) triangle; 3. Condition • •

N
J }else The The 20 else /< not

T
largest lengths 2 for + acute-angled 4 a
U
polygon: + length [2, triangle; 5 = 4,

11, 5, is but less 20] the will than triangle;

W o
lengths not the make sum [2, of
r
a 4, quadrilateral the 5, 10] other y*y;

ld
lengths.

because will.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.9

ANALYZING NUMBER OF EXCHANGES


Pseudocode 1. Create 2. For 3. Collect tions 4. Plot
numExchanges.
Pseudocode 1. For 2. While (a) (b) 3. Plot Note: Question:

N
•? J If mine below ↓ p We each (i=0; is

numPermutations[i] numPermutations[i] which the Apply Add a


are (there all can these permutation #1: #2: p,
i<CnextPermutation(p) possible permutations 1 use
T ;
apply has to crazySort is //No 2ndata a

i++),
numPermutation[numExchanges]. this numExchanges

U
nextPermutation(n) crazySort need to
initialize
permutations idea IN of determine to to S for

CRAZY-SORT to against = p store against analyze n and

W numPermutations[i]
{0, and = in 4 all 1, =

numPermutations[i] determine determine p i. the in i) 2, i of n!


other lexicographic for to ⋅ ⋅ ⋅ , lexicographic = {0,
o
permutations. p) i visualize n = sorting 1, − do

numExchagnes. 0, numExchanges. 2, 1}, the 2, ⋅ ⋅ ⋅ , then

r =
⋅ ⋅ ⋅ , algorithms. following: n the order. C− order?
0.
ld .
how = 2n1}. behavior 0123 0312

1203 2013 2301 0132 0213 ↓ 0321 1023 ↓ 1230 1302 ↓ 2031
2103 ↓ 2310 3012 #(permuta- of
to deter- Shown
3102 ↓ 3120 3201 0231 1032 1320 2130 3021 3210
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.10

PSEUDOCODE vs. CODE


Characteristics + + + + Example. (A) (B) Question:
J
•? Why Shows Algorithm, Avoids Allows Allows

N
mentation Pseudocode: Code: Pseudocode: Code: is (B)

dependency the determining choosing Compute nums[0. and


T
slightly avoiding key of 1. 2. 1.1 2.1 2.2 2.3 1. 2.

3. 1. 2. 3. 4. complexity Initialize Use Initialize Use Let Good


concepts positiveCount for negativeCount . a

U
positiveCount for n negativeCount the each
each more suitable on − the too (i=0; if 1]; Pseudocode:
(i=0; if else positiveCount positiveCount number nums[i] nums[i]
any correctness much (0 efficient assume (0 and analysis.
specific data-structures negativeCount++; i<n; < to > i<n;

W
< = details. nums[i]) the 0 increment of n

nums[i]) to = = − each = = = i++) than increment positive


positiveCount. key i++) 0; n negativeCount 0. of prog.
negativeCount - the nums[i] (A)? one computation //each
positiveCount; //each positiveCount++;
o
language. Algorithm. positiveCount++;

positiveCount of and for the ≠ = an counts negative nums[i] 0.

r ld
0. nums[i] efficient = steps by
by 0; one. one. items ≠ ≠ of 0
imple- 0 the in
Writing a pseudocode requires skills to express an Algorithm in
a concise and yet clear fashion.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.11

PSEUDOCODE FOR SELECTION-SORT


Idea: Algorithm 1. Finding • • • Question: Placing (b.1)

J
Finding It If For (a) (b) know items, item. (a.1) (a.2)
N
(a.3) (a.4) is we Input: Output: each the ith

Successively ith if all assume Find Place This then In smallest


Selection-Sort: (smallestItemIndex ith smallestItemIndex for

T
smallest smallest the i what in gives the smallest (j
if then smallest it kth { Array Array (items[j] at that = 0, ith way
item position i smallest smallestItemIndex the 1, item item +

U
find smallest items[k], 1; ⋅ ⋅ ⋅ , (a.1)-(a.4)

items[] items[] item pseudocode: item among at j<numItems;


numItems-1}, in the < i items position
items[smallestItemIndex]) in items[]: directly in ith item = >
and sorted items[i..numItems 0 the items[]. i; i)
W
smallest is ≤ for // in its remaining better k

why is i in items[]. k size ≤ = in j++) = difficult, increasing in (i


j;

o
items[]. not 0, item, than numItems. − some 1, 1),
smallestItemIndex items. 2, step i are order, = ⋅ ⋅ ⋅ , but −

r
order. 0, (a)? 1] the (i 1, it − = do is ⋅ ⋅ ⋅ . kth 1).

ld
ith (a)-(b): easy smallest smallest if ≠ we

i (b.2) then exchange items[i] and items[smallestItemIndex];


"What" comes before "how".
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.12

J
EXERCISE 1. 2. Which right below Note its this

N
move from 0, Which 2, position reason, 1. 2. 3. 4.
5. 3, items" that its an are 4, input of current item 1] once Put

T
right [2, 3 [0, 2 [0, 0 [0, 5 [0, all the "put we moved

moved already moved while but best items array 5, 5, 5, 1, 1,


steps the in an places make the 0, 2, 2, 2, 2, not position the

U
describes items 3, 3, 3, 3, 3, item putting in
for to to to in items in [2, 4, 4, 4, 4, 4, right an right right right
right the right the 1] 1] 1] 5] 5] 5, is in exchange in two 0, in put
other set place. places place place place the place [3, 3, right

W
methods in numbers 4, 5, selection-sort right

items 1] 0, The places" (and as 2, Fill with [0, 1st [0, 2nd [0, 3rd
[0, all place, we for in insertion 4, not {0, places 5, 1, 1, 1, place
o
place the place 1] proper right showed input and 3,

3, 2, 2, an 1, would you places 2, 2, 3, 3, Algorithm? 2, is


insertion) is filled is "fill items 4, 4, 4, 4, [3, filled after

r
3, filled must places. filled 1] 5]
5]
5]

ld
above. have 5, 4, the properly 0,

removing 5} not by by by 2, places when giv requires It 0 2


change 1 Shown 4, en is 1]. for we
[5,
by
3
maximum number of exchanges in the first approach? 3. Give a
pseudocode for the first approach.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.13

ANOTHER EXAMPLE OF PSEUDOCODE


Problem: 1. 2. Three Question:
J
•? •? •? •? (1) (2) (3) Show Instrument Which Give

N
Search n-1). If to String: the bits (0 start i do }

for for Implementations: position from is = while a how {


Find of found for the n; (i=n-1; if (i=n-1; if pseudocode
⋅ ⋅ ⋅ 000111010101. for right (1)-(3) 0 search //= (0 break;

T
(0 break; each these the (1 of right (i=i-1 if

and == == the till position length == code (0 i>0; i>0; is


implementations for binString[i]) binString[i]) the to "00"

U
left the binString[--i]); //inefficient
// == to left ; 0 for item 0 i--) i--) works least Only find
from is of i>0; binString[i]) of of upto readCount found.
Extend to rightmost the binString efficient //bad the

W
rightmost the its i--) and position rightmost

first left left && && efficient work each but for-loop; of of
is "00" in one (0 (0 1 the 1), //has the "00" terms break;
works differently implementation (initially, "00". == ==
o
fits then in items current binString[i-1])

binString[--i]) binString[0..(n-1)].

r
without the a readCount? body go bug; in pseudocode.

back start position. binString[ using updates checking


ld
find at to to the position step it

return bin-
i
].
(1)
all
It is not necessary to sacrifice clarity for the sake of efficiency.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.14

EXERCISE 1. 2. 3. 4. J BinStrings(n, ones}, order


Which BinStrings(n, (a) (b) Which each Give binary Give "01"

N
portion details items Given erating of in 1. 2. 1. 2.

an the are: x[i] a strings 0 of the so of an these in Generate


lexicographic Throw Generate Strings(n, Successively Strings(n,
efficient string ≤ pseudocode the in 10110011100 arbitrary that
T
m the next the ≤ pseudocodes? three m) in m)={x:

0011, pseudocodes x n. away one string array BinStrings(n, in ∈

U
pseudocode and the The m). m)

characteristics BinStrings(n, lexicographic string can 0101, the


of in until save order. x first strings create are a BinStrings(n,
shows determine strings x x recursive 0110, is all the looked ∈

W
binary below for a in binary m) BinStrings(n,

last the the which binary BinStrings(4, 1001, finding order m),
in of string at. rightmost the for string next Algorithm
lexicographic a strings give have m), 1010, good string is
o
number generating 1the more string mif a 0numOnes

0n−many. of m). pseudocode pseudocode n−m1100. "01".)

r
position of 2) for efficient? length 1of . m length in (The

in order. generating the times ∈ lexicographic Give


ld
≠ of Bin- n Bin-

underlined strings m. n in
rightmost for hold enough various and gen- the for in m
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)

1.15 ALWA YS TEST YOUR METHOD • • •

J Create Use Illustrate outputs. − − rectness data-


N
structures rithm’s Select of Select Algorithm, of

these the the a execution of the few Always problem input input-

T
output some some your example use general AND

including (lists, or Algorithm. input-output of other for in use


U
output. and data-structures your YOUR

some to one trees, examples before pairs illustrate input-output a

method/algorithm. or few of etc.) for more ALGORITHM

W
the you pairs cases testing of the example at
design carefully by based input critical various that pairs

o
showing (but the involve and inputs. on after

selected steps not Algorithm. stages your the the proving)

r
special you corresponding understanding "state" in
ld
design the handling the of Algo- cor-

the the
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.16

EFFICIENCY OF NESTED IF-THEN-ELSE


• Let E = average tion of both Example 1. For if else

else else Value of z 0 1 2 3 Question:


•? Show #(condition av erage E:
N
•? •? J Give Show "case") a the code

T
and improved x (x the and #(condition 2 3 5 4 if

if z #(condition code and (x (x (x (x evaluations) its


U
= if else ((not (x = = = =

negation 3; below, T T, F, F, y) (x) if else and

and ¬x ¬x y evaluations) if = z (y) evaluations). y x) E

W
= = F, (¬x). for = (not = z (y) T, T,
= ¬x T) 3⋅ 5. 0; and each z = and y = = = 2; z y))

o
F, F, y z y) 0; = = for x x We T) 1; = = z

z the T, F)

r
count = = code and 1; 2; 1 ¬y and
ld
for = also evalua- T) else z = 3;

to compute z without using the keyword "else" show

#(condition evaluations) for each value of z.


form of the two code-segments below. the (or (a). if (nums[i]

>= max) max = nums[i]; (b). if (x > 0) z = 1; if ((x >

0) && (y > 0)) z = 2;


www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.17

BRIEF REVIEW OF SORTING


Questions:
••••••
J What Why What Which Can Can (3,6)}?

Strings: Binary Flowcharts N we we do Successively,

est is are sorting trees sort Sorting? we What abb, sort item,
T
some with want non-numerical on ba, a is

Algorithm the A well-known set 3 the 2 to baca, nodes Explain

U
third nodes find of sort result pairs B cab.

smallest (convert data? the (convert uses with after objects


W
sorting smallest of the an sorting? numbers C

items, them them example. following like Algorithms? item, to

o
etc. to the D strings like trees ones the idea: {(1,7),
r
second or shown to E strings sort): (2,7),

ld
small-

below?
to sort):
(5,4),
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.18

EXERCISE

J
1. 2. 3. 4. Give idea comparisons on items[] Write

comparisons on movements on items[] For bubble-sort,


N
exchange Some the (a) (b) (c) the the input each

the "put Give movements In Suppose items[i2], and form


operations a a critical pseudocode. pseudocode. more = = what
pseudocode is pseudocode; items[ik]. [3, [3, the of operation a

T
an a (i.e., and the 2, 2, sense, detailed cycle
permutation items of of we thinking example e4, 4, einvolving
involving 2sorting is merge-sort, i movements : 5, 5, have may

U
items[i2] maximum in in selection-sort

starting 1, 1, Explain Explain Then for pseudocode the the 0].


0]. be questions exchanges also Algorithms insertion-sort. input
right of interleaved data data permutation. argue the {1, and
W
show the of the (resp., determine places". for

initial from from items 2, Algorithm Algorithm items[i3], that


minimizes on (not ⋅ ⋅ ⋅ , the of which minimum). insertion-sort,
selection-sort. items[0..numItems-1] items[0..numItems-1] with
n}. from array the the array code) Determine Determine Note
o
the form indices other ... the the [3, in in data-

movements? after for , detail detail number 2, items-array) that

r
enumber eexchanges. k−1 sorting 14, each : {i1, the : the

selection-sort, 5, items[i1] the Assume items[i(k-1)] for for


ld
1, number successive number i2,

exchange using of the the 0]. of ..., based based


based input
input data-
data- that
and
ik} the of
of
5. Is it true that in bubble-sort if an item moves up, then it never
moves down? Explain with the input items[] = [3, 2, 4, 5, 1, 0].
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.19

AVERAGE #(COMPARISONS) TO LOCATE A

DAT A-ITEM IN A SORTED-ARRAY


Binary Search: Assume N = numItems = 15 = 24 − 1.
A[0] A[0] A[2] A[4] A[6] A[8] A[10] A[12] A[14]
• • < A[1] < A[2] < ⋅ ⋅ ⋅ < #(Nodes at #(Compar. this level

J
per node) Number General Total Av 1248 erage

N Σ
#(Comparisons) = =1+[log(N n−1 If i=0x case of
#(compar
1×1+2×2+3×4 #(Comp.) < x comparisons were A[7],

T U
(N 1234 Av erage = A[6], x 2 >=

. n per node
+ O(log A[3], − 1) then = = 1): for −
1×1+2×2+3×4+4×8 49/15 + 1]. x A[3] we Total N) an ⋅ ⋅ ⋅ > (N
W at level
+ item would A[5], =

i)×#(nodes
n×2#(Comparisons) 3⋅ 3. + 1) x: n−1 and make =

o
A[7] O(N.log =1+(n x = 4 A[6]. comparisons:
A[14]

rld
A[11]

= 49;
=
at level
− 1)2n
N) A[1] A[5] A[9] A[13]
i)

A simpler argument:
• Max(#Comp) = n and hence average ≤ n = O(log N).
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.20

HEAP DAT A-STRUCTURE


Heap: A special kind of binary-tree, which gives an efficient
O(N.log N) implementation of selection-sort.
• Shape constraints: Nodes are added left to right, level by lev
el. − A node has a rightchild only if it has a leftchild. − If there
is a node at level m, then there are no missing nodes at
level m − 1.
• Node-Value constraint: For each node x and its children y,
val(x) ≥ val(y), val(x) = the value associated with node x.
Example: The shape of heaps with upto 7 nodes.

Questions: (1) Each (2) (3) (4) Example. J Each The

N
The 1 leftchild properties node node Which
T
has has node of exactly 0 (1)-(3) or the with 2

following children, define one no parent, brother a except heap.


is true?
except has perhaps the the maximum root.
one.
Heaps with upto 4 nodes and small node-values.
2
1
U
1 height. 33

4
2
21

W
4
23

orld
4
32
31
1
1
2
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.21

ARRAY-IMPLEMENTATION OF HEAP
Array-structure for Heap of 12 nodes:
A[0]
A[1] A[7] A[8] A[9] A[10] A[11]
• A[3] ≥ A[7], A[8] • A[4] ≥ A[9], A[10] • Parent-Child •
N
EXERCISE 1. J Not Show dependent

A[5] ⋅ ⋅ ⋅

T
A[0] A[1] relations ≥ = = on A[11] max{A[0],
U
max{A[2], values in the at A[2] A[1],

W
⋅ ⋅ ⋅ , • • • A[11]}
o rld
A[1] A[2] A[0] ≥≥≥

A[3], A[4] A[5], A[6] A[1], A[2]


A[3], A[5], A[6], A[11]}
Array: the nodes and does not use pointers.
leftchild of A[i] = A[2i + 1] rightchild of A[i] = A[2i + 2]
all possible heaps with 5 nodes and the node values {1, 2, 3, 4,
5}. A[3] A[4] A[5] A[6]
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.22

HEAP-SORTING METHOD
Tw o Parts in Heap-Sort: Let N = numItems.
• Make the input-array into a heap.
• Use the heap to sort as follows: − Exchange the max-item at
root A[0] with A[N − 1]. − Make A[0. . N − 2] into a max-heap:
each child-value < par-
ent-value. − Exchange the next max-item (again) at A[0] with
A[N − 2]. − Make A[0. . N − 3] into a heap and so on, each time
working
with a smaller initial part of the input-array.
Example. Part of the heap-sorting process. 7 4

NT
J 495 0
U Exch(9, 0);
A[0] 8 = 9, A[9] =

W
70

459
59
orld Exch(0, 8)
60 to make

heap 8 8
70
6213
6213
6213
459
Exch(4, 6) Exch(0, 3) Exch(8, 5);
to make heap to make heap 8 A[0] = 8, A[8] =
5
Exch(5, 7)
5 to make heap 7 7 3
73
53
6210
6210
6210
489
489
Exch(5, 6)
to make heap
Exch(7, 4);
7 A[0] = 7, A[7] = 4 4 6 3
63
43
5210
5210
5210
489
789
789
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.23

HEAP-SORTING ALGORITHM
MakeHeap, • • AddToHeap(i, 1. 2. MakeHeap(numItems): 1. 2.

J
HeapSort, • • HeapSort(numItems): 1. 2. nums[(n

For Implements Uses an the nums[n If //nums[i] Else, If


N
Otherwise, (a) (numItems (numItems heap If

Otherwise, (a) (b) i If next = Heap-structture (nums[i] for (this


Find If and n using − by − (nums[j] using − (i=numsItems/2

T
next 2], has 1)..(n numItems): call 2, is adding index
do Selection-Sort. ⋅ ⋅ ⋅ , the n recursion = no = have do the
AddToHeap(j, max the − 1) 1) nums[0] − top-level children 3,

U
the j //sort 1)] > nums[i] following: stop.

stop. recursive of no //make ⋅ ⋅ ⋅ , nums[i]) and following: to is


the children) //call 1, nums[0..(numItems-1)] successively an
and in so 0, - largest if call) to heap. nums[0..(numItems-1)] 1;
that i make for AddToHeap: on, > AddToHeap: the then i≥0;
W
numItems). numItems/2 then order

i=numItems-1, stop. child-items heap filling i--) the


exchange(nums[i], MakeHeap(numItems) find //2i+1 with tail

o
AddToHeap(i, nums[i the part the the - n > of 1.
places = + numItems-1 max, right nums[i..n numItems-2,

r
nums[i]. by numItems. 1..n heap-sort into item. the −

ld
numItems). nums[n nums[j]) 1]. a next −

heap 1] ..., max, into


− 1],
0
(b) Exchange(nums[0], nums[numItems-1]),
AddToHeap(0, numItems-1), and HeapSort(numItems-1).
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.24

UNDERSTANDING MakeHeap(numItems)
Input: nums[] = [3, 2, 4, 5, 1, 0] is not a heap; n = numItems =
6.
a[0]= 3 a[1]= 2 a[2]=
4 MakeHeap(6)

= a[3] 5 =a[4] 1 =a[5]

J
0 a[i] for nums[i], in short. MakeHeap(6): (1) (2) (3)
N
AddToHeap(2,6): AddToHeap(1,6):

TU
AddToHeap(0,6): Makes 3

calls max-child nums[5] max-child nums[3] calls to AddToHeap


W
AddToHeap(3,6); = = index 0 >/ index 5 > 3

o r
42 as j = j = 2 3 15
ld
0

4
shown below:
= 5; nums[2], do nothing = 3; nums[1], exchange(2, 5);
//does nothing
54
210
max-child index j = 1 nums[1] = 5 > 3 = nums[0], exchange(3,
5); calls AddToHeap(3, 6); //does nothing we get the final heap
as shown on top.
Question: How can you modify AddToHeap(i, numItems) to
elimi-
nate some unnecesary calls to AddToHeap?
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.25

UNDERSTANDING HeapSort(numItems)
•••• J MakeHeap(6) [3,2,4,5,1,0] Shown Each We Calls

N
Heap [3, call An child 2, only item or node of to

4, and the below 5, root-node) AddToHeap show is AddToHeap,


T
exchange 1, shows shown 0]. are the

exchg(a[0],a[5]) exchg(a[0],a[4]) exchg(a[0],a[3]) exchg(a[0],a[2])


[2,1,0,−,−,−] [3,2,0,1,−,−] [4,3,0,2,1,−] [5,3,4,2,1,0] the the initial as

U
before operations. resulting input-array
marked recursive and part it the is of by

W
hidden from exchange-action, calls the

AddToHeap(0,5) AddToHeap(0,4) AddToHeap(0,3)

AddToHeap(0,2) overstrike to [0,1,2/,−,−,−] [1,2,0,3/,−,−]


[1,3,0,2,4/,−] [0,3,4,2,1,5/] its array MakeHeap(6) to away action,
o
HeapSort, of (such in interest which remaining

r
for as are calls [1,0,−,−,−,−,−] at 5/ [2,1,0,−,−,−] [3,2,0,1,−,−]

HeapSort(6) [4,3,0,2,1,−] HeapSort(5)


HeapSort(4)

HeapSort(3)

ld
HeapSort(2) is [3,2,4,5,1,0] sorting for not

each a to function- nodes. 5 shown. Make-


in point. input
3rd
[1,0,−,−,−,−] [0,1/,−,−,−,−] [0,−,−,−,−,−,−] exchg(a[0],a[1])

AddToHeap(0,1) HeapSort(1)
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.26

PROGRAMMING EXERCISE
J
1. Implement nums[0..(numItems-1)] Keep (a) (b)

N
First 1, exchanged, initial AddToHeap-function.

(parent, ⋅ ⋅ ⋅
Then, ments, sively with place). Successive [9, [1, [8, [1, ⋅ ⋅ ⋅
a constant ..., 8, 8, 7, 7, void void void the run 6, 6, 6, 6, after

T
heap-formation. the 9], run The 7, 7, 3, 3, child) the

root-item MakeHeap-function AddToHeap(int MakeHeap(int


HeapSort(int 4, 4, 4, 4, and array NUM_ITEMS heap HeapSort-

U
function. commenting one first 5, 5, 5, 5,
show following 2, 2, 2, 2, exchanged: pair after array few 0, 0,
0, 0, as (which 3, 3, 1] 8] a per lines each forming 1] 9] global

W
numItems) and These numItems) out line

itemNum, = of puts pair nums[4]=5, after 10. functions; this the


variable. for (as outputs the This the of output detailed exchange
o
the shown heap current numbers int time input will

numItems) may nums[9]=10 and below), you level you

r
max nums[0..9] be with look after (parent, show
ld
generated output-state- can in root-item:

like: during exchange the succes- child) = right keep


the [0,
by
(c) Repeat (b) also for the input [1, 0, 3, 2, ..., 9, 8].
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)

1.27 COMPLEXITY OF INITIAL HEAP Cost • • • • •


J
It The Each Total Total to − − and of may a

N
At The at particular exchanging terminal Adding

change cost for most least cause number all of


T
log n/2 nodes adding a node. takes

FORMATION at shape Node 2most the of (n nodes ≤

U
at nodes terminal + node a n.O(log of x: 1) .
most changes adding x node an to are with on the n-node a node ≤

W
terminal this a constant heap c.[ logn) to that

path FOR The on node = the heap O(n.log n child). from shape

2= nodes nodes (n time n 6 means: + ITEMS nodes root


o
1) −1] of c (no along n). (finding a to heap work a

r ld
= the terminal O(log for path largest

these).
n). from node child is x
A better bound O(n) for Total Cost: Assume 2m−1 ≤ n < 2m.
• Total cost ≤ 1.(m − 1) + 2.(m − 2) + 4.(m − 3) + ⋅ ⋅ ⋅ +
2(m−2).1 = O(n).
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.28

COMPLEXITY OF HEAP-SORTING
Computing • Each nums[0] − • Total Total • Initial • Rest •

N
Total J for This of of = takes Heap-

Sort: heap O(n) heap-sort: this is max, to O(log the formation:


T
one phase + next O(n.log heap exchange n).

U
O(n.log for max, (of all O(n). size n) nodes:
W
next = and n). less O(n.log next one n.O(log

o
than max, re-heap the n). previous n) ⋅ ⋅ ⋅ :=
r
operation O(n.log one).

ld
n). of adding

www.alljntuworld.in JNTU World


Downloaded From JNTU World (http://www.alljntuworld.in)
1.29
APPLICATIONS OF SORTING
Car-Repair Scheduling:
You hav e a fleet of N cars waiting repair schedule times (order
rk for the car of repairs) Ci, to 1 ≤ minimize being out-of-service.
Example. Let N 6 possible = 3, repair-schedules. and r1 = 7, r2
Repair Schedule Question:

J
•? •? Best Worst Show What ⋅ ⋅ ⋅ , 〈C〈C〈C〈C〈C〈
N
C 112233Cschedule: , , , , , , Nschedule:

CCCCCCdoes that 〉 231312is , , , , , , CCCCCCN.rthe this

T U
323121 〉 〉 〉 〉 〉 〉 7 〈Clost 〈C 72
2 6 6 lost completion 7+6=13 6+7=13 7+2=9 2+7=9 2+6=8

W
Repair 6+2=8 2, service-time C3, C1〉,

1, service-time C3, C2〉, total service-time loss 1 + (N − 1).r2 + (N


say about the optimal k = times for ≤ 7+2+6=15 2, N. and repair,

o r
What the r3 = total with 6. is the service-
ld
time There Total the lost best 31

estimated
time are lost repair- 3! for =
7+6+2=15 35 2+7+6=15 26 2+6+7=15 25 6+7+2=15 34
6+2+7=15 29
= 2 + (2+6) + (2+6+7) = 25
= 7 + (7+6) + (7+6+2) = 35.
for the repair-order − 2).r3 + ⋅ ⋅ ⋅ + 1.rN.
〈C1, C2,
repair-order?
•? If C2〈C, ⋅ ⋅ ⋅ , 1, CCm2, 〉 ⋅ ⋅ ⋅ , an Coptimal N〉 is an
optimal repair-order for all cars, repair-order for Ci, 1 ≤ i ≤ m <
N? is 〈C1,
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.30

PSEUDOCODE FOR OPTIMAL CAR REPAIR-

SCHEDULE
Algorithm OptimalSchedule:
Input: Output: Repair Optimal times repair ri schedule for car C
〈Ci, 1 ≤ i ≤ N.

i1, Ci2, ⋅ ⋅ ⋅ , CiN〉


1. Sort the cars in non-decreasing repair-times ri1 ≤ ri2 ≤ ⋅ ⋅ ⋅ ≤
riN. 2. Optimal repair schedule N.ri1 + (N − 1).ri2 + (N 〈C− 2).ri1,
Ci3 i2+ , ⋅ ⋅ ⋅ , ⋅ ⋅ ⋅ + C1.riN〉, iNwith .
total lost-time =
EXERCISE 1. Give #(additions (rto 1 simplify + r2)+(rthe 1 +
expressions r2 and + r3) multiplications) + ⋅ ⋅ ⋅ first.)
+ (r1 + r2 + needed ⋅ ⋅ ⋅ + rN). to (You compute may want r1 +
2. How much computation is needed to find the lost service-
times
for all schedules? 3. What is the
J a link

T
N B: A: (x, 4 3 y) optimal means C: D: E:

car-repair order for the situation below, car x must be repaired


before car y?
where
2

UW
1 G: 7 6

orld The number next to


each

F: 5
car is its repair time.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.31

ANOTHER APPLICATION: FINDING A

CLOSEST PAIR OF POINTS ON A LINE


Problem: Given a set find Pi and of points P j such that Pi, |P1 i
≤ − i ≤ P j| N (≥ 2) on the x-axis,
Application:
Brute-force 1. 2. Implementation besti for Question:

•? J Give For Find implementation nested new (i=0;


N
for If pair one = (each algorithm Pthe Pa 0;

(j=i+1; if |iof 1 slightly for-loop; {P’s i<numPoints; pair

bestj ((currDist { approach: them. Prepresent 1 i|, 2 besti

T
≤ P (i, (combines i jj<numPoints; using }
U
different < = to j) it means j 1; = P avoid

which ≤ should |national 4 i; a Complexity N), PminDist =


suitable |i++) 6

Dist(points[i], bestj algorithm it steps the compute gives


have might parks repeated ////numPoints j++)
W
(1)-(2) = = test-data. the Pfewer

|Dist(points[0], j; O(Nbe 5 dalong (a smallest ij PminDist


easier variant |assignment to = 3 2computations. ).

o
distance(Pavoid a points[j])) freeway, is to
d{Pclosest of ijminimum. > = storing . find 4the , 1 currDist;

r ld
"besti Pi6pair. } is the

then a closest a camp-site , P j).


dij’s): points[1]); < minDist) } above) and = i" in Explain
in its the the
•? Restate the pseudocode to reflect the implementation.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.32

A BETTER ALGORITHM FOR CLOSEST PAIR

OF POINTS ON A LINE
P|1 P|2 P|4 P|6 P|5 P|3 The New Method:
• The point nearest to Pi • Finding immediate neighbors points
Pi.
Algorithm NearestPairOfPoints Input: An array nums[1:
Output: 1. 2. 3. Complexity:

• • A pair to each Sort nums[1. Find 1 ≤ j J Sorting

N
Total Output time. = O(Nlog nums[j] takes of
T U
other. < N . N] items such and in

increasing nums[j that nums[i] is N] to of its and N

W
(on immediate order. of numbers. nums[j] a
o
each line): P left which i O(Nlog N). A geometric

r
{Pclosest requires or 4, right. P6pair. }
ld
is sorting the

are nearest
the
nums[j + 1] − nums[j] is minimum.
+ 1].
N) time; other computations take O(N)
view sometimes leads to a better Algorithm.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.33

A MATCHING PROBLEM Problem:


• • Brute-force 1. 2. Question: Complexity: • • Scores scores
yx1 1 < < yx2 2 < < ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ < Match male and female
y y
minimizes E ences in scores = Σ(xfor the i •1 •1
J
Computing Computing For matching-pairs Find

N
each the How The method: O(N. permutation
T
permutation E N! except many possible for

U
permutations N!). xa i ways for

permutation: ↔ < yN xN for for N N female male students


students students − matched-pairs. yi′)2 relative interchanging

Mi↔Fi′ in (1 ≤ i ≤ N), the squared y•2 x|1 positions

W o y
xx||1 1 y ••2 1 y•2 xi’s of with xi’s

r ld
an FxM| i2 1-1 . i sum in fashion a
xx||2 2
and yi’s.
yi’s
test, and
that of differ-
y(yi′.
⋅ ⋅ ⋅ , yN′) of yi’s, compute E for the
1′, y2′,
that gives minimum E.
the students can be matched?
takes at least N(N!) time.
O(N); total = O(N. N!).
• Finding minimum takes O(N!).
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.34

A BETTER METHOD FOR THE MATCHING

PROBLEM
Observation: (1) (2) Question:
•? •? Pseudocode 1. 2. Complexity: EXERCISE 1.

J How Consider matching a Sort Match Is problem


N
yThe 2 The smallest iform ’s.) it in xcan possible

each matching Every same i’s Mthat to and you (exploits i N

T
O(Nlog y xwith of ia generalizes holds gives ↔y=
efficient smaller ythe prove 3, ito ’s {xFi and three i′ give (if the

U
solve for 1 output-properties): N) if (1)? ↔

they size) yminimum xAlgorithm all + minimum 1 i cases. yto


and O(N) < the 1N are , all or yx2 > 2 yproblem not by < N i′ ↔

W
= 2: have xE. and O(Nlog divide-and-
conquer? exploits 1 sorted). matching E. y2< } to the y(Your giv

o
3 by all < same es N). some recursion xdistributions

ith the 2 argument < rank. smallest smallest properties

r ld
x3 . E x with for N ith =
Argue that the should be in of xi’s and
(reducing the
of input, output, or input-output relationship.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.35

2-3 TREE: A GENERALIZATION OF SEARCH-

TREE 2-3 Tree:


• An ordered rooted tree, whose nodes are labeled by items from
a linear ordered set (like numbers) with the following shape con-
straints (S.1)-(S.2) and value constraints (V.1)-(V.3). (S.1) Each
node has exactly one parent, except the root, and
each non-terminal node has 2 or 3 children. (S.2) The tree is
height-balanced (all terminal nodes are at the
same level). (L.1) A node x with 2 children has one label,
following property, where and right subtree at x.
TL(x) and TlabelR(x) 1(x), are with the the left
labels(TL(x)) < label1(x) < labels(TR(x))
(L.2) A node x with 3 children labelmiddle 2(x), subtree with at
the x.
following has two property, labels, where TlabelM(x) 1(x) is the
<
labels(TL(x)) < label1(x) < < Example. J 1 (L.3) 1,2

N T
A Some 1 terminal 2 3 small
U
node 2-3 1,2 trees.

may have 1 or labels(Tlabel2 labels.


2(x) M< (x))
labels(TR(x))
min number of labels = 3
3
3,6
W orld
4,5 1

2,4
35
1,2 4,5 7,8 max number min number max number of labels = 5

of labels = 5

of labels = 8
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.36

SEARCHING A 2-3 TREE


J
Searching • • • Role • B-tree • Compare Compare

Compare Ensures These middle kdata-structures 9 kof 1 or

N
and < kBalancedness 1x k, are for = 2
kBoptimum 2 < x x kx +-tree: 10more a kand and and

T
k3 , value 3 the < used the the the kgeneral 4 value

U
search k < Property 4 values kvalues values
in 9 k≤ 5 databases is k< x efficiency. 5
form found, k≤ at at at 6 kkthe the < the of

W
610 , kof : k2-3 7 root: middle else 7 right to <

2-3 trees: koptimize 8 x kchild: trees, < 5 is kkchild: <

o
89k not , , 9 x; kk< 11 10 kwhich branch there. search
r
k8 k10 9 < ≤ < x x k< are efficiency right 11 k≤

ld
k12 < k11the 10; k; 12 branch

if main x for =
very large data-sets. (We talk about them later.)
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.37

BUILDING 2-3 TREES


Shapes of 2-3 Trees (with different M = #(terminal nodes)):
M=1
M=2 M=3
M=4
M=5 M=5 M=6 M=6
Adding 1 to an empty tree:

JN
Adding 2: Adding Adding 3: 4: Find
T U
Find the place 1, 2 Find the place for

3, for 1 split 2, and add if 1 no 2 add space 1, if 2 there adding add 3


1, 2, 3 place 2

Wo
2 is space.
rld
a parent node.
2
split
13
for 4 and add if there is space.
13
add 4
1 3, 4
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.38

CONTD.
Adding 5: Find place for 5, split if no space adjust by merging.
2
2
2
1 3, 4
add 5
1 3, 4, 5
split
13
Adding 6: Find place for 6, and add it if there 2, 4 1 3

JNTU
5

W
6
split
57

o r
4 5 is adding space. merge a

ld
1 parent, 2, 3 4
and
5
2, 4
add 6
1 3 5, 6
Adding 7: Find place for 7, split if no space adding a parent,
adjust by merging, and if no space, then split by adding parent
again.
2, 4
1 3 5,6 Question: Show the results after adding 1.1, 2.3, and 1.2.
2, 4 2, 4
6
add 7
1 3 5, 6, 7
split
135
7
merge
4
2 2, 4, 61 3
1357
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.39

EXERCISE 1. How many ways the 2-3 tree on the left can arise
as we build the 2-3 tree by inputting {1, 2, 3, 4} in different
order. What were the 2-3 trees before the 4th item were added?
Show that the two 2-3 trees on the right arise respectively from
48 and 72 (total = 120 = 5!) permutations of {1, 2, ⋅ ⋅ ⋅ , 5}.
3
1,2 4
2,4
2. Show the minimum and be stored in 2-3 trees labels in the
nodes (using 3.
J N What quickly use of

T
information this find 3, information 4 the 8
U
key-value we 9 to can with the 1,2 find the

W
maximum of store 5 3 numbers the the and 4,5

9th at ith 6 the item 1 terminal smallest 1, number 2,


o
nodes in 3, 3 the ⋅ ⋅ ⋅ ) data-items item? nodes. of

r ld
2-3 for 5 a both tree 2-3 Explain

Show below. that cases. tree can the


the to
15
20, 28 16, 18 21, 25 30
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.40

TOPOLOGICAL SORTING OR ORDERING

NODES OF A DIGRAPH
Topo. Sorting (ordering):
• List the digraph’s nodes so that each link goes from left to
right.
• This can be done if and only if there are no cycles in the
digraph.
An acyclic A
C Tw o digraph.
B

orderings:
A
•B


• Questions:
J
•? •? The Show Is 4 the and "no" nodes it

N
link topological at true answer, all least (A, {A,
T
possible that D), one B, giv each C, how orderings e

U
sink-node? topological an D} acyclic many
W
examples and A topo. A AC = The schedules C

o r ld
BB 〈A, 〈C, C, B〉

A, B〉
for the tasks at nodes.
orderings of the digraph below with two links {(A, B), (C, D)}. If
we add of these top. ordering are eliminated?
BCD
digraph has at least one source-node Is the converse also true?
For each
to illustrate your answer.
•? What is the maximum number of links in an acyclic digraph
with
N nodes? What is the number if we allow cycles?
•? Show all possible acyclic digraphs on 3 nodes (do not label
nodes).A digraph C Any linear arrangement of the nodes will have at

least link going from right to left. with a cycle.


No topological ordering.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.41

PSEUDOCODE FOR TOPOLOGICAL

ORDERING
Pseudocode: 1. Choose a node x which is currently a source-
node, ceding nodes (if any) have been output, 2. Repeat step (1)
until all nodes are output.
AB
J N
Example. Relevant • A − − stack A

T
{A, A node Data to Shown ticular B} keep x

Structures:
U
{B, B enters choice track below C} the of C

{C, of are stack current x D, C possible at E} when each

W
source-nodes. D {D, D G iteration Fit choice

becomes E} E When we remove x from the stack, it, add new


or
source-nodes to the stack i.e.,

ld
all its pre-

of nodes x and a par- of step (1).


{E} {F} {G} E F G
a source-node. we delete the links from (if any), and output it.
• Keep track of inDegree(x) = #(links to x) to determine when it
becomes a source-node.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.42

USE OF STACK DAT A-STRUCTURE FOR

EXERCISE 1. 2. Stack 〈A, 〈A, 〈A〉 〈C, 〈C, 〈C, 〈C〉 on

J
Source Sink D〉 F〉 G〉 right) B〉 E〉 Show the
N
(Remember Show queue (top C Stack link nodes

nodes A in the instead D (G, = Selected B E A D F G C a Node =

T
processing nodes = table G FB {C, the {A, A),
TOPOLOGICAL-SORTING E

U
x G}. of algorithm with B}, form which a A: −

− − − − current stack the in 0 Nodes creates outDegree(y) source-nodes


the inDegree(y) processing sink-nodes stops adjList(D) adjList(G) B: −
− − − − − − adjList(x) in inDegree(x) Topo-Sorting and 0
W
the one when their C: 1 1 0 − topological-

sorting or = = = = =
= = 1 of initial the more number number {x: {z: adjacency-list = the 0 D:

o
110−−−− stack outDegree(z) inDegree(x) and
algorithm or 2 digraph cycles reduced of of not become E: 0 − − − −

r
− − links links yet 1 in of above output. (x, (y, inDegrees is

ld
after the node is Algorithm. F: 2 1 1 0 − −
− 0}
empty.)
0} z) y) 2 digraph. using from to x adding
y

G: 2 2 2 1 0 − − y
〈F, G〉 empty-list
2
a
Use the notation 〈A, B, C〉 for a queue with C as the head and A
as the tail. If we add D, the queue becomes 〈D, A, B, C〉; if we
now remove an item, the queue becomes 〈D, A, B〉.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.43

ADJACENCY-LIST REPRESENTATION OF A

DIGRAPH
AB
CDE

FG

Array Index

Node Adjacency-list name & outDegree 0 A; 2 1 B; 2 2 C; 0 3 D; 3 4 E;


1 5 F; 1 6 7
typedef } Adjacency • J st_graphNode; This

N
is not Matrix struct char int suitable
T
outDegree,

*adjList; //*linkCosts;
nodeName[MAX_LENGTH];
Representation:

U
{ for G C H D F E B A some ABCDEFGH 0 0
W
0 0 0 0 0 0 G; H; 0 0 //array array of

our algorithms. 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0

0 0 0 0 0 0 0 1 1 0 0 0 indices; array-size [2, 3] [3, null [5, [5] [6] null

o
null 4] 6, 7]
r ld
= outDegree(x) of node size =

outDegree size = outDegree 0 0 0 1 0 1 0 0 H 0 0 0 1 0

000
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.44

TOPOLOGICAL SORTING ALGORITHM


Computation 1. 2. Initialization 1. Selection 1. Processing l.
J
2. Algorithm Input: Output: 1. 2. 3. Compute

Initialize While (a) For For Initialize Select Add For

N
add (a) (b) (each (each (each Let node of An A

(stack top(stack) TopSort(): reduce node if 1 topological a the


indegrees i stack acyclic to (inDegree(j) of node node node of

T
node i = to inDegree(j); stack. is stack: inDegrees:

i:
output; top(stack), inDegree(j) not with i) i j to and digraph, and

U
in initialize empty) process: of (stack
adjList(i)) ordering nodes for delete all = each nodes. 0) with =
do delete of inDegree(i) by add it array of indegree

W
the from j one; do adjLists in its j following:

the it of to adjList(i) nodes. the stack; size from following: zero;

o
stack; = representation. numNodes) 0; stack,
r ld
and add it to topOrder-array; (b)

Process node i;
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.45

COMPLEXITY ANALYSIS OF Observations:


• • Fact:
Example. Complexity:
N
• J 0 Each We Since + − − − − − − 0 all
Σ

look + All All updates; look First Then, updated Then, updated
inDegree(x)
Finally, xlink 1 each TOPOLOGICAL-

SORT links For + links at at time, 2 (x, another exactly also all
T
+ the of when by by specifically, are are 1 y) links

in the digraph adding subtracting + each of looked initializing

U
looked 1 it operations inDegree(x) the
=
inDegree(x)
Σ
+ (x, is node all 2 taken digraph 1 + on y) xat till at
outDegree(x)
2 from when once x page 1 = the it out exactly

W
inDegree(x) till 9 reaches listed is and second

in x 1.43, of many it many we processed the becomes computing

the 2 2*inDegree(x) remove above + ALGORITHM second


= #(links
the the stack. times times time 2 + two =
o
value 0 takes 0. exactly 0. in + time. x as as sums

in
r
the course 3 from inDegree(x). it it + indegrees. a 1 is

the
is are twice.
ld
constant + + the 2 successively successively

digraph).
of 1 times. + stack, the 0 = time, stack
9.
we
total computation time is O(#(nodes) + #(links)).
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.46

PROGRAMMING EXERCISE 1. Implement TopSort. • •


JN
Use In and File 8 0 1 2 3 4 5 6 7

inDegree[0..numNodes-1]. for //numNodes; (2) (2) (0) (3) (1)


(1) (0) (0) stack=[0 stack=[0 topologicalSort(), a digraph.dat
build 2 3 5 5 6 (i=0; outDegree adjList for It

T
function 3 4 6 a should 7 function the 1], 4], (j=0;
inDegrees[adjList[j]]++; i<numNodes;

U
node node readDigraph() next produce

adjacency-list for = topologicalSort() lines the selected selected


= nodes[i].adjList; j<outdegree; use
nodes[i].outdegree; digraph one give: a
W
dynamically Compute line = = to

representation node 1, 4, on of read i++) topOrder-array


topOrder-array output page (outdegree) inDegrees an

o
based {
r
j++) input 1.43 allocated as on shown is of file the by

ld
shown adjacent-nodes = = the local [1]

[1 digraph.dat below. algorithm


4]
digraph. below.
array

}orfor (i=0; i<numNodes; i++)


for (j=0; j<nodes[i].outDegree; j++)
inDegrees[nodes[i].adjList[j]]++;
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.47

EXERCISE 1. Given an ordering of the nodes of an you check


if it is a topo. ordering? explain your algorithm using the acyclic
2. How can you compute a topo. ordering (Hint: If outDegree(x)
= 0, can we place 3. Modify topological-sorting algorithm
numPathsTo(y) = #(paths to y starting State clearly the key
ideas. Shown below also the paths for the digraph G →on page

J x
4. Hints: (a) (b) Modify G C D F E B A
N
If sTo(x) which How numPathsTo(x) PathsTo(x)

(x, your num-

T
will 1 1 1 2 1 3 5 y) of and them is algorithm you a
U Paths
numPathsTo(y). link, should for 〈A〉

〈B〉 〈A, 〈A, 〈B, 〈A, 〈A, compute {x: C〉 E〉 D〉, D, D, what
//trivial to be (x, G〉, F〉, compute 〈B,

W
computed is y) numPathsTo(y) 〈B, 〈A, D〉
path the is What D, D, a acyclic Give digraph without x in to

o
1.42. compute are at from A F〉, 〈B, F, G〉, relation

does first? link to numPathsFromTo(x, a some numPathsTo(y) a

r
topo. digraph, using on pseudocode for page source-
ld
node). ordering?) all inDegrees? how

1.43. nodes will and


and y,
to A, with no links.
E, F〉, ⋅ ⋅ ⋅ , 〈B, E, F, G〉,
between numPath- it suggest about
in terms of all y}?
y) = #(paths to node y from node x) for all nodes y to which there
is ≥ 1 path from x (which may not be a source-node). Explain the
algorithm for x = A and y = F using the digraph shown earlier.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.48

TOPOLOGICAL ORDERING AND TASK

SCHEDULING
Precedence Constraint on Repairs:
• Each link (x, y) means car x must be repaired before car y.
A: 3
B: 4
C: 2
D: 1 G: 6 E: 7
Possible Repair Schedules:
• These are exactly • Two repair-schedules Question:

•? •?
J N What What is is 〈A, 〈B, the the
T
optimal algorithm B, A, C, C, all D, D,

U
the E, E, and F, F, topological their G〉: G〉:
W
lost schedule? for creating F: 5

The
o r number next
car is its repair time.
ld to each

orderings. service-times:
3.7 + 4.6 + ⋅ ⋅ + 6.1 = 96 4.7 + 3.6 + ⋅ ⋅ + 6.1 = 95
optimal schedule?
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.49

ALL POSSIBLE SCHEDULES


An Acyclic Digraph of Task Precedence Constraints:
AB
G
CDE F
The Acyclic Digraph for Representing • Each node represents
the tasks • Each path from the source-node gives a schedule.
∅ J AA

NT
B C
B C AB A D
U
EB ABC ABD

W
D
EC ABCD completed.
o
∅ E21 to Schedules: the sink-node

rld
ABCDEFG

AC
EABCE
E
BE ABE

C ABDE
D
ABCDE FCA

F
ABDEF C
G
• The number of these paths gives #(schedules) =
ABCDEFG

#(topological
orderings).

B
ABCDEF
123 12
5 AABABC ACABDABCE
1

3
BBEABEABCD 6
16 ABCDE
21
5
ABCDEF
5
ABCDEFG
ABDE
ABDEF
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)

1.50 SOME OTHER APPLICATIONS OF Expression-


J
Tree: • • • Evaluation • • Each The The The This

N
each operator ing the Visit terminal children

children requires node non-terminal expression Method: at the


T
x∗ 3+2 is the the children nodes of the STACK

x∗ 3+2of x∗ 3node. It a x expression post-order a is non-

U
terminal − at node are non-terminal ∗ an
√ a from xnode. ordered + the 3 2 DAT + gives 9 2

basic left x^2 corresponding traversal A-STRUCTURE =

W
− ((x∗ 3) to tree an node = operands. node

right, xx operator; 2(not are of ^ + give the and 2) a evaluated √


o
+2 binary − to tree: then √ xalso, the √

r
the 2 x x2 + 2 + subtree the tree). 9 operands +

associated 9 before 9
ld
node.

at evaluat- it. of with the


Post-fix form (corresponds to post-order traversal): x 3 ∗ 2 + x
2^9 +√ − 9
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.51

POST-FIX EXPRESSION EVALUATION Processing •


J
• • Example. EXERCISE 1. Processing Processing

N
The the value the Show "x ses uating 2 end as

Stack
stack, final 3 to an needed, 〈12, 〈14, this x If of 〈4,
Method: stack. ∗ 〈12〉 〈14〉 Top infix x value 〈4〉 processing.

T
apply post-fix ∗ an an 3〉 2〉 4〉 = + 4, of operand:

operator: but 2 expression then of stack After processed 3 ∗ 2 +

U
/ x x the Stack 15 no the expression
USING x operator +"; unnecessary is item expression 3 add
remove is the ∗ make initially that 2 its right + A

W
value to for 〈14, x give sure the 〈14,

STACK 2^9 in 〈14, 〈14, those empty. is x ones. 〈14, 16,


Stack
operands the rise that to = 4, the 16〉 25〉 〈9〉 5. +√ stack.
o
2〉 9〉 5〉 notation values, to you only Show − the

equals After processed 2 ^ 9 + √ − use of item post-fix the the

r
and 〈⋅ ⋅ ⋅ 〉. proper item 9. stacks in operator add
ld
the expression parenthe- the in stack

from eval- new


at
2. Show the stacks in converting your infix expression in
Problem
#1 to the post-fix form (using the method on next page).
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.52

CONVERTING ARITHMETIC EXPRESSIONS • •


J
Conversion • • • • • Stack Operator Processing

Processing Processing Processing If and − − Input: Output: end

N Stack
While and Then a has 〈+〉 〈+〉 〈−〉
function-symbol 〈∗ 〉 〈∗ 〉 of 〈〉 output Method: input, priority:

T
add only ((stack an ’(’ an ’)’: x∗ 3+2 x Item proc. ∗

3 + 2 − x ’op’ 3 operand: operator or operators, it. output ∗

U
remove a 2 ≠ TO {+, (See to function-symbol:
Output
Initially, + ∅ ) − 3 ∗ 2 + x stack. x −} sqrt(x every

POST-FIX below ’op’: next and Output 2 everything <⋅


including ^ 9 {∗ , (top(stack) page.) stack thing ^ + it, 2

W
〈−, 〈−, /} sqrt 〈−, 〈−, it. + if 〈−, 〈−, <⋅ sqrt,

sqrt, is 9) in sqrt, sqrt, any; − ^ function-symbols FORM


empty.
Stack
sqrt, sqrt, add from stack. < (’^’ (, (, function-names. (, (, ≥
o
’(’ +〉 +〉 it ^〉 ^〉 ’op’)), (〉 (〉 is stack = to

exponentiation) not stack.

r
^ 2 Item proc. ( + 9 x remove added upto and
ld Output
2 the

^ 9 x to top(stack) output. ’(’.


first ’(’
〈−, sqrt〉 sqrt 〈−〉 ) +, sqrt
〈〉 −
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.53

RIGHT-ASSOCIATIVE OPERATIONS Left • • Right •


J
• • Processing • Processing • Post-fix Post-fix For In

x x x Post-fix exponentiation The Likewise, ’op’) xy∧ z∧ . y; y;}

AND Association: − ∧ = processing Association: y

N
instead y conversion y y = post-fix by − ∧ = 3;}.
ITS z 3 Right Assignment z form form (top(stack) form means

T
means means x of += IMPACT the of of form {x

of Associative operation. to y (x x x x x x post-fix += post-fix +=

U
− = ∧ − = − of > y (y y y; (y Operator (y y)
3 ’op’). − = x y − means ∧ − ON = 3: z += ∧ z) form z) z 3), is

Operator form, x but is y and 3;}. x POST-FIX y i.e.,

W
∧ xyz y x 3 "y "=" not − not += = we z Here,

{y 3 z =. x is =", in − −. (y (x replace = − ’op’: therefore −. Post-


o
fix 3; (y ∧ += ’+=’ we y) x − CONVERSION

3), do = z). ∧ the is y;} i.e., not z, the Form: xyz∧ ∧ test

r
where instead put operator. {y (top(stack)
ld
the += "∧ " instead of value 3; is {x x the

+=
of = of

y in stack (as in the case of processing "y 3 +").
• Other special indicators (called ’lvalue’ are added).
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.54

TREE OF A STRUCTURE-DEFINITION AND THE

ADDRESS ASSIGNMENT PROBLEM


typedef } typedef } ListNode IdName; ListNode;

struct int char double struct IdName struct x;

id; flag, { val; name[14]; ListNodeDummy idName;


ListNodeDummy { *next, *prev; Number of Bytes for
Basic Types:
• size(int) = 4, size(char) = 1, size(double) = 8.
• size(x) = 40, not 4 + 1 + 14 + 8 + 4 + 4 = 35.
id

flag↓

JN
• An actual address
T
x.idName.name[13] x.idName.name[0]

x.idName.name[1] name[0..13] x.idName.flag

U
x.idName.name allocation
x.idName.val x.idName.id x.idName x.next

W
x.prev of the 5 wasted

x bytes components = = = = = = = = = = =

orld
val next prev
of x:
268439696 268439696 268439696 268439700

268439701 268439701 268439702 268439714

268439720 268439728 268439732


• Start-address(x) is a multiple of 8; because displacement(val) =
24 within x, start-address(val) is a multiple of 8.
• It makes start-adrress of id, next, and prev multiples of 4.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.55

CONTD.
typedef struct {
int id; char flag, name[14]; double val; } IdName;

typedef struct ListNodeDummy { IdName idName; struct

ListNodeDummy *next, *prev; } ListNode; ListNode x;

ListNode start=0, end=39 numBytes=40

idName start=0, end=31 numBytes=32


J
id start=0, end=3 numBytes=4 prev EXERCISE 1. Give

N
and Assume numBytes a
TU W
pseudocode

for determining for all nodes of you know the type of each

orld
next start=32, end=35
start=36, end=39 numBytes=4

numBytes=4
flag name val start=4, end=4 start=5, end=18 start=24, end=31 numBytes=1

numBytes=14

numBytes=8
[0] start=5, end=5 numBytes=1
start-address, end-address, an arbitrary structure-tree. terminal
node and you have the structure-tree. (Hint: Your pseudocode
must indicate: (1) the order in which the start, end, and
numBytes at each node of the structure-tree are computed. and
(2) how each of these is com- puted based on values of various
quantities at some other nodes.) [1] [13] start=6, end=6 ⋅ ⋅ ⋅ start=18,

end=18 numBytes=1

numBytes=1
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.56

LONGEST-PATHS • w(x, • Length •


LongestPathFromTo(A, Application:

• Critical-path/critical-task • Assume • The − − B: A: J 43


B

N
A Add The new C: 2 y) 2 of C = 4 length unlimited a

T
acyclic length a new 1 10 path 5 IN D 3 "end"-node
U
of G digraph 1 = (cost AN each resources −1 sum

E
−4 ACYCLIC or 2 link 1 of E): for weight) H F analysis the

W
and (x, 〈A, critical-path for lengths Paths (1)
(2) (3) connect y) C, work of = 〈A, 〈A, 〈A, in G, DIGRAPH

time from link project C C, C, C, of on E〉; G, each E〉; D, its (x, A

o
to tasks analysis: length E〉; E〉; to length links.
r
complete scheduling. y); E sink length length in and it 2 = =

parallel. node can their 2+3 6. = = 2 2+1+1 task be

ld
= + lengths to 5 5 negative. − it. x.
= 1=6 4 D: 1
G: 6
3
A3 B 4
D15
G

6 The digraph for critical-path analysis. The longest-path: 〈B, E, F, G, "end"〉.

end E: 7
4 E The number next to each car is its repair time.
F: 5
1
F7
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.57

TREE OF LONGEST-PATHS
B

4 4
I
2
A
10
C
F23
1
J
1 • • Question: The To which an given nodes

N
ordered obtain tree later x can ≠ contains the

startNode be tree.)
T U
to reduced reached work one

1E
properly) and longest digraph from links D 35

W −1
G −4
o
H

−1
5G

rld
−4 path startNode. (which we from
2
Tr ee of Longest Paths From startNode = A:
• First, we can reduce the digraph so that the only source-node is
the startNode.
B

10 4
I F
2
2A
C from startNode to each node x (It is not a binary tree or
is a must for the algorithm can successively delete source-
those x.
•? Show the reduced digraph to compute longest paths from
node B; also show a tree of longest paths from node B. 2
1D3
3
1
1
E
H
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.58

DIGRAPH REDUCTION
• We actually don’t delete any nodes/links or modify adjaceny-
lists.
• We pretend deletion of a link (x, y) by reducing inDegree of y.
B
4

10 4
I F
2
A
C2
3
11D3
1
5G
−1 E
−4 2
H Reductions Algorithm 1. 2. Notes:

••• J Input: Output: Use We The Compute While have


N
ode digraph and longest-path do a not if then

stack (there ReduceAcyclicDigraph(startNode): not is (x An


Reduced inDegree(x) been indegrees actually for is

T
acyclic to modify is not each processed hold a node
U
algorithm processed) indegrees. not digraph

of y the = the ∈ adjList(x) all x changed. 0. nodes ≠ yet. nodes.

W
adjList(x) startNode works in adjacency-list
Initialize x • • • inDegree(D)=2 inDegree(E)=5 inDegree(F)=2

o r
for form statrtNode − − − 2=3 1=1

ld
1=1 = A: and inDegree(x) = 0) do:
deduce inDegree(y) by 1.
with inDegree(x) = 0 and which stack with all x ≠ startN-
of any node, and thus the
with the reduced indegrees.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.59

LONGEST-PATH COMPUTATION

J
Array Stack • Algorithm 1. 2. 3. Input: Output:

Stack links Apply Initialize for = While (a) (b) inDegree(x) 0


N
Data-structure Data-Structures each parent(x)

and Let For (i) (ii) from holds (stack


ReduceAcyclicDigraph(startNode).
LongestPathsFrom(startNode): parent(startNode) An A d(x)
node (each x a tree which = If Reduce to stack w(x,
T
acyclic all top(stack); ≠ = = = stack (d(x) x of y

empty) with current the path number nodes y) with ∈ adjList(x))

U
have longest Used: and inDegree(y) and

digraph node + Used: to indegree(x) startNode, not do w(x,


parent(y) to print x; longest remove of previous paths the =
which parent(startNode) been links in startNode. y) the

W
following: adjacency-list do: to > path

processed by x to let longest-path = the > each from d(y)), to 1 x.


x 0, d(x) to yet and x longest-path and x on x stack. to reachable
o
= from then if yet. the finally −∞ be it = form to

r
equals current looked startNode. startNode. let and y let

and d(y) from is parent(x) from 0 d(startNode)


ld
at. longest

known, startNode.
then = startNode startNode. d(x) add = but −1
+
y
(using the successive parent-links) and d(y).
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.60

ILLUSTRATION OF LONGEST-PATH

COMPUTATION
B
2
A C Stack Node x 〈A〉 A 〈C〉 C 〈D, G〉 G 〈D〉 〈E〉 〈F, 〈F〉 • J H〉 We

N T 4
can F D E H A; 0 (0, A)

10 4
I F
U
StartNode For each node C; 1 D; 1 (−∞, ?) (−∞, 0+2>−∞ (2, A) 1 →

0 2+1>−∞ (3, C) 1 → 2
3

W 1
1 1 0 ?) 5 y, D 3 inDegree(y) (−∞, 2+3>−∞ (5, 3 E; = G → A.
−1 E
o
3 C) 2 ?) −4 2 and (−∞, F; H (d(y), 1 ?) 7 − 1>5 (6, G) 2 → 1

rld
3+1≤6 parent(y)) G; 1 H; 2 (−∞, ?) (−∞, ?)

2+5>−∞ (7, C) 1 → 0
7 − 4 > −∞ (3, G) 2 → 1

1→0

6+1>−∞ 6+2>3 (7, E) (8, E) 1 → 0 1 → 0

use minus the sum of all positive link-weights as −∞.


www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.61

EXERCISE 1. 2. 3. 4.
J C?)Show

LongestPathsFrom(B) How computation What take Why untill

been (D, N E), to many processed? is can all the all it


T
and remaining complete change those that times and

U
(G, the computations? E) when? a (For as

longest-path link executions links we before in example, (x,


W
process the to y) we suitable y is of (after can

a processed to we RreduceAcyclicDigraph(B) link a must

o
compute table node the (x, look digraph y) forms.
r
during y and cannot the at how the the reduction)

ld
longest-path be links longest-path long

computed (C, does have and


E), to it
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.62

PROGRAMMING EXERCISE 1. J
Develop −Σ|w(x,

N
the discussed. (a) (b) (c) following Print

outDegee link Put should node costs"). Show changed an node


Next, (x, change when and ing output appropriate y) of the
parent(y). a y)|, in link link names the name, inDegree(y)
T
function when the processed, the lines parenthesis)

be in information summed during outputs input (C, (G, in an


d(y) successive links when the parenthesis, nodeIndex,

U
appropriate E): E): digraph, For heading
void then the (C, longest-paths for over d(E) d(E) C, you show
becomes in example, digraph for E), shown
longestPathsFrom(int startNode 2 the stacks all print (3): = =

W
each before with (G, the 5, outdegree, 6,

header-line links form: adjacency-list 3(1), reduction the


parent(E) the parent(E) 0 E), link (one for node node show (x,
are B stack. printing new and 4(3), startNode using (x, per on y),
o
name, computed, d(y) the (D, and (like process. y);

6(5) a instead line) = = the separate the (with final C G E) also,

r
and adjList nodeIndex, "Acyclic startNode). = example

stacks. every should A, of parent(y), values weight for As if the


ld
−∞.) line. with there usual time each

generate digraph:
process- Use digraph of node’s of There Show (Use
link-
give it is d(y) link
and the
the is
a
link (D, E): d(E) = 6, parent(E) = G, final value
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.63

CALL-RETURN TREE Example.


J
EXERCISE 1. Show the Is int {if the

N
definition Fibonacci(int ((n return(1);

resulting the int { } else == call-return if


T
factorial(int 0) below; tree ((n

return(1); OF return(n*factorial(n-1)); ||

U
a FUNCTION-CALLS == binary
tree also (n n) 0) == for show //n fact(3)fact(2)fact(1)tree?

W 3*2 2*1 1
|| 1)) the >= the If initial n)

== 6
(n not, return 0

2
o
//n == what call 1)) values >=
r ld
Fibonacci(4), kind 0 from of

tree each is given


it?
call.
else return(Fibonacci(n-2) + Fibonacci(n-1)); }
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.64

A PROBLEM IN WIRELESS NETWORK


Problem: Given find the the minimum coordinates
transmission-power (xi, yi) of the nodes that vwill i, 1 ≤ i suffice
≤ N,
to form a connected graph on the nodes.
• A node with transmission power P can communicate with all
nodes within distance r = c.√ P from it (c > 0 is a constant).
• Let (reach d(vminir, /c)min node.
v j2 ) be the minimum r ≤ r} form a connected for which the
links E(r) = graph on the nodes. Then, {(vPi, min v j): = gives the
minimum transmission power to be used by

v1 1,1 4,3Question: 1? J What that rAlgorithm min

N TU
rwere min is The links

v
E(1) corresponding 5
W or v
3,2 3,1v 4 6 5,2 v7

5,1v8 to P = 1/c2 r≠ min max for {distance the set of nodes of a


node above? nearest Giv always equal to the maximum , e an

ld
example to then vwhat i: 1 to determine rmin?)
v2

1,2 2,1v3
to show ≤ i ≤ N}. (If would be an
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.65

GROUPING NUMERICAL SCORES INTO

CLASSES
Problem: Find the best grade-assignment A, B, dent-scores
grouping of xthe i, 1 ≤ scores i ≤ N, on a test. That into classes A,
B, Interval-property of a group:
• If scores xi < x xj k (xare i two scores < x j < x• Thus, we only
need to Example. Scores of 23 students 71× || | | × |A bad The best

J
Closest-Neighbor • Each Question: 1? 2? it Give
N × 3-grouping 3-grouping
Find these compared | xi an is

T
application closest to the Property to average
U
of the C(76.2) the best 2-grouping groupings

match | × ×|| ×
× × ××××
|| |k) find 76| are ×

×| ×
W
C(78.9) in |the in × |the in the | group a 81×
×
|same test same × |B(83.4) × |boundaries. (one × |group, goup. |

o ×
B(87.5) 86 ’×’ |(CNP) for Optimal average of the

particular of other groups.


such grouping for weather-data, using CNP for each your
intuition?
× ×
||
×××× × ×××× ×
|| || |then per × || Grouping: data-set × ××××|C,
r ld
⋅ ⋅ ⋅ . is, all etc find in-between

to student).
× ×
|
× |× ×
|91 || A(92.9) A(92.9) group containing
say. below. | × || × || the the × ×|× ××××|stu- best
× × ×|
|96 Do
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.66

TWO EXAMPLES OF BAD ALGORITHMS


Quadrangle DAlgorithm#1 1. 2. Algorithm#2 1. Questions: 1?
2? 3? J Go When Go Which Which What

N
to to would Main the you Algorithm one north-

west FindBuildingA: FindBuildingA: come is


T U
Library. be better a Main Library out

W
of the Administration library, it is on corner

of Quadrangle.
has more clarity?
o
(more efficient)? better Algorithm? EF

rld
AB C

your right.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.67

WHAT IS WRONG IN THIS ALGORITHM


Algorithm 1. Successive • • Cannot Question: 1? 2? 3?

J
j j Input: Output: For and Does nected How with

N
Why = = 2: 3; output (each generate n the i
would do the nodes acyclic can Edges GenerateRandomTree(n):

T
only you above j the be The n = we can 1 = the

possible 1 modify graph)? edge 2, Produced or #(nodes);

U
Always edges want Algorithm be 3, tree: 2,
(i, ⋅ ⋅ ⋅ , 1 2 generated giving GenerateRandomTree(n) to (i, i
j). Show n}, = test for generate 2 3 n j), 1 choose the ≥ and

W
always your i n all 1 2. (i.e., < = edge 3 the

graphs j, 3: //nodes Algorithm. of a the no 2 edge generate (1,

o
random a 1 one random trees generated 3) is = 3 or
is {1, (1, 2 excluded)? (randomly i (2, a 2). 2, ∈ tree. tree

r ld
so 3). ⋅ ⋅ ⋅ , {1, 3 for that n} (i.e.,

2, n ⋅ ⋅ ⋅ , j − 1) a con- = 4.
all trees
or all of them in some order) - what would be an application?
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.68

TREES GENERATED BY GenerateRandomTree(4)


After After
J
first second Question: 1? 2? 3? 4? 1 edge Does

What 1. 2. Give {1, probability? Do applying

N
GenerateRandomTree(4)? Give adding adding 2 edge 3

Let For (a) (b) we 2, a a is 4 the ⋅ ⋅ ⋅ , pseudocode recursive get


(k E the Choose If it?), 1 1 a n}. = = following a {(i, random Only

T
main ∅ 2 2 1, random then Does (empty 2, 3 3 6 j)}∪E

different random Algorithm inefficiency ⋅ ⋅ ⋅ , 4 4 add

U
permutation for it n 1 tree Algorithm
generating (i, set). − trees does generating 2 1), i (each j) are and 21 3

W
do to for not generated, 4 in E; the j, tree

generating to contain this 3 1 1 generate each else a following: the


≤ 4 2 each with random i Algorithm? goto < of nodes 3 with a j the

o
n 4≤ cycle all n−2 degree(4) random step n permutation 1
r
1 same of and trees trees (a). a 2 2 (how = tree (i,

ld
probability) 3 3 1. trees with on j) 4

4 obtained do ∉ n on 1 the of you E. nodes? 2 nodes {1, same test


3 by by
2,
4
⋅ ⋅ ⋅ , n}. Create a program and show the output for n = 3 for 10
runs and the time for 10 runs for n = 100,000.
www.alljntuworld.in JNTU World
Downloaded From JNTU World
(http://www.alljntuworld.in)

1.69 PSEUDOCODES ARE Pseudocode • • J It It to − −

N
must is allow correctness performance not be a

Describing is "work-in-progress" unambiguous a


T U
requires High-Level proof efficiency

SERIOUS Algorithms substantial and. (clear) Algorithm

W
estimation or and THINGS a skill in "rough"
pseudocode concise, and Description: practice.

o r
description. with forms sufficient

ld
details

www.alljntuworld.in JNTU World


Downloaded From JNTU World
(http://www.alljntuworld.in)
1.70

TYPES OF
ALGORITHMS

• Exploit − − Method Successive Greedy Dynamic Depth-first

J Choose ture Exploit problem-input. Exploit rectly

N
to Input/Output Method of involves Pseudocode:

Programming fit a Extension Approximation properties/structures


T
and properties/structures proper the Problem: other (a

solution Programming properties sufficient special solution

U
(problem search Properties: (1) (2) (a (1) (2)

Algorithm method. Key Key kind special (numerical method Input


Output for of methods steps data-structures input-output size tricks of

W
efficient of among (= search) kind the (= Design

first N in given) alone to to Algorithms) the solution-outputs, of


solutions. and find) size the search) solution are relationship.

o r
then different N not + select 1, method recursion)
ld
parts a which data-struc- of indi- the

www.alljntuworld.in JNTU World


Downloaded From JNTU World (http://www.alljntuworld.in)
1.71

USE OF OUTPUT-STRUCTURE

Problem: Example. • Tw (1) (2) • J o There The

nums[i] A for #(additions Use for #(additions nums[i Solutions.


N
N(N partialSums[1] partialSums[1] brute-force

O(N) (i=2 for (i=2 partialSums[i] partialSums[i] is − the


partialSums[1. no at Given ≤ 1)/2 + (j=2 partialSums[i] to to i

T
1]" least Algorithm input-structure ≤ property N) N)
Both involving = involving N. to among nums[1. method. O(Nan

U
once. do i) = = can add nums[1]; nums[1];

the array 2= = ). output be "partialSums[i is nums[1]; . .


following: nums[. partialSums[i nums[j] 5]: 5]: = nums[. optimal
considered of nums[1] to 2, 2, items. exploit N
W
]) -1, 1, ]) to = numbers because N

partialSums[i]; + 5, 6, = here. method nums[2] − − 0 + 3, 9, 1 1]

o
1] + = 12 + 3 nums[1. we O(N). 1 nums[i]; = of + +
r
must extension. partialSums[i] ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ + . N], +

ld
nums[i] look (N compute at − 1) each for

=
+1
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.72

ANOTHER EXAMPLE OF THE USE OF

OUTPUT-STRUCTURE
Problem: Given a binary-matrix vals[1. . M, 1. . N] of 0’s and
1’s, obtain counts(i, j) = #(1’s in vals[. , . ] in the range 1 ≤ i′ ≤ i
and 1 ≤ j′ ≤ j) for all i and j. Example.
vals =
1001 2 00 47
• Since vals[i, j]’s can be arbitrary, there is no relevant input
prop- erty/structure.


J N The the first outputs counts(i
T
counts(1, one counts(i equally counts(i, does

U
counts(i, Not + 1, + not all j j exploitable 1,

+ + input/output help 1) 1) 1) j) j) ≤ hav in e many


properties as shown below;
computing counts(i, j). = = = 101 1

11

Wo
counts(i, j+ 1) 1 11
r ld
1 1

counts(i + 1, j) counts(1, j) + vals[1, j + 1] counts(i, 1) + vals[i +


1, 1] counts(i + 1, j) + counts(i, j + 1) − counts(i, j) + vals[i + 1, j
+ 1]
properties may be in a given computation. 23 counts =

24
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.73

Algorithm: 1. Let counts(1, 1) = vals[1, 1]; compute the


remainder of first row counts(1, j), 2 ≤ j ≤ N, using counts(1, j +
1) = counts(1, j) + vals[1, j + 1]. 2. Compute the first column
counts(i, 1), 1 ≤ i ≤ M, similarly. 3. Compute the remainder of
each row (i + 1 = 2, 3, ⋅ ⋅ ⋅ , M), from left
to right, using the formula for counts(i + 1, j + 1) above.
Exploiting the output-properties includes choosing a proper
order of computing different parts of output.
Complexity Analysis:
We look at the number of additions/subtractions involving
counts(i, j) and vals[i′, j′]. Step 1: Step 2: Step 3: Total: Brute-

force 1. Complexity:
J N For add each to it

all method::
T
1 3(M O(MN); item N M ≤ vals[i′, − i − ≤ 1 1 −

U
vals[i, = M = 1)(N O(N) O(M) j′] this and

for j] − 1 is 1) at 1 ≤ optimal least = ≤ j #(additions) =


i=1 Σ MO(MN)
since once. ≤ N, start i′ ≤ i and 1 we with ≤ j′ Nmust counts(i, ≤ j.

Wo
look at each

j) = 0 and
j=1 Σ Nij = (
rld
i=1 Σ Mi)( j=1Σ j) = O(M2N2)

www.alljntuworld.in JNTU World


Downloaded From JNTU World (http://www.alljntuworld.in)
1.74

MAXIMIZING THE SUM OF Problem: Example:


Brute-Force • • Question: Observations • • • • Simplify

J For Eliminate The If If M tial-sums +ve consecutive


N
consecutive items replace a a = (j solution

solution initial neighbors max Input: = on What nums[1.

consecutive Given mum For the 1 CONSECUTIVE and


Method: {M(j): items immediate (terminal) to [−2, (assume
T
+ve maximum the −ve SSis N), let ij ij M It 7, of the

an equal uses uses . input items is 9] M(j) of items compute 1

U
3,−1, it. array complexity? an ≤ that = −ve

all a a −ve j [10, to left This nums[1. array by −ve +ve is = ≤


Scontaining at − items 0. ij of max 7 N}. items their 4, least −5, =
and S+ item, item, means numbers of ijΣ 3, 3 ,

W
{S 3, ITEMS by . alternate 1 − sum.

nums[k] − are right one 15] ij1 ≤ −4, then 4, then their : − i we 1
not = it 9, nums[i] ≤ 4 9, ≤ nums[1. sides. and S−5, + Ssum. j,
o
can i used −5, ij ij +ve for IN 3 ≤ using uses also −

3, j}. also 4, replace i A 4 in and > 1, ≤ This −20, + . uses 0):

LIST

r
the a the N], k −20, 9 the solution. −ve ≤ = whole 11].
means method find the 13. each j. group 11, items.

ld
immediate −3, the group group we of of −1], maxi-
par-
+ve can of of
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.75

ADDITIONAL OBSERVA TIONS


Another (1) (2) (3) • Search Question:

•? •? J Is Can item (a) (b) (c) (d) (e) A replace aw


N
nums[1] nums[1] M this similar ay For = we

(divide Observation: Final a nums[1]. nums[1. formulate 10 13


method a nums[1. 10 i.e., i.e., i.e., is Solution is consideration or

T
or solution: or and not combined 10 10 10 13 13 12
8 . of 2]. conquer part or or or or or or or or or or . a 3]

U
extension solution solution solution solution

solution solution solution solution solution solution for solution


There M by of nums[] =13=8 nums[1]+nums[2]+nums[3]. an
with applies method)? are optimal from from from from from
from from from from from method (explain)?
W
others three = to − [10, [8, [3, [8, [13, [9, [13,

[12, [4, [12, [3] 4+9=10 nums[N]. solution. possibilities: −5, by


−4, −4, −4, −5, −20, to or −5, −5, −20, −20, 3, starting solution

o
form 9, 9, 9, 4, −4, 4, 4, 11], −5, −5, −5, −20, 11]
r
11]. 9, −20, −20, − Then −5, M. 5 4, 4, 4, at 11], from 4,

ld
+3 −20, −20, −20, 11] 11]. the −20, Then we

− middle [11]. 11] 11], 11]. 11]: can 4 + we 9. throw


+ve can
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.76

A RECURSIVE ALGORITHM
Algorithm 1. 2. 3. Question:

•? •? •?
J Characterize How How Let If (a) (b) else Let

input (N Input: Output: N MM let Let the 3 Let to

nums[1] does do >= nums[1. 1 ≤ = nums[i], M=


MAX_CONSECUTIVE_SUM: you solution i max 3) nums[3]
T
M2 nums[1]. ≤ this 3 = then An bers, Maximum N.

show the be Mand {M. show N]? 3 array the solution 3 do 1=

U
obtained with nums[2] = , ≤ that MMthe that
solution i nums[1] 1≤ 2nums[1. nums[1] . , we sum following: N.
Mthe M3make are by 2 }. (MM Algorithm (in obtained +

W
applying used.) . for 3 nums[2] and N] a is

2(N+1) way a the of nums[N] set / by alternative best similar 2 is

o
the of + − //initial correct? applying nums[3]
consecutive 1 Algorithm solution recursive-calls > to 0.

r ld
that version +ve/-ve and the when of

to items.
Algorithm let Mnums[i], none 3num- M). for 2 be of
an
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.77

AN EXAMPLE OF THE CALL-TREE Question:

•? •? •? J Complete Let Let redundant improved


N
recursive-calls for T(1) rithm T(N) nums[1. an

T(N) T(N) = Mfor and Minput 0. 1 1 . = = an N]? (This = T(N = the

T
10 form 8 computations, array input #(additions
#(comparisons above solution − gives are of 1). IN [10, of array

U
MAX_CONSECUTIVE_SUM. size [8,

[13, −4,
THE −5, call-tree, made T(N) = solution of M involving 3,

N). = size −4, −5, 9, and M= RECURSION ⋅ ⋅ ⋅ max


W
involving −5, in 2 Show 9, 4, N = ⋅ ⋅ ⋅ = N),

examine −5, −20, M4, then ⋅ ⋅ ⋅ {10, 13 − the 2 −20, 1 nums[i] = 4,


Show that = 11] 13, 13 −20, restate O(N).) simplified 11] nums[i]

o
11} T(N) it the 11] [3, = carefully, in 13 solution
r
relationship [9, −4, the the = in −5, T(N 9, M⋅ ⋅ ⋅ ⋅ ⋅ ⋅

new the simplified −5, Algorithm 4, 3 = ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ = −20, identify

ld 4,
− M How ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 11
new Algorithm 2) 3 −20, = between 11] + 11
Algo- 11] many
2 and the and
for
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.78

A DYNAMIC PROGRAMMING SOLUTION


Let Example. Observations: Pseudocode 1. 2. Complexity:

J M(j M(j) For nums[j M M(j) M(1) N = + (j

M 2) M(1) = #(comparisons 10 S= − j max = = = For 11 (it = 1]


3, max max O(N). nums[1]. #(comparisons = 1 =
T
does + nums[] {Snums[1]. #(additions 10 5,

U
nums[j]} {M(j)+nums[j {M(j): ij⋅ ⋅ ⋅ , : not 1

8 SS≤ j = N) "extend 13 33 in = i [10, ≤ 3 = = j computing let and


= involving 8 3 j}; in −5, 1, M(j) computing finally here, a 3,
W
13 SSS3, solution" j + 15 35 55 ⋅ ⋅ ⋅ , = −4,

1]+nums[j 5 = = = both = nums[]) N}. M(j)’s) M 13 8 9 9, max =

o
−5, i, M) - max why?): j 12 {nums[j], SSSSj 4, ∈ {1,

17 37 57 77 = = =(N = + −20, (N {M, N 7 = = = = 2], 12 7 8 4


r
− 3, − − nums[j 1 11],

ld
M(j)}. 1) 1) ⋅ ⋅ ⋅ , / / M(j 2 2 11 SSSSSj

N}. 19 39 59 79 99 = + 9 = = = = = − 2]}. 3 −2 −1 −5 11
2) +
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.79

ANOTHER O(N) METHOD


Observation:
• For 1 ≤ i ≤ j ≤ N, Si,j = S1,j − S1,(i−1); here S1,0 = 0 for i = 1.
• If Sij = M, then S1,(i−1) = min {S1,(i′−1): i′ ≤ j}.
M0
S m =0
1i↑ M 0i→

1| 3| 5|

Solution Method: There 1. 2. 3. J Find - - For - Let


N
Find among sum For Let M each M n (i each =

M= < − the max k−1 max i 1)’s mS= k−1 1,i−1successive

T
m= ik{Swhich k{M, , to max , find let 1,ji the k =
U
can 1, right are decreasing 3, possibly three

of ⋅ ⋅ ⋅ , ik be corresponding the associated : {Sj ≥ 1, ijn: }. ik−1 −

W
mk: 0 ≤ 7m| 1 steps. N. mk−1≤ k ≤ 9|
or
2

ld
15|
give maximum Sij. items (That ; m0 =0= is, m0 m> Sk 1,0mis 1 .)
> the mfirst 2 > ⋅ ⋅ ⋅ > partial-
.
i, i.e., mk = S1,(ik−1) j = jk. j < ik} = S1,jk for 1 ≤ k ≤ n; n}. M1
11|
13|
m2
mn let
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.80

(CONTD.)
A Slightly Larger Example.
nums[i]: 10 −5 3 −4 9 −5 4 i, j: 1 3 5 7 S1,i−1Sm1, ikkj: : : : 0 m1 0 10 5 4 8 8 13 12 Mjkk: :
M0=13 5 Question:

N
•? J Why M = can’t max we {13
T U
call − ji0, 1 1 this = = 7 1, 5, −

W
method (−8), ji2 2 4 = = a − 9, 7, "method
o
(−10)} −20 11 −6 9 −8 m9 1 ij3 3 = = 3 13 13

r ld
10 M 1=7 −3 11 11 7 −17 M−10 2m=5 14 13

13
13 4 2 = 15 = Si2,j2 = S9,11.
of extension"?

Potrebbero piacerti anche