-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortest.c
109 lines (95 loc) · 2.13 KB
/
shortest.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
#include <stdio.h>
#include <stdlib.h>
struct node {
int visited;
int label;
int cost; // cost from parent node to this node
struct node *next;
};
struct node vertices[10];
void graphify(int from, int to, int cost) {
struct node *vertex = (struct node *)malloc(sizeof(struct node));
struct node *temp = vertices[from].next;
if(vertices[from].label == -1) { // this is a new vertex
vertices[from].label = from;
vertices[from].visited = 0;
vertices[from].cost = 0;
}
vertex->visited = 0; // this node is not yet visited
vertex->label = to;
vertex->cost = cost;
// enqueue the 'to' node
while(temp != NULL) {
if(temp->next == NULL)
break;
else temp = temp->next;
}
if(temp == NULL)
vertices[from].next = vertex;
else temp->next = vertex;
vertex->next = NULL;
}
void printAdjencyList() {
int i = 0;
struct node *temp;
for(i = 0; i < 10; i++) {
printf("%d", vertices[i].label);
if(vertices[i].next != NULL) {
temp = vertices[i].next;
while(temp != NULL) {
printf("->(%d, %d)", temp->label, temp->cost);
temp = temp->next;
}
}
printf("\n");
}
}
void shortest(int start) {
struct node *temp;
int currCost, currVertex;
printf("%d ", start);
if(vertices[start].label != -1) {
temp = vertices[start].next;
if(temp != NULL) {
currCost = temp->cost;
currVertex = temp->label;
}
if(vertices[currVertex].next == NULL) { // this is a peek operation :?
temp = temp->next;
if(temp != NULL) {
currCost = temp->cost;
currVertex = temp->label;
}
}
while(temp != NULL) {
if(temp->next != NULL) {
if(currCost > temp->next->cost) {
currCost = temp->next->cost;
currVertex = temp->next->label;
}
}
temp = temp->next;
}
shortest(currVertex);
}
}
int main() {
int i = 0;
for(i = 0; i < 10; i++) {
vertices[i].visited = 0;
vertices[i].label = -1;
vertices[i].next = NULL;
}
graphify(0, 1, 3);
graphify(0, 2, 10);
graphify(2, 4, 1);
graphify(1, 3, 10);
graphify(1, 4, 6);
graphify(3, 4, 10);
graphify(3, 5, 5);
graphify(4, 5, 2);
shortest(0);
printf("\n");
printf("Adjacency list with cost\n");
printAdjencyList();
}