-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDijkstra.cpp
More file actions
176 lines (150 loc) · 4.6 KB
/
Copy pathDijkstra.cpp
File metadata and controls
176 lines (150 loc) · 4.6 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
171
172
173
174
175
176
#include <iostream>
#include <climits>
#include <cassert>
#include <cstdlib>
#include "graph.h"
#include "priority_queue.h"
#define MAX_ELEMENTS 100
using namespace std;
struct Node{
int index;
int dis; // distance from source
int parent; // parent node
};
int get_key(Node* p){
return p->dis;
}
void set_key(Node* p, int key){
p->dis = key;
}
void Dijkstra_array(int** graph, int V, int source, Node** node_array){
// init single source
int i;
for(i=0; i<V; i++){
Node* p = new Node;
node_array[i] = p;
p->index = i;
p->dis = INT_MAX; // unreachable
p->parent = -1; // no parent
}
node_array[source]->dis = 0;
// init priority_queue
MinHeap<Node*> Q(MAX_ELEMENTS);
for(i=0; i<V; i++){
Q.insert(node_array[i], i);
}
//Q.print_node_heap_mapping();
while(Q.get_size() > 0){
Node* u = Q.extract_min();
cout << "Selecting node: " << u->index << endl;
int j;
for(j=0; j<V; j++){
// TODO: assume all weight is positive
if(graph[u->index][j] > 0 && node_array[j]->dis > u->dis + graph[u->index][j]){
//cout << "Check neighbour: " << j << endl;
// set parent
node_array[j]->parent = u->index;
// adjust the heap
Q.decrease_key(j, u->dis + graph[u->index][j]);
//Q.print_node_heap_mapping();
}
}
}
}
// 采用邻接表实现的Dijkstra算法
void Dijkstra_adjlist(vector<vector<AdjNode*> >graph, int V, int source, int* dis, int* parent){
vector<Node*> node_array;
int i;
for(i=0; i<V; i++){
Node* p = new Node;
p->index = i;
p->dis = MAX_DIST; // unreachable
p->parent = -1; // no parent
node_array.push_back(p);
}
node_array[source]->dis = 0;
// init priority_queue
MinHeap<Node*> Q(MAX_ELEMENTS);
for(i=0; i<V; i++){
Q.insert(node_array[i], i);
}
//Q.print_node_heap_mapping();
while(Q.get_size() > 0){
Node* from = Q.extract_min();
cout << "Selecting node: " << from->index << endl;
int j;
for(j=0; j<graph[from->index].size(); j++){
int to_index = graph[from->index][j]->v;
Node* to = node_array[to_index];
//cout << "Check neighbour: " << to->index << endl;
if(to->dis > from->dis + graph[from->index][j]->w){
// set parent
to->parent = from->index;
// adjust the heap
Q.decrease_key(to->index, from->dis + graph[from->index][j]->w);
//Q.print_node_heap_mapping();
}
}
}
for(i=0; i<V; i++){
dis[i] = node_array[i]->dis;
parent[i] = node_array[i]->parent;
}
}
void print_shortest_path(Node** node_array, int V, int source, int target){
if(source == target){
cout << source;
}else{
if(node_array[target]->parent == -1){
cout << "Error: no path from " << source << " to " << target;
}else{
int parent = node_array[target]->parent;
print_shortest_path(node_array, V, source, parent);
cout << " -> " << target;
}
}
}
void test_heap(){
MinHeap<Node*> h(MAX_ELEMENTS);
int a[] = {5, 3, 17, 10, 84, 19, 6, 22, 9};
for(int i=0; i<9; i++){
Node* p = new Node;
p->dis = a[i];
h.insert(p, i);
}
while(h.get_size() > 0){
Node* p = h.extract_min();
cout << p->dis << " ";
}
cout << endl;
}
/*
int main(int argc, const char* argv[]){
if(argc < 3){
printf("./Dijkstra <graph-file-data> <source>");
exit(0);
}
int V;
int source = atoi(argv[2]);
int* dis = (int*)malloc(V * sizeof(int));
int* parent = (int*)malloc(V * sizeof(int));
int** graph = read_directed_weighted_graph(argv[1], V);
print_graph(graph, V);
Node** node_array = (Node**)malloc(V * sizeof(Node*));
Dijkstra_array(graph, V, 0, node_array);
// 从node_array中读取dis和parent信息
for(int i=0; i<V; i++){
dis[i] = node_array[i]->dis;
parent[i] = node_array[i]->parent;
}
print_all_destination_paths(parent, V, source);
//vector<vector<AdjNode*> > graph = read_directed_weighted_adjlist(argv[1], V);
//print_adjlist_graph(graph, V);
//Dijkstra_adjlist(graph, V, source, dis, parent);
//print_all_destination_paths(parent, V, source);
cout << "shortest distance from " << source << ":" << endl;
for(int i=0; i<V; i++){
cout << dis[i] << " ";
}
cout << endl;
}*/