|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Comprehensive example of Kosaraju's algorithm for SCCs. More...
#include <iostream>#include <iomanip>#include <string>#include <vector>#include <cstring>#include <tpl_graph.H>#include <kosaraju.H>#include <Tarjan.H>Go to the source code of this file.
Typedefs | |
| using | Graph = List_Digraph< Graph_Node< string >, Graph_Arc< int > > |
| using | Node = Graph::Node |
| using | Arc = Graph::Arc |
Functions | |
| Node * | find_node (Graph &g, const string &name) |
| void | print_graph (Graph &g) |
| void | example_basic_sccs () |
| void | example_lightweight_version () |
| void | example_strongly_connected () |
| void | example_dag () |
| void | example_comparison_tarjan () |
| void | example_applications () |
| static void | usage (const char *prog) |
| static bool | has_flag (int argc, char *argv[], const char *flag) |
| int | main (int argc, char *argv[]) |
Comprehensive example of Kosaraju's algorithm for SCCs.
This example demonstrates Kosaraju's algorithm for finding Strongly Connected Components (SCCs) in a directed graph. Kosaraju's algorithm is conceptually simpler than Tarjan's, using two DFS passes instead of one, but requiring a graph transpose.
In a directed graph, a strongly connected component (SCC) is a maximal set of vertices such that there is a path from every vertex to every other vertex in the set.
Key property: In an SCC, you can travel from any vertex to any other vertex (and back).
Graph: A → B → C → A, D → E → D
SCCs:
Kosaraju's algorithm works in two phases:
Key: We record when DFS finishes exploring a vertex (post-order).
Key: Process in reverse finish order, on reversed graph.
If vertex u can reach vertex v in graph G, then v can reach u in the transposed graph G^T (where all edges are reversed).
By processing vertices in decreasing finish order:
Note: Tarjan's algorithm is more space-efficient (no transpose needed)
| Aspect | Kosaraju's | Tarjan's |
|---|---|---|
| Passes | 2 DFS | 1 DFS |
| Graph transpose | Required | Not needed |
| Space | O(V+E) for transpose | O(V) |
| Implementation | Simpler | More complex |
| Performance | Slightly slower | Faster |
| Best for | Learning, simplicity | Production, efficiency |
This helps identify problematic circular dependencies.
Definition in file kosaraju_example.cc.
| using Arc = Graph::Arc |
Definition at line 243 of file kosaraju_example.cc.
| using Graph = List_Digraph<Graph_Node<string>, Graph_Arc<int> > |
Definition at line 241 of file kosaraju_example.cc.
| using Node = Graph::Node |
Definition at line 242 of file kosaraju_example.cc.
| void example_applications | ( | ) |
Definition at line 560 of file kosaraju_example.cc.
| void example_basic_sccs | ( | ) |
Definition at line 283 of file kosaraju_example.cc.
References GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), Aleph::kosaraju_connected_components(), Aleph::maps(), NODE_COOKIE, print_graph(), and Aleph::HTList::size().
| void example_comparison_tarjan | ( | ) |
Definition at line 521 of file kosaraju_example.cc.
References Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), and Aleph::maps().
| void example_dag | ( | ) |
Definition at line 476 of file kosaraju_example.cc.
References GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), Aleph::kosaraju_connected_components(), Aleph::maps(), print_graph(), and Aleph::HTList::size().
| void example_lightweight_version | ( | ) |
Definition at line 373 of file kosaraju_example.cc.
References Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), Aleph::kosaraju_connected_components(), Aleph::maps(), and Aleph::HTList::size().
| void example_strongly_connected | ( | ) |
Definition at line 425 of file kosaraju_example.cc.
References Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), Aleph::kosaraju_connected_components(), Aleph::maps(), print_graph(), and Aleph::HTList::size().
Definition at line 249 of file kosaraju_example.cc.
References GraphCommon< GT, Node, Arc >::get_node_it().
|
static |
Definition at line 579 of file kosaraju_example.cc.
References Aleph::maps().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 587 of file kosaraju_example.cc.
| void print_graph | ( | Graph & | g | ) |
Definition at line 257 of file kosaraju_example.cc.
References GraphCommon< GT, Node, Arc >::get_node_it(), GraphCommon< GT, Node, Arc >::get_num_arcs(), GraphCommon< GT, Node, Arc >::get_num_nodes(), GraphCommon< GT, Node, Arc >::get_out_it(), GraphCommon< GT, Node, Arc >::get_tgt_node(), and Aleph::maps().
Referenced by example_basic_sccs(), example_dag(), and example_strongly_connected().
|
static |
Definition at line 573 of file kosaraju_example.cc.