DAA Assignment
DAA Assignment
DAA Assignment
1 Sort a given set of elements using the Quicksort method and determine the time required
to sort the elements. Repeat the experiment for di erent values of n, the number of
elements in the list to be sorted and plot a graph of the time taken versus n. The elements
can be read from a le or can be generated using the random number generator.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Quicksort function
void quicksort(int arr[], int low, int high) {
if (low < high) {
// Partition the array
int pi = partition(arr, low, high);
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
return 0;
}
Output:
2. Implement a parallelized Merge Sort algorithm to sort a given set of elements and
determine the time required to sort the elements. Repeat the experiment for di erent values
of n, the number of elements in the list to be sorted and plot a graph of the time taken
versus n. The elements can be read from a le or can be generated using the random
number generator.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
return 0;
}
Output :
3. Obtain the Topological ordering of vertices in a given digraph. b. Compute the transitive
closure of a given directed graph using Warshall's algorithm
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Graph {
int numVertices;
struct Node** adjLists;
int* visited;
};
return graph;
}
// Push current vertex to stack when all its adjacent vertices are visited
*stack = createNode(v);
(*stack)->next = graph->adjLists[v];
graph->adjLists[v] = *stack;
}
int main() {
int numVertices, numEdges;
printf("Enter the number of vertices: ");
scanf("%d", &numVertices);
printf("Enter the number of edges: ");
scanf("%d", &numEdges);
return 0;
}
Output:
#include<stdio.h>
return K[n][W];
}
int main() {
int val[] = {60, 100, 120};
int wt[] = {10, 20, 30};
int W = 50;
int n = sizeof(val) / sizeof(val[0]);
printf("Maximum value that can be obtained is %d\n", knapsack(W, wt, val, n));
return 0;
}
Output:
5 From a given vertex in a weighted connected graph, nd shortest paths to other vertices
using Dijkstra's algorithm.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
return min_index;
}
int main() {
fi
fi
// Sample graph represented as an adjacency matrix
int graph[V][V] = {
{0, 10, 0, 5, 0, 0},
{0, 0, 1, 2, 0, 0},
{0, 0, 0, 0, 4, 0},
{0, 3, 9, 0, 2, 8},
{0, 0, 0, 0, 0, 6},
{0, 0, 0, 0, 0, 0}
};
dijkstra(graph, src);
return 0;
}
Output:
6 Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal's algorithm.
#include <stdio.h>
#include <stdlib.h>
// If including this edge doesn't cause cycle, include it in the result and increment the index for
result[]
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
}
}
free(subsets);
}
int main() {
int numVertices, numEdges;
printf("Enter the number of vertices and edges: ");
scanf("%d %d", &numVertices, &numEdges);
free(graph);
return 0;
}
Output:
7. a. Print all the nodes reachable from a given starting node in a digraph using BFS method.
fi
fi
#include <stdio.h>
#include <stdlib.h>
return graph;
}
fi
// Function to add an edge to the graph
void addEdge(struct Graph* graph, int src, int dest) {
// Add edge from src to dest
struct Node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
}
int main() {
int numVertices, numEdges;
printf("Enter the number of vertices: ");
scanf("%d", &numVertices);
int startVertex;
printf("Enter the starting vertex for BFS: ");
scanf("%d", &startVertex);
BFS(graph, startVertex);
return 0;
}
Output:
8. Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s algorithm.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// Function to nd the vertex with minimum key value, from the set of vertices not yet included in
MST
int minKey(int key[], int mstSet[], int numVertices) {
int min = INT_MAX, min_index;
// Function to construct and print MST for a graph represented using adjacency matrix
void primMST(int graph[MAX_VERTICES][MAX_VERTICES], int numVertices) {
int parent[MAX_VERTICES]; // Array to store constructed MST
int key[MAX_VERTICES]; // Key values used to pick minimum weight edge in cut
int mstSet[MAX_VERTICES]; // To represent set of vertices included in MST
// Always include rst vertex in MST. Make key 0 so that this vertex is picked as rst vertex.
key[0] = 0;
parent[0] = -1; // First node is always root of MST
// Update key value and parent index of the adjacent vertices of the picked vertex
// Consider only those vertices which are not yet included in MST
for (int v = 0; v < numVertices; v++) {
// graph[u][v] is non-zero only for adjacent vertices of u
// mstSet[v] is false for vertices not yet included in MST
// Update the key only if graph[u][v] is smaller than key[v]
if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}
}
int graph[MAX_VERTICES][MAX_VERTICES];
printf("Enter the adjacency matrix:\n");
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
scanf("%d", &graph[i][j]);
}
}
// Print the Minimum Cost Spanning Tree (MST) using Prim's algorithm
primMST(graph, numVertices);
return 0;
}
Output:
#include <stdio.h>
#include <limits.h>
// Function to nd the shortest paths between all pairs of vertices using Floyd's algorithm
void oydWarshall(int graph[MAX_VERTICES][MAX_VERTICES], int numVertices) {
int dist[MAX_VERTICES][MAX_VERTICES];
int main() {
int numVertices;
printf("Enter the number of vertices: ");
scanf("%d", &numVertices);
int graph[MAX_VERTICES][MAX_VERTICES];
printf("Enter the adjacency matrix:\n");
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
scanf("%d", &graph[i][j]);
}
}
return 0;
}
Output:
fl
10. Implement N Queen's problem using Back Tracking.
#include <stdio.h>
#include <stdbool.h>
return true;
}
// Consider this column and try placing this queen in all rows one by one
for (int i = 0; i < N; i++) {
// Check if the queen can be placed on board[i][col]
if (isSafe(board, i, col)) {
// Place this queen in board[i][col]
board[i][col] = 1;
// If placing queen in board[i][col] doesn't lead to a solution, then remove queen from
board[i][col]
board[i][col] = 0; // BACKTRACK
}
}
// If the queen can't be placed in any row in this column col then return false
return false;
}
if (solveNQueens(board, 0) == false) {
printf("Solution does not exist");
return;
}
printSolution(board);
}
int main() {
solveNQueensWrapper();
return 0;
}
Output: