Beruflich Dokumente
Kultur Dokumente
Problem 1: Perform (i) recursive binary search and (ii) linear search.
Hence, find the time required to search an element.
Algorithm HeapSort(A[1..n], n)
// Input: Array A[1..n] of size n.
// Output: Sorted array A
Build_Max_Heap(A, n)
for i ← n downto 2 do
Exchange A[1] ↔ A[i]
Max_Heapify(A, 1, i - 1)
end HeapSort.
Algorithms Lab Manual 3
Problem 4: Write a C++ program for DFS. Using this display all the strongly
connected components.
Algorithm dfs(G, v)
// Input: A Graph, G and starting vertex v
// Output: DFS tree
visited[v] ← 1 // mark v as visited
for each vertex w adjacent to v do
if visited[w] = 0 // vertex not yet visited
dfs(G, w) // continue to explore
end dfs.
Algorithm DFS_Visit_All(G)
// Input: G{V, E}
// Output: DFS spanning tree or forest
for each vertex v V do
visited[v] ← 0 // mark all vertices as unvisited
for each vertex v V do
if visited[v] = 0
dfs(G, v)
end DFS_Visit_All.
Algorithms Lab Manual 5
Algorithm topSortDFS(G)
// Input: A Graph, G
// Output: Topological sorting order
Initialize array visited by setting all entries to white.
Initialize linked list L
for all v V do
if visited[v] = white
DFSSort(G, v)
print all vertices in L
end topSortDFS.
Algorithm DFSSort(G, v)
// Input: A Graph, G and starting vertex v
// Output: Add vertices in L
visited[v] ← grey
for all w adjacent to v do
if visited[w] = white
DFSSort(G, w)
else if visited[w] = grey
print “G has a cycle”
stop.
visited[v] ← black
L.insertFirst(v)
end DFSSort.
Algorithms Lab Manual 7
Algorithm InsertionSort(A[0..n-1], n)
// Input: An array A[0..n – 1]
// Output: Sorted array A
for j ← 1 to n – 1 do
k ← A[j]
i←j–1
while i 0 and k < A[i] do
A[i + 1] ← A[i]
i←i–1
A[i + 1] ← k
end InsertionSort.
8 BIT, CSE Department
Algorithm MFK(i, j)
// Input: Number of objects and maximum weight of Knapsack
// Output: Solution vector (0/1), V
if V[i, j] = –1
if j < Weights[i]
p ← MFK(i – 1, j) // exclude object i
else // include object i
p ← Max(MFK(i – 1, j), Profits[i] + MFK(i – 1, j – Weights[i])
V[i, j] ← p
return V[i, j]
end MFK.
Algorithms Lab Manual 9
Algorithm Dijkstras(G, s)
// Input: Weighted Graph (directed/undirected) and a stating vertex s
// Output: Shortest path from s to all other vertices, i.e. d[1..n]
for each v ≠ s do // Initialize_Single_Source(V, s)
d[v] ←
d[s] ← 0
S← ø
Q ← V[G] // priority queue contain all vertices of G
while Q ≠ ø do // using the d labels as keys
u ← Extract_Min(Q)
S ← S {u}
for each vertex v adjacent to u and v S do
if d[u] + w(u, v) < d[v] // Relax(u, v, w)
d[v] ← d[u] + w(u, v)
return d.
end Digkstras.
10 BIT, CSE Department
Algorithm Partition(A[p..r], p, r)
// Input: Array A, p and r: lower and upper bounds of the array
// Output: Position for swap with pivot
pivot ← A[p]
i←p
j←r+1
while true do
do i = i + 1 while A[i] < pivot
do j = j – 1 while A[j] > pivot
if i < j
Swap(A[i], A[j]) // A[i] ↔ A[i]
else
return j
end Partition.
Algorithms Lab Manual 11
Algorithm Kruskals(G)
// Input: Undirected graph , G
// Output: Minimum Spanning Tree, T
T←
Ecount ← |V| - 1
while T n - 1 and Ecount 0 do
// assuming that edges are stored in a min heap
Get the mincost edge from E and call it as (u, v)
if (u, v) does not form a cycle
T ← T (u, v) // add (u, v) in T
Delete (u, v) from min heap
Ecount = Ecount – 1
return T
end Kruskals.
12 BIT, CSE Department
Algorithm bfs(G, v)
// Input: Graph with V vertices and E edges
// Output: BFS tree
// visited[0..V] is a Boolean array
visited[v] ← 1
Q.enqueue(v)
while not Q.isEmpty() do
v ← Q.dequeue()
for all w adjacent to v do
if visited[w] = 0
visited[w] = 1
Q.enqueue(w)
end bfs.
Algorithm bfs_Visit_All(G)
// Input: A Graph G = (V, E)
// Output: BFS Tree
for each vertex v V do
visited[v] ← 0
Initialize Queue Q
for all v V do
if visited[v] = 0
bfs(G, v)
end bfs_Visit_All.
Algorithms Lab Manual 13
Problem 13: Implement all pairs shortest path problem using Floyd's
algorithm.
Algorithm Floyd(G)
// Input: Directed Weighted Graph, G (Cost Adjacency Matrix)
// Output: All pairs shortest path of G.
A←C
for k ← 1 to n do
for i ← 1 to n do
for j ← 1 to n do
A[i, j] ← min{A[i, j], A[i, k] + A[k, j]}
return A
end Floyd.
14 BIT, CSE Department
Problem 14: Find a subset of a given set S = (s1, s2, ….sn} of n positive
integers whose sum is equal to a given positive integer d. For example, if
S = {1, 2, 5, 6, 8} and d = 9 there are two solutions {1, 2, 6} and {1, 8}. A
suitable message is to be displayed if the given problem instance doesn't
have a solution.
Algorithm SumofSub(s, k, r)
// Input: Set of n numbers, S[1..n]
// Output: Solution vector, Boolean vector: X[1..n]
Algorithm BC(n, k)
// Input: Integer numbers n and k.
// Output: Binomial Co-efficient of n and k
for i ← 0 to n do
C[i, 0] ← 1 // fill the first column
C[i, i] ← 1 // fill the diagonal elements
for i ←2 to n do
for j ← 1 to i-1 do
C[i, j] ← C[i – 1, j – 1] + C[i – 1, j]
return C[n, k]
end BC.
Algorithms Lab Manual 17
Problem 17: Find the minimum cost spanning tree of a given undirected
graph using Prim's algorithm.
Algorithm Prim(G)
// Input: Directed, weighted graph, G
// Output: Minimum Spanning Tree, T
T←
U ← {1} // consider vertex 1 as starting vertex
while U ≠ V do
let (u, v) be the lowest cost edge such that
u U and v V - U
T ← T {(u, v)} // add edge to spanning tree
U ← U {v}
return T
end Prim.
18 BIT, CSE Department
Algorithm Warshall(G)
// Input: Directed graph, G
// Output: Transitive Closure, R
R(0) ← A // initialize R with adjacency matrix of G
for k ← 1 to n do
for i ← 1 to n do
for j ← 1 to n do
R(k)[i, j] ← R(k–1)[i, j] or (R(k–1)[i, k] and R(k–1) [k, j])
(n)
return R
end Warshall.
Algorithms Lab Manual 19
Problem 20: Write a C++ program to compute the mode of a given set of
numbers.
Algorithm Mode(A[0..n-1])
// Input: Array A of size n.
// Output: Mode of A
highest ← 0
mode ← 0
for i ← 0 to MAX do
array2[i] ← 0
for i ← 0 to n do
array2[a[i]] ← array2[a[i]] + 1
// compute the largest
for i ← 0 to MAX do
if array2[i] > highest
highest ← array2[i]
mode ← i
return mode
end Mode.