0% found this document useful (0 votes)
11 views

ADA Algorithms

Uploaded by

arugunta.cs22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

ADA Algorithms

Uploaded by

arugunta.cs22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Topological Sort by Source Removal:

ALGORITHM TopologicalSortUsingSourceRemoval(n, a[0..n-1][0..n-1])


//Performs topological sort using source removal method
//Input: Integer n (number of vertices), adjacency matrix a[0..n-1][0..n-1]
//Output: Array soln[0..n-1] containing topologically sorted vertices

//Calculate in-degrees of all vertices


for j ← 0 to n-1 do
sum ← 0
for i ← 0 to n-1 do
sum ← sum + a[i][j]
indegree[j] ← sum

//Initialize stack with vertices of in-degree 0


top ← -1
for i ← 0 to n-1 do
if indegree[i] = 0 then
top ← top + 1
s[top] ← i

//Perform topological sort


si ← 0
while top ≠ -1 do
u ← s[top]
top ← top - 1
soln[si] ← u
si ← si + 1
for i ← 0 to n-1 do
if a[u][i] = 1 then
indegree[i] ← indegree[i] - 1
if indegree[i] = 0 then
top ← top + 1
s[top] ← i

//Output sorted array soln

2. Topological Sort by DFS:

ALGORITHM TopologicalSortUsingDFS(n, a[0..n-1][0..n-1])


//Performs topological sort using DFS method
//Input: Integer n (number of vertices), adjacency matrix a[0..n-1][0..n-1]
//Output: Array res[0..n-1] containing topologically sorted vertices

//Initialize visited array and result array


for i ← 0 to n-1 do
vis[i] ← 0
j←0
//Perform DFS on unvisited nodes
for i ← 0 to n-1 do
if vis[i] = 0 then
dfs(i, n, a)

//DFS traversal to visit nodes and record topological order


procedure dfs(u, n, a[0..n-1][0..n-1])
vis[u] ← 1
for i ← 0 to n-1 do
if a[u][i] = 1 and vis[i] = 0 then
dfs(i, n, a)
res[j] ← u
j←j+1

//Output the result in reverse order


for i ← n-1 down to 0 do
print res[i]

3. Merge Sort:

ALGORITHM Mergesort(A[0..n−1])
//Sorts array A[0..n − 1]by recursive mergesort
//Input: An array A[0..n − 1]of orderable elements
//Output: Array A[0..n − 1]sorted in nondecreasing order
if n>1
copy A[0.. n/2 −1]to B[0.. n/2 −1]
copy A[ n/2 ..n−1]to C[0.. n/2 −1]
Mergesort(B[0.. n/2 −1])
Mergesort(C[0.. n/2 −1])
Merge(B, C, A) //see below

ALGORITHM Merge(B[0..p −1],C[0..q −1],A[0..p +q −1])


//Merges two sorted arrays into one sorted array
//Input: Arrays B[0..p − 1]and C[0..q − 1]both sorted
//Output: Sorted array A[0..p + q − 1]of the elements of B and C
i ←0; j ←0; k←0
while i<p and j<q do
if B[i]≤ C[j]
A[k]←B[i]; i ←i +1
else
A[k]←C[j]; j ←j +1
k ←k+1
if i =p
copy C[j..q − 1]to A[k..p + q −1]
else
copy B[i..p − 1]to A[k..p + q −1]

4. Quick Sort:
ALGORITHM Quicksort(A[l..r])
//Sorts a subarray by quicksort
//Input: Subarray of array A[0..n − 1], defined by its left and right indices l and r
//Output: Subarray A[l..r] sorted in nondecreasing order
if l < r
s ← Partition(A[l..r]) // s is a split position
Quicksort(A[l..s − 1])
Quicksort(A[s + 1..r])

ALGORITHM HoarePartition(A[l..r])
//Partitions a subarray by Hoare’s algorithm, using the first element as a pivot
//Input: Subarray of array A[0..n − 1], defined by its left and right indices l and r (l < r)
//Output: Partition of A[l..r], with the split position returned as this function’s value
p ← A[l]
i ← l; j ← r + 1
repeat
repeat i ← i + 1 until A[i] ≥ p
repeat j ← j - 1 until A[j] ≤ p
swap(A[i], A[j])
until i ≥ j
swap(A[i], A[j]) //undo last swap when i ≥ j
swap(A[l], A[j])
return j

5. Heap Sort:

ALGORITHM heapBottomUp(H[1..n])
// Constructs a heap from elements of a given array by the bottom-up algorithm
// Input: An array H[1..n] of orderable items
// Output: A heap H[1..n]

for i ← n/2 downto 1 do


k ← i; v ← H[k]
heapMade ← 0

while not heapMade and 2 * k ≤ n do


j←2*k

if j < n then
if H[j] < H[j + 1] then
j←j+1

if v ≥ H[j] then
heapMade ← 1
else
H[k] ← H[j]
k←j
H[k] ← v

ALGORITHM heapSort(H[1..n])
// Sorts an array using heap sort
// Input: An array H[1..n] of orderable items
// Output: Array H[1..n] sorted in non-decreasing order

heapBottomUp(H, n)

for i ← n downto 2 do
swap(H[1], H[i])
n←n-1
heapBottomUp(H, n)

6. 0-1 Knapsack Problem:

ALGORITHM Knapsack(W, wt[], val[], n)


// Solves the 0/1 Knapsack problem using dynamic programming
// Input:
// W: Maximum weight capacity of the knapsack
// wt[1..n]: Array of weights of items
// val[1..n]: Array of values of items
// n: Number of items
// Output:
// Maximum value that can be obtained without exceeding the weight capacity W

1. Initialize a 2D array K[n+1][W+1] with zeros.


2. for i ← 1 to n do
3. for w ← 1 to W do
4. if wt[i] > w
5. K[i][w] ← K[i-1][w]
6. else
7. K[i][w] ← max(val[i] + K[i-1][w-wt[i]], K[i-1][w])
8. return K[n][W]

7. Floyd's Algorithm for All pairs Shortest Path:

ALGORITHM Floyd(W[1..n,1..n])
// Implements Floyd’s algorithm for the all-pairs shortest-paths problem
// Input: The weight matrix W of a graph with no negative-length cycle
// Output: The distance matrix of the shortest paths’ lengths

D ← W // Make a copy of the weight matrix W (if necessary)


for k ← 1 to n do
for i ← 1 to n do
for j ← 1 to n do
D[i, j] ← min(D[i, j], D[i, k] + D[k, j])
return D
8. Prims for MST:

ALGORITHM Prim(G)
// Prim’s algorithm for constructing a minimum spanning tree
// Input: A weighted connected graph G = (V, E)
// Output: ET, the set of edges composing a minimum spanning tree of G

VT ← {v0} // Initialize the set of tree vertices with any vertex (here v0)
ET ← ∅ // Initialize the set of edges in the minimum spanning tree as empty
for i ← 1 to |V| - 1 do
Find a minimum-weight edge e* = (v*, u*) among all edges (v, u) such that v is in VT and u is in V - VT
VT ← VT ∪ {u*}
ET ← ET ∪ {e*}
return ET

9. Fractional Knapsack:

ALGORITHM FractionalKnapsack(W, v, n, C)
// Solves the Fractional Knapsack problem using greedy method
// Input: Array W of item weights, array v of item values, number of items n, and knapsack capacity C
// Output: Maximum value that can be obtained

1. Compute the value-to-weight ratio v[i] / W[i] for each item i


2. Sort items based on the value-to-weight ratio in non-increasing order
3. Initialize current weight W_curr = 0 and current value V_curr = 0
4. for i ← 1 to n do
5. if W_curr + W[i] <= C then
6. W_curr ← W_curr + W[i]
7. V_curr ← V_curr + v[i]
8. else
9. fraction ← (C - W_curr) / W[i]
10. V_curr ← V_curr + v[i] * fraction
11. break
12. return V_curr

10. Dijkastra for MST:

ALGORITHM Dijkstra(G, src)


// Dijkstra’s algorithm for single-source shortest paths and its vertex src
// Input: A weighted connected graph G = (V, E) with non-negative weights
// Output: Shortest path distances dist[] from src to each vertex and their paths

Initialize dist[], parent[], and visited[] arrays


dist[src] = 0, parent[src] = -1
for each vertex v in V
if v ≠ src
dist[v] = INT_MAX, parent[v] = -1
visited[v] = 0

for count from 0 to |V|-1


u = vertex with minimum dist[] value, not yet visited
Mark u as visited
Update dist[] values of adjacent vertices v of u
if v is not visited and there is an edge from u to v and
dist[u] + weight(u, v) < dist[v]
dist[v] = dist[u] + weight(u, v)
parent[v] = u

Print shortest path distances and paths from src to each vertex

You might also like