0% found this document useful (0 votes)
5 views17 pages

AOA Lab Assignment 4 2024UG100001

The document contains lab exercises for implementing Prim's and Kruskal's algorithms, as well as Dijkstra's algorithm in C. It provides code examples for modifying graph values and accepting user input for vertices and edge weights. Each section includes a solution and expected output for the algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

AOA Lab Assignment 4 2024UG100001

The document contains lab exercises for implementing Prim's and Kruskal's algorithms, as well as Dijkstra's algorithm in C. It provides code examples for modifying graph values and accepting user input for vertices and edge weights. Each section includes a solution and expected output for the algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Lab Sheet-Prim’s Algorithm

Exercise 1. Modify the values of V and Matrix G[V][V] in the above code to
obtain the output for the below graph.

Solution:
#include <stdio.h>
#include <stdbool.h>

#define INF 9999999


#define V 6

int G[V][V] = {
{0, 2, 3, 0, 0, 0},
{2, 0, 5, 3, 0, 0},
{3, 5, 0, 0, 4, 0},
{0, 3, 0, 0, 2, 3},
{0, 0, 4, 2, 0, 5},
{0, 0, 0, 3, 5, 0}
};
int main() {
int i, j, no_edge;
int selected[V];

for (i = 0; i < V; i++) {


selected[i] = false;
}

no_edge = 0;
selected[0] = true;
int x;
int y;

printf("Edge: W eight\n");

while (no_edge < V - 1) {


int min = INF;
x = 0;
y = 0;

for (i = 0; i < V; i++) {


if (selected[i]) {
for (j = 0; j < V; j++) {
if (!selected[j] && G[i][j]) {
if (min > G[i][j]) {
min = G[i][j];
x = i;
y = j;
}
}
}
}
}

printf("%d-%d: %d\n", x, y, G[x][y]);


selected[y] = true;
no_edge++;
}
return 0;
}

OUTPUT:

Exercise 2: Modify the code to take the graph's input interactively from the user,
allowing them to enter the number of vertices, edge weights, etc.

Solution:
#include <stdio.h>
#include <stdbool.h>

#define INF 9999999

int main() {
int V;

printf("Enter the number of vertices: ");


scanf("%d", &V);
int G[V][V];
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
G[i][j] = 0;
}
}

printf("Enter the edges and their weights (enter -1 -1 -1 to stop):\n");


printf("Format: <source> <destination> <weight>\n");
while (1) {
int u, v, weight;
scanf("%d %d %d", &u, &v, &weight);
if (u == -1 && v == -1 && weight == -1) {
break; // End of input
}
G[u][v] = weight;
G[v][u] = weight;
}

int selected[V];
for (int i = 0; i < V; i++) {
selected[i] = false;
}

selected[0] = true;
int no_edge = 0;
int x, y;

printf("Edge: Weight\n");

while (no_edge < V - 1) {


int min = INF;
x = 0;
y = 0;
for (int i = 0; i < V; i++) {
if (selected[i]) {
for (int j = 0; j < V; j++) {
if (!selected[j] && G[i][j]) {
if (min > G[i][j]) {
min = G[i][j];
x = i;
y = j;
}
}
}
}
}

printf("%d - %d : %d\n", x, y, G[x][y]);


selected[y] = true;
no_edge++;
}

return 0;
}

OUTPUT:
Lab Sheet-Kruskal’s Algorithm

Exercise 1: Modify the values of V, E and edges[ ][ ] in the above code to obtain the
output for the below graph(s).

(A)

Solution:

#include<stdio.h>
#define INT_MAX 99999999

#define V 5
int parent[V];

int find(int i) {
while (parent[i] != i)
i = parent[i];
return i;
}

void union1(int i, int j) {


int a = find(i);
int b = find(j);
parent[a] = b;
}

void kruskalMST(int cost[][V]) {


int mincost = 0;

for (int i = 0; i < V; i++)


parent[i] = i;

int edge_count = 0;
while (edge_count < V - 1) {
int min = INT_MAX, a = -1, b = -1;
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (find(i) != find(j) && cost[i][j] < min) {
min = cost[i][j];
a = i;
b = j;
}
}
}
union1(a, b);
printf("Edge %d: (%d, %d) cost: %d\n", edge_count++, a, b, min);
mincost += min;
}
printf("\nMinimum cost = %d\n", mincost);
}

int main() {
int cost[][V] = {
{INT_MAX, 1, 7,10, 5},
{1, INT_MAX, 3, INT_MAX, INT_MAX},
{7, 3, INT_MAX, 4, INT_MAX},
{INT_MAX, INT_MAX, 4, INT_MAX, 2},
{5, INT_MAX, INT_MAX, 2, INT_MAX}
};
kruskalMST(cost);
return 0;
}

OUTPUT:

(B)
CODE:

#include<stdio.h>
#define INT_MAX 99999999

#define V 6
int parent[V];

int find(int i) {
while (parent[i] != i)
i = parent[i];
return i;
}

void union1(int i, int j) {


int a = find(i);
int b = find(j);
parent[a] = b;
}

void kruskalMST(int cost[][V]) {


int mincost = 0;

for (int i = 0; i < V; i++)


parent[i] = i;

int edge_count = 0;
while (edge_count < V - 1) {
int min = INT_MAX, a = -1, b = -1;
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (find(i) != find(j) && cost[i][j] < min) {
min = cost[i][j];
a = i;
b = j;
}
}
}
union1(a, b);
printf("Edge %d: (%d, %d) cost: %d\n", edge_count++, a, b, min);
mincost += min;
}
printf("\nMinimum cost = %d\n", mincost);
}

int main() {
int cost[][V] = {
{INT_MAX, 2, 3, INT_MAX, INT_MAX, INT_MAX}, // a
{2, INT_MAX, 5, 3,4, INT_MAX}, // b
{3, 5, INT_MAX, INT_MAX, 4, INT_MAX}, // c
{INT_MAX, 3, INT_MAX, INT_MAX, 2, 3}, // d
{INT_MAX, INT_MAX, 4, 2, INT_MAX, 5}, // e
{INT_MAX, INT_MAX, INT_MAX, 3, 5, INT_MAX} // f
};

kruskalMST(cost);
return 0;
}

OUTPUT:
Exercise 2: Modify the above code to read the graph vertices and its edge weight values
during execution time.

Solution:

#include<stdio.h>
#include<stdlib.h>
#define INT_MAX 99999999

int V; // Number of vertices


int *parent;

// Find operation for Disjoint Set


int find(int i) {
while (parent[i] != i)
i = parent[i];
return i;
}

// Union operation for Disjoint Set


void union1(int i, int j) {
int a = find(i);
int b = find(j);
parent[a] = b;
}

void kruskalMST(int **cost) {


int mincost = 0;

// Initialize parent array


parent = (int *)malloc(V * sizeof(int));
for (int i = 0; i < V; i++)
parent[i] = i;

int edge_count = 0;
while (edge_count < V - 1) {
int min = INT_MAX, a = -1, b = -1;
// Find the minimum cost edge among all vertices
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (find(i) != find(j) && cost[i][j] < min) {
min = cost[i][j];
a = i;
b = j;
}
}
}

if (a != -1 && b != -1) {
union1(a, b);
printf("Edge %d: (%d, %d) cost: %d\n", edge_count++, a, b, min);
mincost += min;
}
}

printf("\nMinimum cost = %d\n", mincost);


free(parent);
}

int main() {
int edges;

printf("Enter the number of vertices: ");


scanf("%d", &V);

// Dynamically allocate the adjacency matrix


int **cost = (int **)malloc(V * sizeof(int *));
for (int i = 0; i < V; i++)
cost[i] = (int *)malloc(V * sizeof(int));

// Initialize the matrix with INT_MAX


for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
cost[i][j] = INT_MAX;

printf("Enter the number of edges: ");


scanf("%d", &edges);

printf("Enter the edges (u, v, weight):\n");


for (int i = 0; i < edges; i++) {
int u, v, weight;
scanf("%d %d %d", &u, &v, &weight);
cost[u][v] = weight;
cost[v][u] = weight; // Since the graph is undirected
}

kruskalMST(cost);

// Free the dynamically allocated matrix


for (int i = 0; i < V; i++)
free(cost[i]);
free(cost);

return 0;
}
OUTPUT:
Dijkstra’s Algorithm C Code

#include <stdio.h>
#include <limits.h>

#define V 8 // Number of vertices in the graph

int minDistance(int dist[], int visited[]) {


int min = INT_MAX, min_index;

for (int v = 0; v < V; v++)


if (visited[v] == 0 && dist[v] <= min) {
min = dist[v];
min_index = v;
}

return min_index;
}

void dijkstra(int graph[V][V], int src) {


int dist[V];
int visited[V];
int parent[V];

for (int i = 0; i < V; i++) {


dist[i] = INT_MAX;
visited[i] = 0;
parent[i] = -1;
}

dist[src] = 0;

for (int count = 0; count < V - 1; count++) {


int u = minDistance(dist, visited);

visited[u] = 1;
for (int v = 0; v < V; v++) {
if (!visited[v] && graph[u][v] && dist[u] != INT_MAX &&
dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
parent[v] = u;
}
}
}

printf("Vertex\t Distance from Source\tPath");


for (int i = 0; i < V; i++) {
printf("\n%d -> %d\t\t %d\t\t", src, i, dist[i]);
int j = i;
while (parent[j] != -1) {
printf("%d <- ", j);
j = parent[j];
}
printf("%d", src);
}
}

int main() {
int graph[V][V] = {
{0, 2, 0, 0, 0, 0, 6, 0}, // A
{2, 0, 7, 0, 2, 0, 0, 0}, // B
{0, 7, 0, 3, 0, 3, 0, 0}, // C
{0, 0, 3, 0, 0, 0, 0, 2}, // D
{0, 2, 0, 0, 0, 2, 1, 0}, // E
{0, 0, 3, 0, 2, 0, 0, 2}, // F
{6, 0, 0, 0, 1, 0, 0, 4}, // G
{0, 0, 0, 2, 0, 2, 4, 0} // H
};

int src = 0;
dijkstra(graph, src);

return 0;
}

OUTPUT:

You might also like