HAMILTONIAN CIRCUIT ALGORITHMS Vocabulary Application examples of Brute Force Algorithm Nearest Neighbor Algorithm
HAMILTONIAN CIRCUITS and PATHS REVIEW: A Hamiltonian Circuit is a circuit that visits each VERTEX of a graph only once. A Hamiltonian path is a path that visits each VERTEX only once.
Unlike (Euler Circuit) E. C Unlike (Euler Circuit) E.C. problems, we can not simply look at a graph and determine if it has an associated H.C. There are certain types of graphs (fortunately, the most common) that ALWAYS have an associated (Hamilton Circuit) H.C. A COMPLETE GRAPH: A graph with ‘N’ vertices in which every pair of vertices is joined by exactly one edge. In a COMPLETE GRAPH with ‘N’ vertices, each vertex has DEGREE ‘N-1’ All complete graphs have h.c. associated with them.
(For example, C and D are NOT adjacent. A and C are not adjacent) graphs… A B D C graph #1 This graph has hamiltonian paths: ADBC (or its mirror image) DABC (or its mirror image) The graph has no h.c., since you cannot leave vertex C without re-visiting vertex B This is NOT a COMPLETE graph… to prove this, you must find at least one pair of vertices that is NOT connected with an edge (not ADJACENT) (For example, C and D are NOT adjacent. A and C are not adjacent) Another way to prove it is not a complete graph is to find one at least one pair of vertices that is connected by more than one edge. (B and C, for example)
graphs… graph #2 B A C D This graph has hamiltonian paths: ACDB (or its mirror image) BACD (or mirror image) …etc... The graph has h.c.: ACDBA (or its mirror image) BACDB (or mirror image) ...etc... This is NOT a COMPLETE graph… (B and C are not adjacent)
graphs… graph #3 B A E C D This graph has many hamiltonian paths: ABCDE …etc... This graph has many H.C. ABCDEA … etc... This is a COMPLETE graph with 5 vertices… Notice that each vertex is adjacent to every other vertex exactly once, and that each has degree 4.
H.C. example… The vertices will represent cities, and the edges will represent distances(and will be measured in miles.) A D C B AB = 10 BC = 30 DC = 20 AD = 70 AC = 50 BD = 40 Recalling the example from the introductory show should remind you that there are only 3 possible H.C. subgraphs associated with this COMPLETE graph.
For COMPLETE GRAPHS… how many subgraphs? D C B 1. We looked at the same type of graph last time (the ‘lazy Fred’ example) and noticed that there were only 3 possible h.c. subgraphs. 2. We also noticed that (up to the STARTING vertex) there were two hamiltonian circuits associated with each subgraph. 3. Let’s do some math...
Tree Diagram A D B C C D B D C B For every COMPLETE graph with 4 vertices… the DEGREE of each vertex is??? C D B 3 So, once you have chosen a starting point, you have 3 vertices to chose from. D C B After you’ve chosen one of those 3, how many vertices are left to choose from? 2 Tree Diagram And after you’ve chosen one of those 2, how many vertices are left to choose from? To create the h.c., each path will return to A at the end. 1
Complete Graphs with “N” vertices… 4. Will have (N-1)x(N-2)x(N-3)x…x2 Hamiltonian Circuits associated with them. Mathematically, we will use “factorial” shorthand to write this expression. We will write: The number of H.C. for a complete graph with N vertices = (N-1)!
Complete Graphs with “N” vertices… 5. We saw that each H. subgraph could be used to describe 2 hamiltonian circuits (mirror images) 6. So, the number of Hamiltonian Circuit subgraphs for a complete graph with “N” vertices is: (N-1)!/2
back to the H.C. example… This is a complete graph with 4 vertices. D C B AB = 10 BC = 30 DC = 20 AD = 70 AC = 50 BD = 40 This is a complete graph with 4 vertices. It has (4-1)! = (3)! = 3x2 = 6 hamiltonian circuits (after you’ve chosen a starting pt.) It has (4-1)!/2 = (3)!/2 = (3x2)/2 = 6/2 = 3 h.c. subgraphs
Algorithms to find Hamiltonian circuits 9/9/2018 Algorithms to find Hamiltonian circuits They may not yield the optimal solution but will always give us an efficient approximation. We may use them a few times, sometimes with different starting points, and compare the solutions we find They ALWAYS work with COMPLETE graphs, but sometimes fail to produce a solution with other graphs
Algorithm 1: The Brute Force Method Step 1 : Make a list of all the possible Hamilton circuits of the graph. Step 2: For each HC calculate its total weight. (add up the edges in the circuit) Step 3: Choose an optimal circuit.
back to the H.C. example… This is a complete graph with 4 vertices. D C B AB = 10 BC = 30 DC = 20 AD = 70 AC = 50 BD = 40 This is a complete graph with 4 vertices. It has (4-1)! = (3)! = 3x2 = 6 hamiltonian circuits (after you’ve chosen a starting pt.) It has (4-1)!/2 = (3)!/2 = (3x2)/2 = 6/2 = 3 h.c. subgraphs
Brute Force Method… In the “Lazy Fred” example, all 3 of the possible h.c. subgraphs are examined. You should be able to see that this method is fine for such a small graph. When we try to use this method for a complete graph with 5 vertices, though, we’d have to look at: (4)!/2 = (4x3x2)/2 = 12 h.c. subgraphs. Notice how quickly this method becomes computationally inefficient.
Example 20 C B A D 10 70 50 15 30
Algorithm 2: Nearest Neighbor Start : Start at a designated vertex. First Step: From the starting vertex go to its nearest neighbor. (edge with the smallest weight. Middle Steps: Repeat step 2 until you have visited all the vertices exactly once. Last Step: From the last vertex return to the starting vertex.
NEAREST NEIGHBOR ALGORITHM 9/9/2018 NEAREST NEIGHBOR ALGORITHM A D C B AB = 10 BC = 30 DC = 20 AD = 70 AC = 50 BD = 40 1. Choose a starting vertex (ex) I choose vertex B Its nearest neighbor is 10 units away at vertex A. 2. Begin by drawing the edge that connects these points A D C B
NEAREST NEIGHBOR ALGORITHM 9/9/2018 NEAREST NEIGHBOR ALGORITHM A D C B AB = 10 BC = 30 DC = 20 AD = 70 AC = 50 BD = 40 3. Continue by choosing the nearest neighbor of the last vertex touched... A D C B BUT>>> DO NOT… >add any edge that will create a subcircuit (doesn’t contain ALL of the original vertices) >add an edge that will make a vertex have degree greater than 2 The weight of this circuit is 120 miles.