AOA Lab Assignment 4 2024UG100001
AOA Lab Assignment 4 2024UG100001
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>
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];
no_edge = 0;
selected[0] = true;
int x;
int y;
printf("Edge: W eight\n");
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>
int main() {
int V;
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");
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;
}
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;
}
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 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;
}
}
int main() {
int edges;
kruskalMST(cost);
return 0;
}
OUTPUT:
Dijkstra’s Algorithm C Code
#include <stdio.h>
#include <limits.h>
return min_index;
}
dist[src] = 0;
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;
}
}
}
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: