ADA Lab Manual-3
ADA Lab Manual-3
3a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths
problem using Floyd's algorithm.
#include<stdio.h>
// Number of vertices in the graph
#define V 4
// A function to print the solution matrix
void printSolution(int dist[][V]);
void floydWarshall (int graph[][V])
{
int dist[V][V], i, j, k;
// Initialize the solution matrix same as input graph matrix.
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
dist[i][j] = graph[i][j];
// Update the solution matrix by picking all vertices one by one
for (k = 0; k < V; k++)
{
for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
// If vertex k is on the shortest path from i to j,
// then update the value of dist[i][j]
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
printSolution(dist);
}
void printSolution(int dist[][V])
{
printf ("Following matrix shows the shortest distances between every pair of vertices \n");
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (dist[i][j] == 9999)
printf("%7s", "INF");
else
printf ("%7d", dist[i][j]);
}
printf("\n");
}
}
int main()
{
int graph[V][V] = { {0, 5, 9999, 10},
{9999, 0, 3, 9999},
{9999, 9999, 0, 1},
{9999, 9999, 9999, 0}
};
floydWarshall(graph);
return 0;
}
OUTPUT
3b.Design and implement C/C++ Program to find the transitive closure using
Warshal's algorithm
#include<stdio.h>
#include<math.h>
int max(int,int);
void warshal(int p[10][10],int n) {
int i,j,k;
for (k=1;k<=n;k++)
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
p[i][j]=max(p[i][j],p[i][k]&&p[k][j]);
}
int max(int a,int b) {
if(a>b)
return(a);
else
return(b);
}
void main() {
int p[10][10]= {0},n,e,u,v,i,j;
printf("\n Enter the number of vertices:");
scanf("%d",&n);
printf("\n Enter the number of edges:");
scanf("%d",&e);
for (i=1;i<=e;i++) {
printf("\n Enter the end vertices of edge %d:",i);
scanf("%d%d",&u,&v);
p[u][v]=1;
}
printf("\n Matrix of input data: \n");
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
warshal(p,n);
printf("\n Transitive closure: \n");
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
}
OUTPUT
4.Design and implement C/C++ Program to find shortest paths from a given
vertex in a weighted connected graph to other vertices using Dijkstra's
algorithm.
/#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
// Number of vertices in the graph
#define V 9
// A utility function to find the vertex with minimum
// distance value, from the set of vertices not yet included
// in shortest path tree
int minDistance(int dist[], bool sptSet[])
{
// Initialize min value
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
// A utility function to print the constructed distance
// array
void printSolution(int dist[])
{
printf("Vertex \t\t Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t\t\t\t %d\n", i, dist[i]);
}
// Function that implements Dijkstra's single source
// shortest path algorithm for a graph represented using
// adjacency matrix representation
void dijkstra(int graph[V][V], int src)
{
int dist[V]; // The output array. dist[i] will hold the shortest distance from src to i
bool sptSet[V];
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
// Distance of source vertex from itself is always 0
dist[src] = 0;
// Find shortest path for all vertices
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet);
// Mark the picked vertex as processed
sptSet[u] = true;
// Update dist value of the adjacent vertices of the
// picked vertex.
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v]
&& dist[u] != INT_MAX
&& dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
// print the constructed distance array
printSolution(dist);
}
// 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;
}
OUTPUT
5.Design and implement C/C++ Program to obtain the Topological ordering
of vertices in a given digraph.
// C Program to implement Topological Sorting
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// Structure to represent a stack
struct Stack {
int data;
struct Stack* next;
};
struct Graph {
int V; // No. of vertices
// Pointer to an array containing adjacency lists
struct List* adj;
};
// Structure to represent a list (adjacency list)
struct List {
int data;
struct List* next;
};
// Create a new node for the stack
struct Stack* createStackNode(int data)
{
struct Stack* newNode
= (struct Stack*)malloc(sizeof(struct Stack));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Create a new node for the adjacency list
struct List* createListNode(int data)
{
struct List* newNode
= (struct List*)malloc(sizeof(struct List));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to initialize a graph with V vertices
struct Graph* createGraph(int V)
{
struct Graph* graph
= (struct Graph*)malloc(sizeof(struct Graph));
graph->V = V;
graph->adj
= (struct List*)malloc(V * sizeof(struct List));
for (int i = 0; i < V; ++i) {
graph->adj[i].next = NULL;
}
return graph;
}
// Function to add an edge to the graph
void addEdge(struct Graph* graph, int v, int w)
{
struct List* newNode = createListNode(w);
newNode->next = graph->adj[v].next;
graph->adj[v].next = newNode;
}
// A recursive function used by topologicalSort
void topologicalSortUtil(struct Graph* graph, int v,
bool visited[],
struct Stack** stack)
{
visited[v] = true;
struct List* current = graph->adj[v].next;
while (current != NULL) {
int adjacentVertex = current->data;
if (!visited[adjacentVertex]) {
topologicalSortUtil(graph, adjacentVertex,
visited, stack);
}
current = current->next;
}
// Push the current vertex to stack which stores the
// result
struct Stack* newNode = createStackNode(v);
newNode->next = *stack;
*stack = newNode;
}
// The function to do Topological Sort. It uses recursive
// topologicalSortUtil
void topologicalSort(struct Graph* graph)
{
struct Stack* stack = NULL;
// Mark all the vertices as not visited
bool* visited = (bool*)malloc(graph->V * sizeof(bool));
for (int i = 0; i < graph->V; ++i) {
visited[i] = false;
}
// Call the recursive helper function to store
// Topological Sort starting from all vertices one by
// one
for (int i = 0; i < graph->V; ++i) {
if (!visited[i]) {
topologicalSortUtil(graph, i, visited, &stack);
}
}
int main() {
int n_values[] = {5000, 10000, 15000, 20000}; // Array of n values
int num_values = sizeof(n_values) / sizeof(n_values[0]);
double time_taken[num_values];
// Plotting the graph can be done using external tools like gnuplot, matplotlib, etc.
return 0;
}
10.Design and implement C/C++ Program to sort a given set of n integer
elements using Quick Sort method and compute its time complexity. Run the
program for varied values of n> 5000 and record the time taken to sort. Plot
a graph of the time taken versus n. The elements can be generated using the
random number generator.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function prototypes
void quickSort(int arr[], int low, int high);
int partition(int arr[], int low, int high);
void swap(int *a, int *b);
void generateRandomArray(int arr[], int n);
double calculateTimeTaken(clock_t start, clock_t end);
int main() {
int n_values[] = {5000, 10000, 15000, 20000}; // Array of n values
int num_values = sizeof(n_values) / sizeof(n_values[0]);
clock_t start, end;
double time_taken[num_values];
for (int i = 0; i < num_values; i++) {
int n = n_values[i];
int *arr = (int*)malloc(n * sizeof(int));
generateRandomArray(arr, n);
start = clock();
quickSort(arr, 0, n - 1);
end = clock();
time_taken[i] = calculateTimeTaken(start, end);
free(arr);
}
// Partition function
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
11. Design and implement C/C++ Program to sort a given set of n integer
elements using Merge Sort method and compute its time complexity. Run the
program for varied values of n> 5000, and record the time taken to sort. Plot
a graph of the time taken versus n. The elements can be read from a file or
can be generated using the random number generator.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int n_values[] = {5000, 10000, 15000, 20000}; // Array of n values
int num_values = sizeof(n_values) / sizeof(n_values[0]);
double time_taken[num_values];
// Plotting the graph can be done using external tools like gnuplot, matplotlib, etc.
return 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;
}