199using namespace Aleph;
203 for (
int i = 1; i <
argc; ++i)
204 if (std::strcmp(
argv[i], flag) == 0)
212 <<
" [--components] [--scc] [--spanning-tree] [--network-analysis] [--all] [--help]\n";
213 cout <<
"\nIf no flags are given, all demos are executed.\n";
227 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
228 if (it.get_curr()->get_info() == name)
229 return it.get_curr();
234void add_edge(
GT& g,
const string& src,
const string& tgt,
int weight = 1)
245 cout <<
" Vertices: ";
246 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
247 cout << it.get_curr()->get_info() <<
" ";
248 cout <<
"\n Edges:\n";
249 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne()) {
250 auto arc = it.get_curr();
264 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
265 cout <<
"║ EXAMPLE 1: Connected Components (Undirected) ║\n";
266 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
268 cout <<
"A connected component is a maximal set of vertices where\n";
269 cout <<
"every pair of vertices is connected by a path.\n\n";
299 cout <<
" Component " << i++ <<
": ";
300 for (
auto it =
comp.get_node_it(); it.has_curr(); it.next_ne())
301 cout << it.get_curr()->get_info() <<
" ";
302 cout <<
"(size: " <<
comp.get_num_nodes() <<
")\n";
305 cout <<
"\n--- Testing connectivity ---\n\n";
311 cout <<
" Graph is fully connected: " << (is_connected ?
"YES" :
"NO") <<
"\n";
322 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
323 cout <<
"║ EXAMPLE 2: Strongly Connected Components (Directed) ║\n";
324 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
326 cout <<
"A strongly connected component (SCC) is a maximal set where\n";
327 cout <<
"every vertex can reach every other vertex (bidirectional paths).\n\n";
353 cout <<
"Tarjan's Algorithm found " <<
sccs.
size() <<
" SCCs:\n\n";
357 cout <<
" SCC " << i++ <<
": ";
358 for (
auto node :
scc)
359 cout << node->get_info() <<
" ";
364 cout <<
"\n--- Cycle analysis ---\n\n";
365 cout <<
" Graph has cycles: " << (
tarjan.has_cycle(g) ?
"YES" :
"NO") <<
"\n";
366 cout <<
" Graph is a DAG: " << (
tarjan.is_dag(g) ?
"YES" :
"NO") <<
"\n";
368 cout <<
"\n--- Why SCCs matter ---\n\n";
369 cout <<
" • Compiler optimization: basic blocks\n";
370 cout <<
" • Web crawling: identify tightly connected pages\n";
371 cout <<
" • Social networks: find communities\n";
372 cout <<
" • 2-SAT problem solving\n";
382 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
383 cout <<
"║ EXAMPLE 3: Spanning Tree ║\n";
384 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
386 cout <<
"A spanning tree connects all vertices using exactly V-1 edges.\n";
387 cout <<
"It contains no cycles and forms a tree structure.\n\n";
400 cout <<
"Original graph:\n";
411 cout <<
"DFS Spanning Tree:\n";
414 cout <<
" Tree edges:\n";
415 for (
auto it = tree.
get_arc_it(); it.has_curr(); it.next_ne()) {
416 auto arc = it.get_curr();
421 cout <<
"\n--- Properties of spanning tree ---\n\n";
424 cout <<
" • Contains no cycles\n";
425 cout <<
" • Unique path between any two vertices\n";
435 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
436 cout <<
"║ EXAMPLE 4: Network Analysis Application ║\n";
437 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
439 cout <<
"Scenario: Analyzing a computer network for redundancy.\n\n";
461 cout <<
"Network analysis results:\n\n";
462 cout <<
" Total devices: " <<
network.get_num_nodes() <<
"\n";
468 cout <<
" Devices: ";
469 for (
auto it =
comp.get_node_it(); it.has_curr(); it.next_ne())
470 cout << it.get_curr()->get_info() <<
" ";
472 cout <<
" Connections: " <<
comp.get_num_arcs() <<
"\n";
475 if (
comp.get_num_nodes() > 1 &&
comp.get_num_arcs() ==
comp.get_num_nodes() - 1)
476 cout <<
" ⚠️ No redundancy (tree structure)\n";
477 else if (
comp.get_num_arcs() >
comp.get_num_nodes() - 1)
478 cout <<
" ✓ Has redundancy (multiple paths)\n";
480 cout <<
" • Single device\n";
484 cout <<
"Recommendation: Connect segments for full network connectivity.\n";
506 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
507 cout <<
"║ Graph Connectivity Analysis in Aleph-w Library ║\n";
509 cout <<
"║ Aleph-w Library - https://github.com/lrleon/Aleph-w ║\n";
510 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n";
528 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
529 cout <<
"║ Summary ║\n";
530 cout <<
"╠══════════════════════════════════════════════════════════════════╣\n";
531 cout <<
"║ Unconnected_Components: Find connected components (undirected) ║\n";
532 cout <<
"║ Tarjan_Connected: Find SCCs (directed) - O(V+E) ║\n";
533 cout <<
"║ Find_DFS_Spanning_Tree: Build spanning tree via DFS ║\n";
534 cout <<
"║ Find_BFS_Spanning_Tree: Build spanning tree via BFS ║\n";
535 cout <<
"║ Test_Connectivity: Check if graph is connected ║\n";
537 cout <<
"║ All algorithms run in O(V + E) time complexity. ║\n";
538 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
Tarjan's algorithm for strongly connected components.
Generic directed graph (digraph) wrapper template.
Dynamic singly linked list with functional programming support.
Compute a depth-first spanning tree of a graph.
size_t size() const noexcept
Count the number of elements of the list.
Graph implemented with double-linked adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Computes strongly connected components (SCCs) in a directed graph using Tarjan's algorithm.
Compute the connected components of a graph.
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
constexpr size_t get_num_arcs() const noexcept
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Determines if a graph g is connected.
void demo_spanning_tree()
void demo_strongly_connected()
void demo_connected_components()
void print_graph(GT &g, const string &title)
GT::Node * find_or_create(GT &g, const string &name)
static void usage(const char *prog)
static bool has_flag(int argc, char *argv[], const char *flag)
void add_edge(GT &g, const string &src, const string &tgt, int weight=1)
void demo_network_analysis()
Main namespace for Aleph-w library functions.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Arc of graph implemented with double-linked adjacency lists.
Graph connectivity and connected components.
Generic graph and digraph implementations.
Spanning tree generation algorithms.
Graph connectivity testing.