forked from rishabhgarg25699/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
floyd.c
152 lines (142 loc) · 3.11 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct node{
int vertex;
int weight;
struct node *next;
};
struct Graph{
int numVertices;
struct node **adjlist;
};
struct Graph *creategraph(int v){
struct Graph *graph;
graph=(struct Graph *)malloc(v*sizeof(struct Graph));
graph->numVertices=v;
graph->adjlist=(struct node **)malloc(v*sizeof(struct node *));
int i;
for(i=0;i<v;i++){
graph->adjlist[i]=NULL;
}
return graph;
}
struct node *createnode(int dest,int weight){
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->vertex=dest;
newnode->weight=weight;
newnode->next=NULL;
return newnode;
}
void addedge(struct Graph *graph,int src,int dst,int weight){
struct node *new=createnode(dst,weight);
new->next = graph->adjlist[src];
graph->adjlist[src] = new;
}
void printGraph(struct Graph* graph)
{
int v;
for (v = 0; v < graph->numVertices; v++)
{
struct node* temp = graph->adjlist[v];
printf("\n Adjacency list of vertex %d\n ", v);
while (temp)
{
printf(" -> %d(%d)", temp->vertex,temp->weight);
temp = temp->next;
}
printf("\n");
}
}
void printmatrix(int **a,int n){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%5d%c",(a[i][j]==INT_MAX)?-1111:a[i][j],(j==n-1)?'\n':' ');
}
}
}
void printpath(int **p,int u,int v){
printf("->%d",p[u][v]);
if(p[u][v]==v) return;
printpath(p,p[u][v],v);
}
void pathprinting(int **p,int n){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i!=j && p[i][j]!=-1){
printf("Shortest path from %d to %d: ",i,j);
printf("%d",i);
printpath(p,i,j);
printf("\n");
}
}
}
}
void shortestpath(int **a,int **p,int n){
int i,j,k;
for(k=0;k<n;k++){
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(a[i][k]!=INT_MAX && a[k][j]!=INT_MAX && a[i][k]+a[k][j]<a[i][j]){
a[i][j]=a[i][k]+a[k][j];
p[i][j]=p[i][k];
}
}
}
}
printf("\nThe distance matrix is:\n");
printmatrix(a,n);
}
int main(){
int v,e,i,j,src,dst,weight;
printf("Enter no.of vertices: ");
scanf(" %d",&v);
struct Graph *graph=creategraph(v);
printf("Enter no.of edges: ");
scanf(" %d",&e);
for(i=0;i<e;i++){
printf("Enter source,dest,weight: ");
scanf(" %d%d%d",&src,&dst,&weight);
addedge(graph,src,dst,weight);
}
int **a,**n;
a=(int **)malloc(v*sizeof(int *));
for(i=0;i<v;i++){
a[i]=(int *)malloc(v*sizeof(int));
}
n=(int **)malloc(v*sizeof(int *));
for(i=0;i<v;i++){
n[i]=(int *)malloc(v*sizeof(int));
}
for(i=0;i<v;i++){
for(j=0;j<v;j++){
if(i==j){
a[i][j]=0;
}
else a[i][j]=INT_MAX;
}
}
for(i=0;i<graph->numVertices;i++){
struct node *temp=graph->adjlist[i];
while(temp){
a[i][temp->vertex]=temp->weight;
temp=temp->next;
}
}
for(i=0;i<v;i++){
for(j=0;j<v;j++){
if(a[i][j]==INT_MAX || a[i][j]==0){
n[i][j]=-1;
}
else{
n[i][j]=j;
}
}
}
shortestpath(a,n,v);
printf("\n");
pathprinting(n,v);
}