Analysis of Algorithm Computer Graphics Data Structures

All Pairs Shortest Path Using C programming


Given a directed, connected weighted graph G(V,E), for each edge u,vE, a weight w(u,v) is associated with the edge. The all pairs of shortest paths problem (APSP) is to find a shortest path from u to v for every pair of vertices u and v in .
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<conio.h>
#include<stdio.h>
int n,i,j,k,cost[10][10];
void apsp(int[10][10]);
int min(int,int);

void main()
{

// here we accept the cost matrix:
 printf("Enter the no of vertices: ");
 scanf("%d",&n);
 printf("Enter the cost adjacency matrix: ");
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
  {
   scanf("%d",&cost[i][j]);
  }
 }


  apsp(cost);
 getch();
}

void apsp(int cost[10][10])
{

/*since we are finding various versions of the same adjacency matrix, we use a 3D matrix. The first dimension can be considered as the version no. of the matrix*/
 int A[5][10][10];


  for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
  {
   A[0][i][j] = cost[i][j];  // initializing the 0th version of A to the cost matrix
  }
 }
 
 for(k=1;k<=n;k++)
 {
  for(i=1;i<=n;i++)
  {
   for(j=1;j<=n;j++)
   {
    A[k][i][j] = min(A[k-1][i][j],(A[k-1][i][k]+A[k-1][k][j]));
// the standard formula Ak(i,j) =   min(Ak-1(i,j),(Ak-1(i,k)+Ak-1(k,j)))
   }
  }
 }

 for(k=0;k<=n;k++)
 {
  printf("A(%d):\n",k);
  for(i=1;i<=n;i++)
  {
   for(j=1;j<=n;j++)
   {
    printf("%d\t",A[k][i][j]); // printing all versions of the matrix A
   }
   printf("\n");
  }
 }
}
int min(int a,int b)
{
 if(a<b)
  return a;
 else return b;
}






Comments

  1. Sahi hai bhai baki bhi daal na plzzz aur thak gaya rahega to anime ka info le le yaha par se www.sarvanime.com

    ReplyDelete
  2. The best slot machines in Vegas - DrmCD
    The best slot machines 과천 출장안마 in Vegas. I was a smoker 경기도 출장안마 for over two years and never quit. The slot machines are everywhere. 세종특별자치 출장마사지 It's  Rating: 전라남도 출장마사지 4.4 · 김제 출장마사지 ‎9 votes

    ReplyDelete

Post a Comment

Popular posts from this blog

Cohen Sutherland Line Clipping Algorithm In C

Graph Coloring using Backtracking in C

Longest Common Subsequence using Dynamic Programming in C