|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Graph connectivity: components and spanning trees. More...
#include <iostream>#include <string>#include <cstring>#include <tpl_graph.H>#include <tpl_components.H>#include <tpl_spanning_tree.H>#include <tpl_test_connectivity.H>#include <Tarjan.H>Go to the source code of this file.
Typedefs | |
| using | UGraph = List_Graph< Graph_Node< string >, Graph_Arc< int > > |
| using | DGraph = List_Digraph< Graph_Node< string >, Graph_Arc< int > > |
Functions | |
| static bool | has_flag (int argc, char *argv[], const char *flag) |
| static void | usage (const char *prog) |
| template<class GT > | |
| GT::Node * | find_or_create (GT &g, const string &name) |
| template<class GT > | |
| void | add_edge (GT &g, const string &src, const string &tgt, int weight=1) |
| template<class GT > | |
| void | print_graph (GT &g, const string &title) |
| void | demo_connected_components () |
| void | demo_strongly_connected () |
| void | demo_spanning_tree () |
| void | demo_network_analysis () |
| int | main (int argc, char *argv[]) |
Graph connectivity: components and spanning trees.
This example demonstrates fundamental algorithms for analyzing graph connectivity, which is crucial for understanding graph structure and designing efficient algorithms. Connectivity analysis helps identify isolated groups, understand graph structure, and design robust systems.
A connected component is a maximal set of vertices where every vertex can reach every other vertex through a path.
Key property: In undirected graphs, connectivity is symmetric: if u can reach v, then v can reach u.
Uses DFS or BFS traversal:
Time Complexity: O(V + E) - visits each vertex and edge once
A strongly connected component (SCC) is a maximal set of vertices in a directed graph where every vertex can reach every other vertex through directed paths.
Key difference: In directed graphs, connectivity is NOT symmetric: u → v doesn't imply v → u.
Uses a single DFS pass with:
index[v]**: Discovery time (order of first visit)lowlink[v]**: Lowest index reachable from v's DFS subtreeKey insight: When lowlink[v] == index[v], v is the root of an SCC.
Time Complexity: O(V + E) - single DFS traversal
A spanning tree is a subgraph that:
Algorithm: Use DFS or BFS to traverse the graph, keeping track of tree edges (edges used in traversal).
Time Complexity: O(V + E)
| Aspect | Connected Components | Strongly Connected Components |
|---|---|---|
| Graph type | Undirected | Directed |
| Connectivity | Symmetric | Not symmetric |
| Algorithm | Simple DFS/BFS | Tarjan/Kosaraju |
| Complexity | O(V + E) | O(V + E) |
Definition in file graph_components_example.C.
| using DGraph = List_Digraph<Graph_Node<string>, Graph_Arc<int> > |
Definition at line 218 of file graph_components_example.C.
| using UGraph = List_Graph<Graph_Node<string>, Graph_Arc<int> > |
Definition at line 217 of file graph_components_example.C.
| void add_edge | ( | GT & | g, |
| const string & | src, | ||
| const string & | tgt, | ||
| int | weight = 1 |
||
| ) |
Definition at line 234 of file graph_components_example.C.
References find_or_create(), and Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc().
Referenced by demo_connected_components(), demo_network_analysis(), demo_spanning_tree(), demo_strongly_connected(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
| void demo_connected_components | ( | ) |
Definition at line 261 of file graph_components_example.C.
References add_edge(), find_or_create(), Aleph::maps(), print_graph(), and Aleph::HTList::size().
Referenced by main().
| void demo_network_analysis | ( | ) |
Definition at line 432 of file graph_components_example.C.
References add_edge(), find_or_create(), Aleph::maps(), and Aleph::HTList::size().
Referenced by main().
| void demo_spanning_tree | ( | ) |
Definition at line 379 of file graph_components_example.C.
References add_edge(), find_or_create(), GraphCommon< GT, Node, Arc >::get_arc_it(), GraphCommon< GT, Node, Arc >::get_num_arcs(), GraphCommon< GT, Node, Arc >::get_num_nodes(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), and Aleph::maps().
Referenced by main().
| void demo_strongly_connected | ( | ) |
Definition at line 319 of file graph_components_example.C.
References add_edge(), find_or_create(), Aleph::maps(), print_graph(), and Aleph::HTList::size().
Referenced by main().
Definition at line 225 of file graph_components_example.C.
References GraphCommon< GT, Node, Arc >::get_node_it(), and Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node().
Referenced by add_edge(), demo_connected_components(), demo_network_analysis(), demo_spanning_tree(), and demo_strongly_connected().
|
static |
Definition at line 201 of file graph_components_example.C.
References Aleph::maps().
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 491 of file graph_components_example.C.
References demo_connected_components(), demo_network_analysis(), demo_spanning_tree(), demo_strongly_connected(), has_flag(), Aleph::maps(), and usage().
Definition at line 242 of file graph_components_example.C.
References GraphCommon< GT, Node, Arc >::get_arc_it(), GraphCommon< GT, Node, Arc >::get_node_it(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), and Aleph::maps().
Referenced by demo_connected_components(), and demo_strongly_connected().
|
static |
Definition at line 209 of file graph_components_example.C.
References Aleph::maps().
Referenced by main().