-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstraAlgorithm.cpp
More file actions
116 lines (101 loc) · 3.27 KB
/
Copy pathDijkstraAlgorithm.cpp
File metadata and controls
116 lines (101 loc) · 3.27 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
#include "DijkstraAlgorithm.h"
#include <iostream>
#include <limits>
#include <queue>
using namespace std;
DijkstraAlgorithm::DijkstraAlgorithm(){}
DijkstraAlgorithm::~DijkstraAlgorithm(){}
void DijkstraAlgorithm::DijkstraMatrix(Graph & graph){
amountVertices = graph.getVertices();
if(amountVertices <=1){
return;
}
firstVertice = graph.getFirstVertice();
amoutEdges = graph.getEdges();
vector <int> dist;
vector<int> parent;
dist.resize(amountVertices);
parent.resize(amountVertices);
for (int i =0;i<amountVertices;i++){
dist[i] = numeric_limits<int>::max();
parent[i] = -1;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
dist[firstVertice] = 0;
parent[firstVertice] = firstVertice;
pq.push(make_pair(0, firstVertice));
while(!pq.empty()){
int currentNode = pq.top().second;
int currentDist = pq.top().first;
pq.pop();
for (int z = 0;z<amountVertices;z++){
if(graph.tableMatrix[currentNode][z] != numeric_limits<int>::max()){
int y = (currentDist + graph.tableMatrix[currentNode][z]);
if (y < dist[z]) {
dist[z] = currentDist + graph.tableMatrix[currentNode][z];
parent[z] = currentNode;
pq.push(std::make_pair(dist[z], z));
}
}
}
}
for(int i = 0; i<amountVertices;i++){
cout << i << " | " << dist[i] << " |";
int temp = i;
if(i == firstVertice){
cout << " " << firstVertice;
}
while(temp != firstVertice){
cout << " "<< parent[temp];
temp = parent[temp];
}
cout << endl;
}
}
void DijkstraAlgorithm::DijkstraList(Graph & graph){
amountVertices = graph.getVertices();
if(amountVertices <=1){
return;
}
firstVertice = graph.getFirstVertice();
amoutEdges = graph.getEdges();
vector <int> dist;
vector<int> parent;
dist.resize(amountVertices);
parent.resize(amountVertices);
for (int i =0;i<amountVertices;i++){
dist[i] = numeric_limits<int>::max();
parent[i] = -1;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
dist[firstVertice] = 0;
parent[firstVertice] = firstVertice;
pq.push(make_pair(0, firstVertice));
Node *x;
while(!pq.empty()){
int currentNode = pq.top().second;
int currentDist = pq.top().first;
pq.pop();
for (x = graph.tableList[currentNode]; x!=NULL; x = x->next) {
/// minimizing distances
int y = (currentDist + x->weight);
if (y < dist[x->index]) {
dist[x->index] = currentDist + x->weight;
parent[x->index] = currentNode;
pq.push(std::make_pair(dist[x->index], x->index));
}
}
}
for(int i = 0; i<amountVertices;i++){
cout << i << " | " << dist[i] << " |";
int temp = i;
if(i == firstVertice){
cout << " " << firstVertice;
}
while(temp != firstVertice){
cout << " "<< parent[temp];
temp = parent[temp];
}
cout << endl;
}
}