Академический Документы
Профессиональный Документы
Культура Документы
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
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
U
IN access/store Faster: of in + Good =
read/write new = DataStructure a read/write
W
(output those method data to of of data-access
r
compute a are Steps step) computed computed.
ld
faster data. External Output is (and to
N
structures: Three An − − − Implementation
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
W
practical. + three (2); code multiplication.) y
r
variable. i*sizeof(int), compute not an and array-item
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
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
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
U
= the (x (x of the of small small actual <
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
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
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
ld
< if final numItems"
N
j=4, j=5, j=2, j=3, j=4, j=5, j=3,
T U
= = = = = = 5, 9, 12,
0 0 numExchanges numExchanges 3 3 3 3 3 3 3 2
2 2 2 2 numExchanges 2 2 numExchanges 2
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
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.
N
J }else The The 20 else /< not
T
largest lengths 2 for + acute-angled 4 a
U
polygon: + length [2, triangle; 5 = 4,
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
N
•? J If mine below ↓ p We each (i=0; is
i++),
numPermutation[numExchanges]. this numExchanges
U
nextPermutation(n) crazySort need to
initialize
permutations idea IN of determine to to S for
W numPermutations[i]
{0, and = in 4 all 1, =
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
N
mentation Pseudocode: Code: Pseudocode: Code: is (B)
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
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
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
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)
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
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
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,
r
3, filled must places. filled 1] 5]
5]
5]
ld
above. have 5, 4, the properly 0,
N
Search n-1). If to String: the bits (0 start i do }
T
(0 break; each these the (1 of right (i=i-1 if
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.
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
N
portion details items Given erating of in 1. 2. 1. 2.
U
pseudocode and the The m). m)
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
r
position of 2) for efficient? length 1of . m length in (The
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)
these the the a execution of the few Always problem input input-
T
output some some your example use general AND
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
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
T
and improved x (x the and #(condition 2 3 5 4 if
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;
est is are sorting trees sort Sorting? we What abb, sort item,
T
some with want non-numerical on ba, a is
U
third nodes find of sort result pairs B cab.
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
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
r
enumber eexchanges. k−1 sorting 14, each : {i1, the : the
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
N
The 1 leftchild properties node node Which
T
has has node of exactly 0 (1)-(3) or the with 2
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] ≥≥≥
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
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.
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 −
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)
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
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
U
before operations. resulting input-array
marked recursive and part it the is of by
W
hidden from exchange-action, calls the
r
for as are calls [1,0,−,−,−,−,−] at 5/ [2,1,0,−,−,−] [3,2,0,1,−,−]
HeapSort(3)
ld
HeapSort(2) is [3,2,4,5,1,0] sorting for not
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
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
r
max nums[0..9] be with look after (parent, show
ld
generated output-state- can in root-item:
N
At The at particular exchanging terminal Adding
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
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-
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
J
•? •? Best Worst Show What ⋅ ⋅ ⋅ , 〈C〈C〈C〈C〈C〈
N
C 112233Cschedule: , , , , , , Nschedule:
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〉,
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
SCHEDULE
Algorithm OptimalSchedule:
Input: Output: Repair Optimal times repair ri schedule for car C
〈Ci, 1 ≤ i ≤ N.
T
N B: A: (x, 4 3 y) optimal means C: D: E:
UW
1 G: 7 6
F: 5
car is its repair time.
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.31
T
≤ P (i, (combines i jj<numPoints; using }
U
different < = to j) it means j 1; = P avoid
o
distance(Pavoid a points[j])) freeway, is to
d{Pclosest of ijminimum. > = storing . find 4the , 1 currDist;
r ld
"besti Pi6pair. } is the
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:
N
Total Output time. = O(Nlog nums[j] takes of
T U
other. < N . N] items such and in
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
N
each the How The method: O(N. permutation
T
permutation E N! except many possible for
U
permutations N!). xa i ways for
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
PROBLEM
Observation: (1) (2) Question:
•? •? Pseudocode 1. 2. Complexity: EXERCISE 1.
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)? ↔
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
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
N T
A Some 1 terminal 2 3 small
U
node 2-3 1,2 trees.
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
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 <
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
JN
Adding 2: Adding Adding 3: 4: Find
T U
Find the place 1, 2 Find the place for
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
r ld
2-3 for 5 a both tree 2-3 Explain
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
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
W
source-nodes. D {D, D G iteration Fit choice
ld
all its pre-
J
Source Sink D〉 F〉 G〉 right) B〉 E〉 Show the
N
(Remember Show queue (top C Stack link nodes
T
processing nodes = table G FB {C, the {A, A),
TOPOLOGICAL-SORTING E
U
x G}. of algorithm with B}, form which a A: −
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
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
o
null 4] 6, 7]
r ld
= outDegree(x) of node size =
000
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.44
N
add (a) (b) (each (each (each Let node of An A
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:
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
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
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
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
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
o
based {
r
j++) input 1.43 allocated as on shown is of file the by
ld
shown adjacent-nodes = = the local [1]
J x
4. Hints: (a) (b) Modify G C D F E B A
N
If sTo(x) which How numPathsTo(x) PathsTo(x)
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
r
topo. digraph, using on pseudocode for page source-
ld
node). ordering?) all inDegrees? how
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
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)
N
each operator ing the Visit terminal children
U
terminal − at node are non-terminal ∗ an
√ a from xnode. ordered + the 3 2 DAT + gives 9 2
W
− ((x∗ 3) to tree an node = operands. node
r
the 2 x x2 + 2 + subtree the tree). 9 operands +
associated 9 before 9
ld
node.
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:
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,
r
and 〈⋅ ⋅ ⋅ 〉. proper item 9. stacks in operator add
ld
the expression parenthe- the in stack
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. ∗
U
remove a 2 ≠ TO {+, (See to function-symbol:
Output
Initially, + ∅ ) − 3 ∗ 2 + x stack. x −} sqrt(x every
W
〈−, 〈−, /} sqrt 〈−, 〈−, it. + if 〈−, 〈−, <⋅ sqrt,
r
^ 2 Item proc. ( + 9 x remove added upto and
ld Output
2 the
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
U
− = ∧ − = − of > y (y y y; (y Operator (y y)
3 ’op’). − = x y − means ∧ − ON = 3: z += ∧ z) form z) z 3), is
W
∧ xyz y x 3 "y "=" not − not += = we z Here,
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
flag↓
JN
• An actual address
T
x.idName.name[13] x.idName.name[0]
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
CONTD.
typedef struct {
int id; char flag, name[14]; double val; } IdName;
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
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
o
to tasks analysis: length E〉; E〉; to length links.
r
complete scheduling. y); E sink length length in and it 2 = =
ld
= + lengths to 5 5 negative. − it. x.
= 1=6 4 D: 1
G: 6
3
A3 B 4
D15
G
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:
T
acyclic to modify is not each processed hold a node
U
algorithm processed) indegrees. not digraph
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:
U
have longest Used: and inDegree(y) and
W
following: adjacency-list do: to > path
r
equals current looked startNode. startNode. let and y let
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
EXERCISE 1. 2. 3. 4.
J C?)Show
U
(G, the computations? E) when? a (For as
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
PROGRAMMING EXERCISE 1. J
Develop −Σ|w(x,
N
the discussed. (a) (b) (c) following Print
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,
r
and adjList nodeIndex, "Acyclic startNode). = example
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
N
definition Fibonacci(int ((n return(1);
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
N TU
rwere min is The links
v
E(1) corresponding 5
W or v
3,2 3,1v 4 6 5,2 v7
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
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
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
N
to to would Main the you Algorithm one north-
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
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
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
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
N
GenerateRandomTree(4)? Give adding adding 2 edge 3
T
main ∅ 2 2 1, random then Does (empty 2, 3 3 6 j)}∪E
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
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
N
must is allow correctness performance not be a
W
estimation or and THINGS a skill in "rough"
pseudocode concise, and Description: practice.
o r
description. with forms sufficient
ld
details
TYPES OF
ALGORITHMS
N
to Input/Output Method of involves Pseudocode:
U
(problem search Properties: (1) (2) (a (1) (2)
W
efficient of among (= search) kind the (= Design
o r
then different N not + select 1, method recursion)
ld
parts a which data-struc- of indi- the
USE OF OUTPUT-STRUCTURE
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];
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
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,
11
Wo
counts(i, j+ 1) 1 11
r ld
1 1
24
www.alljntuworld.in JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
1.73
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
Wo
look at each
j) = 0 and
j=1 Σ Nij = (
rld
i=1 Σ Mi)( j=1Σ j) = O(M2N2)
U
3,−1, it. array complexity? an ≤ that = −ve
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 −
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
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
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
A RECURSIVE ALGORITHM
Algorithm 1. 2. 3. Question:
•? •? •?
J Characterize How How Let If (a) (b) else Let
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
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
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,
o
11} T(N) it the 11] [3, = carefully, in 13 solution
r
relationship [9, −4, the the = in −5, T(N 9, M⋅ ⋅ ⋅ ⋅ ⋅ ⋅
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
U
nums[j]} {M(j)+nums[j {M(j): ij⋅ ⋅ ⋅ , : not 1
o
−5, i, M) - max why?): j 12 {nums[j], SSSSj 4, ∈ {1,
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
1| 3| 5|
T
m= ik{Swhich k{M, , to max , find let 1,ji the k =
U
can 1, right are decreasing 3, possibly three
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"?