Chapter 3 Page 1 of 28 Chapter 3 Hamilton Circuits Spring 2016 1/8/16 Learning Objectives: Students will determine the number of Hamilton circuits in a complete graph and demonstrate the ability to use various methods to find Hamilton circuits in graphs. Students will know when a Hamilton circuit is appropriate to use in solving a real life problem. Students will understand the notion of an “optimal” Hamilton circuit and recognize the limitations of certain methods for finding optimal Hamilton circuits. Chapter 3 Page 2 of 28 Chapter 3 - Hamilton Circuits Hamilton Circuit - a route which must start and end at same vertex, visit each vertex only once. Think of the vertices as cities. (different than Euler circuit; Euler visited each edge only once. Numbers are weights (cost, time, etc.). A B 126 116 88 107 113 There are many Hamilton circuits in this graph 96 C D A B 126 ABCDA is one Hamilton circuit It weighs 126+116+96+88 = 426 116 88 ADCBA (reverse) also weighs 426 96 C A D B ADBCA is another Hamilton circuit It weighs 88+107+116+113 = 424 116 88 107 113 ACBDA is its reverse, also weighs 424 C D Hamilton Path - visit each vertex (city) only once, but don’t start and end at start vertex. (different than Euler Path; Euler visited each edge (street) only once.) A B 126 116 88 107 113 A 96 C There are many Hamilton paths in this graph 126 B D ABCD is one Hamilton path 116 C D 96 Chapter 3 Page 3 of 28 Complete graphs - every vertex is adjacent to (touches) every other vertex. A complete graph has (N-1)! Hamilton circuits. N = number of vertices ! means factorial = N(N-1)(N-2)….etc. If N = 5, N! = (5)(4)(3)(2)(1) = 120 (N-1)! = (4)(3)(2)(1) = 24 X A B K L C H V XX C D M complete graph with 3 vertices N = 3 (N-1)! = (3-1)! = 2 Hamilton Circuits complete graph with 4 vertices N = 4 (4-1)! = (3)! = 6 Hamilton Circuits S complete graph with 6 vertices 6-1)! = (5)! = 5X4X3X2X1= 120 Hamilton Circuits Find cheapest Hamilton Circuit? 4 Algorithms: 1.) Brute Force 2.) Nearest Neighbor 3.) Repetitive Nearest Neighbor 4.) Cheapest Link (optimal, inefficient b/c it’s too long) (approximate, efficient b/c it’s short) (approximate, efficient b/c it’s short) (approximate, efficient b/c it’s short) Chapter 3 Page 4 of 28 BRUTE FORCE ALGORITHM Only algorithm that always yields optimal answer, but is time consuming. Procedure: 1.) Pick any start vertex. 2.) List all (N-1)! Hamilton Circuits from start vertex. No duplicates allowed! 3.) Select the cheapest of all. Simple example: A 4 5 This graph has 6 Hamilton circuits 3 B 6 7 D 9 C Pick any start vertex…I’ll pick A. List all (N-1! ) Hamilton circuits that start (and end) at A. (N-1)! = (4-1)! = 3 X 2 X 1 = 6 List all 6 Hamilton circuits starting and ending at A. Write all 6 that start at A no duplicates A A A A A A D C B A = 5 + 9 + 7 + 4 = 25 B C D A = 4 + 7 + 9 + 5 = 25 (reverse) B D C A = 4 + 3 + 9 + 6 = 22 C D B A = 6 + 9 + 3 + 4 = 22 (reverse) D B C A = 5 + 3 + 7 + 6 = 21 C B D A = 6 + 7 + 3 + 5 = 21 (reverse) cheapest Hamilton circuit is ADBCA or ACBDA = 21 A 5 3 B 6 7 C D Chapter 3 Page 5 of 28 Rewriting a circuit - any Hamilton circuit can be rewritten without resolving again.. A ADBCA or ACBDA or BCADB or BDACB or CADBC or CBDAC or DBCAD or DACBD 5 3 B 6 7 D C these are all the same Hamilton circuit costing 21 Chapter 3 Page 6 of 28 Another example with 5 vertices; N=5 A A 133 A B C D E 185 119 200 E B 152 121 199 185 119 152 133 B 185 121 150 200 C 119 121 174 120 120 150 174 D C Pick any start vertex…I’ll pick A. List all (N-1!) Hamilton circuits that start (and end) at A. (N-1)! = (5-1)! = 4(3)(2)(1) = 24 Write all 24 Hamilton circuits starting at A. No duplicates. 1.) 3.) 5.) 7.) 9.) 11.) 13.) 15) 17.) 19.) 21.) 23.) A A A A A A A A A A A A BCDEA and 2.) AEDCBA (reverse); BCEDA “ 4.) ADECBA “ BDCEA “ 6.) AECDBA “ BDECA “ 8.) ACEDBA “ BECDA “ 10.) ADCEBA “ BEDCA “ 12.) ACDEB “ CBDEA “ 14.) AEDBCA “ CBEDA “ 16.) ADEBCA “ CDBEA “ 18.) AEBDCA “ CEBDA “ 20.) ADBECA “ DBCEA “ 22.) AECBDA “ DCBEA “ 24.) AEBCDA “ weight = “ “ “ “ “ “ “ “ “ “ “ 812 each 777 each 762 each 773 each 831 each 877 each 722 each cheapest 791 each 776 each 741 each 676 each 780 each Cheapest Hamilton Circuits: ADBCEA and AECBDA cost $676 each. Atlanta AD DB BC CE EA 133 Erie 152 Buffalo 150 Denver 120 121 $152 $150 $121 ADBCEA $120 $676 $133 $676 Note: ADBCEA ......Can re-write it as DBCEAD or BCEADB or CEADBC or EADBCE or others using different start vertices... each costs $676 Chicago Video of Brute Force and Nearest Neighbor Algorithms http://mediaplayer.pearsoncmg.com/assets/cV_e4_OrKFiYHQs1QelD1N_KI2SVybPW D 152 150 174 199 E 133 200 120 199 Chapter 3 Page 7 of 28 Solutions Practice problems page 98: 27.) Brute Force algorithm from A: A 22 28 D 48 20 B 32 18 C Brute Force Algorithm Pick any start vertex…I’ll pick A. List all (N-1! ) Hamilton circuits that start (and end) at A. (N-1)! = (4-1)! = 3 X 2 X 1 = 6 List all 6 Hamilton circuits starting and ending at A. No duplicates A A A A A A D C B A = 22 + 18 + 32 + 48 = 120 B C D A = 48 + 32 + 18 + 22 = 120 (reverse) B D C A = 48 + 20 + 18 + 28 = 124 C D B A = 28 + 18 + 20 + 48 = 124 (reverse) D B C A = 22 + 20 + 32 + 28 = 102 C B D A = 28 + 32 + 20 + 22 = 102 (reverse) cheapest Hamilton circuit A 22 28 D 20 B 32 C ACBDA or ADBCA = 102 Chapter 3 Page 8 of 28 29.) Brute Force Algorithm from A A 35 18 40 18 E B 21 Procedure: Pick a start vertex …I’ll pick A List all (N-1)! from A = 24 circuits No duplicates Select cheapest 32 9 22 23 25 D 1.) 3.) 5.) 7.) 9.) 11.) 13.) 15.) 17.) 19.) 21.) 23.) C A A A A A A A A A A A A DBCEA CEBDA CDBEA CBEDA BCDEA BCEDA BDCEA BDECA BEDCA BECDA EBCDA CBDEA and “ “ “ “ “ “ “ “ “ “ “ 2.) AECBDA (reverse); 4.) ADBECA “ 6.) AEBDCA “ 8.) ADEBCA “ 10.) AEDCBA “ 12.) ADECBA “ 14.) AECDBA “ 16.) ACEDBA “ 18.) ACDEBA “ 20.) ADCEBA “ 22.) ADCBEA “ 24.) AEDBCA “ weight = 93 each “ 120 each “ 122 each “ 121 each “ 119 each “ 121 each “ 122 each “ 151 each “ 150 each “ 123 each “ 92 each “ 120 each Cheapest = AEBCDA or mirror image ADCBEA = 92 weight A 18 18 E B ….could be rewritten as BCDAEB = 92 = BEADCB CDAEBC = 92 = CBEADC DAEBCD = 92 = DCBEAD EBCDAE = 92 = EADCBE 9 22 25 D C Supercomputer - The world's fastest supercomputer (Tianhe-2 in China) has achieved a performance 33,900 trillion floating point operations per second. 30 city sales territory = 280 million years using Brute Force Algorithm Chapter 3 Page 9 of 28 Nearest Neighbor Algorithm - 2nd method (approximate) - not as much work as Brute Force - but, usually won’t give optimal answer A 133 185 E 120 A B 200 152 A B C D E 119 150 199 121 185 119 152 133 B 185 121 150 200 C 119 121 D 152 150 174 174 120 E 133 200 120 199 199 174 D C Procedure 1.) Pick a start vertex 2.) Travel on cheapest edge from start vertex to any other vertex. If there is a tie, pick one at random 3.) Travel on cheapest edge from current vertex to another vertex except one you’ve visited 4.) same 5.) same …….continue procedure until you return to start vertex Step1.) Pick a start vertex…I’ll pick A. Draw a 5 vertex graph A A 133 185 200 E 120 152 B E B 119 150 199 121 D D 174 C C Chapter 3 Page 10 of 28 Step 2.) - examine all edges from A going to any other vertex, pick the cheapest = AC 119 A A 133 E 185 E 152 119 150 199 D 119 B 200 120 B D C 1st edge chosen 121 C 174 Step 3.) - examine all edges from C going to any other vertex except back to A, pick the cheapest = CE 120 A 133 A 185 E B 200 120 152 119 B 120 150 199 119 E 121 D C 2nd edge chosen 174 D C Step 4.) - examine all edges from E going to any other vertex except back to A, C pick the cheapest = ED 199 A A 133 185 E B 200 120 152 119 150 199 199 B 120 121 D C 3rd edge chosen 174 D 119 E C Chapter 3 Page 11 of 28 Step 5.) - examine all edges from D going to any other vertex except back to A, C, E pick the cheapest = DB 150 there was no other choice A A 133 185 E B 200 152 120 E B 119 150 199 121 D C 4th edge chosen 174 D C Step 6.) – There is no choice except to return to A from B to complete the Hamilton Circuit = BA 185 A 133 A 185 200 E 120 152 B E B 119 150 199 121 D C finished circuit 174 D C Hamilton Circuit is ACEDBA or reverse ABDECA Hamilton Circuit = ACEDBA = 119 + 120 + 199 + 150 + 185 = 773 AC CE ED DB BA = ACEDBA or you can rewrite it: BACEDB or CEDBAC or DBACED or EDBACE or other ways following the sequence of edges you used… all weigh 773 Brute Force Algorithm on this graph yielded best answer = 676 weight Improvement: Percentage relative error = Approximate answer - Optimal answer = 773-676 = .14 = 14% improvement Optimal answer 676 Video of Brute Force and Nearest Neighbor Algorithms http://mediaplayer.pearsoncmg.com/assets/cV_e4_OrKFiYHQs1QelD1N_KI2SVybPW Chapter 3 Page 12 of 28 RNN Algorithm - 3rd method (approximate) - more work than NN, but not as much as Brute Force - but, won’t always give optimal answer Use RNN to find cheapest Hamilton Circuit from A: A 133 185 E B 200 120 152 119 150 199 121 A A B C D E 185 119 152 133 B 185 121 150 200 C 119 121 D 152 150 174 174 120 E 133 200 120 199 199 174 D Procedure: 1.) Perform NN algo from A. 2.) Then NN from B 3.) Then NN from C 4.) Then NN from D 5.) Finally NN from E 6.) Select the cheapest C NN algo 5 times A NN from A 1st: AC 119 2nd: CE 120 E 3rd: ED 199 4th: DB 150 5th: BA 185 D 773 ACEDBA A B C NN from B 1st: BC 121 2nd: CA 119 E 3rd: AE 133 4th: ED 199 5th: DB 150 D 722 BCAEDB B C A NN from C 1st: CA 119 E 2nd: AE 133 3rd: ED 199 4th: DB 150 5th: BC 121 D 722 CAEDBC B C NN from D 1st: DB 150 2nd: BC 121 E rd 3 : CA 119 4th: AE 133 5th: ED 199 722 D DBCAED A B C Chapter 3 Page 13 of 28 NN from E 1st: EC 120 2nd: CA 119 E rd 3 : AD 152 4th: DB 150 5th: BE 200 741 ECADBE A B D C Now pick cheapest Hamilton Circuit: (3) tied with weight of 722 BCAEDB CAEDBC all are the same DBCAED $722 circuit..... different start vertices Re-write the circuit using A to start: ADBCEA or AECBDA (reverse) video: http://mediaplayer.pearsoncmg.com/assets/BySt7j_wWrnrmlYx2ac0LQoXJQdn0FgT Chapter 3 Page 14 of 28 Practice problems 39a.) Nearest neighbor Algorithm from A A 18 35 E B 18 23 22 40 21 32 9 25 D C Procedure Step1.) Pick cheapest edge from A = Step2.) Pick cheapest edge from E but not back to A = Step3.) Pick cheapest edge from B but not back to E or A = Step4.) Pick cheapest edge from C but not back to B,E, or A = Step5.) Pick cheapest edge from D no choice back to A = AE = 18 EB = 18 BC = 9 CD = 25 DA = 22 92 A E B D C ADCBEA = 92 ADCBEA can be re-written as DCBEAD or CBEADC or BEADCB or EADCBE or 5 reverses Chapter 3 Page 15 of 28 39b.) Nearest neighbor Algorithm from B but rewrite it from A A 18 35 E B 18 23 22 40 21 32 9 25 D C Procedure Step1.) Pick cheapest edge from B = BC = 9 Step2.) Pick cheapest edge from C but not back to B = CE = 23 Step3.) Pick cheapest edge from E but not back to C or B = EA = 18 Step4.) Pick cheapest edge from A but not back to E,C, or B = AD = 22 Step5.) Pick cheapest edge from D no choice back to B = DB = 21 93 A E B D C BCEADB = 93 BCEADB re-written as ADBCEA = 93 Chapter 3 Page 16 of 28 43.) RNN Algorithm A 18 35 18 E 23 B 22 40 21 32 9 25 D Procedure: 1.) Perform NN algo from A. 2.) Then NN from B 3.) Then NN from C 4.) Then NN from D 5.) Finally NN from E 6.) Select the cheapest C NN algo 5 times NN algo from A = ADCBEA = 92 (problem 39a) A E B D NN algo from B = BCEADB = 93 (problem 39b) C A E B D C Chapter 3 Page 17 of 28 NN algo from C A 18 35 E B 18 22 23 40 21 32 9 25 D C A CB = 9 BE = 18 EA = 18 AD = 22 DC = 25 92 E B D CBEADC = 92 C NN algo from D A 18 35 18 E 23 B 22 40 21 32 9 25 D C A DB = 21 BE = 18 EA = 18 AC = 40 CD = 25 122 E B D C DBEACD = 122 Chapter 3 Page 18 of 28 NN algo from E A 18 35 E B 18 22 23 40 21 32 9 25 D C EB = 18 or EA = 18? Which? Pick one at random…I’ll pick EB A EB = 18 BC = 9 CD = 25 DA = 22 AE = 18 92 E B D EBCDAE = 92 C Calculated 5 Hamilton Circuits, pick cheapest: CBEADC = 92 ADCBEA = 92 EBCDAE = 92 A E B D C all 3 same Ham circuit Chapter 3 Page 19 of 28 Cheapest Link Algorithm - 4th Method (approximate) - more work than NN, but not as much as Brute Force - but, won’t always give optimal answer Procedure: 1.) Even if problem wants a Hamilton Circuit starting from a particular vertex, don’t pick a start vertex...instead select cheapest edge anywhere 2.) Select next cheapest anywhere 3.) Select next cheapest edge anywhere, but no 3 edges from same vertex and don’t complete the circuit until all vertices have been contacted 4.) Repeat step 3 until you have a Hamilton circuit 5.) Add the weights 6.) Write the Hamilton Circuit from the desired start vertex Use Cheapest Link algorithm to find cheapest Hamilton Circuit from A 133 185 E B 200 120 D 152 119 150 199 121 A A B C D E 185 119 152 133 B 185 121 150 200 174 D C Draw a 5 vertex graph: A E B D when done, rewrite circuit from D C C 119 121 174 120 D 152 150 174 199 E 133 200 120 199 Chapter 3 Page 20 of 28 Don’t pick a start vertex… Pick 1st cheapest edge anywhere = AC = 119 If there was a tie, 2 with same weight, pick either one) A E B D when done, rewrite circuit from D C Pick 2nd cheapest edge anywhere = EC = 120 A E B D C when done, rewrite circuit from D Pick 3rd cheapest edge anywhere = BC = 121 no good…3 edges from C A E B no good (3 edge rule) D when done, rewrite circuit from D C Chapter 3 Page 21 of 28 Pick 3rd cheapest edge anywhere again = AE = 133 no good…created circuit A no good (created circuit) E B D C when done, rewrite circuit from D Pick 3rd cheapest edge anywhere again = DB = 150 A E B D C when done, rewrite circuit from D Pick 4th cheapest edge anywhere = DA = 152 A E B D when done, rewrite circuit from D C Chapter 3 Page 22 of 28 Pick 5th cheapest edge anywhere = DC = 174 no good…created circuit and 3 edges from D and C A E B D no good C when done, rewrite circuit from D Pick 5th cheapest edge anywhere = AB = 185 no good…created circuit and 3 edges from A A no good E B D C when done, rewrite circuit from D Pick 5th cheapest edge anywhere = DE = 199 no good…created circuit and 3 edges from D A E B no good D when done, rewrite circuit from D C Chapter 3 Page 23 of 28 Pick 5th cheapest edge anywhere = BE = 200 A E B D C when done, rewrite circuit from D Write Hamilton circuit from D: DBECAD or reverse. It weighs: AC = 119 EC = 120 DB = 150 DA = 152 BE = 200 741 Video for Cheapest Link Algorithm http://mediaplayer.pearsoncmg.com/assets/cthirMi_nYN4OUkoI9qbrEyqM0hZd1Fp Do practice problems page 101: 49,56a This is the order edges were chosen Chapter 3 Page 24 of 28 Practice problem page 101: 49.) Use Cheapest Link Algorithm to find circuit starting at A A when done, write circuit from A 18 35 18 E B 22 23 40 21 32 9 25 D C Don’t pick a start vertex Select cheapest edge anywhere. CB = 9 A when done, write circuit from A E B 9 D Select C next cheapest edge anywhere. If tie, pick one at random. EA = 18 A when done, write circuit from A 18 E B 9 D C Chapter 3 Page 25 of 28 Select next cheapest edge anywhere. EB = 18 A when done, write circuit from A 18 18 E B 9 D Select C next cheapest edge anywhere. BD = 21 NO, 3 edge rule A when done, write circuit from A 18 B 18 E 21 D Select 9 C next cheapest edge anywhere. AD = 22 OK A when done, write circuit from A 18 B 18 E 22 D 9 C Chapter 3 Page 26 of 28 Select next cheapest edge anywhere. EC = 23 No, 3 edge rule and circuit rule A when done, write circuit from A 18 B 18 E 9 23 22 D Select C next cheapest edge anywhere. CD = 25 A when done, write circuit from A 18 B 18 E 22 9 D 25 C 1st edge BC = 9 2nd edge EA = 18 3rd edge BE = 18 4th edge AD = 22 5th edge CD = 25 92 Write the circuit starting at A ADCBEA or AEBCDA Chapter 3 Page 27 of 28 56a.) Cheapest Link Algorithm start at A A A B C D E A B C D E 1.2 0.7 1.0 1.3 B C 1.2 0.7 0.9 0.9 0.8 1.2 1.1 0.8 D 1.0 0.8 1.2 A B D C E 1.3 1.1 0.8 0.9 0.9 E Ignore upper right or lower left (it’s duplicate data) 1.2 0.7 0.9 1.0 0.8 1.2 1.3 1.1 0.8 0.9 Don’t pick a start vertex Select cheapest edge anywhere. AC = 0.7 Draw 5 vertices (any shape is OK). Label them ABCDE in any order. B E D 0.7 C A Select next cheapest edge anywhere. If tie, pick one at random DB = 0.8 B 0.8 E D 0.7 C A Chapter 3 Page 28 of 28 Select next cheapest edge anywhere. EC = 0.8 B 0.8 E D 0.8 0.7 C A Select next cheapest edge anywhere. If tie, pick one at random ED = 0.9 B 0.8 0.9 E D 0.8 0.7 C A Select next cheapest edge anywhere. If tie, pick one at random CB = 0.9 No, 3 edge rule and circuit DA = 1.0 No, 3 edge rule and circuit EB = 1.1 No, 3 edge rule and circuit DC = 1.2 No, 3 edge rule and circuit AB = 1.2 OK B 0.8 0.9 E D 1.2 0.8 0.7 C A Write circuit from A: ABDECA = 1.2 + 0.8 + 0.9 + 0.8 + 0.7 = 4.4 or reverse