u08282 - algorithm and advance data structure

19
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: Tong Quang Long E-mail Address: [email protected] Contact No: 84568570 Name of Lecturer: Dr Namasivayam Krishnamoorthy Student ID: 018800020088 Class Code: U08282 Subject Code: U08282 – Algorithms and Advanced Data Structures Assignment T3 – 2012 No. of pages including this cover page : Other Specifications: Enclosed CD: Yes Attached turnitin.com Report: Yes Submission Due Date: Declaration : 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 Signature : Question Total Marker Internal Moderator External Moderator Marker's Comment: T312 U08282 CW QPMS Page 1 of 19 BACHELOR OF SCIENCE Honours Computer and Information System

Upload: italiandude604

Post on 13-Apr-2015

76 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: U08282 - Algorithm and Advance Data Structure

COURSEWORK SUBMISSION FORMInstructions1. 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: Tong Quang Long

E-mail Address: [email protected] Contact No: 84568570

Name of Lecturer: Dr Namasivayam Krishnamoorthy

Student ID: 018800020088 Class Code: U08282

Subject Code:

U08282 –Algorithms and Advanced Data Structures 

Assignment T3 – 2012

No. of pages includingthis cover page :

OtherSpecifications:

Enclosed CD: Yes Attached turnitin.com

Report: Yes

Submission Date: Due Date:Declaration : 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.

Signature :

Question Total

Marker

Internal Moderator

External Moderator

Marker's Comment:

OXFORD BROOKES UNIVERSITY

T312 U08282 CW QPMS Page 1 of 16

BACHELOR OF SCIENCEHonours Computer and

Information System

Page 2: U08282 - Algorithm and Advance Data Structure

BACHELOR OF SCIENCE (HONOURS)U08282 – ALGORITHMS AND ADVANCED DATA

STRUCTURES

TERM 3, 2012

T312 U08282 CW QPMS Page 2 of 16

Page 3: U08282 - Algorithm and Advance Data Structure

Table of Contents

A. Introduction....................................................................................................4

B. History and Development...................................................................................4

C. Solving Methods..............................................................................................5

D. Minimum Spanning Tree Based Heuristics............................................................7

E. Nearest Neighbour Heuristic.............................................................................12

F. Comparison..................................................................................................13

G. Conclusion....................................................................................................15

H. Appendix......................................................................................................16

T312 U08282 CW QPMS Page 3 of 16

Page 4: U08282 - Algorithm and Advance Data Structure

A. 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 doesn’t know total cost of tour. Thus “Traveling

Salesman Problem” was born to solve the problem.

B. History and Development

The origins of the traveling salesman problem are unclear. The travelling

salesman problem was defined in the nineteen century by the Irish mathematician

“William Rowan Hamilton” and by the British mathematician “Thomas Kirkman”. The

general case of TSP has been researched by mathematicians during in the nineteen-

thirty, especially is “Karl Menger” who defines the problem, consider and find out

nearest neighbor algorithm is not optimal. During nineteen-fifty and nineteen-sixty, the

problem become popular in the world of scientific research at Europe and United

States. In nineteen-seventy-two, “Richard M. Karp” had proved that Hamiltonian

Cycle problem was NP-complete which implies the NP-hardness of TSP. Follow the

development of scientific world, the TSP has been solved in many ways until now.

Traveling Salesman Problem can be modelled as an undirected weight graph

include cities are the graph’s vertices, paths are the graph edges and path distance is

the edge length. The problem can be imagine as salesman begin from a random city,

delivery a packet through another city and go back the first city. In this case, each city

can only visit once at each time and the distance has been known. The problem is he

need to find out a shortest route that satisfied all conditions.

INCLUDEPICTURE

"http://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Weighted_K4.svg/180px-

Weighted_K4.svg.png" \* MERGEFORMATINET INCLUDEPICTURE

"http://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Weighted_K4.svg/180px-

Weighted_K4.svg.png" \* MERGEFORMATINET INCLUDEPICTURE

"http://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Weighted_K4.svg/180px-

Weighted_K4.svg.png" \* MERGEFORMATINET INCLUDEPICTURE

T312 U08282 CW QPMS Page 4 of 16

Page 5: U08282 - Algorithm and Advance Data Structure

"http://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Weighted_K4.svg/180px-

Weighted_K4.svg.png" \* MERGEFORMATINET INCLUDEPICTURE

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

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

T312 U08282 CW QPMS Page 5 of 16

Page 6: U08282 - Algorithm and Advance Data Structure

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

Nearest Neighbour Heuristic

T312 U08282 CW QPMS Page 6 of 16

Page 7: U08282 - Algorithm and Advance Data Structure

D. Minimum Spanning Tree Based Heuristics

1) Definition

At first, I will explain what is spanning tree. A spanning tree is a sub-graph

of a given undirected and connected graph. This spanning tree will connect all

the vertices together. A single graph can have many different spanning trees

and these spanning trees will contain every vertex of graph.

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:

T312 U08282 CW QPMS Page 7 of 16

Page 8: U08282 - Algorithm and Advance Data Structure

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

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, I will use Prim’s Algorithm as the solution for my Traveling

Salesman Problem. Because Kruskal’s Algorithm allows to join both “new”

vertex to “new” vertex and “old” vertex to “old” vertex to get connected, so

Kruskal’s Algorithm has a larger complexity than Prim’s Algorithm

2) Prim’s 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

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.

T312 U08282 CW QPMS Page 8 of 16

Page 9: U08282 - Algorithm and Advance Data Structure

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

T312 U08282 CW QPMS Page 9 of 16

Page 10: U08282 - Algorithm and Advance Data Structure

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:

T312 U08282 CW QPMS Page 10 of 16

Page 11: U08282 - Algorithm and Advance Data Structure

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

Page 12: U08282 - Algorithm and Advance Data Structure

E. Nearest Neighbour Heuristic

1) Definition

This algorithm also called “Greedy Algorithm” was the first algorithm has

been used to solve Traveling Salesman Problem. In this algorithm, the

salesman will start at a random city and keep visiting to the nearest cities until

all cities have been visited. And at last, the salesman returns back to the start.

This nearest neighbor algorithm is easy to implement and have quickly

executive, but it can miss shorter routes sometimes. 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. Most of the step, 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.

I will list the steps for better viewing about the algorithm:

Pick up a random city as the starting city

Next, 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, the salesman will back to the first starting point when he visit

all the cities in list

3) Implementation and Statistics

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

T312 U08282 CW QPMS Page 12 of 16

Page 13: U08282 - Algorithm and Advance Data Structure

Statistic of the experiment with one-hundred cities:

F. 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 Minimum Spanning Tree Nearest Neighbor Heuristic

N = 100average execution time : 18ms

average length of tour : 9465

average execution time : 18ms

average length of tour : 11739

N = 200average execution time : 48ms

average length of tour : 12508

average execution time : 46ms

average length of tour : 16798

N = 300average execution time : 65ms

average length of tour : 15188

average execution time : 56ms

average length of tour : 20700

N = 400average execution time : 114ms

average length of tour : 17567

average execution time : 81ms

average length of tour : 23072

N = 500 average execution time : 173ms average execution time : 104ms

T312 U08282 CW QPMS Page 13 of 16

Page 14: U08282 - Algorithm and Advance Data Structure

average length of tour : 19207 average length of tour : 26031

N = 600average execution time : 252ms

average length of tour : 20741

average execution time : 111ms

average length of tour : 28257

N = 700average execution time : 372ms

average length of tour : 22382

average execution time : 121ms

average length of tour : 30609

N = 800average execution time : 516ms

average length of tour : 23813

average execution time : 133ms

average length of tour : 32922

N = 900average execution time : 723ms

average length of tour : 25642

average execution time : 151ms

average length of tour : 34313

N = 1000average execution time : 957ms

average length of tour : 26747

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.

T312 U08282 CW QPMS Page 14 of 16

Page 15: U08282 - Algorithm and Advance Data Structure

G. 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 Prim’s 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.

T312 U08282 CW QPMS Page 15 of 16

Page 16: U08282 - Algorithm and Advance Data Structure

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

T312 U08282 CW QPMS Page 16 of 16