# § 6.5 - 6.8 Algorithms, Algorithms, Algorithms Recall that an algorithm is a set of procedures or rules that, when followed, lead to a ‘solution’ to

Post on 21-Dec-2015

213 views

Embed Size (px)

TRANSCRIPT

<ul><li> Slide 1 </li> <li> Slide 2 </li> <li> 6.5 - 6.8 Algorithms, Algorithms, Algorithms </li> <li> Slide 3 </li> <li> Recall that an algorithm is a set of procedures or rules that, when followed, lead to a solution to a particular problem. </li> <li> Slide 4 </li> <li> Think back to the `Homer` example from yesterday. The method we used there can be written as a slightly more formal algorithm: The Brute-Force Algorithm First, make a list of all the possible Hamiltonian circuits of the graph. First, make a list of all the possible Hamiltonian circuits of the graph. For each circuit, calculate its total weight by summing the weights of all the edges traveled. For each circuit, calculate its total weight by summing the weights of all the edges traveled. Find the circuits* with the least total weight. Any of these can be chosen as an optimal H.C. Find the circuits* with the least total weight. Any of these can be chosen as an optimal H.C. </li> <li> Slide 5 </li> <li> THE METHOD USED IN OUR OTHER (EVEN GEEKIER) EXAMPLE FROM YESTERDAY CAN ALSO BE MADE A BIT MORE FORMAL... THE NEAREST NEIGHBOR ALGORITHM PICK ANY VERTEX AS A STARTING POINT. FROM THE STARTING VERTEX GO TO ITS NEAREST NEIGHBOR--THE NEAREST VERTEX FOR WHICH THE CORRESPONDING EDGE HAS THE SMALLEST WEIGHT. CONTINUE BUILDING THE CIRCUIT, ONE VERTEX AT A TIME, ALWAYS CHOOSING THE NEAREST NEIGHBOR FROM THE VERTICES THAT HAVE NOT BEEN TRAVELED YET. FROM THE LAST VERTEX RETURN TO YOUR STARTING POINT. </li> <li> Slide 6 </li> <li> So which of these methods is better? Brute-Force The Brute-Force Method will always give the optimal solution, but it is inefficient. Nearest Neighbor The Nearest Neighbor Algorithm on the other hand is efficient but not always accurate. Given its efficiency, one might ask if there is a way to improve on the second algorithm. </li> <li> Slide 7 </li> <li> The Repetitive Nearest-Neighbor Algorithm Let X be any vertex. Apply the Nearest-Neighbor Algorithm using X as the starting point and calculate the total cost for the circuit. Repeat the process for each of the other vertices of the graph. Choose the best Hamiltonian circuit you find. If we have designated a starting vertex, then rewrite the circuit with that vertex as the reference point. </li> <li> Slide 8 </li> <li> Example: The Galactica needs to survey a set of planets (A, B, C, D, E, F, G) in order to find water for the Fleet. the Commander has asked the helm to chart the course that will use the lowest amount of tylium fuel. LET US REVISIT THE SECOND EXAMPLE FROM YESTERDAY, ARMED THIS TIME WITH THE REPETITIVE NEAREST NEIGHBOR ALGORITHM... </li> <li> Slide 9 </li> <li> ABCDEFG A755028351522 B753060806550 C 3040483528 D 6040203029 E358048204032 F156535304013 G225028293213 THE FOLLOWING TABLE TELLS US HOW MANY TONS OF TYLIUM IT TAKES TO TRAVEL FROM ONE PLANET TO ANOTHER: YESTERDAY WE FOUND THAT THE N.N. ALGORITHM APPLIED TO A GAVE US THE FOLLOWING CIRCUIT AND WEIGHT: a,f,g,c,b,d,e,a 15+13+28+30+60+20+35=201 </li> <li> Slide 10 </li> <li> ABCDEFG A755028351522 B753060806550 C 3040483528 D 6040203029 E358048204032 F156535304013 G225028293213 THE FOLLOWING TABLE TELLS US HOW MANY TONS OF TYLIUM IT TAKES TO TRAVEL FROM ONE PLANET TO ANOTHER: NOW STARTING WITH B WE GET: 30+28+13+15+28+20+80= 214 b,c,g,f,a,d,e,b </li> <li> Slide 11 </li> <li> ABCDEFG A755028351522 B753060806550 C 3040483528 D 6040203029 E358048204032 F156535304013 G225028293213 THE FOLLOWING TABLE TELLS US HOW MANY TONS OF TYLIUM IT TAKES TO TRAVEL FROM ONE PLANET TO ANOTHER: NOW STARTING WITH C WE GET: 28+13+15+28+20+80+30= 214 c,g,f,a,d,e,b,c </li> <li> Slide 12 </li> <li> ABCDEFG A755028351522 B753060806550 C 3040483528 D 6040203029 E358048204032 F156535304013 G225028293213 THE FOLLOWING TABLE TELLS US HOW MANY TONS OF TYLIUM IT TAKES TO TRAVEL FROM ONE PLANET TO ANOTHER: SIMILARLY FOR d, e, f AND g WE GET: 20+35+15+13+28+50+60= 221 d,e,a,f,g,c,b,d 20+28+15+13+28+30+80= 214 e,d,a,f,g,c,b,e 15+22+29+20+48+30+65= 229 f,a,g,d,e,c,b,e 13+15+28+20+48+30+50= 204 g,f,a,d,e,c,b </li> <li> Slide 13 </li> <li> The Cheapest-Link Algorithm Pick the edge with the smallest weight first. Mark the edge (or otherwise note that you have chosen it). Pick the next cheapest edge. Mark or note it. Continue picking the cheapest edge available and mark the edge as long as (a) it does not close a circuit and (b) it does not result in three edges coming out of a single verrtex. When there are no more vertices left, close the circuit. </li> <li> Slide 14 </li> <li> A B C G F E D 75 22 50 13 40 20 40 30 65 50 80 35 29 15 48 35 60 28 30 32 28 WE CAN USE THIS NEW ALGORITHM ON OUR SECOND EXAMPLE: </li> <li> Slide 15 </li> <li> A B C G F E D 75 22 50 13 40 20 40 30 65 50 80 35 29 15 48 35 60 28 30 32 28 </li> <li> Slide 16 </li> <li> A Few Final notes: The Nearest-Neighbor, Repetitive N.N. and Cheapest- Link Algorithms are all approximate algorithms. I.e. - The routes they make may not be the optimal solution, but they are usually better than a random approach. The amount of time needed to arrive at these approximate routes is much shorter than using the Brute-Force method; which is why they are used more often in real-life situations. The Nearest-Neighbor, Repetitive N.N. and Cheapest- Link Algorithms are all approximate algorithms. I.e. - The routes they make may not be the optimal solution, but they are usually better than a random approach. The amount of time needed to arrive at these approximate routes is much shorter than using the Brute-Force method; which is why they are used more often in real-life situations. </li> </ul>