|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Tarjan's Algorithm: Finding Strongly Connected Components. More...
#include <iostream>#include <iomanip>#include <string>#include <vector>#include <tpl_graph.H>#include <Tarjan.H>#include <tclap/CmdLine.h>Go to the source code of this file.
Typedefs | |
| using | SNode = Graph_Node< string > |
| using | SArc = Graph_Arc< int > |
| using | SDigraph = List_Digraph< SNode, SArc > |
Functions | |
| SDigraph | build_web_graph () |
| Build a sample web page link graph. | |
| SDigraph | build_module_graph () |
| Build a software module dependency graph. | |
| SDigraph | build_dag () |
| Build a simple graph with no cycles (DAG) | |
| SDigraph::Node * | find_node (SDigraph &g, const string &name) |
| Find a node by name. | |
| void | print_graph (SDigraph &g, const string &title) |
| Print the graph structure. | |
| void | demo_find_sccs (SDigraph &g, const string &description) |
| Demonstrate finding SCCs with Tarjan's algorithm. | |
| void | demo_cycle_detection (SDigraph &g, const string &description) |
| Demonstrate cycle detection. | |
| void | demo_web_communities () |
| Demonstrate practical application: web communities. | |
| void | demo_dependency_analysis () |
| Demonstrate practical application: dependency analysis. | |
| void | demo_dag_detection () |
| Demonstrate DAG detection. | |
| int | main (int argc, char *argv[]) |
Tarjan's Algorithm: Finding Strongly Connected Components.
This example demonstrates Tarjan's algorithm for finding strongly connected components (SCCs) in directed graphs. Tarjan's algorithm is one of the most efficient algorithms for this problem, using a single DFS pass.
A strongly connected component (SCC) is a maximal subset of vertices in a directed graph where:
Key insight: In an SCC, you can travel from any vertex to any other vertex (and back).
Graph: A → B → C → A, D → E → D
SCCs:
Note: In undirected graphs, SCCs are just connected components.
Tarjan's algorithm uses a single DFS pass with two key values:
index[v]**: Order in which vertex v was first visited (discovery time)lowlink[v]**: Smallest index reachable from v (including v itself)A vertex v is the root of an SCC if and only if:
This means v cannot reach any vertex discovered earlier, so it's the "entry point" to a new SCC.
lowlink[v]** tracks the earliest vertex reachable from vlowlink[v] == index[v], v cannot reach earlier verticesAdvantage: More efficient than Kosaraju's (no graph transpose needed)
| Aspect | Tarjan's | Kosaraju's |
|---|---|---|
| Passes | 1 DFS | 2 DFS (original + transpose) |
| Graph transpose | No | Yes (O(V+E) space) |
| Implementation | More complex | Simpler |
| Performance | Faster | Slightly slower |
| Best for | General use | When simplicity preferred |
This helps identify website structure and navigation patterns.
Definition in file tarjan_example.C.
Definition at line 207 of file tarjan_example.C.
| using SDigraph = List_Digraph<SNode, SArc> |
Definition at line 208 of file tarjan_example.C.
| using SNode = Graph_Node<string> |
Definition at line 206 of file tarjan_example.C.
| SDigraph build_dag | ( | ) |
Build a simple graph with no cycles (DAG)
Definition at line 318 of file tarjan_example.C.
Referenced by demo_dag_detection().
| SDigraph build_module_graph | ( | ) |
Build a software module dependency graph.
Core <--> Utils | | v v DB <--> Cache <--> Logger
API --> Auth --> Session ^ | +---------—+
Definition at line 277 of file tarjan_example.C.
References Aleph::maps().
Referenced by demo_dependency_analysis().
| SDigraph build_web_graph | ( | ) |
Build a sample web page link graph.
Represents web pages and their hyperlinks:
Homepage <—> About | | v v Products --> Services ^ | | v +------— Contact
Blog <--> Article1 | | v v Article2 <– Article3
Definition at line 228 of file tarjan_example.C.
References Aleph::maps().
Referenced by demo_web_communities().
| void demo_cycle_detection | ( | SDigraph & | g, |
| const string & | description | ||
| ) |
Demonstrate cycle detection.
Definition at line 416 of file tarjan_example.C.
References LocateFunctions< Container, Type >::get_it(), Aleph::maps(), and Aleph::HTList::size().
Referenced by demo_dag_detection(), and demo_dependency_analysis().
| void demo_dag_detection | ( | ) |
Demonstrate DAG detection.
Definition at line 500 of file tarjan_example.C.
References build_dag(), demo_cycle_detection(), demo_find_sccs(), Aleph::maps(), and print_graph().
Referenced by main().
| void demo_dependency_analysis | ( | ) |
Demonstrate practical application: dependency analysis.
Definition at line 477 of file tarjan_example.C.
References build_module_graph(), demo_cycle_detection(), demo_find_sccs(), Aleph::maps(), and print_graph().
Referenced by main().
| void demo_find_sccs | ( | SDigraph & | g, |
| const string & | description | ||
| ) |
Demonstrate finding SCCs with Tarjan's algorithm.
Definition at line 378 of file tarjan_example.C.
References LocateFunctions< Container, Type >::get_it(), Aleph::maps(), and Aleph::HTList::size().
Referenced by demo_dag_detection(), demo_dependency_analysis(), and demo_web_communities().
| void demo_web_communities | ( | ) |
Demonstrate practical application: web communities.
Definition at line 454 of file tarjan_example.C.
References build_web_graph(), demo_find_sccs(), Aleph::maps(), and print_graph().
Referenced by main().
| SDigraph::Node * find_node | ( | SDigraph & | g, |
| const string & | name | ||
| ) |
Find a node by name.
Definition at line 340 of file tarjan_example.C.
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 517 of file tarjan_example.C.
References demo_dag_detection(), demo_dependency_analysis(), demo_web_communities(), and Aleph::maps().
| void print_graph | ( | SDigraph & | g, |
| const string & | title | ||
| ) |
Print the graph structure.
Definition at line 351 of file tarjan_example.C.
References Aleph::maps().
Referenced by demo_dag_detection(), demo_dependency_analysis(), and demo_web_communities().