Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
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
Already have an account? Sign in
文件
master
Branches (3)
master
Prometheus
dev
master
Branches (3)
master
Prometheus
dev
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (3)
master
Prometheus
dev
test.cpp 53.44 KB
Copy Edit Raw Blame History
Y_Dash authored 2023年07月03日 18:41 +08:00 . 增加部分readme代码指向
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
/*!
* @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...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

通用的C++数据结构代码实现,使用模板。代码完整,注释齐全,可直接运行,可使用doxygen生成网页和PDF文档,跨Windows/Linux/Mac平台兼容
No labels
MulanPSL-2.0
Use MulanPSL-2.0
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 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
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /