C++ Library Extensions 2022.12.09
To help learn modern C++ programming
060-graph02.cpp
Go to the documentation of this file.
1#include "060-graph02.hpp"
2
5auto nl = tpf::nl;
6
9
10// In courtesy of
11// [Data Structure and Algorithm] Graph Search DFS, BFS Implmentation in Java
12// https://www.youtube.com/watch?reload=9&v=_hxFgg7TLZQ&list=PL1_C6uWTeBDFgjmvxNA7Okb_UQtEOGNfy&index=25
14{
15 graph_t g;
16
17 g.emplace_back(0, 1);
18 g.emplace_back(1, 0, 2, 3);
19 g.emplace_back(2, 1, 3, 4);
20 g.emplace_back(3, 1, 2, 4, 5);
21 g.emplace_back(4, 2, 3);
22 g.emplace_back(5, 3, 7, 6);
23 g.emplace_back(6, 5, 8);
24 g.emplace_back(7, 5);
25 g.emplace_back(8, 6);
26
27 stream << g.build_graph() << endl;
28}
29
31{
32 graph_t g;
33
34 g.emplace_back(0, 1);
35 g.emplace_back(1, 0, 2, 3);
36 g.emplace_back(2, 1, 3, 4);
37 g.emplace_back(3, 1, 2, 4, 5);
38 g.emplace_back(4, 2, 3);
39 g.emplace_back(5, 3, 7, 6);
40 g.emplace_back(6, 5, 8);
41 g.emplace_back(7, 5);
42 g.emplace_back(8, 6);
43
44// Breadth First Serach: { 0, 1, 2, 3, 4, 5, 6, 7, 8 }
45// Depth First Serach : { 1, 3, 5, 7, 6, 8, 4, 2, 0 }
46
47 stream << "Breadth First Serach: "
48 << g.traverse_bfs()<< endl;
49
50 stream << "Depth First Serach: "
51 << g.traverse_dfs()<< endl;
52
53}
54
56{
57 graph_t g;
58
59 g.emplace_back(0, 1);
60 g.emplace_back(1, 0, 2, 3);
61 g.emplace_back(2, 1, 3, 4);
62 g.emplace_back(3, 1, 2, 4, 5);
63 g.emplace_back(4, 2, 3);
64 g.emplace_back(5, 3, 7, 6);
65 g.emplace_back(6, 5, 8);
66 g.emplace_back(7, 5);
67 g.emplace_back(8, 6);
68
69// Breadth First Serach: { 0, 1, 2, 3, 4, 5, 6, 7, 8 }
70// Depth First Serach : { 1, 3, 5, 7, 6, 8, 4, 2, 0 }
71
72 stream << "Breadth First Serach: "<<endl;
73 stream<< g.build_graph(visit_mode::visit_bfs)<< endl;
74
75 stream << "Depth First Serach: "<<endl;
76 stream<< g.build_graph(visit_mode::visit_dfs)<< endl;
77
78}
79
80
81int main()
82{
83 // test_graph_graphic_presentation();
84
85 // test_graph_search_dfs_bfs();
86
88}
void test_graph_graphic_presentation()
Definition: 060-graph02.cpp:13
typename graph_t::visit_mode visit_mode
Definition: 060-graph02.cpp:8
tpf::sstream stream
Definition: 060-graph02.cpp:3
void test_graph_search_dfs_bfs_graphic_representation()
Definition: 060-graph02.cpp:55
void test_graph_search_dfs_bfs()
Definition: 060-graph02.cpp:30
auto endl
Definition: 060-graph02.cpp:4
auto nl
Definition: 060-graph02.cpp:5
int main()
Definition: 060-graph02.cpp:81
std::vector< index_t > traverse_dfs(index_t start=0)
std::string build_graph(visit_mode vmode=visit_mode::graph)
std::vector< index_t > traverse_bfs(index_t start=0)
void emplace_back(EleType &&value, Type &&index0, Types &&... indices)
constexpr auto endl
Definition: tpf_output.hpp:973
constexpr auto nl
Definition: tpf_output.hpp:971