实验五完成
This commit is contained in:
+25
-1
@@ -5,6 +5,7 @@
|
|||||||
#ifndef MATRIXGRAPH_H
|
#ifndef MATRIXGRAPH_H
|
||||||
#define MATRIXGRAPH_H
|
#define MATRIXGRAPH_H
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include<vector>
|
#include<vector>
|
||||||
|
|
||||||
@@ -48,7 +49,7 @@ public:
|
|||||||
|
|
||||||
// 连接顶点
|
// 连接顶点
|
||||||
void connect(int id1, int id2, T weight = 0) {
|
void connect(int id1, int id2, T weight = 0) {
|
||||||
if (id1 > data.size() || id2 > data.size() || (id1 < 0 || id2 < 0)) {
|
if (id1 >= data.size() || id2 >= data.size() || (id1 < 0 || id2 < 0)) {
|
||||||
throw NodeIdOutOfRangeException("您提供的id不合理", 0X001);
|
throw NodeIdOutOfRangeException("您提供的id不合理", 0X001);
|
||||||
}
|
}
|
||||||
if (id1 == id2) {
|
if (id1 == id2) {
|
||||||
@@ -97,5 +98,28 @@ public:
|
|||||||
}
|
}
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vector<T> DFS(int startID) {
|
||||||
|
if (startID >= matrix.size() || startID < 0) {
|
||||||
|
throw NodeIdOutOfRangeException("您提供的id不合理", 0X004);
|
||||||
|
}
|
||||||
|
vector<bool> visited(matrix.size(), false); //访问标记
|
||||||
|
vector<T> result;
|
||||||
|
|
||||||
|
function<void(int)> dfsHelper = [&](int node) {
|
||||||
|
visited[node] = true;
|
||||||
|
result.push_back(data.at(node));
|
||||||
|
|
||||||
|
for (int i = 0; i < matrix[node].size(); ++i) {
|
||||||
|
if (matrix[node][i].connected && !visited[i]) {
|
||||||
|
dfsHelper(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
dfsHelper(startID);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif //MATRIXGRAPH_H
|
#endif //MATRIXGRAPH_H
|
||||||
|
|||||||
+8
-1
@@ -26,7 +26,7 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
MatrixGraph<int> graph = new MatrixGraph<int>();
|
MatrixGraph<int> graph = MatrixGraph<int>();
|
||||||
int n, e;
|
int n, e;
|
||||||
cin >> n >> e;
|
cin >> n >> e;
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
@@ -39,5 +39,12 @@ int main() {
|
|||||||
}
|
}
|
||||||
cout << "图的邻接矩阵为:" << endl;
|
cout << "图的邻接矩阵为:" << endl;
|
||||||
cout << graph.to_string() << endl;
|
cout << graph.to_string() << endl;
|
||||||
|
vector<int> dfs = graph.DFS(0);
|
||||||
|
cout << "图的DFS序列为:" << endl;
|
||||||
|
vector<int>::iterator it;
|
||||||
|
for (it = dfs.begin(); it != dfs.end(); it++) {
|
||||||
|
cout << *it << " ";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-15
@@ -27,25 +27,25 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
ListGraph<int> graph=ListGraph<int>();
|
ListGraph<int> graph = ListGraph<int>();
|
||||||
int n,e;
|
int n, e;
|
||||||
cin>>n>>e;
|
cin >> n >> e;
|
||||||
for(int i=0;i<n;i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
graph.insert(i);
|
graph.insert(i);
|
||||||
}
|
}
|
||||||
int id1,id2;
|
int id1, id2;
|
||||||
for(int i=0;i<e;i++) {
|
for (int i = 0; i < e; i++) {
|
||||||
cin>>id1>>id2;
|
cin >> id1 >> id2;
|
||||||
graph.connect(id1,id2);
|
graph.connect(id1, id2);
|
||||||
}
|
}
|
||||||
cout<<"邻接表:"<<endl;
|
cout << "邻接表:" << endl;
|
||||||
cout<<graph.to_string()<<endl;
|
cout << graph.to_string() << endl;
|
||||||
cout<<"BFS序列:"<<endl;
|
cout << "BFS序列:" << endl;
|
||||||
vector<int> bfs=graph.BFS(0);
|
vector<int> bfs = graph.BFS(0);
|
||||||
vector<int>::iterator it;
|
vector<int>::iterator it;
|
||||||
for(it=bfs.begin();it!=bfs.end();++it) {
|
for (it = bfs.begin(); it != bfs.end(); ++it) {
|
||||||
cout<<*it<<" ";
|
cout << *it << " ";
|
||||||
}
|
}
|
||||||
cout<<endl;
|
cout << endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user