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();
219 cout <<
"\n=== " << title <<
" ===" << endl;
223 cout <<
"\nAdjacency list:" << endl;
224 for (
auto nit = g.
get_node_it(); nit.has_curr(); nit.next())
226 auto* node = nit.get_curr();
227 cout <<
" " << node->get_info() <<
" -- ";
232 auto* arc = ait.get_curr();
234 if (not first) cout <<
", ";
235 cout << neighbor->get_info();
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: ";
256 if (visit_num > 0) cout <<
" -> ";
257 cout << node->get_info();
264 size_t visited = bfs(start, visitor);
267 cout <<
"Total nodes visited: " << visited << endl;
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: ";
284 if (visit_num > 0) cout <<
" -> ";
285 cout << node->get_info();
292 size_t visited = dfs(start, visitor);
295 cout <<
"Total nodes visited: " << visited << endl;
303 cout <<
"\n" << string(60,
'=') << endl;
304 cout <<
"BFS vs DFS: Side-by-Side Comparison" << endl;
305 cout << string(60,
'=') << 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;
327 cout << string(60,
'=') << 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: ";
349 if (not first) cout <<
" -> ";
350 cout << it.get_curr()->get_info();
357 cout <<
"No path found!" << endl;
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;
370 cout << string(60,
'=') << 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;
391 if (not first) cout <<
" -> ";
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;
409 cout << string(60,
'=') << endl;
414 string target =
"Eve";
416 cout <<
"\nSearching for '" << target <<
"' starting from 'Alice'..." << endl;
419 int nodes_visited = 0;
423 cout <<
" Visiting: " << node->get_info() << endl;
425 if (node->get_info() == target)
433 cout <<
"\nUsing BFS:" << endl;
434 found_node =
nullptr;
437 bfs(alice, search_visitor);
438 cout <<
"Nodes visited before finding '" << target <<
"': " << nodes_visited << endl;
440 cout <<
"\nUsing DFS:" << endl;
441 found_node =
nullptr;
444 dfs(alice, search_visitor);
445 cout <<
"Nodes visited before finding '" << target <<
"': " << nodes_visited << endl;
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;
457 cout << string(60,
'=') << endl;
478 cout <<
"\nFinding connected components using Unconnected_Components..." << endl;
484 int component_num = 0;
485 for (
auto & comp : components)
488 cout <<
"\nComponent " << component_num <<
": ";
491 for (
auto nit = comp.get_node_it(); nit.has_curr(); nit.next_ne())
493 if (not first) cout <<
", ";
494 cout << nit.get_curr()->get_info();
500 cout <<
"\nTotal components: " << components.size() << endl;
503int main(
int argc,
char* argv[])
507 TCLAP::CmdLine
cmd(
"BFS/DFS Graph Traversal Example",
' ',
"1.0");
509 TCLAP::SwitchArg compareArg(
"c",
"compare",
510 "Compare BFS and DFS on same graph",
false);
511 TCLAP::SwitchArg degreesArg(
"d",
"degrees",
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);
529 cmd.parse(argc, argv);
531 bool runCompare = compareArg.getValue();
532 bool runDegrees = degreesArg.getValue();
533 bool runPath = pathArg.getValue();
534 bool runEarly = earlyArg.getValue();
535 bool runComp = compArg.getValue();
536 bool runAll = allArg.getValue();
538 if (not runCompare
and not runDegrees
and not runPath
and
539 not runEarly
and not runComp)
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;
546 if (runAll or runCompare)
549 if (runAll or runDegrees)
552 if (runAll or runPath)
555 if (runAll or runEarly)
558 if (runAll or runComp)
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.
auto has_curr() const noexcept(noexcept(std::declval< I & >().has_curr())) -> decltype(std::declval< I & >().has_curr())
true if there is a 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.
Doubly-linked list (defined in tpl_dynList.H).
void next()
Advances the iterator to the next filtered element.
Performs a breadth-first search for a path between a pair of nodes.
Performs a depth-first search for a path between a pair of nodes.
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
Return the number of arcs of the graph.
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.
and
Check uniqueness with explicit hash + equality functors.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
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.