You are on page 1of 7

Travelling Salesman Problem (Greedy Approach)

The t ravelling salesman problem is a graph comput at ional problem where t he salesman needs t o visit
all cit ies (represent ed using nodes in a graph) in a list just once and t he dist ances (represent ed using
edges in t he graph) bet ween all t hese cit ies are known. The solut ion t hat is needed t o be found for
t his problem is t he short est possible rout e in which t he salesman visit s all t he cit ies and ret urns t o
t he origin cit y.

If you look at t he graph below, considering t hat t he salesman st art s from t he vert ex ‘a’, t hey need t o
t ravel t hrough all t he remaining vert ices b, c, d, e, f and get back t o ‘a’ while making sure t hat t he
cost t aken is minimum.

There are various approaches t o find t he solut ion t o t he t ravelling salesman problem: naive approach,
greedy approach, dynamic programming approach, et c. In t his t ut orial we will be learning about
solving t ravelling salesman problem using greedy approach.

Travelling Salesperson Algorithm


As t he definit ion for greedy approach st at es, we need t o find t he best opt imal solut ion locally t o
figure out t he global opt imal solut ion. The input s t aken by t he algorit hm are t he graph G {V, E}, where
V is t he set of vert ices and E is t he set of edges. The short est pat h of graph G st art ing from one
vert ex ret urning t o t he same vert ex is obt ained as t he out put .

Algorithm
Travelling salesman problem t akes a graph G {V, E} as an input and declare anot her graph as
t he out put (say G’) which will record t he pat h t he salesman is going t o t ake from one node t o
anot her.
The algorit hm begins by sort ing all t he edges in t he input graph G from t he least dist ance t o
t he largest dist ance.
The first edge select ed is t he edge wit h least dist ance, and one of t he t wo vert ices (say A
and B) being t he origin node (say A).

Then among t he adjacent edges of t he node ot her t han t he origin node (B), find t he least
cost edge and add it ont o t he out put graph.
Cont inue t he process wit h furt her nodes making sure t here are no cycles in t he out put graph
and t he pat h reaches back t o t he origin node A.
However, if t he origin is ment ioned in t he given problem, t hen t he solut ion must always st art
from t hat node only. Let us look at some example problems t o underst and t his bet t er.

Examples

Consider t he following graph wit h six cit ies and t he dist ances bet ween t hem −

From t he given graph, since t he origin is already ment ioned, t he solut ion must always st art from t hat
node. Among t he edges leading from A, A → B has t he short est dist ance.
Then, B → C has t he short est and only edge bet ween, t herefore it is included in t he out put graph.

There’s only one edge bet ween C → D, t herefore it is added t o t he out put graph.

There’s t wo out ward edges from D. Even t hough, D → B has lower dist ance t han D → E, B is already
visit ed once and it would form a cycle if added t o t he out put graph. Therefore, D → E is added int o
t he out put graph.
There’s only one edge from e, t hat is E → F. Therefore, it is added int o t he out put graph.

Again, even t hough F → C has lower dist ance t han F → A, F → A is added int o t he out put graph in
order t o avoid t he cycle t hat would form and C is already visit ed once.
The short est pat h t hat originat es and ends at A is A → B → C → D → E → F → A

The cost of t he pat h is: 16 + 21 + 12 + 15 + 16 + 34 = 114.

Even t hough, t he cost of pat h could be decreased if it originat es from ot her nodes but t he quest ion
is not raised wit h respect t o t hat .

Example
The complet e implement at ion of Travelling Salesman Problem using Greedy Approach is given below

C C++ Java Pyt hon

#include <stdio.h>
int tsp_g[10][10] = {
{12, 30, 33, 10, 45},
{56, 22, 9, 15, 18},
{29, 13, 8, 5, 12},
{33, 28, 16, 10, 3},
{1, 4, 30, 24, 20}
};
int visited[10], n, cost = 0;

/* creating a function to generate the shortest path */


void travellingsalesman(int c){
int k, adj_vertex = 999;
int min = 999;

/* marking the vertices visited in an assigned array */


visited[c] = 1;

/* displaying the shortest path */


printf("%d ", c + 1);

/* checking the minimum cost edge in the graph */


for(k = 0; k < n; k++) {
if((tsp_g[c][k] != 0) && (visited[k] == 0)) {
if(tsp_g[c][k] < min) {
min = tsp_g[c][k];
}
adj_vertex = k;
}
}
if(min != 999) {
cost = cost + min;
}
if(adj_vertex == 999) {
adj_vertex = 0;
printf("%d", adj_vertex + 1);
cost = cost + tsp_g[c][adj_vertex];
return;
}
travellingsalesman(adj_vertex);
}

/* main function */
int main(){
int i, j;
n = 5;
for(i = 0; i < n; i++) {
visited[i] = 0;
}
printf("Shortest Path: ");
travellingsalesman(0);
printf("\nMinimum Cost: ");
printf("%d\n", cost);
return 0;
}

Output

Shortest Path: 1 5 4 3 2 1
Minimum Cost: 99

You might also like