C++ Library Extensions 2022.12.09
To help learn modern C++ programming
061-graph03.cpp
Go to the documentation of this file.
1#include <tpf_graph.hpp>
2
6auto nl = tpf::nl;
7
8// Graph Data Structure 4. Dijkstra’s Shortest Path Algorithm
9// https://youtu.be/pVfj6mxhdMw
11{
14
16 using path_info = graph::path_info;
17
18 graph g;
19
20 try
21 {
22 g.emplace_back_node("A", 1, 3);
23 g.emplace_back_node("B", 0, 2, 3, 4);
24 g.emplace_back_node("C", 1, 4);
25 g.emplace_back_node("D", 0, 1, 4);
26 g.emplace_back_node("E", 3, 1, 2);
27
28 g.integrity_check_nodes_throws();
29
30 g.emplace_back_edge(0, 1, 6);
31 g.emplace_back_edge(0, 3, 1);
32
33 g.emplace_back_edge(1, 0, 6);
34 g.emplace_back_edge(1, 2, 5);
35 g.emplace_back_edge(1, 3, 2);
36 g.emplace_back_edge(1, 4, 2);
37
38 g.emplace_back_edge(2, 1, 5);
39 g.emplace_back_edge(2, 4, 5);
40
41 g.emplace_back_edge(3, 0, 1);
42 g.emplace_back_edge(3, 1, 2);
43 g.emplace_back_edge(3, 4, 1);
44
45 g.emplace_back_edge(4, 1, 2);
46 g.emplace_back_edge(4, 2, 5);
47
48 }
49 catch(const std::exception& e)
50 {
51 stream << e << endl;
52 return;
53 }
54
55 stream << g.shortest_path_report("A", "C") << endl;
56
57}
58
59// Dijkstra's Algorithm by Nathaniel Fan
60// https://www.youtube.com/watch?v=gdmfOwyQlcI
62{
65
67 using path_info = graph::path_info;
68
69 graph g;
70
71 try
72 {
73 g.emplace_back_node("A", 1, 2, 4);
74 g.emplace_back_node("B", 0, 2, 3);
75 g.emplace_back_node("C", 0, 1, 3, 4);
76 g.emplace_back_node("D", 1, 2, 4, 5, 6);
77 g.emplace_back_node("E", 0, 2, 3, 6);
78 g.emplace_back_node("F", 3, 6);
79 g.emplace_back_node("G", 3, 4, 5);
80
81 g.integrity_check_nodes_throws();
82
83 g.emplace_back_edge(0, 1, 4);
84 g.emplace_back_edge(0, 2, 3);
85 g.emplace_back_edge(0, 4, 7);
86
87 g.emplace_back_edge(1, 0, 4);
88 g.emplace_back_edge(1, 2, 6);
89 g.emplace_back_edge(1, 3, 5);
90
91 g.emplace_back_edge(2, 0, 3);
92 g.emplace_back_edge(2, 1, 6);
93 g.emplace_back_edge(2, 3, 11);
94 g.emplace_back_edge(2, 4, 8);
95
96 g.emplace_back_edge(3, 1, 5);
97 g.emplace_back_edge(3, 2, 11);
98 g.emplace_back_edge(3, 4, 2);
99 g.emplace_back_edge(3, 5, 2);
100 g.emplace_back_edge(3, 6, 10);
101
102 g.emplace_back_edge(4, 0, 7);
103 g.emplace_back_edge(4, 2, 8);
104 g.emplace_back_edge(4, 3, 2);
105 g.emplace_back_edge(4, 6, 5);
106
107 g.emplace_back_edge(5, 3, 2);
108 g.emplace_back_edge(5, 6, 3);
109
110 g.emplace_back_edge(6, 5, 3);
111 g.emplace_back_edge(6, 3, 10);
112 g.emplace_back_edge(6, 4, 5);
113
114 }
115 catch(const std::exception& e)
116 {
117 stream << e << endl;
118 return;
119 }
120
121 stream << g.shortest_path_report("A", "G") << endl;
122
123}
124
125int main()
126{
127 // test_Dijkstra_shortest_path_by_Computer_Science();
128
130}
typename graph_t::visit_mode visit_mode
Definition: 060-graph02.cpp:8
auto endL
Definition: 061-graph03.cpp:5
void test_dijksta_by_Nathaniel_Fan()
Definition: 061-graph03.cpp:61
tpf::sstream stream
Definition: 061-graph03.cpp:3
void test_Dijkstra_shortest_path_by_Computer_Science()
Definition: 061-graph03.cpp:10
auto endl
Definition: 061-graph03.cpp:4
auto nl
Definition: 061-graph03.cpp:6
int main()
graph< DirectedEdge, EdgePlurality, ElementType, NodeIndexType, EdgeWeightType, NodeContainerType, IndexContainerType, EdgeContainerType > graph_t
Definition: tpf_graph.hpp:1341
constexpr auto endL
Definition: tpf_output.hpp:974
constexpr auto endl
Definition: tpf_output.hpp:973
constexpr auto nl
Definition: tpf_output.hpp:971
static constexpr int undirected_weighted
Definition: tpf_graph.hpp:47
static constexpr int single_edged
Definition: tpf_graph.hpp:66
Implements set operations.