U08282 - Algorithm and Advance Data Structure
U08282 - Algorithm and Advance Data Structure
U IV R IT N E S Y
OF R XO D
Other Specifications:
Submission Date: Due Date: Declaration : Signature : I declare that this assignment is my original work and that I have acknowledged any use of published or unpublished works of other people. I understand that I will be penalized for plagiarism and late submission.
Total
Page 1 of 16
OXFORD BROOKES UNIVERSITY BACHELOR OF SCIENCE (HONOURS) U08282 ALGORITHMS AND ADVANCED DATA STRUCTURES
TERM 3, 2012
Page 2 of 16
Table of Contents
EnclosedCD:Yes ........................................................................................................................................... 1 Attachedturnitin.comReport:Yes ............................................................................................................. 1 TableofContents .................................................................................................................................................... 3 Introduction ............................................................................................................................................................. 4 HistoryandDevelopment ..................................................................................................................................... 4 SolvingMethods ..................................................................................................................................................... 6 MinimumSpanningTreeBasedHeuristics ........................................................................................................ 7 NearestNeighbourHeuristic .............................................................................................................................. 12 Comparison ........................................................................................................................................................... 13 Conclusion ............................................................................................................................................................. 15 Appendix ............................................................................................................................................................... 16
Page 3 of 16
Introduction
Traveling Salesman Problem is a mathematical point of view of the following problem taken from the everyday life. The purpose of Traveling Salesman Problem is a traveling salesman has to visit exactly once each one of a list of n cities and then return to the home city. In this case, salesman known the cost of traveling from current city to another city but he doesnt know total cost of tour. Thus Traveling Salesman Problem was born to solve the problem.
Weighted_K4.svg.png"
\*
MERGEFORMATINET
"http://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Weighted_K4.svg/180pxWeighted_K4.svg.png" Weighted_K4.svg.png"
T312 U08282 CW QPMS
\* \*
MERGEFORMATINET MERGEFORMATINET
"http://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Weighted_K4.svg/180px-
Weighted_K4.svg.png" \* MERGEFORMATINET
According to the research on the TSP we have categorizes the problems of TSP into three types: a) Euclidian Symmetric In this term, we can calculate the distance between two points. But the problem is the distance in both direction is equal such as distance from city A to city B is equal city B to city A. In other words, symmetric Euclidian problem is undirected graphs. b) Asymmetric Opposite with Euclidian symmetric, asymmetric problem has been defined as the distance is not equal from City A to City B and City B to City A. Asymmetric problem can be imagined as directed graphs c) Random distance matrixes This problem is unable calculate the distance and it called Random distance matrix problems.
Page 5 of 16
Solving Methods
Follow the research on the TSP, we learnt that there are many ways to solve TSP problem. Under my studying, I will make some explanation about two different methods way to solve TSP problem in the categories below. And I will discuss some methods that related to each category 1) Tour Construction Method This is a simple method that can create tour by using an unordered collection of city from TSP. Although it can create the tour but it does not improve the quality of tour. More than that, this method also depend on which construction heuristic using. There are few construction that will be categorized below: Nearest Neighbor: in this construction heuristic, the salesman starts at a city and then visits the city nearest to the starting city. After that, the salesman will be continued to visit the nearest city that was not visited until the salesman visited all cities and returned back to the starting point city Greedy: this construction heuristic is a simple construction by growing Hamiltonian cycle in the graph with one time visited only. Firstly, the algorithm will pick the shortest edge and then add this shortest edge to the tour until all the cities are included in the tour. 2) Local Search Heuristics A local search method will accept the tour of some construction heuristic as an input and using some heuristic algorithm to make a change in the tour such as produce a tour shorter the previous one. In case if it does not found any improvement, it will try to obtain a new modification of solution Depend on my topic, I will need to explain the Traveling Salesman Problem in two ways of solving the problem include: Minimum Spanning Tree Based Heuristic
Page 6 of 16
And among all the spanning trees of a weighted and connected graph, the spanning tree with the least total weight is called Minimum Spanning Tree. Below is an example of minimum spanning tree:
Page 7 of 16
The first algorithm had been found in nineteen-twenty-six by a Czech scientist named Otakar Boruvka and his name had been named for the algorithm. But now, there are two common algorithms that usually used are Prims Algorithm This is a minimum spanning tree has been grown from the current spanning tree by adding the nearest vertex and the edge connecting the nearest vertex to the minimum spanning tree Kruskals Algorithm This minimum spanning tree grown from a forest of spanning tree by adding the smallest edge connecting two spanning tree In my opinion, I will use Prims Algorithm as the solution for my Traveling Salesman Problem. Because Kruskals Algorithm allows to join both new vertex to new vertex and old vertex to old vertex to get connected, so Kruskals Algorithm has a larger complexity than Prims Algorithm 2) Prims Algorithm At first, a point will be choose randomly as a vertex for the tree and I will name it P1 as the assignment requirement. P1 will have the coordinate as x1 and y1 and the limitation of x1 and y1 between from 0 to 999. The S set will contain all the points as the roots of the Spanning Tree. As because the P1 has been attached to the tree so the S in this explanation will contain all the rest of points as the rest of cities. The formula will be as the salesman will visit all the cities and return back to starting point P1
Page 8 of 16
In every execution, a new point will be attached to the tree without numbering order. When a new point attached to graph tree, it will be deleted from set S until all the points attach to the graph. In other meaning, the salesman must visit all the cities. After all the cities has been visited and salesman returns back to the starting point, the algorithm will stop and output the minimum spanning tree.
3) Implementation and Statistic To implement the algorithm, we will construct files such as Cities and Travel to collect information of cities. In this Cities.java, it include coordinates of cities
Page 9 of 16
Next is Travel.java, this is also a graph for cities information. This file also include the distance between each cities. The picture below will explain this more clearly
After prepare necessary file, I will begin to exploit the algorithm. At first, I will pick up a random number as a root tree by using the code in the picture below:
Page 10 of 16
Next, I will insert the math formula to show the coordinates and distance of the tour into the code by using the code below:
At last, the algorithm need to repeat ten times to finish the experiment
Page 11 of 16
Page 12 of 16
Most of the step the nearest algorithm is as same as minimum spanning tree algorithm but there is only one step is different from minimum spanning tree
Comparison
In this section, I will compare between two algorithms in different number of cities. The comparison will follow include: 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 cities. Each of the comparison will repeat ten times and show the average length of tour and average execution time for statistics The comparison will be shown in the table below: Cities No N = 100 average length of tour : 9465 average execution time : 48ms N = 200 average length of tour : 12508 average length of tour : 16798 average length of tour : 11739 average execution time : 46ms Minimum Spanning Tree average execution time : 18ms Nearest Neighbor Heuristic average execution time : 18ms
Page 13 of 16
average execution time : 65ms N = 300 average length of tour : 15188 average execution time : 114ms N = 400 average length of tour : 17567 average execution time : 173ms N = 500 average length of tour : 19207 average execution time : 252ms N = 600 average length of tour : 20741 average execution time : 372ms N = 700 average length of tour : 22382 average execution time : 516ms N = 800 average length of tour : 23813 average execution time : 723ms N = 900 average length of tour : 25642 average execution time : 957ms N = 1000 average length of tour : 26747
average execution time : 56ms average length of tour : 20700 average execution time : 81ms average length of tour : 23072 average execution time : 104ms average length of tour : 26031 average execution time : 111ms average length of tour : 28257 average execution time : 121ms average length of tour : 30609 average execution time : 133ms average length of tour : 32922 average execution time : 151ms average length of tour : 34313 average execution time : 166ms average length of tour : 36252
As the comparison has shown that nearest neighbor heuristic had the execution time lower compare with minimum spanning tree because the algorithm is not more complex than minimum spanning tree. And we can see that spanning tree with higher execution time but it had found the shorter route compare with nearest neighbor heuristic. Minimum spanning tree had satisfied the condition of traveling salesman problem so this algorithm will be a better solution for salesman traveling problem.
Page 14 of 16
Conclusion
Through this assignment and especially the traveling salesman problem, I have learnt more knowledge about Algorithms. More than that, I have more experience about using Java Language. In this assignment, although these two algorithms are common algorithms using on the world but after a lot of works and comparison I had found out that the Prims Algorithm or Minimum Spanning Tree Algorithm is powerful than Nearest Neighbor Algorithm. That algorithm can give the shortest path for salesman that business can save more time and cost for delivery on different places.
Page 15 of 16
Appendix
http://en.wikipedia.org/wiki/Nearest_neighbour_algorithm http://en.wikipedia.org/wiki/Travelling_salesman_problem http://en.wikipedia.org/wiki/Minimum_spanning_tree http://algs4.cs.princeton.edu/43mst/ http://www.people.vcu.edu/~gasmerom/MAT131/mst.html http://www.javamex.com/tutorials/random_numbers/#.UNM2u29vMww http://www.java2s.com/Tutorial/Java/CatalogJava.htm http://www.baseball-reference.com/travel/class/nearest.html http://en.wikipedia.org/wiki/Prim's_algorithm http://students.ceid.upatras.gr/~papagel/project/prim.htm http://weierstrass.is.tokushima-u.ac.jp/ikeda/suuri/dijkstra/Prim.shtml http://www.cprogramming.com/tutorial/computersciencetheory/mst.html http://lcm.csa.iisc.ernet.in/dsa/node183.html http://www.brpreiss.com/books/opus4/html/page577.html http://profesores.elo.utfsm.cl/~agv/elo320/animation/prim/prim_kruskal.html http://math.wikia.com/wiki/Proof_of_Prim's_algorithm
Page 16 of 16