Design and analysis of algorithm By Creative Developper
1. @OMKAR VERMA
GITM LUCKNOW
Department Of Computer Science & Engineering
Design & Analysis Of Algorithm
HoD Er. Brijesh Pandey Faculty Mr. Peeyush Kr. Pathak
1.HEAP SORT
MAX-HEAPIFY(A,i)
1. L=Left(i)
2. R=Right(i)
3. If l<= A.heap-size and A[l]>A[i]
4. Largest = l
5. Else largest = i
6. If r<= A.heap-size and A[r]>A[largest]
7. Largest = r
8. If largest ≠ i
9. Exchange A[i] with A[largest]
10. MAX-HEAPIFY(A,largest)
2. @OMKAR VERMA
BUILD-MAX-HEAP(A)
1. A.heap-size = A.length
2. For I = [A.length/2] downto 1
3. MAX_HEAPIFY (A,i)
2.QUICK SORT
QUICK SORT(A,p,r)
1. If p <r
2 . q=PARTITION(A,p,r)
3.QUICKSORT(A,p,q-1)
4.QUICKSORT(A,q+1,r)
Partition in Quick Sort
PARTITION (A,p,r)
1. X=A[r]
2. I=p-1
3. for j=p to r-1
4. if A[j] ≤ x
5. i=i+1
6. exchange A[i] with A[j]
7. exchange A[i+1] with A[r]
8. return i+1
3.Counting Sort
COUNTING-SORT(A,B,k)
1. let C[0……..k] be a new array
2. for i=0 to k
3. C[i] = 0
4. For j = 1 to A.length
5. C[A[j]] = C[A[j]+1] // C[i] now contains the number of elements equal to i.
6. For I = 1 to k
7. C[i] = C[i] + C[i-1]
8. //C[i] now contains the number of elements less than or equal to i.
9. For j= A.length downto 1
10. B[C[A[j]]] = A[j]
11. C[A[j]] = C[A[j]] - 1
3. @OMKAR VERMA
4.Radix Sort
RADIX_SORT(A,d)
1. For i=1 to d
2. Use a stable sort to sort array A on digit i.
5.Bucket Sort
BUCKET-SORT(A)
1. Let B[0. . . . . . .n-1] be a new array
2. N=A.length
3. For i=0 to n-1
4. Make B[i] an empty list
5. For i= 1 to n
6. Insert A[i] into list B[[nA[i]]]
7. For i= 0 to n-1
8. Sort list B[i] with insertion sort
9. Concatenate the lists B[0],B[1]. . . . . . . B[n-1] together in order.
ALGORITHM OF B -TREE
# B Tree Searching
B-TREE-SEARCH(x,k)
1. I=1
2. While i≤ x.n and k > x.keyi
3. i=i=i+1
4. if i ≤ x.n and k == x.keyi
5. return (x,i)
6. elseif x.leaf
7. return NIL
8. else DISK-READ(x,Ci)
9. return B-TREE-SEARCH(x.Ci,k)
4. @OMKAR VERMA
# B Tree Creating
B-TREE-CREATE(T)
1. X=ALLOCATE-NODE()
2. X.leaf = TRUE
3. X.n = 0
4. DISK-WRITE(x)
5. T.root = x
# B Tree Spliting
B-TREE-SPLIT-CHILD(x,i)
1. Z=ALLOCATE-NODE()
2. Y=x.Ci
3. Z.leaf=y.leaf
4. Z.n=t-1
5. For j=1 to t-1
6. Z.key(j) = y.key(j+1)
7. If not y.leaf
8. For j = 1 to t
9. Z.C(j)= y.C(j+t)
10. Y.n=t-1
11. For j=x.n+1 downto i+1
12. X.C(j+1)=x.C(j)
13. X.C(i+1)=z
14. For j=x.n downto i
15. X.key(j+1)=x.key(j)
16. X.key(i)=y.key(t)
17. X.n=x.n+1
18. DISK-WRITE(y)
19. DISK-WRITE(z)
20. DISK-WRITE(x)
# B Tree Insertion
B-TREE-INSERT(T,k)
1. R=T.root
2. If r.n==2t-1
3. S=ALLOCATE-NODE()
4. T.root=s
5. S.leaf=FALSE
6. S.n=0
7. S.C1=r
8. B-TREE-SPLIT-CHILD(s,1)
9. B-TREE-INSERT-NONFULL(s,k)
5. @OMKAR VERMA
10. Else B-TREE-INSERT-NONFULL(r,k)
ALGORITHM OF FIBONACCI HEAP
# Inserting A Node
FIB-HEAP-INSERT(H,x)
1. X.degree = 0
2. X.p = NIL
3. X.child = NIL
4. X.mark = FALSE
5. If H.min==NIL
6. Create a root list for H containing just x
7. H.min==x
8. Else insert x into H’s root list
9. If x.key < H.min.key
10. H.min=x
11. H.n = H.n+1
# Uniting Two Fibonacci Heap
FIB-HEAP-UNION(H1,H2)
1. H=MAKE-FIB-HEAP()
2. H.min=H1.min
3. Concatenate the root list of H2 with the root list of H
4. If(H1.min==NIL) or (H2.min≠NIL and H2.min.key<H1.min.key)
5. H.min=H2.min
6. H.n=H1.n+H2.n
7. Return H
#Extacting The Minimum Node
FIB-HEAP-EXTRACT-MIN(H)
1. Z=H.min
2. If z≠ NIL
3. For each child x of z
4. Add x to the root list of H
5. X.p=NIL
6. Remove z from the root list of H
7. If z == z.right
8. H.min= NIL
6. @OMKAR VERMA
9. Else H.min = z.right
10. CONSOLIDATE(H)
11. H.n = H.n-1
12. Return z
#CONSOLIDATE
1. Let A[0. . . . . . . . D(H.n)be a new array]
2. For i= 0 to D(H.n)
3. A[i]=NIL
4. For each node w in the root list of H
5. X=w
6. d= x.degree
7. while A[d] ≠ NIL
8. y=A[d] //another node with the same degree as x
9. if x.key > y.key
10. exchange x with y
11. FIB-HEAP-LINK(H,y,x)
12. A[d] = NIL
13. d = d+1
14. A[d] = x
15. H.min = NIL
16. For i=0 to D(H.n)
17. If A[i]≠ NIL
18. If H.min==NIL
19. Create a root list for H containing just A[i]
20. H.min = A[i]
21. Else insert A[i] into H’s root list
22. If A[i].key<H.min.key
23. H.min = A[i]
FIB-HEAP-LINK(H,y,x)
1. Remove y from the root list of H
2. Make y a child of x,incrementing x.degree
3. Y.mark=FALSE
#Decreasing a key in Fibonacci Heap
FIB-HEAP-DECREASE-KEY(H,x,k)
1. If k> x.key
2. Error “new key is greater than current key”
3. X.key=k
7. @OMKAR VERMA
4. Y=x.p
5. If ≠NIL and x.key<y.key
6. CUT(H,x,y)
7. CASCADING-CUT(H,y)
8. If x.key<H.min.key
9. H.min=x
# CUT(H,x,y)
1. Remove x from the child list of y,decrementing y.degree
2. Add x to the root list of H
3. X.p= NIL
4. X.mark=FALSE
# CASCADING-CUT(H,y)
1. Z=y.p
2. If z≠ NIL
3. If y.mark==FALSE
4. Y.mark = TRUE
5. Else CUT(H,y,z)
6. CASCADING-CUT(H,z)
# Deleting a node in Fibonacci Heap
FIB-HEAP-DELETE(H,x)
1. FIB-HEAP-DECREASE-KEY(H,x,-∞)
2. FIB-HEAP-EXTRACT-MIN(H)
ALGORITHM OF MINIMUM SPANNING TREE
#Kruskal’s Algorithm
MST-KRUSKAL(G,w)
1. A=ϕ
2. For each vertex v € G,V
3. MAKE-SET(v)
4. Sort the edges of G.E into nondecreasing order by weight w
5. For each edge (u,v) € G.E,taken in nondecreasing order by weight
6. If FIND-SET(u) ≠ FIND-SET(v)
7. A = A Ự {(u,v)}
8. @OMKAR VERMA
8. Union(u,v)
9. Return A
#Prim’s Algorithm
MST-PRIM(G,w,r)
1. For each u € G.V
2. U.key = ∞
3. U.π = NIL
4. R.key = 0
5. Q = G.V
6. While Q ≠ ϕ
7. U = EXTRACT-MIN(Q)
8. For each v € G.Adj[u]
9. If v € Q and w(u,v) < v.key
10. V.π = u
11. V.key = w(u,v)
ALGORITHM OF SINGLE SOURCE SORTEST PATH
# The Bellman-Ford Algorithm
BELLMAN-FORD(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. For i=1 to |G.V|-1
3. For each edge (u,v) € G.E
4. RELAX(u,v,w)
5. For each edge (u,v) € G.E
6. If v.d > u.d + w(u,v)
7. Return FALSE
8. Return TRUE
# Dijkstra’s Algorithm
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S = ϕ
3. Q = G.V
4. While Q ≠ ϕ
5. U=EXTRACT-MIN(Q)
6. S = S U {u}