Sei sulla pagina 1di 17

20/11/2013

1
DAASummary
CSE3
rd
YearGCET
Syllabus
UnitI:Introduction:Algorithms,Analyzing
algorithms Complexity of algorithms Growth of algorithms,Complexityofalgorithms,Growthof
functions,Performancemeasurements,Sorting
andorderStatistics Shellsort,Quicksort,
Mergesort,Heapsort,Comparisonofsorting
algorithms,Sortinginlineartime.
UnitII:AdvancedDataStructures:RedBlack
trees,B trees,BinomialHeaps,FibonacciHeaps.
11Nov2013 2 DAA(ECS502)
20/11/2013
2
Syllabus
Unit III:DivideandConquerwithexamplessuchas
Sorting,MatrixMultiplication,Convexhulland
Searching Greedy methods with examples such as Searching.Greedymethodswithexamplessuchas
OptimalReliabilityAllocation,Knapsack,Minimum
Spanningtrees PrimsandKruskals algorithms,Single
sourceshortestpaths Dijkstras andBellmanFord
algorithms.
Unit IV:Dynamicprogrammingwithexamplessuch
K k All i h t t th W h l d asKanpsack,Allpairshortestpaths Warshals and
Floydsalgorithms,Resourceallocationproblem.
Backtracking,BranchandBoundwithexamplessuchas
TravellingSalesmanProblem, GraphColoring, nQueen
Problem, HamiltonianCyclesandSumofsubsets.
11Nov2013 3 DAA(ECS502)
Syllabus
UnitV:SelectedTopics:AlgebraicComputation, Fast
FourierTransform, StringMatching, TheoryofNP
completeness,Approximationalgorithmsand
Randomizedalgorithms.
References:
1. ThomasH.Coreman,CharlesE.Leiserson andRonaldL.Rivest,
IntroductiontoAlgorithms,Printice HallofIndia.
2. RCT Lee, SS Tseng, RC Chang and YT Tsai, Introduction to the Design 2. RCTLee,SSTseng,RCChangandYTTsai, IntroductiontotheDesign
andAnalysisofAlgorithms,McGraw Hill,2005.
3. E.Horowitz&SSahni,"FundamentalsofComputerAlgorithms",
4. Aho,Hopcraft,Ullman,TheDesignandAnalysisofComputer
AlgorithmsPearson
11Nov2013 4 DAA(ECS502)
20/11/2013
3
INSERTIONSORT
Alg.: INSERTIONSORT(A)
forj 2 ton
a
8
a
7
a
6
a
5
a
4
a
3
a
2
a
1
1 2 3 4 5 6 7 8
dokey A[ j ]
InsertA[ j ] intothesortedsequenceA[1 . . j -1]
i j - 1
whilei > 0 andA[i] > key
d A[i 1] A[i]
key
5
doA[i + 1] A[i]
i i 1
A[i + 1] key
Insertionsort sortstheelementsinplace
MergeSort
Alg.: MERGESORT(A, p, r)
1 2 3 4 5 6 7 8
6 2 3 1 7 4 2 5
p r
q
ifp < r Checkforbasecase
thenq (p + r)/2 Divide
MERGESORT(A, p, q) Conquer
MERGESORT(A, q + 1, r) Conquer
(A )
6
MERGE(A, p, q, r) Combine
Initialcall: MERGESORT(A, 1, n)
20/11/2013
4
Merge Pseudocode
Alg.: MERGE(A,p,q,r)
1. Computen
1
and n
2
1 2 3 4 5 6 7 8
6 3 2 1 7 5 4 2
p r
q
2. Copythefirstn
1
elementsinto L[1
. . n
1
+ 1] andthenextn
2
elementsintoR[1 . . n
2
+ 1]
3. L[n
1
+ 1] ; R[n
2
+ 1]
4. i 1; j 1
5. fork p tor
6 do if L[ i ] R[ j ]
p q
7 5 4 2
6 3 2 1
r q+1
L
R

n
1
n
2
7
6. doifL[ i ] R[ j ]
7. thenA[k] L[ i ]
8. i i + 1
9. elseA[k] R[ j ]
10. j j + 1
RandomizedQuicksort
Alg. : RANDOMIZEDQUICKSORT(A, p, r)
ifp < r
thenq RANDOMIZEDPARTITION(A, p, r)
(A )
8
RANDOMIZEDQUICKSORT(A, p, q)
RANDOMIZEDQUICKSORT(A, q + 1, r)
20/11/2013
5
RandomizedPARTITION
Alg.: RANDOMIZEDPARTITION(A, p, r)
i RANDOM(p, r)
exchange A[p] A[i]
9
exchangeA[p] A[i]
returnPARTITION(A, p, r)
PARTITION
Alg.:PARTITION(A,p,r)
x A[r]
i p - 1
A[pi] x A[i+1j-1] > x
p i i+1 r j1 j
i p 1
forj p tor - 1
doifA[ j ] x
theni i + 1
exchangeA[i] A[j]
exchangeA[i + 1] A[r]
unknown
pivot
10
returni + 1
Choosesthelastelementofthearrayasapivot
Growsasubarray [p..i]ofelementsx
Growsasubarray [i+1..j1]ofelements>x
RunningTime:O(n),wheren=rp+1
20/11/2013
6
Alg: HEAPSORT(A)
1. BUILDMAXHEAP(A)
O(n)
2. fori length[A] downto2
3. doexchangeA[1] A[i]
4. MAXHEAPIFY(A, 1, i - 1)
( )
O(lgn)
n-1 times
11
Runningtime:O(nlgn) --- Can
be shown to be (nlgn)
BuildingaHeap
ConvertanarrayA[1 n] intoamaxheap(n = length[A])
TheelementsinthesubarrayA[(n/2+1) .. n] areleaves
Alg: BUILDMAXHEAP(A)
1. n =length[A]
2. for i n/2 downto 1
ApplyMAXHEAPIFYonelementsbetween1 andn/2
1
4
3
1
2 3
12
3. do MAXHEAPIFY(A, i, n)
2
14 8
16
7
9 10
4 5 6 7
8 9 10
4 1 3 2 16 9 10 14 8 7
A:
20/11/2013
7
MaintainingtheHeapProperty
Assumptions:
LeftandRight
bt f i
Alg: MAXHEAPIFY(A, i, n)
1. l LEFT(i)
2 RIGHT(i) subtrees ofi
aremaxheaps
A[i] maybe
smallerthan
itschildren
2. r RIGHT(i)
3. if l n andA[l] > A[i]
4. then largest l
5. else largest i
6. if r n andA[r] > A[largest]
7 then largest r
13
7. then largest r
8. if largest = i
9. then exchangeA[i] A[largest]
10. MAXHEAPIFY(A, largest, n)
AnalysisofCountingSort
Alg.: COUNTINGSORT(A,B,n,k)
1. fori 0 tor
2 do C[ i ] 0
O(r)
2. doC[ i ] 0
3. forj 1 ton
4. doC[A[ j ]] C[A[ j ]] + 1
5. C[i] containsthenumberofelementsequaltoi
6. fori 1 tor
7. doC[ i ] C[ i ] + C[i -1]
O(n)
O(r)
14
[ ] [ ] [ ]
8. C[i] containsthenumberofelements i
9. forj n downto 1
10. doB[C[A[ j ]]] A[ j ]
11. C[A[ j ]] C[A[ j ]] - 1
O(n)
Overalltime:O(n + r)
20/11/2013
8
AnalysisofBucketSort
Alg.: BUCKETSORT(A,n)
for i 1 to n fori 1 ton
doinsertA[i]intolistB[nA[i]]
fori 0 ton - 1
dosortlistB[i] withquicksortsort
B[0] B[1] B[ 1]
O(n)
O(n)
15
concatenatelistsB[0], B[1], . . . , B[n -1]
togetherinorder
returntheconcatenatedlists
O(n)
O(n)
RBINSERT(T, z)
1. y NIL
Initializenodesxandy
Th h t th l ith i t
26
17 41
30 47
38 50
2. x root[T]
3. whilex = NIL
4. doy x
5. ifkey[z] < key[x]
Throughoutthealgorithmypoints
totheparentofx
Godownthetreeuntil
reachingaleaf
Atthatpointyisthe
16
6. thenx left[x]
7. elsex right[x]
8. p[z] y
p y
parentofthenodetobe
inserted
Setstheparentofztobey
20/11/2013
9
RBINSERT(T, z)
9. ify = NIL
10. thenroot[T] z
Thetreewasempty:
setthenewnodetobetheroot
26
17 41
30 47
38 50
11. elseifkey[z] < key[y]
12. thenleft[y] z
13. elseright[y] z
14. left[z] NIL
Otherwise,setztobetheleftor
rightchildofy,dependingon
whethertheinsertednodeissmaller
orlargerthanyskey
17
15. right[z] NIL
16. color[z] RED
17. RBINSERTFIXUP(T, z)
Setthefieldsofthenewlyaddednode
Fixanyinconsistenciesthatcouldhave
beenintroducedbyaddingthisnewred
node
RBINSERTFIXUP(T, z)
1. whilecolor[p[z]] =RED
2. doifp[z] = left[p[p[z]]]
i h [ [ [ ]]]
Thewhilelooprepeatsonlywhen
case1isexecuted:O(lgn) times
Set the value of xs uncle
3. theny right[p[p[z]]]
4. ifcolor[y] = RED
5. thenCase1
6. elseifz = right[p[z]]
7. thenCase2
Setthevalueofx s uncle
18
8. Case3
9. else(sameasthenclausewithright
andleftexchanged)
10. color[root[T]] BLACK
Wejustinsertedtheroot,or
Theredviolationreachedthe
root
20/11/2013
10
LEFTROTATE(T,x)
1. y right[x] Sety
2. right[x] left[y] ysleftsubtreebecomesxsrightsubtree
3 if left[y] = NIL 3. ifleft[y] = NIL
4. thenp[left[y]] x Settheparentrelationfromleft[y]tox
5. p[y] p[x] Theparentofxbecomestheparentofy
6. ifp[x] = NIL
7. thenroot[T] y
8 else if x = left[p[x]]
19
8. elseifx = left[p[x]]
9. thenleft[p[x]] y
10. elseright[p[x]] y
11. left[y] x Putxonysleft
12. p[x] y ybecomesxsparent
PRIM(V, E, w, r)
1. Q C
2. foreachu e V
3. dokey[u]
O(V) ifQisimplementedas
Totaltime:O(VlgV + ElgV) = O(ElgV)
y[ ]
4. [u] NIL
5. INSERT(Q, u)
6. DECREASEKEY(Q, r, 0)key[r] 0
7. whileQ = C
8. dou EXTRACTMIN(Q)
aminheap
Executed|V|times
TakesO(lgV)
Minheap
operations:
O(VlgV)
O(lgV)
20
9. foreachv e Adj[u]
10. doifv e Q andw(u, v) < key[v]
11. then[v] u
12. DECREASEKEY(Q, v, w(u, v))
ExecutedO(E)timestotal
Constant
TakesO(lgV)
O(ElgV)
20/11/2013
11
UsingFibonacciHeaps
Dependingontheheapimplementation,runningtime
could be improved! couldbeimproved!
21
1. AC
2. foreachvertexv e V
3. do MAKESET(v)
KRUSKAL(V, E, w)
O(V)
3. doMAKE SET(v)
4. sortEintonondecreasingorderbyw
5. foreach(u, v) takenfromthesortedlist
6. doifFINDSET(u)= FINDSET(v)
7. thenAA {(u, v)}
8. UNION(u v)
O(ElgE)
O(E)
O(lgV)
22
8. UNION(u, v)
9. returnA
Runningtime:O(V+ElgE+ElgV)=O(ElgE) dependentonthe
implementationofthedisjointsetdatastructure
Runningtime:O(V+ElgE+ElgV)=O(ElgE)
SinceE=O(V
2
),wehavelgE=O(2lgV)=O(lgV)
O(lgV)
O(ElgV)
20/11/2013
12
BELLMANFORD(V, E, w, s)
1. INITIALIZESINGLESOURCE(V,s)
2. fori1to|V| 1
O(V)
O(V)
O(VE)
3. doforeachedge(u,v)e E
4. doRELAX(u,v,w)
5. foreachedge(u,v)e E
6. doifd[v]>d[u]+w(u,v)
7. then return FALSE
O(E)
O(E)
O(VE)
23
7. thenreturnFALSE
8. returnTRUE
Runningtime:O(V+VE+E)=O(VE)
RelaxationStep
Relaxinganedge(u,v)=testingwhetherwecan
improvetheshortestpathtovfoundsofarby
goingthroughu
d[ ] d[ ] ( ) Ifd[v] > d[u] + w(u, v)
wecanimprovetheshortestpathtov
d[v]=d[u]+w(u,v)
t[v] u
2
u v
2
u v
Afterrelaxation:
d[v] s d[u] + w(u, v)
s s
24
5 9
5 7
2
u v
RELAX(u,v,w)
5 6
5 6
2
u v
RELAX(u,v,w)
nochange
20/11/2013
13
Dijkstra(G,w,s)
1. INITIALIZESINGLESOURCE(V, s)
2. SC
3 Q V[G]
O(V)
O(V) build min heap
3. QV[G]
4. whileQ= C
5. do u EXTRACTMIN(Q)
6. SS {u}
7. foreachvertexv e Adj[u]
O(V)buildminheap
ExecutedO(V)times
O(lgV)
O(E)times
O(VlgV)
25
j
8. doRELAX(u, v, w)
9. UpdateQ(DECREASE_KEY)
Runningtime:O(VlgV + ElgV) = O(ElgV)
( )
(total)
O(lgV)
O(ElgV)
Floyd'sAlgorithm:Using2D
matrices
Floyd
1.D W//initializeD arraytoW[]
2.P 0//initializeParrayto[0]
3.fork 1ton
//ComputingDfromD
4.dofori 1ton
5.doforj 1ton
6. if(D[i,j ]>D[i,k ]+ D[k,j ])
7 th D[ i j ] D[ i k ] D[ k j ]
FloydsAlgorithm26
7. thenD[i,j ] D[i,k ]+ D[k,j ]
8. P[i,j ] k;
9. elseD[i,j ] D[i,j ]
10. MoveDtoD.
20/11/2013
14
Printingintermediatenodeson
shortestpathfromqtor
path(i ndex q,r)
i f (P[q,r]!=0)
h( P[ ])
3 0 0
1 2 3
1
path(q,P[q,r])
pr i nt l n(v+P[q,r])
path(P[q,r],r)
r et ur n;
//nointermediatenodes
el se r et ur n
BeforecallingpathcheckD[q,r]<,andprint
3 0 0
0 0 1
2 0 0
1
2
3
P=
1
5
FloydsAlgorithm27
efore calling path check [q, r] , and print
nodeq,afterthecallto
pathprintnoder
2
3
3
2
4
THE PUTQUEEN
RECURSIVE METHOD
void putQueen(int row)
{
for (int col=0;col<squares;col++)
if (column[col]==available &&
leftDiagonal[row+col]==available &&
rightDiagonal[row-col]== available)
{{
positionInRow[row]=col;
column[col]=!available;
leftDiagonal[row+col]=!available;
20/11/2013
15
rightDiagonal[row-col]=!available;
if (row< squares-1)
tQ ( +1) putQueen(row+1);
else
print(" solution found);
column[col]=available;
leftDiagonal[row+col]=available;
rightDiagonal[row-col]= available;
}}
}
Navestringmatching
Runningtime:O((nm+1)m).
20/11/2013
16
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
20/11/2013
17
11Nov2013 DAA(ECS502) 33
11Nov2013 DAA(ECS502) 34

Potrebbero piacerti anche