-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
170 lines (139 loc) · 3.83 KB
/
graph.cpp
File metadata and controls
170 lines (139 loc) · 3.83 KB
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
/*
* File: graph.cpp
* Name: Fonz Hamilton
*/
#include <iostream>
#include "graph.h"
#include "heap.h"
using namespace std;
extern VERTEX *vertice;
// print path
void printPath(int n, int source, int destination, int s, int t) {
PATH *pointPath;
PATH *pointNode;
int u, v;
if (vertice[t].color == 0) {
if (destination < 1 || destination > n || t == destination) {
printf("No %d-%d path exists.\n", s, t);
return;
}
else {
printf("No %d-%d path has been computed.\n", s, t);
return;
}
}
else if (vertice[t].color == 1) {
printf("Path not known to be shortest: <%d", s);
}
else if (vertice[t].color == 2) {
printf("Shortest path: <%d", s);
}
// push onto stack
pointNode = new PATH;
pointNode->vertex = t; // sets the node to destination vertex
pointNode->next = NULL; // sets next to NULL
pointPath = pointNode; // path = node
v = pointNode->vertex; // v = vertex of pointNode
while (vertice[v].pi) {
u = vertice[v].pi;
pointNode = new PATH;
pointNode->vertex = u;
pointNode->next = pointPath;
pointPath = pointNode;
v = pointNode->vertex;
}
pointNode = pointPath;
pointPath = pointPath->next;
delete pointNode;
while (pointPath) {
pointNode = pointPath;
printf(", %d", pointNode->vertex);
pointPath = pointPath->next;
delete pointNode;
}
delete pointPath;
printf(">\n");
printf("The path weight is: %12.4f\n", vertice[t].dist);
}
// dijkstras shortest path algorithm
int dijkstra(int n, pNODE *adjacencyList, int s, int t, int flag) {
pNODE node; // struct with v, w, next
HEAP *heap; // heap just heaps
ELEMENT *element; // struct with int vertex, float key
int u, v; // u for current, v for next
float w;
int pos;
int heapifyCount;
vertice = new VERTEX[n+1];
for (int i = 1; i <= n; i++) {
vertice[i].color = 0; // 0 is used for white
vertice[i].pi = 0; // 0 is used for nil
}
vertice[s].dist = 0; // current distance from source
vertice[s].color = 1; // 1 is used for grey
heap = heapInit(n); // builds a heap with n nodes
element = new ELEMENT;
element->vertex = s;
element->key = 0;
insert(heap, element); // adds the source node to the priority queue
if (flag == 1) {
printf("Insert vertex %d, key=%12.4f\n", element->vertex, element->key);
}
while (heap->size != 0) {
element = deleteMin(heap, &flag); // extracts the min vertex value from heap and sets it to u
if (flag == 1) {
printf("Delete vertex %d, key=%12.4f\n", element->vertex, element->key);
}
u = element->vertex;
vertice[u].color = 2; // sets the color to black
delete element;
if (u == t) {
if (heap != NULL) {
for (int i = 0; i < heap->size; i++) {
delete heap->elements[i];
}
}
delete[] heap;
if (node != NULL) {
delete node;
}
return 0;
}
node = adjacencyList[u];
while (node != NULL) {
v = node->v;
w = node->w;
if (vertice[v].color == 0) {
vertice[v].dist = vertice[u].dist + w;
vertice[v].pi = u;
vertice[v].color = 1;
element = new ELEMENT;
element->vertex = node->v;
element->key = vertice[v].dist;
if (flag == 1) {
printf("Insert vertex %d, key=%12.4f\n", element->vertex, element->key);
}
insert(heap, element);
}
else if (vertice[v].dist > vertice[u].dist + w) {
float test = vertice[v].dist;
vertice[v].dist = vertice[u].dist + w;
vertice[v].pi = u;
if (flag == 1) {
printf("Decrease key of vertex %d, from %12.4f to %12.4f\n", v, test, vertice[v].dist);
}
heap->elements[vertice[v].position]->key = vertice[v].dist;
movingUp(heap, vertice[v].position);
}
node = node->next;
}
}
if (heap != NULL) {
for (int i = 0; i < heap->size; i++) {
delete heap->elements[i];
}
}
delete[] heap;
delete node;
return 1;
}