-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgraph.cpp
More file actions
198 lines (171 loc) · 4.62 KB
/
Copy pathgraph.cpp
File metadata and controls
198 lines (171 loc) · 4.62 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <iostream>
#include <fstream>
#include <vector>
#include <cassert>
#include "graph.h"
using namespace std;
/*
邻接矩阵的图表示处理
*/
// make a empty graph
int** make_empty_graph(int V){
int** graph;
graph = (int**)malloc(V * sizeof(int*));
int i, j;
for(i=0; i<V; i++){
graph[i] = (int*)malloc(V * sizeof(int));
for(j=0; j<V; j++){
graph[i][j] = MAX_DIST;
}
graph[i][i] = 0;
}
return graph;
}
int** read_unweighted_graph(const char* path, int& V, bool is_directed){
cout << "Reading undirected unweighted graph..." << endl;
fstream fs(path, fstream::in);
fs >> V; // number of vertices
int** graph = make_empty_graph(V);
int v1, v2;
while(fs >> v1 >> v2){
assert(v1 >= 0 && v1 < V);
assert(v2 >= 0 && v2 < V);
graph[v1][v2] = 1;
if(!is_directed)
graph[v2][v1] = 1;
}
fs.close();
return graph;
}
int** read_weighted_graph(const char* path, int& V, bool is_directed){
cout << "Reading undirected weighted graph..." << endl;
fstream fs(path, fstream::in);
fs >> V; // number of vertices
int i, j;
int** graph = make_empty_graph(V);
int v1, v2, w;
while(fs >> v1 >> v2 >> w){
assert(v1 >= 0 && v1 < V);
assert(v2 >= 0 && v2 < V);
graph[v1][v2] = w;
if(!is_directed)
graph[v2][v1] = w;
}
fs.close();
return graph;
}
int** read_undirected_unweighted_graph(const char* path, int& V){
return read_unweighted_graph(path, V, false);
}
int** read_directed_unweighted_graph(const char* path, int& V){
return read_unweighted_graph(path, V, true);
}
int** read_undirected_weighted_graph(const char* path, int& V){
return read_weighted_graph(path, V, false);
}
int** read_directed_weighted_graph(const char* path, int& V){
return read_weighted_graph(path, V, true);
}
void print_graph(int** graph, int V){
cout << "Num. of vertice: " << V << endl;
int i, j;
for(i=0; i<V; i++){
for(j=0; j<V; j++){
cout << graph[i][j] << " ";
}
cout << endl;
}
}
vector<Edge*> read_directed_weighted_edgelist(const char* path, int&V){
vector<Edge*> edgelist;
fstream f(path, fstream::in);
f >> V;
int from, to, w;
while(f >> from >> to >> w){
assert(from>=0 && from<V);
assert(to>=0 && to<V);
Edge* p = new Edge;
p->from = from;
p->to = to;
p->w = w;
edgelist.push_back(p);
}
return edgelist;
}
vector< vector<AdjNode*> > read_directed_weighted_adjlist(const char* path, int& V){
vector< vector<AdjNode*> > adjlist;
fstream f(path, fstream::in);
f >> V;
for(int i=0; i<V; i++){
vector<AdjNode*> tmp;
adjlist.push_back(tmp); // 压入空链表
}
int from, to, w;
while(f >> from >> to >> w){
assert(from>=0 && from<V);
assert(to>=0 && to<V);
AdjNode *p = new AdjNode;
p->v = to;
p->w = w;
adjlist[from].push_back(p);
}
return adjlist;
}
void print_adjlist_graph(vector<vector<AdjNode*> > graph, int V){
int i;
cout << "Num. of vertice: " << V << endl;
for(i=0; i<V; i++){
vector<AdjNode*> tolist = graph[i];
for(int j=0; j<tolist.size(); j++){
cout << i << " -> " << tolist[j]->v << " with weight: " << tolist[j]->w << endl;
}
}
}
/// 关于输出路径的函数
void print_path(int* parent, int s, int v){
if(s == v){
cout << s;
}else{
if(parent[v] == -1){
cout << "No path from " << s << " to " << v;
}else{
print_path(parent, s, parent[v]);
cout << " -> " << v;
}
}
}
// print paths to all destinations from single source
void print_all_destination_paths(int* parent, int V, int source){
int i;
for(i=0; i<V; i++){
if(i != source){
cout << "Path from " << source << " to " << i << ": ";
print_path(parent, source, i);
cout << endl;
}
}
}
void print_all_pair_shortest_paths(int** parent, int V){
int i;
for(i=0; i<V; i++){
cout << "Source node: " << i << endl;
print_all_destination_paths(parent[i], V, i);
}
}
void print_shortest_distance(int* dis, int V){
int i;
for(i=0; i<V; i++){
cout << dis[i] << " ";
}
cout << endl;
}
void print_all_pair_shortest_distance(int** dis, int V){
int i;
for(i=0; i<V; i++){
cout << "Source node: " << i << endl;
for(int j=0; j<V; j++){
cout << dis[i][j] << " ";
}
cout << endl;
}
}