ADS&AA Lab Part 2
ADS&AA Lab Part 2
ADS&AA Lab Part 2
Exercise :5
{
private int V, E; // No. of vertices & Edges respectively
private List<int> []adj;
// Constructor
public Graph(int v)
{
V = v;
E = 0;
adj = new List<int>[v];
for (int i = 0; i < v; ++i)
adj[i] = new List<int>();
}
// Check if the subtree rooted with 'v' has a connection to one of the ancestors of 'u'.
// Case 1 -- per Strongly Connected Components Article
if (low[u] > low[v])
low[u] = low[v];
count++;
}
}
// Update low value of 'u' only if 'v' is still in stack (i.e. it's a back edge, not cross edge).
// Case 2 -- per Strongly Connected Components Article
else if (v != parent[u] && disc[v] < disc[u] )
{
if (low[u] > disc[v])
low[u] = disc[v];
void BCC()
{
int []disc = new int[V];
int []low = new int[V];
int []parent = new int[V];
List<Edge> st = new List<Edge>();
// Driver code
public static void Main(String []args)
{
Graph g = new Graph(12);
g.addEdge(0, 1);
g.addEdge(1, 0);
g.addEdge(1, 2);
g.addEdge(2, 1);
g.addEdge(1, 3);
g.addEdge(3, 1);
g.addEdge(2, 3);
g.addEdge(3, 2);
g.addEdge(2, 4);
g.addEdge(4, 2);
g.addEdge(3, 4);
g.addEdge(4, 3);
g.addEdge(1, 5);
g.addEdge(5, 1);
g.addEdge(0, 6);
g.addEdge(6, 0);
g.addEdge(5, 6);
g.addEdge(6, 5);
g.addEdge(5, 7);
g.addEdge(7, 5);
g.addEdge(5, 8);
g.addEdge(8, 5);
g.addEdge(7, 8);
g.addEdge(8, 7);
g.addEdge(8, 9);
g.addEdge(9, 8);
g.addEdge(10, 11);
g.addEdge(11, 10);
g.BCC();
Implement Quick Sort and merge sort and observe the execution time for various input
sizes(Average, Worst, and Best Cases.
1) Quick Sort
#include <stdio.h>
int partition (int a[], int start, int end)
{
int pivot = a[end]; // pivot element
int i = (start - 1);
{
if (start < end)
{
int p = partition(a, start, end); //p is the partitioning index
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}
/* function to print an array */
void printArr(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
int main()
{
int a[] = { 24, 9, 29, 14, 19, 27 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
quick(a, 0, n - 1);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
2) Merge Sort
// C program for the implementation of merge sort
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr[] = { 12, 11, 13, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
Compare the performance of single source shortest paths using Greedy method when the graph is
represented by adjacent matrix and adjacency lists.
/ C program for Dijkstra's single source shortest path algorithm. The program is for
adjacency matrix representation of the graph
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
return min_index;
}
// driver's code
int main()
{
/* Let us create the example graph discussed above */
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
// Function call
dijkstra(graph, 0);
return 0;
}
Exercise :8
// Driver's code
int main()
{
Job arr[] = { { 'a', 2, 100 },
{ 'b', 1, 19 },
{ 'c', 2, 27 },
{ 'd', 1, 25 },
{ 'e', 3, 15 } };
int n = sizeof(arr) / sizeof(arr[0]);
printf(
"Following is maximum profit sequence of jobs \n");
// Function call
printJobScheduling(arr, n);
return 0;
}
Exercise :9
#include <stdio.h>
if (wt[n - 1] > W)
return knapSack(W, wt, val, n - 1);
// Return the maximum of two cases: (1) nth item included, (2) not included.
else
return max(
val[n - 1]
+ knapSack(W - wt[n - 1], wt, val, n - 1),
knapSack(W, wt, val, n - 1));
}
// Driver code
int main()
{
int profit[] = { 60, 100, 120 };
int weight[] = { 10, 20, 30 };
int W = 50;
int n = sizeof(profit) / sizeof(profit[0]);
printf("%d", knapSack(W, weight, profit, n));
return 0;
}
Exercise : 10
#define N 4
#include <stdbool.h>
#include <stdio.h>
return true;
}
bool solveNQUtil(int board[N][N], int col)
{
// Base case: If all queens are placedthen return true
if (col >= N)
return true;
// Consider this column and try placingthis queen in all rows one by one
for (int i = 0; i < N; i++) {
if (isSafe(board, i, col)) {
board[i][col] = 0; // BACKTRACK
}
}
return false;
}
bool solveNQ()
{
int board[N][N] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
if (solveNQUtil(board, 0) == false) {
printf("Solution does not exist");
return false;
}
printSolution(board);
return true;
}
// Driver program to test above function
int main()
{
solveNQ();
return 0;
}
Exercise 11
{
// Base Case
if (n == 0 || W == 0)
return 0;
if (wt[n - 1] > W)
return knapsackRecursive(W, wt, val, n - 1);
else
return max(val[n - 1]
+ knapsackRecursive(W - wt[n - 1],
wt, val, n - 1),
knapsackRecursive(W, wt, val, n - 1));
}
// Driver Code
int main()
{
int values[] = { 3, 4, 5, 6 };
int weight[] = { 2, 3, 4, 5 };
int W = 8;
// Find the number of items
int n = sizeof(values) / sizeof(values[0]);
return 0;
}
Exercise 12
Implement travelling sales person problem using branch and bound approach.
static int N = 4;
// final_path[] stores the final solution ie, the path of the salesman.
static int[] final_path = new int[N + 1];
// Function to find the minimum edge cost having an end at the vertex i
static int firstMin(int[, ] adj, int i)
{
int min = Int32.MaxValue;
for (int k = 0; k < N; k++)
if (adj[i, k] < min && i != k)
min = adj[i, k];
return min;
}
// function to find the second minimum edge cost having an end at the vertex i
static int secondMin(int[, ] adj, int i)
{
int first = Int32.MaxValue, second = Int32.MaxValue;
for (int j = 0; j < N; j++) {
if (i == j)
continue;
// for any other level iterate for all vertices to build the search space tree recursively.
for (int i = 0; i < N; i++) {
if (adj[curr_path[level - 1], i] != 0
&& visited[i] == false) {
int temp = curr_bound;
curr_weight += adj[curr_path[level - 1], i];
// Else we have to prune the node by resetting all changes to curr weight and curr_bound.
curr_weight -= adj[curr_path[level - 1], i];
curr_bound = temp;
int curr_bound = 0;
Array.Fill(curr_path, -1);
Array.Fill(visited, false);
// Driver code
static public void Main()
{
// Adjacency matrix for the given graph
int[, ] adj = { { 0, 10, 15, 20 },
{ 10, 0, 35, 25 },
{ 15, 35, 0, 30 },
{ 20, 25, 30, 0 } };
TSP(adj);