-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra_WarshallFloyd_algorithm.cpp
179 lines (150 loc) · 3.68 KB
/
Dijkstra_WarshallFloyd_algorithm.cpp
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
# include <iostream>
using namespace std;
struct Edge{
int s; //wierzcholek
int t; //sasiad
double w; //waga krawêdzi
};
class Graph{
private:
double** Matrix;
int n; //liczba wêz³ów
bool directed;
public:
Graph(int n, int m, Edge edges[], bool directed); //tworzy graf w oparciu o pdan¹ listê krawêdzi z wagami
void Dijkstra(int s);
void wypisz();
int szukaj(int* S, int* d, int n);
void shortestPath(int s, int t, int* parent);
void WarshallFloyd();
friend ostream& operator<<(ostream& out, Graph& g);
};
Graph::Graph(int n, int m, Edge edges[], bool directed){
this->n=n;
this->directed=directed;
Matrix = new double*[n];
for(int i=0; i<n;i++){
Matrix[i]= new double[n];
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(i==j){
Matrix[i][j]=0;
}
else{
Matrix[i][j]=1234;
}
}
}
for(int i=0; i<m; i++){
Matrix[edges[i].s][edges[i].t]=edges[i].w;
if(directed==false){
Matrix[edges[i].t][edges[i].s]=edges[i].w;
}
}
}
void Graph::wypisz(){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
cout<<Matrix[i][j]<<" ";
}
cout<<endl;
}
}
int znajdz(int* S, int* d, int n){
int index=0;
for(int i=0; i<n; i++){
if(S[i]==0){
index=i;
}
}
for(int i=0; i<n; i++){
if(S[i]==0 && d[i]<d[index]){
index=i;
}
}
return index;
}
void Graph::Dijkstra(int s){
int parent[n];
int d[n];
int S[n]{};
for(int i=0; i<n; i++){
d[i]=2115;
}
d[s]=0;
int u;
for(int i=0; i<n; i++){
u=znajdz(S,d,n);
S[u]=1;
for(int j=0; j<n; j++){
if(Matrix[u][j]!=1234 && u!=j){
if(d[j]>Matrix[u][j]+d[u]){
d[j]=Matrix[u][j]+d[u];
parent[j]=u;
}
}
}
}
for(int i=0; i<n; i++){
cout<<d[i]<<" ";
}
cout<<endl;
shortestPath(0,1,parent); //poczatek, koniec
}
void Graph::shortestPath(int s, int t, int* parent){
int droga[n];
int size=0;
droga[size]=t;
size++;
int x=t;
for(int i=0; i<n; i++){
if(x!=s){
x=parent[x];
droga[size]=x;
size++;
}else{
break;
}
}
for(int i=size-1; i>=0; i--){
cout<<droga[i]<<" ";
}
}
void Graph::WarshallFloyd(){
int d[n][n];
int p[n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
d[i][j]=Matrix[i][j];
if(Matrix[i][j]!=0 && Matrix[i][j]!=1234){
p[i][j]=i;
}
}
}
for(int k=0; k<n; k++){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(d[i][j]>d[i][k]+d[k][j]){
d[i][j]=d[i][k]+d[k][j];
p[i][j]=p[k][j];
}
}
}
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
cout<<d[i][j]<<" ";
}
}
}
int main(){
int n=6;
int m=15;
Edge edges[]={{0,4,6}, {0,5,1},{1,0,2}, {1,2,5}, {1,4,1}, {2,1,3}, {2,3,2}, {2,4,1}, {3,2,1}, {3,5,4}, {4,0,6}, {4,1,4}, {4,3,2}, {5,3,1}, {5,4,1}};
Graph g(n,m,edges,true);
g.wypisz();
cout<<endl;
//g.Dijkstra(0);
g.WarshallFloyd();
}