-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbc_floyd.c
69 lines (65 loc) · 2.13 KB
/
bc_floyd.c
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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"header.h"
#include"ini_mat.h"
/*below floyd wrshall bc code uses distance matrix and graph to compute
bc values. First it will form distance matrix from modified .txt file
and then uses floyd warshall algorithm to compute bc values.*/
float* bc_flyd_wrshl(struct graph* graph,char* filename,bool connected)
{
int ver,**adj;
float *bc,**sum;
ver = graph->no_vertices;
sum = sum_mat(ver); //will store number of shortest paths among vertices
adj = dist_mat(graph,filename,1); //will store minimum path distance between vertices
bc = (float*)calloc(ver,sizeof(float));
double time_spent =0.0;
clock_t begin = clock();
//Floyd Warshall Algorithm...
for(int k=0;k<ver;k++)
{
for(int i=0;i<ver;i++)
{
for(int j=0;j<ver;j++)
{
if(i!=j && j!=k && k!=i)
{
if(adj[i][j]>(adj[i][k]+adj[k][j]))
{
adj[i][j]=(adj[i][k]+adj[k][j]);
sum[i][j]=sum[i][k]*sum[k][j];
}
else if(adj[i][j]==(adj[i][k]+adj[k][j]))
{
sum[i][j] = sum[i][j] + sum[i][k]*sum[k][j];
}
}
}
}
}
//BC Computation...
for(int k=0;k<ver;k++)
{
for(int i=0;i<ver;i++)
{
for(int j=i+1;j<ver;j++)
{
if(i!=j && j!=k && k!=i)
{
if(adj[i][j]==(adj[i][k]+adj[k][j]))
{
bc[k] = bc[k] + (sum[i][k]*sum[k][j])/(sum[i][j]);
}
}
}
}
}
clock_t end = clock();
time_spent = (double)(end-begin) / CLOCKS_PER_SEC;
free(sum);
free(adj);
printf("\n\nTime spent by Floyd Warshall : %lf\n",time_spent);
printf("\nBetweenesss Centrality Using Floyd Warshall --> \n");
return bc;
}