§ 6.5 - 6.8 Algorithms, Algorithms, Algorithms

15
§ 6.5 - 6.8 § 6.5 - 6.8 Algorithms, Algorithms, Algorithms, Algorithms Algorithms, Algorithms

description

§ 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 a particular problem. - PowerPoint PPT Presentation

Transcript of § 6.5 - 6.8 Algorithms, Algorithms, Algorithms

Page 1: § 6.5 - 6.8 Algorithms, Algorithms, Algorithms

§ 6.5 - 6.8 § 6.5 - 6.8 Algorithms, Algorithms,

Algorithms, AlgorithmsAlgorithms, Algorithms

Page 2: § 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 a particular problem.

Page 3: § 6.5 - 6.8 Algorithms, Algorithms, Algorithms

Think back to the `Homer` example from Think back to the `Homer` example from yesterday.yesterday.The method we used there can be The method we used there can be written as a slightly more formal written as a slightly more formal algorithm:algorithm:The Brute-Force The Brute-Force AlgorithmAlgorithm

• First, make a list of First, make a list of all the possible all the possible Hamiltonian circuits of the Hamiltonian circuits of the graph.graph.

• For each circuit, For each circuit, calculate its calculate its totaltotal weight weight by summing the weights of by summing the weights of all the edges traveled.all the edges traveled.

• Find the circuits* with Find the circuits* with the least total weight. the least total weight. Any of these can be chosen Any of these can be chosen as an optimal H.C.as an optimal H.C.

Page 4: § 6.5 - 6.8 Algorithms, Algorithms, Algorithms

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.

Page 5: § 6.5 - 6.8 Algorithms, Algorithms, Algorithms

So which of these methods is better?

The Brute-ForceBrute-Force Method will always give the optimal solution, but it is inefficient.

The Nearest NeighborNearest 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.

Page 6: § 6.5 - 6.8 Algorithms, Algorithms, Algorithms

The Repetitive Nearest-Neighbor The Repetitive Nearest-Neighbor AlgorithmAlgorithm Let Let XX be any vertex. Apply the Nearest- be any vertex. Apply the Nearest-Neighbor Algorithm using Neighbor Algorithm using XX as the starting point as the starting point and calculate the total cost for the circuit.and calculate the total cost for the circuit.

Repeat the process for each of the other Repeat the process for each of the other vertices of the graph.vertices of the graph.

Choose the best Hamiltonian circuit you find. Choose the best Hamiltonian circuit you find. If we have designated a starting vertex, then If we have designated a starting vertex, then rewrite the circuit with that vertex as the rewrite the circuit with that vertex as the reference point.reference point.

Page 7: § 6.5 - 6.8 Algorithms, Algorithms, Algorithms

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.

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. . .

Page 8: § 6.5 - 6.8 Algorithms, Algorithms, Algorithms

A B C D E F G

A 75 50 28 35 15 22

B 75 30 60 80 65 50

C 50 30 40 48 35 28

D 28 60 40 20 30 29

E 35 80 48 20 40 32

F 15 65 35 30 40 13

G 22 50 28 29 32 13

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,a15+13+28+30+60+20+35=201

Page 9: § 6.5 - 6.8 Algorithms, Algorithms, Algorithms

A B C D E F G

A 75 50 28 35 15 22

B 75 30 60 80 65 50

C 50 30 40 48 35 28

D 28 60 40 20 30 29

E 35 80 48 20 40 32

F 15 65 35 30 40 13

G 22 50 28 29 32 13

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=214b,c,g,f,a,d,e,b

Page 10: § 6.5 - 6.8 Algorithms, Algorithms, Algorithms

A B C D E F G

A 75 50 28 35 15 22

B 75 30 60 80 65 50

C 50 30 40 48 35 28

D 28 60 40 20 30 29

E 35 80 48 20 40 32

F 15 65 35 30 40 13

G 22 50 28 29 32 13

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=214c,g,f,a,d,e,b,c

Page 11: § 6.5 - 6.8 Algorithms, Algorithms, Algorithms

A B C D E F G

A 75 50 28 35 15 22

B 75 30 60 80 65 50

C 50 30 40 48 35 28

D 28 60 40 20 30 29

E 35 80 48 20 40 32

F 15 65 35 30 40 13

G 22 50 28 29 32 13

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=221d,e,a,f,g,c,b,d

20+28+15+13+28+30+80=214e,d,a,f,g,c,b,e

15+22+29+20+48+30+65=229f,a,g,d,e,c,b,e

13+15+28+20+48+30+50=204g,f,a,d,e,c,b

Page 12: § 6.5 - 6.8 Algorithms, Algorithms, Algorithms

The Cheapest-Link Algorithm The Cheapest-Link Algorithm

Pick the edge with the smallest Pick the edge with the smallest weight first. Mark the edge (or weight first. Mark the edge (or otherwise note that you have chosen otherwise note that you have chosen it).it).

Pick the next ‘cheapest’ edge. Pick the next ‘cheapest’ edge. Mark or note it.Mark or note it.

Continue picking the ‘cheapest’ Continue picking the ‘cheapest’ edge available and mark the edge as edge available and mark the edge as long aslong as

(a) it does not close a circuit (a) it does not close a circuit andand

(b) it does not result in three (b) it does not result in three edges coming out of a singleedges coming out of a single

verrtex.verrtex.

When there are no more vertices When there are no more vertices left, close the circuit.left, close the circuit.

Page 13: § 6.5 - 6.8 Algorithms, Algorithms, Algorithms

A

B

C

G

FE

D

75

22

50

13

40

20

40

30 6550

80

35

2915

4835

60

28

30 32

28

WE CAN USE THIS NEW ALGORITHM ON OUR SECOND EXAMPLE:

Page 14: § 6.5 - 6.8 Algorithms, Algorithms, Algorithms

A

B

C

G

FE

D

75

22

50

13

40

20

40

30 6550

80

35

2915

4835

60

28

30 32

28

Page 15: § 6.5 - 6.8 Algorithms, Algorithms, Algorithms

A Few Final notes:A Few Final notes:A Few Final notes: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.