Travelling Salesman Problem
Travelling Salesman Problem
If you look at the graph below, considering that the salesman starts from
the vertex ‘a’, they need to travel through all the remaining vertices b, c,
d, e, f and get back to ‘a’ while making sure that the cost taken is
minimum.
To find the best optimal solution locally to figure out the global optimal
solution. The inputs taken by the algorithm are the graph G {V, E}, where
V is the set of vertices and E is the set of edges. The shortest path of graph
G starting from one vertex returning to the same vertex is obtained as the
output.
Algorithm
• The algorithm begins by sorting all the edges in the input graph G
from the least distance to the largest distance.
• The first edge selected is the edge with least distance, and one of the
two vertices (say A and B) being the origin node (say A).
• Then among the adjacent edges of the node other than the origin
node (B), find the least cost edge and add it onto the output graph.
• Continue the process with further nodes making sure there are no
cycles in the output graph and the path reaches back to the origin
node A.
Examples
Consider the following graph with six cities and the distances between
them −
From the given graph, since the origin is already mentioned, the solution
must always start from that node. Among the edges leading from A, A →
B has the shortest distance.
There are two outward edges from D. Even though, D → B has lower
distance than D → E, B is already visited once and it would form a cycle if
added to the output graph. Therefore, D → E is added into the output
graph.
There is only one edge from e, that is E → F. Therefore, it is added into the
output graph.
Again, even though F → C has lower distance than F → A, F → A is added
into the output graph to avoid the cycle that would form and C is already
visited once.
Example
Program
#include <stdio.h>
int tsp_g[10][10] = {
};
visited[c] = 1;
/* displaying the shortest path */
min = tsp_g[c][k];
adj_vertex = k;
if(min != 999) {
if(adj_vertex == 999) {
adj_vertex = 0;
return;
travellingsalesman(adj_vertex);
}
/* main function */
int main(){
int i, j;
n = 5;
visited[i] = 0;
travellingsalesman(0);
printf("%d\n", cost);
return 0;
Output
Shortest Path: 1 4 5 2 3 1
Minimum Cost: 55
EVALUATION OF EXPRESSION
• Infix Notation
These notations are named as how they use operator in expression. We shall
learn the same here in this chapter.
Infix Notation
We write expression in infix notation, e.g. a - b + c, where operators are used in-
between operands. It is easy for us humans to read, write, and speak in infix
notation but the same does not go well with computing devices. An algorithm to
process infix notation could be difficult and costly in terms of time and space
consumption.
Prefix Notation
Postfix Notation
This notation style is known as Reversed Polish Notation. In this notation style,
the operator is postfixed to the operands i.e., the operator is written after the
operands. For example, ab+. This is equivalent to its infix notation a + b.
The following table briefly tries to show the difference in all three notations −
2 (a + b) ∗ c ∗+abc ab+c∗
3 a ∗ (b + c) ∗a+bc abc+∗
5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗
Parsing Expressions
Precedence
When an operand is in between two different operators, which operator will take
the operand first, is decided by the precedence of an operator over others. For
example −
As multiplication operation has precedence over addition, b * c will be evaluated
first. A table of operator precedence is provided later.
Associativity
Associativity describes the rule where operators with the same precedence
appear in an expression. For example, in expression a + b − c, both + and − have
the same precedence, then which part of the expression will be evaluated first,
is determined by associativity of those operators. Here, both + and − are left
associative, so the expression will be evaluated as (a + b) − c.
Right
1 Exponentiation ^ Highest
Associative
The above table shows the default behavior of operators. At any point of time in
expression evaluation, the order can be altered by using parenthesis. For
example −
In a + b*c, the expression part b*c will be evaluated first, with multiplication as
precedence over addition. We here use parenthesis for a + b to be evaluated first,
like (a + b)*c.
Postfix Evaluation Algorithm
perform operation
Example
#include<stdio.h>
#include<string.h>
#include<ctype.h>
//char stack
char stack[25];
stack[++top] = item;
char pop() {
return stack[top--];
switch(symbol) {
case '+':
case '-':
return 2;
break;
case '*':
case '/':
return 3;
break;
case '^':
return 4;
break;
case '(':
case ')':
case '#':
return 1;
break;
}
}
switch(symbol) {
case '+':
case '-':
case '*':
case '/':
case '^':
case '(':
case ')':
return 1;
break;
default:
return 0;
int i,symbol,j = 0;
stack[++top] = '#';
for(i = 0;i<strlen(infix);i++) {
symbol = infix[i];
if(isOperator(symbol) == 0) {
postfix[j] = symbol;
j++;
} else {
if(symbol == '(') {
push(symbol);
} else {
if(symbol == ')') {
while(stack[top] != '(') {
postfix[j] = pop();
j++;
} else {
if(precedence(symbol)>precedence(stack[top])) {
push(symbol);
} else {
while(precedence(symbol)<=precedence(stack[top])) {
postfix[j] = pop();
j++;
push(symbol);
while(stack[top] != '#') {
postfix[j] = pop();
j++;
//int stack
int stack_int[25];
int top_int = -1;
stack_int[++top_int] = item;
char pop_int() {
return stack_int[top_int--];
char ch;
int i = 0,operand1,operand2;
if(isdigit(ch)) {
} else {
operand2 = pop_int();
operand1 = pop_int();
switch(ch) {
case '+':
push_int(operand1+operand2);
break;
case '-':
push_int(operand1-operand2);
break;
case '*':
push_int(operand1*operand2);
break;
case '/':
push_int(operand1/operand2);
break;
return stack_int[top_int];
void main() {
convert(infix,postfix);
Output