Task 8 and 9
Task 8 and 9
Aim:
Create a C program to find Minimum Cost Spanning Tree of a given undirected graph using
Kruskal’s algorithm
Algorithm:
Step 1 : Create a graph where all edges are connected.
Step 2 : Sort all the edges from low weight to high
Step 3: Take the edge with the lowest weight and add it to the spanning tree
Step 4 : If adding the edge created the cycle then reject that edge
Step 4 : Keep adding the edges until we reach all vertices
Program:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int i, j, k, a, b, u, v, n, ne = 1;
int min, mincost = 0, cost[9][9], parent[9];
int find(int);
int uni(int, int);
void main() {
printf("\n\tImplementation of Kruskal's Algorithm\n");
printf("\nEnter the no. of vertices:");
scanf("%d", & n);
printf("\nEnter the cost adjacency matrix:\n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", & cost[i][j]);
if (cost[i][j] == 0)
cost[i][j] = 999;
}
}
printf("The edges of Minimum Cost Spanning Tree are\n");
while (ne < n) {
for (i = 1, min = 999; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (cost[i][j] < min) {
min = cost[i][j];
a = u = i;
b = v = j;
}
}
}
u = find(u);
v = find(v);
if (uni(u, v)) {
printf("%d edge (%d,%d) =%d\n", ne++, a, b, min);
mincost += min;
}
cost[a][b] = cost[b][a] = 999;
}
printf("\n\tMinimum cost = %d\n", mincost);
getch();
}
int find(int i) {
while (parent[i])
i = parent[i];
return i;
}
int uni(int i, int j) {
if (i != j) {
parent[j] = i;
return 1;
}
return 0;
}
OUTPUT:
Enter the no. of vertices:3
Enter the cost adjacency matrix:
9
8
7
6
5
4
3
2
3
The edges of Minimum Cost Spanning Tree are
1 edge (3,2) =2
2 edge (3,1) =3
Minimum cost = 5
TestCase 1: Assume an input graph with more than one edge having a same weight, in such case
which edge should be selected as the next visiting vertex by applying Kruskal’s algorithm.
Algorithm:
Step 1 : Create a graph where all edges are connected.
Step 2 : Sort all the edges from low weight to high
Step 3: Take the edge with the lowest weight and add it to the spanning tree
Step 4 : If adding the edge created the cycle then reject that edge
Step 4 : Keep adding the edges until we reach all vertices
Program:
#include <stdio.h>
#define MAX 30
typedef struct edge {
int u, v, w;
} edge;
typedef struct edge_list {
edge data[MAX];
int n;
} edge_list;
edge_list elist;
int Graph[MAX][MAX], n;
edge_list spanlist;
void kruskalAlgo();
int find(int belongs[], int vertexno);
void applyUnion(int belongs[], int c1, int c2);
void sort();
void print();
// Applying Krushkal Algo
void kruskalAlgo() {
int belongs[MAX], i, j, cno1, cno2;
elist.n = 0;
for (i = 1; i < n; i++)
for (j = 0; j < i; j++) {
if (Graph[i][j] != 0) {
elist.data[elist.n].u = i;
elist.data[elist.n].v = j;
elist.data[elist.n].w = Graph[i][j];
elist.n++;
}
}
sort();
for (i = 0; i < n; i++)
belongs[i] = i;
spanlist.n = 0;
for (i = 0; i < elist.n; i++) {
cno1 = find(belongs, elist.data[i].u);
cno2 = find(belongs, elist.data[i].v);
if (cno1 != cno2) {
spanlist.data[spanlist.n] = elist.data[i];
spanlist.n = spanlist.n + 1;
applyUnion(belongs, cno1, cno2);
}
}
}
int find(int belongs[], int vertexno) {
return (belongs[vertexno]);
}
void applyUnion(int belongs[], int c1, int c2) {
int i;
for (i = 0; i < n; i++)
if (belongs[i] == c2)
belongs[i] = c1;
}
// Sorting algo
void sort() {
int i, j;
edge temp;
for (i = 1; i < elist.n; i++)
for (j = 0; j < elist.n - 1; j++)
if (elist.data[j].w > elist.data[j + 1].w) {
temp = elist.data[j];
elist.data[j] = elist.data[j + 1];
elist.data[j + 1] = temp;
}
}
// Printing the result
void print() {
int i, cost = 0;
for (i = 0; i < spanlist.n; i++) {
printf("\n%d - %d : %d", spanlist.data[i].u, spanlist.data[i].v, spanlist.data[i].w);
cost = cost + spanlist.data[i].w;
}
printf("\nSpanning tree cost: %d", cost);
}
int main() {
int i, j, total_cost;
n = 6;
Graph[0][0] = 0;
Graph[0][1] = 4;
Graph[0][2] = 4;
Graph[0][3] = 0;
Graph[0][4] = 0;
Graph[0][5] = 0;
Graph[0][6] = 0;
Graph[1][0] = 4;
Graph[1][1] = 0;
Graph[1][2] = 2;
Graph[1][3] = 0;
Graph[1][4] = 0;
Graph[1][5] = 0;
Graph[1][6] = 0;
Graph[2][0] = 4;
Graph[2][1] = 2;
Graph[2][2] = 0;
Graph[2][3] = 3;
Graph[2][4] = 4;
Graph[2][5] = 0;
Graph[2][6] = 0;
Graph[3][0] = 0;
Graph[3][1] = 0;
Graph[3][2] = 3;
Graph[3][3] = 0;
Graph[3][4] = 3;
Graph[3][5] = 0;
Graph[3][6] = 0;
Graph[4][0] = 0;
Graph[4][1] = 0;
Graph[4][2] = 4;
Graph[4][3] = 3;
Graph[4][4] = 0;
Graph[4][5] = 0;
Graph[4][6] = 0;
Graph[5][0] = 0;
Graph[5][1] = 0;
Graph[5][2] = 2;
Graph[5][3] = 0;
Graph[5][4] = 3;
Graph[5][5] = 0;
Graph[5][6] = 0;
kruskalAlgo();
print();
}
Output:
Solution 1:
2-1:2
5-2:2
3-2:3
4-3:3
1-0:4
Spanning tree cost: 14
Test Case 2: Construct a Maximum Spanning tree for the input graph by applying Kruskal’s
algorithm.
Algorithm:
Algorithm:
Step 1 : Create a graph where all edges are connected.
Step 2 : Sort all the edges from high weight to low
Step 3: Take the edge with the highestt weight and add it to the spanning tree
Step 4 : If adding the edge created the cycle then reject that edge
Step 4 : Keep adding the edges until we reach all vertices
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int u, v, w;
} Edge;
int n, m;
Edge edges[MAX_EDGES];
int parent[MAX_NODES];
int find(int x) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x]);
}
void kruskal() {
int i, u, v, cost = 0;
int main() {
int i;
kruskal();
return 0;
}
OUTPUT:
Enter the number of nodes and edges: 3 3
Enter the edges (u v w):
125
238
137
The maximum spanning tree has a cost of 15.
Aim:
Create to c program to find shortest path using dijkstra’s algorithm
Algorithm:
Step 1 : Create a set shortPath to store vertices that come in the way of the shortest path tree.
Step 2 : Initialize all distance values as INFINITE and assign distance values as 0 for source
vertex so that it is picked first.
Step 3 : Loop until all vertices of the graph are in the shortPath.
Step 4 : Take a new vertex that is not visited and is nearest.
Step 5 : Add this vertex to shortPath.
Step 6 : For all adjacent vertices of this vertex update distances. Now check every adjacent
vertex of V, if sum of distance of u and weight of edge is less the update it
Program:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
#include<math.h>
#define IN 99
#define N 6
int dijkstra(int cost[][N], int source, int target);
int main()
{
int cost[N][N],i,j,w,ch,co;
int source, target,x,y;
printf("\t The Shortest Path Algorithm ( DIJKSTRA'S ALGORITHM in C \n\n");
for(i=1;i< N;i++)
for(j=1;j< N;j++)
cost[i][j] = IN;
for(x=1;x< N;x++)
{
for(y=x+1;y< N;y++)
{
printf("Enter the weight of the path between nodes %d and %d: ",x,y);
scanf("%d",&w);
if(w<0)
{
printf("Dijkstra’s Algorithm cannot work for a weighted connected graph
with negative weights.");
exit(0);
}
else
cost [x][y] = cost[y][x] = w;
}
printf("\n");
}
printf("\nEnter the source:");
scanf("%d", &source);
printf("\nEnter the target");
scanf("%d", &target);
co = dijsktra(cost,source,target);
printf("\nThe Shortest Path: %d",co);
}
int dijsktra(int cost[][N],int source,int target)
{
int dist[N],prev[N],selected[N]={0},i,m,min,start,d,j;
char path[N];
for(i=1;i< N;i++)
{
dist[i] = IN;
prev[i] = -1;
}
start = source;
selected[start]=1;
dist[start] = 0;
while(selected[target] ==0)
{
min = IN;
m = 0;
for(i=1;i< N;i++)
{
d = dist[start] +cost[start][i];
if(d< dist[i]&&selected[i]==0)
{
dist[i] = d;
prev[i] = start;
}
if(min>dist[i] && selected[i]==0)
{
min = dist[i];
m = i;
}
}
start = m;
selected[start] = 1;
}
start = target;
j = 0;
while(start != -1)
{
path[j++] = start+65;
start = prev[start];
}
path[j]='\0';
strrev(path);
printf("%s", path);
return dist[target];
}
Sample Input/output:
Test case 1:
Enter no. of vertices:4
Distance of node1=1
Path=1<-0
Distance of node2=2
Path=2<-0
Distance of node3=5
Path=3<-2<-0
Testcase 2:
Enter no. of vertices:3
Dijkstra’s Algorithm cannot work for a weighted connected graph with negative weights
Result:
Thus the dijikstra’s algorithm was executed successfully.
Aim:
Implement a c program for topological ordering
Algorithm:
Step 1:Store each vertex’s In-Degree in an array D
Step 2. Initialize queue with all “in-degree=0” vertices
Step 3. While there are vertices remaining in the queue:
(a) Dequeue and output a vertex
(b) Reduce In-Degree of all vertices adjacent to it by 1
(c) Enqueue any of these vertices whose In-Degree became zero
Step 4. If all vertices are output then success, otherwise there is a cycle.
Program:
#include<stdio.h>
#include<stdlib.h>
int n;
int adj[MAX][MAX];
void create_graph();
int main()
{
int i,v,count,topo_order[MAX],indeg[MAX];
create_graph();
for(i=0;i<n;i++)
{
indeg[i] = indegree(i);
if( indeg[i] == 0 )
insert_queue(i);
}
count = 0;
return 0;
}
int indegree(int v)
{
int i,in_deg = 0;
for(i=0; i<n; i++)
if(adj[i][v] == 1)
in_deg++;
return in_deg;
}
void create_graph()
{
int i,max_edges,origin,destin;
Sample Input/Output:
Testcase 1:
Result: