Ada
Ada
lab4 topologicl
#include <stdio.h>const int MAX = 10;void fnTopological(int a[MAX][MAX], int n);int main(void){
int a[MAX][MAX],n; int i,j;
printf("Topological Sorting Algorithm -\n");
printf("\nEnter the number of vertices : ");
scanf("%d",&n); printf("Enter the adjacency matrix -\n");
for (i=0; i<n; i++)
for (j=0; j<n; j++)scanf("%d",&a[i][j]);
fnTopological(a,n);printf("\n");return 0;}
void fnTopological(int a[MAX][MAX], int n) { int in[MAX], out[MAX], stack[MAX], top=-1;
int i,j,k=0; for (i=0;i<n;i++){ in[i] = 0;for (j=0; j<n; j++) if (a[j][i] == 1)
in[i]++; } while(1) {for (i=0;i<n;i++) { if (in[i] == 0) {stack[++top] = i; in[i] = -1; }}
if (top == -1) break; out[k] = stack[top--];for (i=0;i<n;i++) { if (a[out[k]][i] == 1) in[i]--; }
k++; }printf("Topological Sorting (JOB SEQUENCE) is:- \n"); for (i=0;i<k;i++)
printf("%d ",out[i] + 1);}
lab5 floyd algo
#include<stdio.h>#include<conio.h>#define MAX 10
int D[MAX][MAX];int Min(int a, int b) { if(a<b) return a; else return b;}
void Floyd(int W[MAX][MAX], int n){ int i,j,k; for(i=1;i<=n;i++) for(j=1;j<=n;j++)
D[i][j]=W[i][j]; for(k=1;k<=n;k++) for(i=1;i<=n;i++) for(j=1;j<=n;j++)
D[i][j]=Min(D[i][j], D[i][k]+D[k][j]); } void main() {
int W[MAX][MAX], n, i, j; printf("Enter the number of vertices in a graph\n");
scanf("%d",&n);printf("enter the weight matrix of graph with no negative length cycle\n");
printf("enter 1000 For(i,j) If No Such edge And 0 For(i,i)\n"); for(i=1;i<=n;i++)
for(j=1;j<=n;j++) scanf("%d",&W[i][j]); Floyd(W,n);
printf("the all-pairs-shortest-path matrix of a graph\n"); for(i=1;i<=n;i++) {
for(j=1;j<=n;j++){ printf("%d\t",D[i][j]); } printf("\n"); } getch();}
lab 6 warshalles
#include<stdio.h>#include<conio.h>#define MAX 10
int R[MAX][MAX]; void Warshall(int A[MAX][MAX], int n) { int i,j,k; for(i=1;i<=n;i++)
for(j=1;j<=n;j++) R[i][j]=A[i][j]; for(k=1;k<=n;k++) for(i=1;i<=n;i++)
for(j=1;j<=n;j++) R[i][j]=R[i][j]||(R[i][k]&&R[k][j]); }
void main() { int A[MAX][MAX], n, i, j;
printf("enter the number of vertices in a digraph\n"); scanf("%d",&n);
printf("enter the adjacent matrix of a digraph\n"); for(i=1;i<=n;i++) for(j=1;j<=n;j++)
scanf("%d",&A[i][j]); Warshall(A, n);
printf("the transitive closure or path matrix of a digraph\n");
for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("%d\t",R[i][j]); } printf("\n"); }
getch(); }