AOA
AOA
1 : Sort a given set of elements using the Quicksort method and determine the me required to sort the elements.
Repeat the experiment for different valuesof n, the number of elements in the list to be sorted and plot a graph of the
me taken versus n. The elements can be read from a file or can be generatedusing the random number generator.
Solu on :
int[] sizes = {1000, 5000, 10000, 20000, 50000, 100000}; // Array sizes to test
int n = sizes[i];
// Quicksort method
quicksort(arr, pi + 1, high);
// Par on method
public sta c int par on(int[] arr, int low, int high) {
i++;
arr[i] = arr[j];
arr[j] = temp;
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
return array;
} }
Q.2 : Implement a parallelized Merge Sort algorithm to sort a given set of elementsand determine the me required to
sort the elements. Repeat the experiment for different values of n, the number of elements in the list to be sorted
andplot a graph of the me taken versus n. The elements can be read from a file orcan be generated using the random
number generator.
int[] sizes = {1000, 5000, 10000, 20000, 50000, 100000}; // Array sizes to test
int n = sizes[i];
pool.invoke(new MergeSortTask(array));
}
// Merge Sort Task for parallel execu on
MergeSortTask(int[] array) {
this.array = array;
@Override
if (array.length <= 1) {
return array;
int i = 0, j = 0, k = 0;
result[k++] = le [i++];
} else {
result[k++] = right[j++];
result[k++] = le [i++];
result[k++] = right[j++];
return result;
return array;
}
Q.3 : Obtain the Topological ordering of ver ces in a given digraph. b. Computethe transi ve closure of a given directed
graph using Warshall's algorithm.
adjacencyList[from].add(to);
if (!visited[i]) {
while (!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
System.out.println();
visited[current] = true;
if (!visited[neighbor]) {
stack.push(current);
graph.addEdge(5, 2);
graph.addEdge(5, 0);
graph.addEdge(4, 0);
graph.addEdge(4, 1);
graph.addEdge(2, 3);
graph.addEdge(3, 1);
graph.topologicalSort();
}
Q.4 : Implement 0/1 Knapsack problem using Dynamic Programming.
int n = values.length;
if (weights[i - 1] <= w) {
dp[i][w] = Math.max(
);
} else {
return dp[n][capacity];
// Example data
int capacity = 5;
}
Q.5 : From a given vertex in a weighted connected graph, find shortest paths toother ver ces using Dijkstra's algorithm.
class Dijkstra {
public Dijkstra(int V) {
this.V = V;
graph[u][v] = w;
graph[v][u] = w;
min = dist[v];
min_index = v;
return min_index;
}
void dijkstra(int src) {
int dist[] = new int[V]; // The output array. dist[i] will hold
dist[i] = Integer.MAX_VALUE;
sptSet[i] = false;
dist[src] = 0;
// Pick the minimum distance vertex from the set of ver ces not
sptSet[u] = true;
// Update dist value of the adjacent ver ces of the picked vertex.
}
public sta c void main(String[] args) {
int V = 9;
graph.addEdge(0, 1, 4);
graph.addEdge(0, 7, 8);
graph.addEdge(1, 2, 8);
graph.addEdge(1, 7, 11);
graph.addEdge(2, 3, 7);
graph.addEdge(2, 8, 2);
graph.addEdge(2, 5, 4);
graph.addEdge(3, 4, 9);
graph.addEdge(4, 5, 10);
graph.addEdge(5, 6, 2);
graph.addEdge(6, 7, 1);
graph.addEdge(6, 8, 6);
graph.addEdge(7, 8, 7);
graph.dijkstra(0);
}
Q.6 : Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal's algorithm.
// Constructor
this.src = src;
this.dest = dest;
this.weight = weight;
class Subset {
// Constructor
this.edges = edges;
} }
if (subsets[u].parent != u)
return subsets[u].parent;
}
subsets[rootX].parent = rootY;
subsets[rootY].parent = rootX;
} else {
subsets[rootY].parent = rootX;
subsets[rootX].rank++;
void kruskalMST() {
Arrays.sort(edge);
subsets[i].parent = i;
subsets[i].rank = 0;
if (x != y) {
result[e++] = nextEdge;
union(subsets, x, y);
} }
int minimumCost = 0;
minimumCost += result[i].weight;
graph.kruskalMST();
} }
Q.7 : a. Print all the nodes reachable from a given star ng node in a digraph using BFS method
// Constructor
adj[src].add(dest);
visited[start] = true;
queue.add(start);
while (!queue.isEmpty()) {
if (!visited[neighbor]) {
visited[neighbor] = true;
queue.add(neighbor);
System.out.println();
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 3);
graph.addEdge(2, 3);
graph.addEdge(3, 4);
graph.addEdge(4, 5);
// Constructor
adj[src].add(dest);
visited[v] = true;
if (!visited[neighbor]) {
dfs(neighbor, visited);
boolean isConnected() {
dfs(0, visited);
return true;
}
public sta c void main(String[] args) {
graph.addEdge(0, 1);
graph.addEdge(1, 2);
graph.addEdge(2, 3);
graph.addEdge(3, 4);
if (graph.isConnected()) {
} else {
}
Q.8 : Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s algorithm.
// Func on to find the vertex with the minimum key value that is not yet included in the MST
min = key[v];
minIndex = v;
return minIndex;
int totalCost = 0;
System.out.println("Edge \tWeight");
totalCost += graph[i][parent[i]];
// Prim's algorithm
Arrays.fill(key, Integer.MAX_VALUE);
Arrays.fill(mstSet, false);
mstSet[u] = true;
// Update the key only if graph[u][v] is smaller and v is not yet in MST
parent[v] = u;
key[v] = graph[u][v];
}
public sta c void main(String[] args) {
int[][] graph = {
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}
};
mst.primMST(graph);
}
Q.9 : Implement All-Pairs Shortest Paths Problem using Floyd's algorithm.
final sta c int INF = 99999; // Represents infinity for unreachable nodes
// Ini alize the solu on matrix same as the input graph matrix
dist[i][j] = graph[i][j];
printSolu on(dist);
if (dist[i][j] == INF) {
System.out.print("INF ");
} else {
System.out.println();
int[][] graph = {
{8, 0, 2, INF},
};
fw.floydWarshall(graph);
} }
Q.10 : Implement N Queen's problem using Back Tracking.
int N;
public NQueens(int N) {
this.N = N;
System.out.println();
// Check column
if (board[i][col] == 1) {
return false;
if (board[i][j] == 1) {
return false;
}
// Check upper right diagonal
if (board[i][j] == 1) {
return false;
return true;
if (row >= N) {
return true;
return true;
board[row][col] = 0; // Backtrack
return false;
}
// Method to solve the N-Queens problem
return false;
printSolu on(board);
return true;
if (queens.solveNQueens()) {