-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdijkstra.c
154 lines (140 loc) · 2.65 KB
/
dijkstra.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
153
154
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int v;
int w;
struct node* next;
}nd;
typedef struct adjacent{
int c;
int w;
int p;
int i;
}adj;
void heapify(adj **a,int* sup,int i,int n){
int lc = 2*i + 1;
int rc = 2*i + 2;
int small = i;
if (lc<n && (a[sup[lc]]->w)<(a[sup[small]]->w))
small = lc;
if (rc<n && (a[sup[rc]]->w)<(a[sup[small]]->w))
small = rc;
if (small!=i){
int temp = sup[small];
sup[small] = sup[i];
sup[i] = temp;
a[sup[i]]->i = i;
a[sup[small]]->i = small;
heapify(a,sup,small,n);
}
}
void buildHeap(adj **a,int* sup,int n){
int i = (n/2)-1;
while(i >= 0){
heapify(a,sup,i,n);
i--;
}
}
int extract(adj** a,int *s,int *n){
int r = s[0];
a[s[0]]->c = 1;
s[0] = s[(*n)-1];
s[(*n)-1] = r;
(*n)--;
heapify(a,s,0,*n);
return r;
}
void dec_prio(adj** a,int* s,int t){
if(t>0){
int n = (t-1)/2;
if (a[s[n]]->w > a[s[t]]->w){
int temp = s[t];
s[t] = s[n];
s[n] = temp;
a[s[t]]->i = t;
a[s[n]]->i = n;
dec_prio(a,s,n);
}
}
}
void dijkstra(nd** a,adj** s,int* sup,int n){
buildHeap(s,sup,n);
int t = n;
int hts = n;
while(t--){
int u = extract(s,sup,&hts);
nd* p = a[u];
while(p){
if ((s[p->v]->c == 0) && ((p->w)+s[u]->w < s[p->v]->w)){
s[p->v]->w = s[u]->w + p->w;
s[p->v]->p = u;
dec_prio(s,sup,s[p->v]->i);
}
p=p->next;
}
}
}
void path(adj **s, int n, int de){
if (de >= 0 ){
path(s,n,s[de]->p);
printf("%d ",de);
}
}
int main(){
int n,i,c;
printf("Enter number of nodes:");
scanf("%d",&n);
nd** a = (nd **)malloc(n*sizeof(nd *));
for (i = 0;i<n;i++){
a[i] = NULL;
while(1){
printf ("Does node %d has anymore adjacent nodes?(0/1):",i);
scanf("%d",&c);
if (c==0){
break;
} else {
nd *p = (nd *)malloc(sizeof(nd));
p->next = a[i];
printf ("Enter the node and weight:");
scanf("%d",&p->v);
scanf("%d",&p->w);
a[i] = p;
}
}
}
for (i = 0;i<n;i++){
nd *p = a[i];
printf ("%d ",i);
while(p){
printf ("->");
printf("|%d|",p->v);
printf("%d|",p->w);
p = p->next;
}
printf("\n");
}
adj** s = (adj **)malloc(n*sizeof(adj *));
int* sup = (int *)malloc(n*sizeof(int));
for(i = 0;i<n;i++){
nd *p = a[i];
s[i] = (adj *)malloc(sizeof(adj));
s[i]->c = 0;
s[i]->w = 9999;
s[i]->p = -1;
s[i]->i = i;
sup[i] = i;
}
printf("Enter the source and destination: \n");
int so,de;
scanf("%d %d",&so,&de);
s[so]->w = 0;
//ks(s,n,t);
dijkstra(a,s,sup,n);
printf("Cost : %d\n",s[de]->w);
printf("Path: ");
path(s,n,de);
printf("\n");
//for (i=0;i<n;i++){if (i) printf(","); printf("|%d|%d|%d|%d| ",s[i]->c,s[i]->w,s[i]->p,s[i]->i ); }
//for (i=0;i<n;i++) printf("%d ,",sup[i]);
return 0;
}