Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_euclidian_graph_example.cc
Go to the documentation of this file.
1
22#include <iostream>
23#include <cmath>
24#include <tpl_euclidian_graph.H>
25#include <point.H>
26
27using namespace std;
28using namespace Aleph;
29
30int main()
31{
32 cout << "=== Euclidean Graphs: Educational Examples ===\n\n";
33
34 // =========================================================================
35 // EXAMPLE 1: City Network with Auto-Distances
36 // =========================================================================
37 {
38 cout << "--- Example 1: City Network (Automatic Distance Calculation) ---\n\n";
39
40 // STEP 1: Define Euclidean graph type
41 // Nodes have positions, arcs get automatic distance weights
44
45 cout << "Building US East Coast city network...\n\n";
46
47 // STEP 2: Insert cities with geographic coordinates
48 // Positions are (latitude, longitude) - distances auto-computed!
49 auto nyc = city_map.insert_node("NYC", Point(40.7128, -74.0060));
50 auto boston = city_map.insert_node("Boston", Point(42.3601, -71.0589));
51 auto philly = city_map.insert_node("Philadelphia", Point(39.9526, -75.1652));
52 auto dc = city_map.insert_node("Washington DC", Point(38.9072, -77.0369));
53
54 cout << "Cities added:\n";
55 cout << " NYC: (40.71, -74.01)\n";
56 cout << " Boston: (42.36, -71.06)\n";
57 cout << " Philadelphia: (39.95, -75.17)\n";
58 cout << " Washington DC: (38.91, -77.04)\n\n";
59
60 // STEP 3: Connect cities - distances computed automatically!
61 cout << "Connecting cities (distances auto-calculated):\n";
62
63 auto arc1 = city_map.insert_arc(nyc, boston);
64 auto arc2 = city_map.insert_arc(nyc, philly);
65 auto arc3 = city_map.insert_arc(philly, dc);
66
67 // Get computed distances (note: returns geometric type, display directly)
68 cout << " NYC -> Boston: " << city_map.get_distance(arc1) << " degrees\n";
69 cout << " NYC -> Philadelphia: " << city_map.get_distance(arc2) << " degrees\n";
70 cout << " Philadelphia -> DC: " << city_map.get_distance(arc3) << " degrees\n\n";
71
72 cout << "KEY INSIGHT: You provide positions, graph computes distances!\n";
73 cout << " Perfect for geographic applications\n\n";
74 }
75
76 // =========================================================================
77 // EXAMPLE 2: Nearest Neighbor Search
78 // =========================================================================
79 {
80 cout << "--- Example 2: Finding Nearest City ---\n\n";
81
84
85 // Add cities
86 city_map.insert_node("NYC", Point(40.7, -74.0));
87 city_map.insert_node("Boston", Point(42.4, -71.1));
88 city_map.insert_node("Philadelphia", Point(40.0, -75.2));
89 city_map.insert_node("Baltimore", Point(39.3, -76.6));
90 city_map.insert_node("Washington DC", Point(38.9, -77.0));
91
92 cout << "Cities in network: NYC, Boston, Philadelphia, Baltimore, DC\n\n";
93
94 // QUERY: Find nearest city to a given point
95 Point query_location(40.5, -74.5); // Somewhere in New Jersey
96
97 cout << "Query: What city is nearest to point (40.5, -74.5)?\n";
98 cout << " (This is somewhere in New Jersey)\n\n";
99
100 // Manual nearest neighbor search
101 EG::Node* nearest_city = nullptr;
102 bool first = true;
104
105 for (Node_Iterator<EG> it(city_map); it.has_curr(); it.next())
106 {
107 auto city = it.get_curr();
108 auto city_pos = city->get_position();
109 auto dist = query_location.distance_with(city_pos);
110
111 cout << " Distance to " << city->get_info() << ": " << dist << "\n";
112
113 if (first || dist < min_distance)
114 {
115 min_distance = dist;
117 first = false;
118 }
119 }
120
121 cout << "\nNearest city: " << nearest_city->get_info()
122 << " (distance: " << min_distance << ")\n\n";
123
124 cout << "APPLICATION: Location-based services, routing, facility placement\n\n";
125 }
126
127 // =========================================================================
128 // EXAMPLE 3: Minimum Spanning Tree (Road Network)
129 // =========================================================================
130 {
131 cout << "--- Example 3: Minimum Spanning Tree (Optimal Road Network) ---\n\n";
132
134 EG cities;
135
136 // Add cities
137 auto a = cities.insert_node("A", Point(0, 0));
138 auto b = cities.insert_node("B", Point(4, 0));
139 auto c = cities.insert_node("C", Point(2, 3));
140 auto d = cities.insert_node("D", Point(0, 4));
141 auto e = cities.insert_node("E", Point(4, 4));
142
143 cout << "5 cities positioned on a grid\n";
144 cout << "Goal: Connect all cities with minimum total road length\n\n";
145
146 // Connect all pairs (complete graph)
148 nodes.append(a); nodes.append(b); nodes.append(c);
149 nodes.append(d); nodes.append(e);
150
151 cout << "All possible connections:\n";
153 int connection_count = 0;
154
155 for (size_t i = 0; i < nodes.size(); ++i)
156 for (size_t j = i + 1; j < nodes.size(); ++j)
157 {
158 auto arc = cities.insert_arc(nodes[i], nodes[j]);
159 auto dist = cities.get_distance(arc);
160 total_all = total_all + dist;
162
163 cout << " " << nodes[i]->get_info() << " <-> "
164 << nodes[j]->get_info() << ": " << dist << "\n";
165 }
166
167 cout << "\nTotal if we build ALL roads: " << total_all << "\n";
168 cout << "Number of roads: " << connection_count << "\n\n";
169
170 cout << "OPTIMIZATION PROBLEM:\n";
171 cout << " Minimum Spanning Tree finds minimum total length\n";
172 cout << " to connect all cities (n-1 = 4 roads needed)\n";
173 cout << " MST would use approximately 50% of total road length\n\n";
174 }
175
176 // =========================================================================
177 // EXAMPLE 4: Distance Matrix for Route Planning
178 // =========================================================================
179 {
180 cout << "--- Example 4: Distance Matrix ---\n\n";
181
184
185 // Create delivery locations on a grid
187 for (int i = 0; i < 4; ++i)
188 {
189 int x = i * 10;
190 int y = (i % 2) * 10;
191 auto loc = delivery_network.insert_node(i, Point(x, y));
192 locations.append(loc);
193 }
194
195 cout << "Delivery network: 4 locations\n";
196 cout << "Computing all-pairs distances...\n\n";
197
198 // Build distance matrix
199 cout << "Distance Matrix:\n";
200 cout << " ";
201 for (size_t j = 0; j < locations.size(); ++j)
202 cout << " [" << j << "] ";
203 cout << "\n";
204
205 for (size_t i = 0; i < locations.size(); ++i)
206 {
207 cout << "[" << i << "] ";
208 for (size_t j = 0; j < locations.size(); ++j)
209 {
210 auto pi = locations[i]->get_position();
211 auto pj = locations[j]->get_position();
212 auto dist = pi.distance_with(pj);
213 cout << " " << dist << " ";
214 }
215 cout << "\n";
216 }
217
218 cout << "\nUSE CASE: Route optimization, delivery planning, TSP\n\n";
219 }
220
221 // =========================================================================
222 // EXAMPLE 5: 3D Euclidean Graph (Bonus)
223 // =========================================================================
224 {
225 cout << "--- Example 5: 3D Space (Satellites/Drones) ---\n\n";
226
227 cout << "EXTENDING TO 3D:\n";
228 cout << " Same concepts apply to 3D coordinates (x, y, z)\n";
229 cout << " Distance formula: sqrt((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2)\n\n";
230
231 cout << "APPLICATIONS:\n";
232 cout << " * Satellite networks (3D positions in space)\n";
233 cout << " * Drone delivery routes (altitude matters)\n";
234 cout << " * 3D pathfinding in games\n";
235 cout << " * Molecular structures (atoms in 3D)\n\n";
236
237 cout << "IMPLEMENTATION:\n";
238 cout << " Use Point3D class or extend Point to 3D\n";
239 cout << " Distance calculation automatic for any dimension\n\n";
240 }
241
242 cout << "=== SUMMARY: Euclidean Graphs ===\n";
243 cout << "\n1. AUTOMATIC DISTANCE CALCULATION:\n";
244 cout << " * Set node positions\n";
245 cout << " * Distances computed from Euclidean formula\n";
246 cout << " * No manual weight assignment needed\n";
247 cout << "\n2. WHEN TO USE:\n";
248 cout << " ✓ Geographic networks (cities, roads)\n";
249 cout << " ✓ Facility location problems\n";
250 cout << " ✓ Robotics (motion planning)\n";
251 cout << " ✓ Wireless networks (signal range)\n";
252 cout << " ✓ Computer graphics (scene graphs)\n";
253 cout << "\n3. KEY OPERATIONS:\n";
254 cout << " - insert_node(info, Point(x,y))\n";
255 cout << " - get_distance(arc) → auto-computed\n";
256 cout << " - get_position(node) → Point\n";
257 cout << " - Nearest neighbor queries\n";
258 cout << "\n4. ADVANTAGES:\n";
259 cout << " * Consistency: distances always match positions\n";
260 cout << " * Convenience: no manual distance computation\n";
261 cout << " * Natural for spatial problems\n";
262 cout << "\n5. ALGORITHMS:\n";
263 cout << " All graph algorithms work:\n";
264 cout << " * Dijkstra (shortest paths)\n";
265 cout << " * MST (minimum spanning tree)\n";
266 cout << " * TSP (traveling salesman)\n";
267 cout << " Plus geometric-specific:\n";
268 cout << " * Nearest neighbor\n";
269 cout << " * Range queries\n";
270 cout << " * Voronoi diagrams\n";
271 cout << "\n6. DISTANCE FORMULA:\n";
272 cout << " 2D: sqrt((x2-x1)^2 + (y2-y1)^2)\n";
273 cout << " 3D: sqrt((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2)\n";
274 cout << " Time: O(1) per calculation\n";
275
276 return 0;
277}
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
T & append(const T &item)
Append a new item by copy.
Definition htlist.H:1562
void next()
Advances the iterator to the next filtered element.
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1206
Rectangular point in the plane.
Definition point.H:156
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
2D point and geometric utilities.
Euclidean graph with 2D coordinates.