Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Algorithm Lab 4th Sem

NotificationsYou must be signed in to change notification settings

githubhosting/DAA-Lab

Repository files navigation


Algorithms Code in C

Prims
#include<stdio.h>#include<stdlib.h>#defineinfinity 9999#defineMAX 20intG[MAX][MAX],spanning[MAX][MAX],n;intprims();intmain(){inti,j,total_cost;printf("Enter no. of vertices:");scanf("%d",&n);printf("\nEnter the adjacency matrix:\n");for (i=0;i<n;i++)for (j=0;j<n;j++)scanf("%d",&G[i][j]);total_cost=prims();printf("\nspanning tree matrix:\n");for (i=0;i<n;i++)    {printf("\n");for (j=0;j<n;j++)printf("%d\t",spanning[i][j]);    }printf("\n\nTotal cost of spanning tree = %d",total_cost);return0;}intprims(){intcost[MAX][MAX];intu,v,min_distance,distance[MAX],from[MAX];intvisited[MAX],no_of_edges,i,min_cost,j;// create cost[][] matrix,spanning[][]for (i=0;i<n;i++)for (j=0;j<n;j++)        {if (G[i][j]==0)cost[i][j]=infinity;elsecost[i][j]=G[i][j];spanning[i][j]=0;        }// initialise visited[],distance[] and from[]distance[0]=0;visited[0]=1;for (i=1;i<n;i++)    {distance[i]=cost[0][i];from[i]=0;visited[i]=0;    }min_cost=0;// cost of spanning treeno_of_edges=n-1;// no. of edges to be addedwhile (no_of_edges>0)    {// find the vertex at minimum distance from the treemin_distance=infinity;for (i=1;i<n;i++)if (visited[i]==0&&distance[i]<min_distance)            {v=i;min_distance=distance[i];            }u=from[v];// insert the edge in spanning treespanning[u][v]=distance[v];spanning[v][u]=distance[v];no_of_edges--;visited[v]=1;// updated the distance[] arrayfor (i=1;i<n;i++)if (visited[i]==0&&cost[i][v]<distance[i])            {distance[i]=cost[i][v];from[i]=v;            }min_cost=min_cost+cost[u][v];    }return (min_cost);}
Krushkal
#include<stdio.h>#include<stdlib.h>inti,j,k,a,b,u,v,n,ne=1;intmin,mincost=0,cost[9][9],parent[9];intfind(int);intuni(int,int);voidmain(){printf("Kruskal's algorithm in C\n");printf("========================\n");printf("Enter 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("\nMinimum cost = %d\n",mincost);}intfind(inti){while (parent[i])i=parent[i];returni;}intuni(inti,intj){if (i!=j)    {parent[j]=i;return1;    }return0;}

Prims Input/Output:

Enter no. of vertices: 5Enter the adjacency matrix:0 1 2 0 01 0 2 0 42 2 0 3 00 0 3 0 20 4 0 2 0spanning tree matrix:0120010000200300030200020Total cost of spanning tree = 8

Krushkal Input/Output:

Example 1:

Kruskals algorithmin C========================Enter the no. of vertices: 5Enter the cost adjacency matrix:0 1 2 0 11 0 3 0 12 3 0 6 50 0 6 0 01 1 5 0 0The edges of Minimum Cost Spanning Tree are1 edge (1,2) = 12 edge (1,5) = 13 edge (1,3) = 24 edge (3,4) = 6Minimum cost = 10

Example 2:

Kruskals algorithmin C========================Enter the no. of vertices: 6Enter the cost adjacency matrix:0 3 1 6 0 03 0 5 0 3 01 5 0 5 6 46 0 5 0 0 20 3 6 0 0 60 0 4 2 6 0The edges of Minimum Cost Spanning Tree are1 edge (1,3) = 12 edge (4,6) = 23 edge (1,2) = 34 edge (2,5) = 35 edge (3,6) = 4Minimum cost = 13

Dijkstra Input/Output:

Enter no. of vertices: 5Enter the adjacency matrix:0 1 0 3 91 0 5 0 00 5 0 2 13 0 2 0 69 0 1 6 0Enter the starting node: 0Distance of node1= 1Path=1<-0Distance of node2= 5Path=2<-3<-0Distance of node3= 3Path=3<-0Distance of node4= 6Path=4<-2<-3<-0

Travelling Salesman Input/Output:

Enter No.of Cities: 6Enter Cost Matrix: Enter Elements of Row#: 199 10 15 20 99 8Enter Elements of Row#: 25 99 9 10 8 99Enter Elements of Row#: 36 13 99 12 99 5Enter Elements of Row#: 48 8 9 99 6 99Enter Elements of Row#: 599 10 99 6 99 99Enter Elements of Row#: 610 99 5 99 99 99The cost list is:991015209985999108996139912995889996999910996999910995999999The Path is: 1->6->3->4->5->2->1Minimum cost: 46

About

Algorithm Lab 4th Sem

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp