Rp3

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Rp3 as PDF for free.

More details

  • Words: 241
  • Pages: 4
Practical Assignment #3 Solution 1) #include<stdio.h> #include<stdlib.h> void main() { int graph[15][15],s[15],pathestimate[15],mark[15]; int num_of_vertices,source,i,j,u,predecessor[15]; int count=0; int minimum(int a[],int m[],int k); void printpath(int,int,int[]); printf("\nenter the no.of vertices\n"); scanf("%d",&num_of_vertices); if(num_of_vertices<=0) { printf("\nthis is meaningless\n"); exit(1); } printf("\nenter the adjacent matrix\n"); for(i=1;i<=num_of_vertices;i++) { printf("\nenter the elements of row %d\n",i); for(j=1;j<=num_of_vertices;j++) { scanf("%d",&graph[i][j]); } } printf("\nenter the source vertex\n"); scanf("%d",&source); for(j=1;j<=num_of_vertices;j++) { mark[j]=0; pathestimate[j]=999; predecessor[j]=0; } pathestimate[source]=0; while(count0) {

if(mark[i]!=1) { if(pathestimate[i]>pathestimate[u]+graph[u][i]) { pathestimate[i]=pathestimate[u]+graph[u][i]; predecessor[i]=u; } } } } } for(i=1;i<=num_of_vertices;i++) { printpath(source,i,predecessor); if(pathestimate[i]!=999) printf("->(%d)\n",pathestimate[i]); } } int minimum(int a[],int m[],int k) { int mi=999; int i,t; for(i=1;i<=k;i++) { if(m[i]!=1) { if(mi>=a[i]) { mi=a[i]; t=i; } }} return t; } void printpath(int x,int i,int p[]) { printf("\n"); if(i==x) { printf("%d",x); } else if(p[i]==0) printf("no path from %d to %d",x,i); else { printpath(x,p[i],p);printf("..%d",i);}

Solution 2) #include #define MAX 20 main() { int i,j,k,n; int w_adj[MAX][MAX],adj[MAX][MAX],path[MAX][MAX]; printf("Enter number of vertices : "); scanf("%d",&n); printf("Enter weighted adjacency matrix :\n"); for(i=0;i for(j=0;j scanf("%d",&w_adj[i][j]); printf("The weighted adjacency matrix is :\n"); display(w_adj,n); /* Make weighted adjacency matrix into boolean adjacency matrix*/ for(i=0;i for(j=0;j if(w_adj[i][j]==0) adj[i][j]=0; else adj[i][j]=1; printf("The adjacency matrix is :\n"); display(adj,n); for(i=0;i for(j=0;j path[i][j]=adj[i][j]; for(k=0;k { printf("P%d is :\n",k); display(path,n); for(i=0;i for(j=0;j path[i][j]=( path[i][j] || ( path[i][k] && path[k][j] ) ); } printf("Path matrix P%d of the given graph is :\n",k); display(path,n); }/*End of main() */ display(int matrix[MAX][MAX],int n)

{ int i,j; for(i=0;i { for(j=0;j printf("%3d",matrix[i][j]); printf("\n"); } }/*End of display()*/

Related Documents

Rp3
June 2020 0
Patches Rp3 Do Site
July 2020 2