|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Example demonstrating Hamiltonian graph testing in Aleph-w. More...
#include <iostream>#include <iomanip>#include <string>#include <tclap/CmdLine.h>#include <tpl_graph.H>#include <hamiltonian.H>Go to the source code of this file.
Typedefs | |
| using | SNode = Graph_Node< string > |
| using | IArc = Graph_Arc< int > |
| using | UGraph = List_Graph< SNode, IArc > |
Functions | |
| void | print_section (const string &title) |
| void | print_subsection (const string &title) |
| void | print_degrees (UGraph &g) |
| void | build_complete_graph (UGraph &g, size_t n) |
| void | demo_comparison () |
| void | demo_ore_theorem () |
| void | demo_practical () |
| void | demo_density () |
| void | demo_dirac () |
| int | main (int argc, char *argv[]) |
Example demonstrating Hamiltonian graph testing in Aleph-w.
A Hamiltonian cycle (or path) visits every vertex exactly once, which is fundamentally different from Eulerian paths (which visit every edge exactly once). Finding Hamiltonian cycles is NP-complete, so we can only test sufficient conditions, not necessary ones.
A Hamiltonian path is a path that visits every vertex exactly once. The path may start and end at different vertices.
A Hamiltonian cycle (or circuit) is a Hamiltonian path that starts and ends at the same vertex, forming a cycle.
| Property | Hamiltonian | Eulerian |
|---|---|---|
| Visits | Every vertex once | Every edge once |
| Complexity | NP-complete | Polynomial O(V+E) |
| Test | Sufficiency only | Exact conditions |
| Solution | No efficient algorithm | Efficient algorithm exists |
NP-completeness: Determining if a graph has a Hamiltonian cycle is NP-complete, meaning:
Implication: We can only test sufficient conditions (if condition holds, graph is Hamiltonian), not necessary conditions (graph may be Hamiltonian without satisfying condition).
Statement: For a graph with n ≥ 3 vertices:
If for every pair of non-adjacent vertices u, v:
Then the graph has a Hamiltonian cycle.
Intuition: If vertices have high enough degrees, there are enough edges to form a Hamiltonian cycle.
Statement: For a graph with n ≥ 3 vertices:
If every vertex has degree ≥ n/2:
Then the graph has a Hamiltonian cycle.
Note: Dirac's is a special case of Ore's (if all degrees ≥ n/2, then sum of any two ≥ n).
Sufficient: If condition holds → graph is Hamiltonian Necessary: If graph is Hamiltonian → condition holds
Ore's theorem is sufficient but NOT necessary:
Many Hamiltonian graphs don't satisfy Ore's condition but are still Hamiltonian.
Time complexity: O(V!) worst case (exponential!)
| Problem | Complexity | Notes |
|---|---|---|
| Test sufficient condition | O(V²) | Check degrees |
| Find Hamiltonian cycle | O(V!) | Exponential (backtracking) |
| Decision problem | NP-complete | No polynomial algorithm known |
Definition in file hamiltonian_example.C.
Definition at line 212 of file hamiltonian_example.C.
| using SNode = Graph_Node<string> |
Definition at line 211 of file hamiltonian_example.C.
| using UGraph = List_Graph<SNode, IArc> |
Definition at line 213 of file hamiltonian_example.C.
| void build_complete_graph | ( | UGraph & | g, |
| size_t | n | ||
| ) |
Definition at line 245 of file hamiltonian_example.C.
References Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), Aleph::maps(), nodes, and Aleph::to_string().
Referenced by demo_dirac(), demo_ore_theorem(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
| void demo_comparison | ( | ) |
Definition at line 263 of file hamiltonian_example.C.
References Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), Aleph::maps(), print_section(), and print_subsection().
Referenced by main().
| void demo_density | ( | ) |
Definition at line 439 of file hamiltonian_example.C.
References Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), Aleph::maps(), nodes, print_section(), test(), and Aleph::to_string().
Referenced by main().
| void demo_dirac | ( | ) |
Definition at line 487 of file hamiltonian_example.C.
References build_complete_graph(), Aleph::maps(), print_degrees(), print_section(), print_subsection(), and Aleph::to_string().
Referenced by main().
| void demo_ore_theorem | ( | ) |
Definition at line 315 of file hamiltonian_example.C.
References build_complete_graph(), Aleph::maps(), print_section(), and print_subsection().
Referenced by main().
| void demo_practical | ( | ) |
Definition at line 375 of file hamiltonian_example.C.
References bar(), Aleph::maps(), print_degrees(), print_section(), print_subsection(), and test().
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 589 of file hamiltonian_example.C.
References demo_comparison(), demo_density(), demo_dirac(), demo_ore_theorem(), demo_practical(), and Aleph::maps().
| void print_degrees | ( | UGraph & | g | ) |
Definition at line 231 of file hamiltonian_example.C.
References GraphCommon< GT, Node, Arc >::get_node_it(), and Aleph::maps().
Referenced by demo_dirac(), and demo_practical().
| void print_section | ( | const string & | title | ) |
Definition at line 219 of file hamiltonian_example.C.
References Aleph::maps().
Referenced by demo_comparison(), demo_density(), demo_dirac(), demo_ore_theorem(), and demo_practical().
| void print_subsection | ( | const string & | title | ) |
Definition at line 226 of file hamiltonian_example.C.
References Aleph::maps().
Referenced by demo_comparison(), demo_dirac(), demo_ore_theorem(), and demo_practical().