120#include <tclap/CmdLine.h>
123using namespace Aleph;
185 vector<Graph::Node*>
nodes;
186 for (
int i = 1; i <= 10; ++i)
208 for (
auto it = g.
get_node_it(); it.has_curr(); it.next())
209 if (it.get_curr()->get_info() == name)
210 return it.get_curr();
223 cout <<
"\nAdjacency list:" <<
endl;
226 auto* node =
nit.get_curr();
227 cout <<
" " << node->get_info() <<
" -- ";
232 auto* arc =
ait.get_curr();
247 cout <<
"\n--- BFS (Breadth-First Search) ---" <<
endl;
248 cout <<
"Uses: Queue (FIFO)" <<
endl;
249 cout <<
"Explores: Level by level" <<
endl;
250 cout <<
"Starting from: " << start->get_info() <<
endl;
252 cout <<
"\nVisit order: ";
257 cout << node->get_info();
275 cout <<
"\n--- DFS (Depth-First Search) ---" <<
endl;
276 cout <<
"Uses: Stack (LIFO)" <<
endl;
277 cout <<
"Explores: As deep as possible first" <<
endl;
278 cout <<
"Starting from: " << start->get_info() <<
endl;
280 cout <<
"\nVisit order: ";
285 cout << node->get_info();
303 cout <<
"\n" << string(60,
'=') <<
endl;
304 cout <<
"BFS vs DFS: Side-by-Side Comparison" <<
endl;
315 cout <<
"\n--- Analysis ---" <<
endl;
316 cout <<
"BFS visits nodes level by level: 1, then 2-3-4, then 5-6-7, etc." <<
endl;
317 cout <<
"DFS explores one branch completely before backtracking." <<
endl;
325 cout <<
"\n" << string(60,
'=') <<
endl;
326 cout <<
"Practical Example: Degrees of Separation (BFS)" <<
endl;
335 cout <<
"\nFinding shortest path from Alice to Henry..." <<
endl;
343 cout <<
"Path found! Degrees of separation: " << (path.
size() - 1) <<
endl;
344 cout <<
"Connection: ";
350 cout << it.get_curr()->get_info();
360 cout <<
"\nNote: BFS guarantees finding the shortest path (fewest edges)." <<
endl;
368 cout <<
"\n" << string(60,
'=') <<
endl;
369 cout <<
"Practical Example: Finding Any Path (DFS)" <<
endl;
377 cout <<
"\nFinding a path (any path) from Alice to Henry using DFS..." <<
endl;
384 cout <<
"Path found (may not be shortest): " <<
endl;
385 cout <<
"Length: " << (path.
size() - 1) <<
" edges" <<
endl;
392 cout << it.get_curr()->get_info();
398 cout <<
"\nNote: DFS doesn't guarantee shortest path, but uses less memory" <<
endl;
399 cout <<
" on deep graphs and can be useful for exploring all possibilities." <<
endl;
407 cout <<
"\n" << string(60,
'=') <<
endl;
408 cout <<
"Early Termination: Stop When Target Found" <<
endl;
416 cout <<
"\nSearching for '" <<
target <<
"' starting from 'Alice'..." <<
endl;
423 cout <<
" Visiting: " << node->get_info() <<
endl;
425 if (node->get_info() ==
target)
447 cout <<
"\nNote: BFS may find closer targets faster, DFS may explore more." <<
endl;
455 cout <<
"\n" << string(60,
'=') <<
endl;
456 cout <<
"Practical Example: Finding Connected Components" <<
endl;
478 cout <<
"\nFinding connected components using Unconnected_Components..." <<
endl;
491 for (
auto nit =
comp.get_node_it();
nit.has_curr();
nit.next_ne())
494 cout <<
nit.get_curr()->get_info();
507 TCLAP::CmdLine
cmd(
"BFS/DFS Graph Traversal Example",
' ',
"1.0");
510 "Compare BFS and DFS on same graph",
false);
512 "Show degrees of separation example",
false);
513 TCLAP::SwitchArg
pathArg(
"p",
"path",
514 "Show any path example (DFS)",
false);
515 TCLAP::SwitchArg
earlyArg(
"e",
"early",
516 "Show early termination example",
false);
517 TCLAP::SwitchArg
compArg(
"o",
"components",
518 "Show connected components example",
false);
519 TCLAP::SwitchArg
allArg(
"a",
"all",
520 "Run all demos",
false);
542 cout <<
"=== Graph Traversal: BFS vs DFS ===" <<
endl;
543 cout <<
"BFS: Breadth-First (Queue) - Finds shortest paths" <<
endl;
544 cout <<
"DFS: Depth-First (Stack) - Explores deeply first" <<
endl;
561 cout <<
"\n=== Summary ===" <<
endl;
562 cout <<
"BFS: Use when shortest path matters (unweighted graphs)" <<
endl;
563 cout <<
"DFS: Use for topological sort, cycle detection, or when any path suffices" <<
endl;
564 cout <<
"Both: O(V + E) time complexity" <<
endl;
568 catch (TCLAP::ArgException& e)
570 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() <<
endl;
WeightedDigraph::Node Node
void demo_dfs(Graph &g, Graph::Node *start)
Demonstrate DFS traversal.
Graph build_social_network()
Build a sample social network graph.
void demo_any_path()
Practical example: Finding any path (DFS)
void demo_bfs(Graph &g, Graph::Node *start)
Demonstrate BFS traversal.
void print_graph(Graph &g, const string &title)
Print graph structure.
void demo_connected_components()
Demonstrate finding connected components.
void demo_comparison()
Compare BFS and DFS on the same graph.
Graph build_tree_graph()
Build a tree-like graph for clear traversal comparison.
void demo_early_termination()
Demonstrate early termination.
Graph::Node * find_node(Graph &g, const string &name)
Find a node by name.
void demo_degrees_of_separation()
Practical example: Finding degrees of separation.
bool has_curr() const noexcept
Return true if the iterator has current item.
Dynamic queue of elements of generic type T based on single linked list.
Dynamic stack of elements of generic type T based on a singly linked list.
Dynamic singly linked list with functional programming support.
Busca en amplitud un camino entre un par de nodos.
Busca en profundidad un camino entre un par de nodos.
size_t size() const noexcept
Count the number of elements of the list.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
size_t size() const noexcept
Return the path length in nodes.
Iterator get_it() const
Returns an iterator on the path.
Compute the connected components of a graph.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
constexpr size_t get_num_arcs() const noexcept
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Traverse a graph depth-first or breadth-first and execute a visit function.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Graph traversal algorithms (DFS, BFS).
DynArray< Graph::Node * > nodes
Main namespace for Aleph-w library functions.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Arc of graph implemented with double-linked adjacency lists.
Node belonging to a graph implemented with a double linked adjacency list.
Filtered iterator of adjacent arcs of a node.
Graph connectivity and connected components.
Path finding algorithms in graphs.
Generic graph and digraph implementations.