You are on page 1of 16

B O KS R O E

U IV R IT N E S Y

OF R XO D

BACHELOR OF SCIENCE Honours Computer and Information System

COURSEWORK SUBMISSION FORM
Instructions 1. Please complete the form using Capital Letters. 2. Coursework must be submitted through the Service counter and it is the responsibility of the student to complete the Coursework Submission Log Book. Name of Student: E-mail Address: Name of Lecturer: Student ID: Subject Code: U08282 – Algorithms and Advanced Data Structures Dr Namasivayam Krishnamoorthy Class Code: Assignment U08282 T3 – 2012 Contact No:

No. of pages including this cover page :

Other Specifications:

Enclosed CD: Yes Attached turnitin.com Report: Yes

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.

Question Marker Internal Moderator External Moderator Marker's Comment:

Total

T312 U08282 CW QPMS

Page 1 of 16

2012 T312 U08282 CW QPMS Page 2 of 16 .OXFORD BROOKES UNIVERSITY BACHELOR OF SCIENCE (HONOURS) U08282 – ALGORITHMS AND ADVANCED DATA STRUCTURES TERM 3.

.........................................................................................................................................................................................................................................................................................................................................................................  6     Minimum Spanning Tree Based Heuristics                                                                                                              ............................  1     Table of Contents                                                                                                                                                          ...........  16     T312 U08282 CW QPMS Page 3 of 16 ........................................................................  15     Appendix                                                                                                                                                                      ....................................  4     History and Development                                                                                                                                           .......................................................................Table of Contents   Enclosed CD:  Yes                                                                                                                                                 ................................................................................................................................................  4     Solving Methods                                                                                                                                                           ....................................................................................................................................................................................................  12     Comparison                                                                                                                                                                  ...........................................................................  7     Nearest Neighbour Heuristic                                                                                                                                     .....................................................................................................  1     Attached turnitin............................  13     Conclusion                                                                                                                                                                    .......................  3     Introduction                                                                                                                                                                   ...................................com Report:  Yes                                                                                                                    .....................................................................................................................................................................................................................................................................................................................................................................

svg.svg/180pxWeighted_K4. Follow the development of scientific world.svg. During nineteen-fifty and nineteen-sixty.wikimedia.org/wikipedia/commons/thumb/3/30/Weighted_K4. The general case of TSP has been researched by mathematicians during in the nineteenthirty. The problem can be imagine as salesman begin from a random city. especially is “Karl Menger” who defines the problem. consider and find out nearest neighbor algorithm is not optimal. the TSP has been solved in many ways until now. In this case. In this case. The travelling salesman problem was defined in the nineteen century by the Irish mathematician “William Rowan Hamilton” and by the British mathematician “Thomas Kirkman”.org/wikipedia/commons/thumb/3/30/Weighted_K4. The problem is he need to find out a shortest route that satisfied all conditions. the problem become popular in the world of scientific research at Europe and United States. “Richard M. paths are the graph edges and path distance is the edge length.svg/180px­   Weighted_K4.                                                                                    INCLUDEPICTURE  "http://upload. Thus “Traveling Salesman Problem” was born to solve the problem.svg. 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. History and Development The origins of the traveling salesman problem are unclear.wikimedia. Traveling Salesman Problem can be modelled as an undirected weight graph include cities are the graph’s vertices.svg/180px- . salesman known the cost of traveling from current city to another city but he doesn’t know total cost of tour.png"   \*   MERGEFORMATINET    INCLUDEPICTURE   INCLUDEPICTURE INCLUDEPICTURE Page 4 of 16 "http://upload.png" Weighted_K4. In nineteen-seventy-two.png" T312 U08282 CW QPMS \* \* MERGEFORMATINET MERGEFORMATINET "http://upload. Karp” had proved that Hamiltonian Cycle problem was NP-complete which implies the NP-hardness of TSP.Introduction Traveling Salesman Problem is a mathematical point of view of the following problem taken from the everyday life. delivery a packet through another city and go back the first city.org/wikipedia/commons/thumb/3/30/Weighted_K4.wikimedia. each city can only visit once at each time and the distance has been known.

we can calculate the distance between two points.svg. T312 U08282 CW QPMS Page 5 of 16 .org/wikipedia/commons/thumb/3/30/Weighted_K4. b) Asymmetric Opposite with Euclidian symmetric.wikimedia. 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”."http://upload. symmetric Euclidian problem is undirected graphs.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.org/wikipedia/commons/thumb/3/30/Weighted_K4. asymmetric problem has been defined as the distance is not equal from “City A” to “City B” and “City B” to “City A”.svg. In other words.svg/180pxWeighted_K4.svg/180px- Weighted_K4. 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.wikimedia.png" \* MERGEFORMATINET INCLUDEPICTURE "http://upload.

After that. 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. this method also depend on which construction heuristic using. Under my studying. Although it can create the tour but it does not improve the quality of tour. it will try to obtain a new modification of solution Depend on my topic. Firstly. There are few construction that will be categorized below: • Nearest Neighbor: in this construction heuristic. 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. the salesman starts at a city and then visits the city nearest to the starting city. More than that. I will need to explain the Traveling Salesman Problem in two ways of solving the problem include: • Minimum Spanning Tree Based Heuristic T312 U08282 CW QPMS Page 6 of 16 . 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. I will make some explanation about two different methods way to solve TSP problem in the categories below. 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. In case if it does not found any improvement.Solving Methods Follow the research on the TSP. we learnt that there are many ways to solve TSP problem.

Below is an example of minimum spanning tree: T312 U08282 CW QPMS Page 7 of 16 . This spanning tree will connect all the vertices together. And among all the spanning trees of a weighted and connected graph. A spanning tree is a sub-graph of a given undirected and connected graph. A single graph can have many different spanning trees and these spanning trees will contain every vertex of graph.• Nearest Neighbour Heuristic Minimum Spanning Tree Based Heuristics 1) Definition At first. I will explain what is spanning tree. the spanning tree with the least total weight is called Minimum Spanning Tree.

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. The formula will be as the salesman will visit all the cities and return back to starting point P1 T312 U08282 CW QPMS Page 8 of 16 . I will use Prim’s Algorithm as the solution for my Traveling Salesman Problem. The S set will contain all the points as the roots of the Spanning Tree. Because Kruskal’s Algorithm allows to join both “new” vertex to “new” vertex and “old” vertex to “old” vertex to get connected. But now. 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. so Kruskal’s Algorithm has a larger complexity than Prim’s Algorithm 2) Prim’s Algorithm At first. 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. there are two common algorithms that usually used are • Prim’s 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 • Kruskal’s Algorithm This minimum spanning tree grown from a forest of spanning tree by adding the smallest edge connecting two spanning tree In my opinion.

the salesman must visit all the cities. it will be deleted from set S until all the points attach to the graph. we will construct files such as Cities and Travel to collect information of cities. 3) Implementation and Statistic To implement the algorithm. the algorithm will stop and output the minimum spanning tree. After all the cities has been visited and salesman returns back to the starting point. In other meaning. When a new point attached to graph tree. In this Cities.In every execution. a new point will be attached to the tree without numbering order. it include coordinates of cities T312 U08282 CW QPMS Page 9 of 16 .java.

At first. The picture below will explain this more clearly After prepare necessary file. I will pick up a random number as a root tree by using the code in the picture below: T312 U08282 CW QPMS Page 10 of 16 . This file also include the distance between each cities.Next is Travel.java. I will begin to exploit the algorithm. this is also a graph for cities information.

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 Statistics of the experiment with one-hundred cities: T312 U08282 CW QPMS Page 11 of 16 .

I will list the steps for better viewing about the algorithm: • • • Pick up a random city as the starting city Next. This nearest neighbor algorithm is easy to implement and have quickly executive. the salesman will start at a random city and keep visiting to the nearest cities until all cities have been visited. In this algorithm. This algorithm might be find a short tour in faster way but it is not the optimal way 2) Nearest Neighbour Algorithm The next is the nearest neighbor algorithms. the salesman will back to the first starting point when he visit all the cities in list 3) Implementation and Statistics T312 U08282 CW QPMS Page 12 of 16 . And at last. Most of the step.Nearest Neighbour Heuristic 1) Definition This algorithm also called “Greedy Algorithm” was the first algorithm has been used to solve Traveling Salesman Problem. but it can miss shorter routes sometimes. the nearest neighbor nearly have same formula with Minimum Spanning Tree but the nearest neighbor will find the nearest point with the current point and keep connect until all the points visited. the salesman returns back to the start. the salesman will visit the nearest city with the current point Besides that. the graph will delete the first point to the list and update new city as current city for looping the action • At last.

700. 400. The comparison will follow include: 100. 500. 200. 300.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 Statistic of the experiment with one-hundred cities: Comparison In this section. 800. 1000 cities. 900. I will compare between two algorithms in different number of cities. 600. 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 T312 U08282 CW QPMS Page 13 of 16 .

Minimum spanning tree had satisfied the condition of traveling salesman problem so this algorithm will be a better solution for salesman traveling problem.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. T312 U08282 CW QPMS Page 14 of 16 . And we can see that spanning tree with higher execution time but it had found the shorter route compare with nearest neighbor heuristic.

I have more experience about using Java Language. I have learnt more knowledge about Algorithms. T312 U08282 CW QPMS Page 15 of 16 . More than that. In this assignment. That algorithm can give the shortest path for salesman that business can save more time and cost for delivery on different places.Conclusion Through this assignment and especially the traveling salesman problem. 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 Prim’s Algorithm or Minimum Spanning Tree Algorithm is powerful than Nearest Neighbor Algorithm.

html http://lcm.java2s.Appendix http://en.shtml http://www.ernet.html http://en.com/tutorials/random_numbers/#.upatras.cl/~agv/elo320/animation/prim/prim_kruskal.html http://profesores.html http://math.cprogramming.htm http://www.javamex.csa.people.jp/ikeda/suuri/dijkstra/Prim.ac.htm http://weierstrass.com/Tutorial/Java/CatalogJava.com/books/opus4/html/page577.cs.org/wiki/Nearest_neighbour_algorithm http://en.edu/43mst/ http://www.org/wiki/Prim's_algorithm http://students.vcu.wikipedia.elo.brpreiss.princeton.UNM2u29vMww http://www.wikia.wikipedia.html http://www.in/dsa/node183.org/wiki/Travelling_salesman_problem http://en.tokushima-u.org/wiki/Minimum_spanning_tree http://algs4.is.com/tutorial/computersciencetheory/mst.com/wiki/Proof_of_Prim's_algorithm T312 U08282 CW QPMS Page 16 of 16 .com/travel/class/nearest.gr/~papagel/project/prim.iisc.wikipedia.utfsm.ceid.baseball-reference.edu/~gasmerom/MAT131/mst.html http://www.wikipedia.