1 Star 0 Fork 127

hangerfor/数据结构(C++模板实现)

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
test.cpp 53.44 KB
Copy Edit Raw Blame History
Y_Dash authored 2023-07-03 18:41 +08:00 . 增加部分readme代码指向
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
/*!
* @file test.cpp
* @author CyberDash计算机考研, cyberdash@163.com(抖音id:cyberdash_yuan)
* @brief 图测试.cpp文件
* @version 0.2.1
* @date 2021-10-9
*/
#include "test.h"
#include "min_priority_queue.h"
#include "graph_algorithm.h"
#include "graph_algorithm.cpp"
using namespace std;
/*!
* @brief **测试-图-基础函数**
* @note
* 测试-图-基础函数
* --------------
* --------------
*
* --------------
* + **1 初始化图的基本信息**\n\n
* 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)\n
* 初始化边信息\n\n
* + **2 测试基础函数**\n\n
* 构造adjacency_list_directed_graph(邻接表有向图)\n
* 依次删除1个城市, 然后打印图\n\n
* 构造adjacency_list_undirected_graph(邻接表无向图)\n
* 依次删除1个城市, 然后打印图\n\n
* 构造matrix_directed_graph(矩阵有向图)\n
* 依次删除1个城市, 然后打印图\n\n
* 构造matrix_undirected_graph(矩阵无向图)\n
* 依次删除1个城市, 然后打印图\n\n
*
*
* --------------
*/
void TestBaseFunctions() {
cout<<endl;
cout<<"|------------------------ CyberDash ------------------------|"<<endl;
cout<<"| Test Graph BaseFunctions |"<<endl;
cout<<"| 测试-图-基础函数 |"<<endl;
cout<<"| |"<<endl;
cout<<"| 北京 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 0.1 0.12 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 上海---0.01---广州 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 0.13 0.14 0.05 0.17 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 杭州--0.09-- 深圳 --0.11--成都 |"<<endl;
cout<<endl;
// ---------- 1 初始化图的基本信息 ----------
// 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)
vector<string> vertices{ "北京", "上海", "广州", "深圳", "杭州", "成都" };
// 初始化边信息
unsigned int edge_count = 9;
vector<string> starting_vertices{ "北京", "北京", "上海", "上海", "上海", "广州", "广州", "深圳", "深圳" }; // 初始化起点数组
vector<string> ending_vertices{ "上海", "广州", "广州", "深圳", "杭州", "深圳", "成都", "杭州", "成都" }; // 初始化终点数组
vector<double> weights{ 0.1, 0.12, 0.01, 0.14, 0.13, 0.05, 0.17, 0.09, 0.11 }; // 初始化边权值数组
vector<Edge<string, double> > edges; // 声明边vector
// 构造边vector
for (unsigned int i = 0; i < edge_count; i++) {
Edge<string, double> edge(starting_vertices[i], ending_vertices[i], weights[i]);
edges.push_back(edge);
}
// ---------- 2 测试基础函数 ----------
// 构造adjacency_list_directed_graph(邻接表有向图)
AdjacencyListGraph<string, double> adjacency_list_directed_graph(Graph<string, double>::DIRECTED, 10, 1000, edges, vertices);
// 依次删除1个城市, 然后打印图
cout << "---------- 1 邻接表有向图删除结点 ----------" << endl << endl;
adjacency_list_directed_graph.RemoveVertex("北京");
adjacency_list_directed_graph.RemoveVertex("上海");
adjacency_list_directed_graph.RemoveVertex("深圳");
cout << adjacency_list_directed_graph << endl << endl;
// 构造adjacency_list_undirected_graph(邻接表无向图)
AdjacencyListGraph<string, double> adjacency_list_undirected_graph(Graph<string, double>::UNDIRECTED, 10, 1000, edges, vertices);
// 依次删除1个城市, 然后打印图
cout << "---------- 2 邻接表无向图删除结点 ----------" << endl << endl;
adjacency_list_undirected_graph.RemoveVertex("北京");
adjacency_list_undirected_graph.RemoveVertex("上海");
adjacency_list_undirected_graph.RemoveVertex("深圳");
cout << adjacency_list_undirected_graph << endl << endl;
// 构造matrix_directed_graph(矩阵有向图)
MatrixGraph<string, double> matrix_directed_graph(Graph<string, double>::DIRECTED, 10, 1000, edges, vertices);
// 依次删除1个城市, 然后打印图
cout << "---------- 3 矩阵有向图删除结点 ----------" << endl << endl;
matrix_directed_graph.RemoveVertex("北京");
matrix_directed_graph.RemoveVertex("上海");
matrix_directed_graph.RemoveVertex("深圳");
cout << matrix_directed_graph << endl << endl;
// 构造matrix_undirected_graph(矩阵无向图)
MatrixGraph<string, double> matrix_undirected_graph(Graph<string, double>::UNDIRECTED, 10, 1000, edges, vertices);
// 依次删除1个城市, 然后打印图
cout << "---------- 4 矩阵无向图删除结点 ----------" << endl << endl;
matrix_undirected_graph.RemoveVertex("北京");
matrix_undirected_graph.RemoveVertex("上海");
matrix_undirected_graph.RemoveVertex("深圳");
cout << matrix_undirected_graph << endl << endl;
cout << "-------------------------------------------------------------" << endl;
}
/*!
* @brief **测试-矩阵图-打印矩阵**
* @note
* 测试-矩阵图-打印矩阵
* -----------------
* -----------------
*
* -----------------
* + **1 初始化图的基本信息**\n\n
* 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)\n
* 初始化边信息\n\n
* 构造无向矩阵图\n\n
* + **2 打印矩阵**\n\n
*
*
* -----------------
*/
void TestMatrixGraphPrintMatrix() {
cout<<endl;
cout<<"|------------------------ CyberDash ------------------------|"<<endl;
cout<<"| Test MatrixGraph PrintMatrix |"<<endl;
cout<<"| 测试-矩阵图-打印矩阵 |"<<endl;
cout<<"| |"<<endl;
cout<<"| 北京 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 0.1 0.12 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 上海---0.01---广州 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 0.13 0.14 0.05 0.17 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 杭州--0.09-- 深圳 --0.11--成都 |"<<endl;
cout<<endl;
// ---------- 1 初始化图的基本信息 ----------
unsigned int edge_count = 9;
// 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)
vector<string> vertices{ "北京", "上海", "广州", "深圳", "杭州", "成都" };
// 初始化边信息
vector<string> starting_vertices{ "北京", "北京", "上海", "上海", "上海", "广州", "广州", "深圳", "深圳" };
vector<string> ending_vertices{ "上海", "广州", "广州", "深圳", "杭州", "深圳", "成都", "杭州", "成都" };
vector<double> weights{ 0.1, 0.12, 0.01, 0.14, 0.13, 0.05, 0.17, 0.09, 0.11 }; // 边权重数组
vector<Edge<string, double> > edges;
for (unsigned int i = 0; i < edge_count; i++) {
Edge<string, double> edge(starting_vertices[i], ending_vertices[i], weights[i]);
edges.push_back(edge);
}
// 构造无向矩阵图
MatrixGraph<string, double> matrix_graph(10, 1000, edges, vertices);
// ---------- 2 打印矩阵 ----------
cout << "打印矩阵:" << endl << endl;
matrix_graph.PrintMatrix();
cout<<"-------------------------------------------------------------"<<endl;
}
/*!
* @brief **测试-图-深度优先遍历(递归)**
* @note
* 测试-图-深度优先遍历(递归)
* -----------------------
* -----------------------
*
* -----------------------
* + **1 初始化图的基本信息**\n\n
* 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)\n
* 初始化边信息\n\n
* + **2 测试邻接表图深度优先遍历**\n\n
* 构造adjacency_list_graph(邻接表无向图)\n
* 以"北京"为起点进行深度优先遍历\n\n
* + **3 测试矩阵图深度优先遍历**\n\n
* 构造matrix_graph(矩阵无向图)\n
* 以"北京"为起点进行深度优先遍历\n
*
*
* -----------------------
*/
void TestDfsRecursive() {
cout<<endl;
cout<<"|------------------------ CyberDash ------------------------|"<<endl;
cout<<"| Test Graph Dfs |"<<endl;
cout<<"| 测试-图-深度优先遍历(递归) |"<<endl;
cout<<"| |"<<endl;
cout<<"| 北京 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 0.1 0.12 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 上海---0.01---广州 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 0.13 0.14 0.05 0.17 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 杭州--0.09-- 深圳 --0.11--成都 |"<<endl;
cout<<endl;
// ---------- 1 初始化图的基本信息 ----------
// 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)
unsigned int edge_count = 9;
vector<string> vertices{ "北京", "上海", "广州", "深圳", "杭州", "成都" };
vector<string> starting_vertices{ "北京", "北京", "上海", "上海", "上海", "广州", "广州", "深圳", "深圳" };
vector<string> ending_vertices{ "上海", "广州", "广州", "深圳", "杭州", "深圳", "成都", "杭州", "成都" };
vector<double> weights{ 0.1, 0.12, 0.01, 0.14, 0.13, 0.05, 0.17, 0.09, 0.11 };
// 初始化边信息
vector<Edge<string, double> > edges;
for (unsigned int i = 0; i < edge_count; i++) {
Edge<string, double> edge(starting_vertices[i], ending_vertices[i], weights[i]);
edges.push_back(edge);
}
// ---------- 2 测试邻接表图深度优先遍历 ----------
cout<<"---------- 邻接表图 ----------"<<endl;
AdjacencyListGraph<string, double> adjacency_list_graph(10, 1000, edges, vertices); // 构造adjacency_list_graph(邻接表无向图)
Dfs(adjacency_list_graph, vertices[0]); // "北京"为起点进行深度优先遍历
// ---------- 3 测试矩阵图深度优先遍历 ----------
cout<<endl<<"---------- 矩阵图 ----------"<<endl;
MatrixGraph<string, double> matrix_graph(10, 1000, edges, vertices); // 构造matrix_graph(矩阵无向图)
Dfs(matrix_graph, vertices[0]); // 以"北京"为起点进行深度优先遍历
cout<<"-------------------------------------------------------------"<<endl;
}
/*!
* @brief **测试-图-广度优先遍历**
* @note
* 测试-图-广度优先遍历
* -----------------
* -----------------
*
* -----------------
*
*
* -----------------
* + **1 初始化图的基本信息**\n\n
* 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)\n
* 初始化边信息\n\n
* + **2 测试邻接表图广度优先遍历**\n\n
* 构造adjacency_list_undirected_graph(邻接表无向图)\n
* 以"北京"为起点进行bfs遍历\n\n
* + **3 测试矩阵图广度优先遍历**\n\n
* 构造matrix_undirected_graph(矩阵无向图)\n
* 以"北京"为起点进行bfs遍历\n
*/
void TestBFS() {
cout<<endl;
cout<<"|------------------------ CyberDash ------------------------|"<<endl;
cout<<"| Test Graph Bfs |"<<endl;
cout<<"| 测试-图-广度优先遍历 |"<<endl;
cout<<"| |"<<endl;
cout<<"| 北京 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 0.1 0.12 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 上海---0.01---广州 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 0.13 0.14 0.05 0.17 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 杭州--0.09-- 深圳 --0.11--成都 |"<<endl;
cout<<endl;
// ---------- 1 初始化图的基本信息 ----------
unsigned int edge_count = 9;
// 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)
vector<string> vertices{ "北京", "上海", "广州", "深圳", "杭州", "成都" };
// 初始化边信息
vector<string> starting_vertices{ "北京", "北京", "上海", "上海", "上海", "广州", "广州", "深圳", "深圳" };
vector<string> ending_vertices{ "上海", "广州", "广州", "深圳", "杭州", "深圳", "成都", "杭州", "成都" };
vector<double> weights{ 0.1, 0.12, 0.01, 0.14, 0.13, 0.05, 0.17, 0.09, 0.11 };
vector<Edge<string, double> > edges;
for (unsigned int i = 0; i < edge_count; i++) {
Edge<string, double> edge(starting_vertices[i], ending_vertices[i], weights[i]);
edges.push_back(edge);
}
// ---------- 2 测试邻接表图广度优先遍历 ----------
cout<<"---------- 邻接表图 ----------"<<endl;
AdjacencyListGraph<string, double> adjacency_list_undirected_graph(10, 1000, edges, vertices); // 构造adjacency_list_undirected_graph(邻接表无向图)
Bfs(adjacency_list_undirected_graph, vertices[0]); // 以"北京"为起点进行bfs遍历
// ---------- 3 测试矩阵图广度优先遍历 ----------
cout<<endl<<"---------- 矩阵图 ----------"<<endl;
MatrixGraph<string, double> matrix_undirected_graph(10, 1000, edges, vertices); // 构造matrix_undirected_graph(矩阵无向图)
Bfs(matrix_undirected_graph, vertices[0]); // 以"北京"为起点进行bfs遍历
cout<<"-------------------------------------------------------------"<<endl;
}
/*!
* @brief **测试-图-连通分量**
* @note
* 测试-图-连通分量
* --------------
* --------------
*
* --------------
*
*
* --------------
* + **1 构造邻接表图**\n\n
* 声明adjacency_list_graph(邻接表图)\n
* 插入结点0, 1, 2, 3\n
* 插入边(0 , 1)和边(2, 3)\n\n
* + **2 构造矩阵图**\n\n
* 声明matrix_graph(矩阵图)\n
* 插入结点0, 1, 2, 3\n
* 插入边(0 , 1)和边(2, 3)\n\n
* + **3 邻接表图求连通分量**\n\n
* 调用Components\n\n
* + **4 矩阵图求连通分量**\n\n
* 调用Components\n\n
*
*
* --------------
*/
void TestComponents() {
cout<<endl;
cout<<"|------------------------ CyberDash ------------------------|"<<endl;
cout<<"| Test Graph Components |"<<endl;
cout<<"| 测试-图-连通分量 |"<<endl;
cout<<"| 节点: |"<<endl;
cout<<"| 0, 1, 2, 3 |"<<endl;
cout<<"| 边: |"<<endl;
cout<<"| 0-1权值: 0.8 |"<<endl;
cout<<"| 2-3权值: 7.3 |"<<endl<<endl;
// ---------- 1 构造邻接表图 ----------
// 声明adjacency_list_graph(邻接表图)
AdjacencyListGraph<int, double> adjacency_list_graph(10, 10000);
// 插入结点0, 1, 2, 3
adjacency_list_graph.InsertVertex(0);
adjacency_list_graph.InsertVertex(1);
adjacency_list_graph.InsertVertex(2);
adjacency_list_graph.InsertVertex(3);
// 插入边(0 , 1)和边(2, 3)
adjacency_list_graph.InsertEdge(0, 1, 0.8);
adjacency_list_graph.InsertEdge(2, 3, 7.3);
// ---------- 2 构造矩阵图 ----------
// 声明matrix_graph(矩阵图)
MatrixGraph<int, double> matrix_graph(10, 10000);
// 插入结点0, 1, 2, 3
matrix_graph.InsertVertex(0);
matrix_graph.InsertVertex(1);
matrix_graph.InsertVertex(2);
matrix_graph.InsertVertex(3);
// 插入边(0 , 1)和边(2, 3)
matrix_graph.InsertEdge(0, 1, 0.8);
matrix_graph.InsertEdge(2, 3, 7.3);
// ---------- 3 邻接表图求连通分量 ----------
cout<<"---------- 邻接表图 ----------"<<endl;
Components(adjacency_list_graph); // 调用Components
// ---------- 4 矩阵图求连通分量 ----------
cout<<"---------- 矩阵图 ----------"<<endl;
Components(matrix_graph); // 调用Components
cout<<"-------------------------------------------------------------"<<endl;
}
/*!
* @brief **测试-图-最小生成树Kruskal**
* @note
* 测试-图-最小生成树Kruskal
* ----------------------
* ----------------------
*
* ----------------------
* + **1 初始化图的基本信息**\n\n
* 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)\n
* 初始化边信息\n\n
* + **2 测试邻接表图Kruskal**\n\n
* 构造adjacency_list_undirected_graph(邻接表无向图)\n
* 初始化adj_min_span_tree(邻接表图的最小生成树)\n\n
* 调用Kruskal求最小生成树\n
* 打印最小生成树\n\n
* + **3 测试矩阵图Kruskal**\n\n
* 构造matrix_undirected_graph(矩阵无向图)\n
* 初始化matrix_min_span_tree(矩阵图的最小生成树)\n\n
* 调用Kruskal求最小生成树\n
* 打印最小生成树\n\n
*
*
* -------------------
*/
void TestKruskal() {
cout<<endl;
cout<<"|------------------------ CyberDash ------------------------|"<<endl;
cout<<"| Test Graph Kruskal |"<<endl;
cout<<"| 测试-图-最小生成树Kruskal |"<<endl;
cout<<"| |"<<endl;
cout<<"| 北京 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 0.1 0.12 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 上海---0.01---广州 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 0.13 0.14 0.05 0.17 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 杭州--0.09-- 深圳 --0.11--成都 |"<<endl;
cout<<endl;
// ---------- 1 初始化图的基本信息 ----------
unsigned int edge_count = 9;
// 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)
vector<string> vertices{ "北京", "上海", "广州", "深圳", "杭州", "成都" };
// 初始化边信息
vector<string> starting_vertices{ "北京", "北京", "上海", "上海", "上海", "广州", "广州", "深圳", "深圳" };
vector<string> ending_vertices{ "上海", "广州", "广州", "深圳", "杭州", "深圳", "成都", "杭州", "成都" };
vector<double> weights{ 0.1, 0.12, 0.01, 0.14, 0.13, 0.05, 0.17, 0.09, 0.11 };
vector<Edge<string, double> > edges;
for (unsigned int i = 0; i < edge_count; i++) {
Edge<string, double> edge(starting_vertices[i], ending_vertices[i], weights[i]);
edges.push_back(edge);
}
// ---------- 2 测试邻接表图Kruskal ----------
cout<<"---------- 邻接表图 ----------"<<endl;
AdjacencyListGraph<string, double> adjacency_list_graph(10, 1000, edges, vertices); // 构造adjacency_list_undirected_graph(邻接表无向图)
MinimumSpanTree<string, double> adj_min_span_tree(100); // 初始化adj_min_span_tree(邻接表图的最小生成树)
Kruskal(adjacency_list_graph, adj_min_span_tree); // 调用Kruskal求最小生成树
adj_min_span_tree.Print(); // 打印最小生成树
// ---------- 3 测试矩阵图Kruskal ----------
cout<<endl<<"---------- 矩阵图 ----------"<<endl;
MatrixGraph<string, double> matrix_graph(10, 1000, edges, vertices); // 构造matrix_undirected_graph(矩阵无向图)
MinimumSpanTree<string, double> matrix_min_span_tree(100); // 初始化matrix_min_span_tree(邻接表图的最小生成树)
Kruskal(matrix_graph, matrix_min_span_tree); // 调用Kruskal求最小生成树
matrix_min_span_tree.Print(); // 打印最小生成树
cout << "-------------------------------------------------------------" << endl;
}
/*!
* @brief **测试-图-最小生成树Prim**
* @note
* 测试-图-最小生成树Prim
* -------------------
* -------------------
*
* -------------------
* + **1 初始化图的基本信息**\n\n
* 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)\n
* 初始化边信息\n\n
* + **2 测试邻接表图Prim**\n\n
* 构造adjacency_list_undirected_graph(邻接表无向图)\n
* 初始化adj_min_span_tree(邻接表图的最小生成树)\n\n
* 调用Prim求最小生成树\n
* 打印最小生成树\n\n
* + **3 测试矩阵图Prim**\n\n
* 构造matrix_undirected_graph(矩阵无向图)\n
* 初始化matrix_min_span_tree(矩阵图的最小生成树)\n\n
* 调用Prim求最小生成树\n
* 打印最小生成树\n\n
*
*
* -------------------
*/
void TestPrim() {
cout<<endl;
cout<<"|------------------------ CyberDash ------------------------|"<<endl;
cout<<"| Test Graph Prim |"<<endl;
cout<<"| 测试-图-最小生成树Prim |"<<endl;
cout<<"| |"<<endl;
cout<<"| 北京 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 0.1 0.12 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 上海---0.01---广州 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 0.13 0.14 0.05 0.17 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 杭州--0.09-- 深圳 --0.11--成都 |"<<endl;
cout<<endl;
// ---------- 1 初始化图的基本信息 ----------
unsigned int edge_count = 9;
// 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)
vector<string> vertices{ "北京", "上海", "广州", "深圳", "杭州", "成都" };
// 初始化边信息
vector<string> starting_vertices{ "北京", "北京", "上海", "上海", "上海", "广州", "广州", "深圳", "深圳" };
vector<string> ending_vertices{ "上海", "广州", "广州", "深圳", "杭州", "深圳", "成都", "杭州", "成都" };
vector<double> weights{ 0.1, 0.12, 0.01, 0.14, 0.13, 0.05, 0.17, 0.09, 0.11 };
vector<Edge<string, double> > edges;
for (unsigned int i = 0; i < edge_count; i++) {
Edge<string, double> edge(starting_vertices[i], ending_vertices[i], weights[i]);
edges.push_back(edge);
}
// ---------- 2 测试邻接表图Prim ----------
cout<<"---------- 邻接表图 ----------"<<endl;
AdjacencyListGraph<string, double> adjacency_list_undirected_graph(10, 1000, edges, vertices); // 构造adjacency_list_undirected_graph(邻接表无向图)
MinimumSpanTree<string, double> adj_min_span_tree(100); // 初始化adj_min_span_tree(邻接表图的最小生成树)
Prim(adjacency_list_undirected_graph, adj_min_span_tree); // 调用Prim求最小生成树
adj_min_span_tree.Print(); // 打印最小生成树
// ---------- 3 测试矩阵图Prim ----------
cout<<endl<<"---------- 矩阵图 ----------"<<endl;
MatrixGraph<string, double> matrix_undirected_graph(10, 1000, edges, vertices); // 构造matrix_undirected_graph(矩阵无向图)
MinimumSpanTree<string, double> matrix_min_span_tree(100); // 初始化matrix_min_span_tree(邻接表图的最小生成树)
Prim(matrix_undirected_graph, matrix_min_span_tree); // 调用Prim求最小生成树
matrix_min_span_tree.Print(); // 打印最小生成树
cout<<endl<<"-------------------------------------------------------------"<<endl<<endl;
}
/*!
* @brief **测试-图-最短路径Dijkstra**
* @note
* 测试-图-最短路径Dijkstra
* ----------------------
* ----------------------
*
* ----------------------
* + **1 初始化图的基本信息**\n\n
* 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)\n
* 初始化边信息\n\n
* + **2 测试邻接表图Dijkstra**\n\n
* 构造adjacency_list_undirected_graph(邻接表无向图)\n
* 声明adjacency_list_graph_min_distances(邻接表图最短路径数组)\n
* 声明adjacency_list_graph_predecessors(最短路径前驱结点数组)\n\n
* 调用Dijkstra函数, 求"北京"到其他各点的最短路径\n
* 调用PrintSingleSourceShortestPath打印"北京"到各城市的最短路径\n\n
* + **3 测试矩阵图Dijkstra**\n\n
* 构造matrix_undirected_graph(矩阵无向图)\n
* 声明matrix_graph_min_distances(邻接表图最短路径数组)\n
* 声明matrix_graph_predecessors(最短路径前驱结点数组)\n\n
* 调用Dijkstra函数, 求"北京"到其他各点的最短路径\n
* 调用PrintSingleSourceShortestPath打印"北京"到各城市的最短路径\n\n
*
*
* ----------------------
*/
void TestDijkstra() {
cout<<endl;
cout<<"|------------------------ CyberDash ------------------------|"<<endl;
cout<<"| Test Dijkstra |"<<endl;
cout<<"| 测试-图-最短路径Dijkstra |"<<endl;
cout<<"| |"<<endl;
cout<<"| 北京 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 0.1 0.12 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 上海---0.01---广州 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 0.13 0.14 0.05 0.17 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 杭州--0.09-- 深圳 --0.11--成都 |"<<endl;
cout<<endl;
// ---------- 1 初始化图的基本信息 ----------
unsigned int edge_count = 9;
// 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)
vector<string> vertices{ "北京", "上海", "广州", "深圳", "杭州", "成都" };
// 初始化边信息
vector<string> starting_vertices{ "北京", "北京", "上海", "上海", "上海", "广州", "广州", "深圳", "深圳" };
vector<string> ending_vertices{ "上海", "广州", "广州", "深圳", "杭州", "深圳", "成都", "杭州", "成都" };
vector<double> weights{ 0.1, 0.12, 0.01, 0.14, 0.13, 0.05, 0.17, 0.09, 0.11 };
vector<Edge<string, double> > edges;
for (unsigned int i = 0; i < edge_count; i++) {
Edge<string, double> edge(starting_vertices[i], ending_vertices[i], weights[i]);
edges.push_back(edge);
}
// ---------- 2 测试邻接表图Dijkstra ----------
cout << "---------- 邻接表图 ----------" << endl;
AdjacencyListGraph<string, double> adjacency_list_undirected_graph(10, 1000, edges, vertices); // 构造adjacency_list_undirected_graph(邻接表无向图)
double adjacency_list_graph_min_distances[10]; // 声明adjacency_list_graph_min_distances(邻接表图最短路径数组)
int adjacency_list_graph_predecessors[10]; // 声明adjacency_list_graph_predecessors(最短路径前驱结点数组)
// 调用Dijkstra函数, 求"北京"到其他各点的最短路径
Dijkstra(adjacency_list_undirected_graph, vertices[0], adjacency_list_graph_min_distances, adjacency_list_graph_predecessors);
// 调用PrintSingleSourceShortestPath打印"北京"到各城市的最短路径
PrintSingleSourceShortestPath(adjacency_list_undirected_graph, vertices[0], adjacency_list_graph_min_distances, adjacency_list_graph_predecessors);
// ---------- 3 测试矩阵图Dijkstra ----------
cout << endl << endl << "---------- 矩阵图 ----------" << endl;
MatrixGraph<string, double> matrix_undirected_graph(1, 10, 1000, edges, vertices); // 构造matrix_undirected_graph(矩阵无向图)
double matrix_graph_min_distances[10]; // 声明matrix_graph_min_distances(邻接表图最短路径数组)
int matrix_graph_predecessors[10]; // 声明matrix_graph_predecessors(最短路径前驱结点数组)
// 调用Dijkstra函数, 求"北京"到其他各点的最短路径
Dijkstra(matrix_undirected_graph, vertices[0], matrix_graph_min_distances, matrix_graph_predecessors);
// 调用PrintSingleSourceShortestPath打印"北京"到各城市的最短路径
PrintSingleSourceShortestPath(matrix_undirected_graph, vertices[0], matrix_graph_min_distances, matrix_graph_predecessors);
cout<<"-------------------------------------------------------------"<<endl<<endl;
}
/*!
* @brief **测试-图-最短路径BellmanFord**
* @note
* 测试-图-最短路径BellmanFord
* ------------------------
* ------------------------
*
* ------------------------
* + **1 初始化图的基本信息**\n\n
* 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)\n
* 初始化边信息\n\n
* + **2 测试邻接表图BellmanFord**\n\n
* 构造adjacency_list_undirected_graph(邻接表无向图)\n
* 声明adjacency_list_graph_min_distances(邻接表图最短路径数组)\n
* 声明adjacency_list_graph_predecessors(最短路径前驱结点数组)\n\n
* 调用BellmanFord函数, 求"北京"到其他各点的最短路径\n
* 调用PrintSingleSourceShortestPath打印"北京"到各城市的最短路径\n\n
* + **3 测试矩阵图BellmanFord**\n\n
* 构造matrix_undirected_graph(矩阵无向图)\n
* 声明matrix_graph_min_distances(邻接表图最短路径数组)\n
* 声明matrix_graph_predecessors(最短路径前驱结点数组)\n\n
* 调用BellmanFord函数, 求"北京"到其他各点的最短路径\n
* 调用PrintSingleSourceShortestPath打印"北京"到各城市的最短路径\n\n
*
*
* ------------------------
*/
void TestBellmanFord() {
cout<<endl;
cout<<"|------------------------ CyberDash ------------------------|"<<endl;
cout<<"| Test Graph BellmanFord |"<<endl;
cout<<"| 测试-图-最短路径BellmanFord |"<<endl;
cout<<"| |"<<endl;
cout<<"| 北京 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 0.1 0.12 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 上海---0.01---广州 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 0.13 0.14 0.05 0.17 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 杭州--0.09-- 深圳 --0.11--成都 |"<<endl;
cout<<endl;
// ---------- 1 初始化图的基本信息 ----------
unsigned int edge_count = 9;
// 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)
vector<string> vertices{ "北京", "上海", "广州", "深圳", "杭州", "成都" };
// 初始化边信息
vector<string> starting_vertices{ "北京", "北京", "上海", "上海", "上海", "广州", "广州", "深圳", "深圳" };
vector<string> ending_vertices{ "上海", "广州", "广州", "深圳", "杭州", "深圳", "成都", "杭州", "成都" };
vector<double> weights{ 0.1, 0.12, 0.01, 0.14, 0.13, 0.05, 0.17, 0.09, 0.11 }; // 边权重数组
vector<Edge<string, double> > edges;
for (unsigned int i = 0; i < edge_count; i++) {
Edge<string, double> edge(starting_vertices[i], ending_vertices[i], weights[i]);
edges.push_back(edge);
}
// ---------- 2 测试邻接表图BellmanFord ----------
cout << "---------- 邻接表图 ----------" << endl;
AdjacencyListGraph<string, double> adjacency_list_graph(10, 1000, edges, vertices); // 构造adjacency_list_undirected_graph(邻接表无向图)
double adjacency_list_graph_min_distances[10]; // 声明adjacency_list_graph_min_distances(邻接表图最短路径数组)
int adjacency_list_graph_predecessors[10]; // 声明adjacency_list_graph_predecessors(最短路径前驱结点数组)
// 调用BellmanFord函数, 求"北京"到其他各点的最短路径
BellmanFord(adjacency_list_graph, vertices[0], adjacency_list_graph_min_distances, adjacency_list_graph_predecessors);
// 调用PrintSingleSourceShortestPath打印"北京"到各城市的最短路径
PrintSingleSourceShortestPath(adjacency_list_graph, vertices[0], adjacency_list_graph_min_distances, adjacency_list_graph_predecessors);
// ---------- 3 测试矩阵图BellmanFord ----------
cout << endl << endl << "---------- 矩阵图 ----------" << endl;
MatrixGraph<string, double> matrix_graph(10, 1000, edges, vertices); // 构造matrix_undirected_graph(矩阵无向图)
double matrix_graph_min_dists[10]; // 声明matrix_graph_min_distances(邻接表图最短路径数组)
int matrix_graph_predecessors[10]; // 声明matrix_graph_predecessors(最短路径前驱结点数组)
// 调用BellmanFord函数, 求"北京"到其他各点的最短路径
BellmanFord(matrix_graph, vertices[0], matrix_graph_min_dists, matrix_graph_predecessors);
// 调用PrintSingleSourceShortestPath打印"北京"到各城市的最短路径
PrintSingleSourceShortestPath(matrix_graph, vertices[0], matrix_graph_min_dists, matrix_graph_predecessors);
cout<<"-------------------------------------------------------------"<<endl<<endl;
}
/*!
* @brief **测试-图-最短路径Floyd**
* @note
* 测试-图-最短路径Floyd
* ------------------
* ------------------
*
* ------------------
* + **1 初始化图的基本信息**\n\n
* 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)\n
* 初始化边信息\n\n
* + **2 测试邻接表有向图Floyd**\n\n
* 构造adjacency_list_directed_graph(邻接表有向图)\n\n
* 初始化adj_list_min_distances(邻接表最短路径二维向量)\n
* 初始化adj_list_predecessors(最短路径前驱结点二维向量)\n\n
* 调用Floyd执行弗洛伊德算法\n
* 打印多源最短路径\n\n
* + **3 测试矩阵有向图Floyd**\n\n
* 构造matrix_directed_graph(矩阵有向图)\n\n
* 初始化matrix_min_distances(矩阵最短路径二维向量)\n
* 初始化matrix_predecessors(最短路径前驱结点二维向量)\n\n
* 调用Floyd执行弗洛伊德算法\n
* 打印多源最短路径\n\n
*
*
* ------------------
*/
void TestFloyd() {
cout<<endl;
cout<<"|------------------------ CyberDash ------------------------|"<<endl;
cout<<"| Test Floyd-Warshall |"<<endl;
cout<<"| 测试-图-最短路径Floyd |"<<endl;
cout<<"| |"<<endl;
cout<<"| 北京 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 0.1 0.12 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 上海---0.01---广州 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 0.13 0.14 0.05 0.17 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 杭州--0.09-- 深圳 --0.11--成都 |"<<endl;
cout<<endl;
// ---------- 1 初始化图的基本信息 ----------
// 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)
vector<string> vertices{ "北京", "上海", "广州", "深圳", "杭州", "成都" };
// 初始化边信息
unsigned int edge_count = 9;
vector<string> starting_vertices{ "北京", "北京", "上海", "上海", "上海", "广州", "广州", "深圳", "深圳" };
vector<string> ending_vertices { "上海", "广州", "广州", "深圳", "杭州", "深圳", "成都", "杭州", "成都" };
vector<double> weights { 0.1, 0.12, 0.01, 0.14, 0.13, 0.05, 0.17, 0.09, 0.11 };
vector<Edge<string, double> > edges;
for (unsigned int i = 0; i < edge_count; i++) {
Edge<string, double> edge(starting_vertices[i], ending_vertices[i], weights[i]);
edges.push_back(edge);
}
// ---------- 2 测试邻接表有向图Floyd ----------
cout << "---------- 邻接表图 ----------" << endl;
// 构造adjacency_list_directed_graph(邻接表有向图)
AdjacencyListGraph<string, double> adjacency_list_directed_graph(Graph<string, double>::DIRECTED, 10, 1000, edges, vertices);
// 初始化adj_list_min_distances(邻接表最短路径二维向量)
vector<vector<double> > adj_list_min_distances(adjacency_list_directed_graph.VertexCount(),
vector<double>(adjacency_list_directed_graph.VertexCount()));
// 初始化adj_list_predecessors(最短路径前驱结点二维向量)
vector<vector<int> > adj_list_predecessors(adjacency_list_directed_graph.VertexCount(),
vector<int>(adjacency_list_directed_graph.VertexCount()));
// 调用Floyd执行弗洛伊德算法
Floyd(adjacency_list_directed_graph, adj_list_min_distances, adj_list_predecessors);
// 打印多源最短路径
PrintMultipleSourceShortestPath(adjacency_list_directed_graph,
adj_list_min_distances,
adj_list_predecessors);
// ---------- 3 测试矩阵有向图Floyd ----------
cout << endl << endl << "---------- 矩阵图 ----------" << endl;
// 构造matrix_directed_graph(矩阵有向图)
MatrixGraph<string, double> matrix_directed_graph(Graph<string, double>::DIRECTED, 10, 1000, edges, vertices); // 构造矩阵图
// 初始化matrix_min_distances(矩阵最短路径二维向量)
vector<vector<double> > matrix_min_distances(matrix_directed_graph.VertexCount(),
vector<double>(matrix_directed_graph.VertexCount()));
// 初始化matrix_predecessors(最短路径前驱结点二维向量)
vector<vector<int> > matrix_predecessors(matrix_directed_graph.VertexCount(),
vector<int>(matrix_directed_graph.VertexCount()));
// 调用Floyd执行弗洛伊德算法
Floyd(matrix_directed_graph, matrix_min_distances, matrix_predecessors);
// 打印多源最短路径
PrintMultipleSourceShortestPath(matrix_directed_graph,
matrix_min_distances,
matrix_predecessors);
cout << "-------------------------------------------------------------" << endl << endl;
}
/*!
* @brief **测试-图-拓扑排序**
* @note
* 测试-图-拓扑排序
* --------------
* --------------
*
* --------------
* + **1 初始化图的基本信息**\n\n
* 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)\n
* 初始化边信息\n\n
* + **2 测试邻接表有向图拓扑排序**\n\n
* 初始化adj_list_directed_graph(邻接表有向图)\n
* 打印拓扑排序结果\n\n
* + **3 测试矩阵无向图拓扑排序**\n\n
* 初始化matrix_undirected_graph(矩阵无向图)\n
* 打印拓扑排序结果\n
*
*
* --------------
*/
void TestTopologicalSort() {
cout<<endl;
cout<<"|------------------------ CyberDash ------------------------|"<<endl;
cout<<"| Test Graph TopologySort |"<<endl;
cout<<"| 测试-图-拓扑排序 |"<<endl;
cout<<"| |"<<endl;
cout<<"| 北京 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 0.1 0.12 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 上海---0.01---广州 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 0.13 0.14 0.05 0.17 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 杭州--0.09-- 深圳 --0.11--成都 |"<<endl;
cout<<endl;
// ---------- 1 初始化图的基本信息 ----------
// 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)
vector<string> vertices{ "北京", "上海", "广州", "深圳", "杭州", "成都" };
// 初始化边信息
unsigned int edge_count = 9;
vector<string> starting_vertices{ "北京", "北京", "上海", "上海", "上海", "广州", "广州", "深圳", "深圳" };
vector<string> ending_vertices { "上海", "广州", "广州", "深圳", "杭州", "深圳", "成都", "杭州", "成都" };
vector<double> weights { 0.1, 0.12, 0.01, 0.14, 0.13, 0.05, 0.17, 0.09, 0.11 };
vector<Edge<string, double> > edges;
for (unsigned int i = 0; i < edge_count; i++) {
Edge<string, double> edge(starting_vertices[i], ending_vertices[i], weights[i]);
edges.push_back(edge);
}
vector<string> topology_sorted_list;
// ---------- 2 测试邻接表有向图拓扑排序 ----------
cout << "---------- 邻接表图 ----------" << endl;
// 初始化adj_list_directed_graph(邻接表有向图)
AdjacencyListGraph<string, double> adj_list_directed_graph(Graph<string, double>::DIRECTED, 10, 1000, edges, vertices);
bool res = TopologicalSort(adj_list_directed_graph, vertices[0], topology_sorted_list);
if (!res) {
cout<<"拓扑排序失败"<<endl;
return;
}
// 打印拓扑排序结果
for (auto iter = topology_sorted_list.begin(); iter != topology_sorted_list.end(); iter++) {
cout<<*iter<<' ';
}
cout<<endl;
topology_sorted_list.clear(); // 清空topology_sorted_list
// ---------- 3 测试矩阵无向图拓扑排序 ----------
cout << endl << endl << "---------- 矩阵图 ----------" << endl;
// 初始化matrix_undirected_graph(矩阵无向图)
MatrixGraph<string, double> matrix_undirected_graph(Graph<string, double>::UNDIRECTED, 10, 1000, edges, vertices);
res = TopologicalSort(matrix_undirected_graph, vertices[0], topology_sorted_list);
if (!res) {
cout<<"拓扑排序失败"<<endl;
}
// 打印拓扑排序结果
for (auto iter = topology_sorted_list.begin(); iter != topology_sorted_list.end(); iter++) {
cout<<*iter<<' ';
}
cout<<endl;
cout << "-------------------------------------------------------------" << endl << endl;
}
/*!
* @brief **测试-图-关键路径**
* @note
* 测试-图-关键路径
* --------------
* --------------
*
* --------------
* + **1 初始化图的基本信息**\n\n
* 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)\n
* 初始化边信息\n\n
* + **2 测试邻接表有向图关键路径**\n\n
* 初始化adj_list_directed_graph(邻接表有向图)\n
* 调用GetCriticalPath, 求"北京"到各城市的关键路径\n
* 打印各关键路径\n\n
* + **3 测试矩阵有向图关键路径**\n\n
* 初始化matrix_directed_graph(矩阵有向图)\n
* 调用GetCriticalPath, 求"北京"到各城市的关键路径\n
* 打印各关键路径\n
*
*
* --------------
*/
void TestCriticalPaths() {
cout<<endl;
cout<<"|------------------------ CyberDash ------------------------|"<<endl;
cout<<"| Test Graph CriticalPaths |"<<endl;
cout<<"| 测试-图-关键路径 |"<<endl;
cout<<"| |"<<endl;
cout<<"| 北京 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 0.1 0.12 |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| / \\ |"<<endl;
cout<<"| 上海---0.01---广州 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 0.13 0.14 0.05 0.17 |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| / \\ / \\ |"<<endl;
cout<<"| 杭州--0.09-- 深圳 --0.11--成都 |"<<endl;
cout<<endl;
// ---------- 1 初始化图的基本信息 ----------
// 初始化结点信息(北京, 上海, 广州, 深圳, 杭州, 成都 6座城市)
vector<string> vertices{ "北京", "上海", "广州", "深圳", "杭州", "成都" };
// 初始化边信息
unsigned int edge_count = 9;
vector<string> starting_vertices{ "北京", "北京", "上海", "上海", "上海", "广州", "广州", "深圳", "深圳" };
vector<string> ending_vertices { "上海", "广州", "广州", "深圳", "杭州", "深圳", "成都", "杭州", "成都" };
vector<double> weights { 0.1, 0.12, 0.01, 0.14, 0.13, 0.05, 0.17, 0.09, 0.11 };
vector<Edge<string, double> > edges;
for (unsigned int i = 0; i < edge_count; i++) {
Edge<string, double> edge(starting_vertices[i], ending_vertices[i], weights[i]);
edges.push_back(edge);
}
string starting_vertex = vertices[0];
// ---------- 2 测试邻接表有向图关键路径 ----------
cout << "---------- 邻接表图 ----------" << endl;
// 初始化adj_list_directed_graph(邻接表有向图)
AdjacencyListGraph<string, double> adj_list_directed_graph(Graph<string, double>::DIRECTED, 10, 1000, edges, vertices);
// 调用GetCriticalPath, 求"北京"到各城市的关键路径
vector<double> critical_paths = GetCriticalPath(adj_list_directed_graph, starting_vertex);
// 打印各关键路径
for (unsigned int i = 0; i < critical_paths.size(); i++) {
cout<<"北京 ---> "<<vertices[i]<<" 关键路径长度: "<<critical_paths[i]<<endl;
}
// ---------- 3 测试矩阵有向图关键路径 ----------
cout << endl << endl << "---------- 矩阵图 ----------" << endl;
// 初始化matrix_directed_graph(矩阵有向图)
MatrixGraph<string, double> matrix_directed_graph(Graph<string, double>::DIRECTED, 10, 1000, edges, vertices);
// 调用GetCriticalPath, 求"北京"到各城市的关键路径
critical_paths = GetCriticalPath(matrix_directed_graph, starting_vertex);
// 打印各关键路径
for (unsigned int i = 0; i < critical_paths.size(); i++) {
cout<<"北京 ---> "<<vertices[i]<<" 关键路径长度: "<<critical_paths[i]<<endl;
}
cout << "-------------------------------------------------------------" << endl << endl;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/hangerfor/data-structures-cpp.git
git@gitee.com:hangerfor/data-structures-cpp.git
hangerfor
data-structures-cpp
数据结构(C++模板实现)
master

Search