repl.it
@pankajyd/

Least distance in Graph

C

This code prints least distance between two vertices v1 and v2 in a weighted graph.

fork
loading
Files
  • main.c
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
Input:
First line contains number of vertices(nv) and number of edges(ne)

Next 'ne' lines contains edge between v1 and v2 and the weight of the edge.

Last line contains vertices between distance is to be found.

e.g.
5 8
0 1 4
0 2 2
1 2 3
2 1 1
1 3 2
2 4 5
4 2 5
1 4 3
0 4

number of vertices nv = 5; number of edges ne = 8;
0 1 4: edge is from v1 to v2 with weight w = 4;

last line: v1 = 0; v2 = 4;
find least distance between v1 and v2;
*/






#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

typedef struct vertex{
    int v;
    int color;
    int parent;
    int d;
    struct list *head;
}vertex;

typedef struct list{
    int v;
    int w;
    struct list* next;
}list;


typedef struct Heap{
    vertex **A;
    int size;
}Heap;

void swap(Heap *H, int i, int j){
    vertex* tmp;
    tmp = H->A[i];
    H->A[i] = H->A[j];
    H->A[j] = tmp;
}

void Heapify(Heap *H, int i){
    int min_index = i;
    
    if(2*i + 1 >= H->size){
        return;
    }
    else{
        if(H->A[i]->d > H->A[2*i + 1]->d){
            min_index = 2*i + 1;
        }
        if(2*i + 2 < H->size && H->A[2*i + 2]->d < H->A[min_index]->d){
            min_index = 2*i + 2;
        }
        if(min_index != i){
            swap(H, i, min_index);
            Heapify(H, min_index);
        }
    }
}

void Build_min_Heap(Heap *H){
    int n = H->size;
    for(int i = n/2; i >= 0; i--){
        Heapify(H, i);
    }
}

vertex* Extract_min(Heap* H){
    vertex* min = H->A[0];
    H->A[0] = H->A[H->size - 1];
    H->size--;
    Heapify(H, 0);
    return min;
}

void Decrease_key(Heap *H, int i, int key){
    H->A[i]->d = key;
    while(i >0 && H->A[i]->d < H->A[(i-1)/2]->d){
        H->A[i] = H->A[(i-1)/2];
        i = (i-1)/2;
    }
    H->A[i]->d = key;
}

void BFS(vertex* V, vertex **pv, int v1, int v2, int nv){
    //printf("\nIn BFS:\n");
	////////////////////
	V[v1].d = 0;
    V[v1].parent = -1;
    ///////////////////

	//////////////////////////////////////
    Heap* H = (Heap*)malloc(sizeof(Heap));
    H->A = pv;
    H->size = nv;
    Build_min_Heap(H);
    //////////////////////////////////////
	// printf("\nPrinting Min heap at start:\n");
	// for(int i = 0; i < nv; i++){
	// 	printf("(v = %d, d = %d)\n", pv[i]->v, pv[i]->d);
	// }
	//printf("\nHeap Size: %d.\n", H->size);
	// vertex* u = Extract_min(H);
	// printf("\nExtracted v = %d with d = %d", u->v, u->d);

    while(H->size > 0){
        vertex* u = Extract_min(H);
		//printf("\nExtracted %d with d = %d", u->v, u->d);
		u->color = 2;
        list* head = u->head;
		while(head != NULL){
			if(V[head->v].d > u->d + head->w && V[head->v].color != 2){
				V[head->v].d = u->d + head->w;
				V[head->v].parent = u->v;
			}
			head = head->next;
		}
		if(H->size > 0){
			Build_min_Heap(H);
		}
    }
}


int main(){
    int t = 1;
    
    vertex *V;
    int v1, v2, w;
    int nv, ne; //Number of vertices and edges
    list *tmp;
    
    vertex G[2500];
    vertex* pv[2500];
    V = G;

    while(t--){
        scanf("%d %d", &nv, &ne);
        
        for(int i = 0; i < nv; i++){
            V[i].v = i;
            V[i].color = 0;
            V[i].head = NULL;
            V[i].parent = -1;
            V[i].d = 2147483647;
            pv[i] = &V[i];
        }
        
        while(ne--){
            scanf("%d %d %d", &v1, &v2, &w);
            
            //Making Edge v1 to v2.
            list* ptr = (list*)malloc(sizeof(list));
            ptr->v = v2;
            ptr->w = w;

            if(V[v1].head == NULL){
                V[v1].head = ptr;
            }
            else{
                tmp = V[v1].head;
                V[v1].head = ptr;
                ptr->next = tmp;
            }
            
        }
        scanf("%d %d", &v1, &v2);


        //Remove multiline comment below to print the graph.
        
        // printf("\nPrinting G:\n");
        // for(int i = 0; i < nv; i++){
        //     list* head = pv[i]->head;
        //     printf("%d: ", i);
        //     while(head != NULL){
        //         printf("%d(w = %d), ", head->v, head->w);
        //         head = head->next;
        //     }
        //     printf("\n");
        // }
        
        // printf("V1: %d, v2: %d", v1, v2);
        //CALL BFS
		BFS(V, pv, v1, v2, nv);
		printf("%d", V[v2].d - V[v1].d);
		// int child = v2;
		// int parent = V[v2].parent;
		// printf("\n");
		// while(parent != v1){
		// 	printf("(w = %d)", V[child].d - V[parent].d);
		// 	child = parent;
		// 	parent = V[parent].parent;
		// }
		// printf("(w = %d)", V[child].d - V[parent].d);
    }
    return 0;
}