SlideShare a Scribd company logo
1 of 4
The assignment is to solve the Travelling Salesman Problem using brute force for an arbitrary
number of points in 2D space. You must design the total solution using classes for Vertex, Edge,
and Path, and you must have a main.cpp which prints out the path for your solution.
Solution
Answer:
Note: vertex and edges are defined in the 2D space array. this is the simple task, usage of
classes will make it complicated.
C++ code :
#include<stdio.h>
#include<iostream>
using namespace std;
int c1 = 0,cost1 = 999;
//2D space
int graph1[4][4] = { {0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}
};
//method to swap the value
void swapFunc (int *x1, int *y1)
{
int temp1;
temp1 = *x1;
*x1 = *y1;
*y1 = temp1;
}
//method to copy the array value
void copy_arrayVal(int *a1, int n1)
{
int i1, sum1 = 0;
for(i1 = 0; i1 <= n1; i1++)
{
sum1 += graph1[a1[i1 % 4]][a1[(i1 + 1) % 4]];
}
if (cost1 > sum1)
{
cost1 = sum1;
}
}
//method to permute the value
void permuteVal(int *a1, int i1, int n1)
{
int j1, k1;
if (i1 == n1)
{
//method to call
copy_arrayVal(a1, n1);
}
else
{
for (j1 = i1; j1 <= n1; j1++)
{
//call of functions
swapFunc((a1+ i1), (a1 + j1));
permuteVal(a1, i1 + 1, n1);
swapFunc((a1+ i1), (a1 + j1));
}
}
}
//main method
int main()
{
int i1, j1;
int a1[] = {0, 1, 2, 3};
permuteVal(a1, 0, 3);
cout<<"minimum cost for Path:"<<cost1<<endl;
}

More Related Content

Similar to The assignment is to solve the Travelling Salesman Problem using brute.docx

Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]
Palak Sanghani
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2doc
Srikanth
 
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docxCOMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
TashiBhutia12
 
June 05 P2
June 05 P2June 05 P2
June 05 P2
Samimvez
 
Lec 7 28_aug [compatibility mode]
Lec 7 28_aug [compatibility mode]Lec 7 28_aug [compatibility mode]
Lec 7 28_aug [compatibility mode]
Palak Sanghani
 
Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10
alish sha
 

Similar to The assignment is to solve the Travelling Salesman Problem using brute.docx (20)

Trident International Graphics Workshop 2014 5/5
Trident International Graphics Workshop 2014 5/5Trident International Graphics Workshop 2014 5/5
Trident International Graphics Workshop 2014 5/5
 
Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2doc
 
Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01
 
Apclass (2)
Apclass (2)Apclass (2)
Apclass (2)
 
Computational Assignment Help
Computational Assignment HelpComputational Assignment Help
Computational Assignment Help
 
openMP loop parallelization
openMP loop parallelizationopenMP loop parallelization
openMP loop parallelization
 
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docxCOMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
 
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : NotesCUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
 
C++ TUTORIAL 9
C++ TUTORIAL 9C++ TUTORIAL 9
C++ TUTORIAL 9
 
June 05 P2
June 05 P2June 05 P2
June 05 P2
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
 
Solving a “Transportation Planning” Problem through the Programming Language “C”
Solving a “Transportation Planning” Problem through the Programming Language “C”Solving a “Transportation Planning” Problem through the Programming Language “C”
Solving a “Transportation Planning” Problem through the Programming Language “C”
 
Lec 7 28_aug [compatibility mode]
Lec 7 28_aug [compatibility mode]Lec 7 28_aug [compatibility mode]
Lec 7 28_aug [compatibility mode]
 
Computer graphics 2
Computer graphics 2Computer graphics 2
Computer graphics 2
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
RSA SIGNATURE: BEHIND THE SCENES
RSA SIGNATURE: BEHIND THE SCENESRSA SIGNATURE: BEHIND THE SCENES
RSA SIGNATURE: BEHIND THE SCENES
 
Calculus Assignment Help
 Calculus Assignment Help Calculus Assignment Help
Calculus Assignment Help
 
Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10
 
Xgboost
XgboostXgboost
Xgboost
 

More from chrisflorence13710

Telling geological time using fossils You are becoming a very good pal.docx
Telling geological time using fossils You are becoming a very good pal.docxTelling geological time using fossils You are becoming a very good pal.docx
Telling geological time using fossils You are becoming a very good pal.docx
chrisflorence13710
 

More from chrisflorence13710 (20)

The cost of capital is 15-- Cash flows associated with projects A-B-C-.docx
The cost of capital is 15-- Cash flows associated with projects A-B-C-.docxThe cost of capital is 15-- Cash flows associated with projects A-B-C-.docx
The cost of capital is 15-- Cash flows associated with projects A-B-C-.docx
 
The completion of this project will improve employee working with slow.docx
The completion of this project will improve employee working with slow.docxThe completion of this project will improve employee working with slow.docx
The completion of this project will improve employee working with slow.docx
 
The City Commission of Nashville has decided to build a botanical gard (1).docx
The City Commission of Nashville has decided to build a botanical gard (1).docxThe City Commission of Nashville has decided to build a botanical gard (1).docx
The City Commission of Nashville has decided to build a botanical gard (1).docx
 
The adrenal cortex secretes all of the following EXCEPT- Epinephrine a.docx
The adrenal cortex secretes all of the following EXCEPT- Epinephrine a.docxThe adrenal cortex secretes all of the following EXCEPT- Epinephrine a.docx
The adrenal cortex secretes all of the following EXCEPT- Epinephrine a.docx
 
The Board of Directors of Teton Pearl- Inc-- a private foundation- con.docx
The Board of Directors of Teton Pearl- Inc-- a private foundation- con.docxThe Board of Directors of Teton Pearl- Inc-- a private foundation- con.docx
The Board of Directors of Teton Pearl- Inc-- a private foundation- con.docx
 
The Bellman-Ford algorithm is run on the following graph- The start ve.docx
The Bellman-Ford algorithm is run on the following graph- The start ve.docxThe Bellman-Ford algorithm is run on the following graph- The start ve.docx
The Bellman-Ford algorithm is run on the following graph- The start ve.docx
 
The biological function of NADH is to provide an electron source for t.docx
The biological function of NADH is to provide an electron source for t.docxThe biological function of NADH is to provide an electron source for t.docx
The biological function of NADH is to provide an electron source for t.docx
 
The Bellman-Ford Algorithm to find the shortest path from a vertex s t.docx
The Bellman-Ford Algorithm to find the shortest path from a vertex s t.docxThe Bellman-Ford Algorithm to find the shortest path from a vertex s t.docx
The Bellman-Ford Algorithm to find the shortest path from a vertex s t.docx
 
The average nominal incomes earned in Eturia and the CPI are shown in.docx
The average nominal incomes earned in Eturia and the CPI are shown in.docxThe average nominal incomes earned in Eturia and the CPI are shown in.docx
The average nominal incomes earned in Eturia and the CPI are shown in.docx
 
The assumptions of mainstream market economics are- The individual is.docx
The assumptions of mainstream market economics are- The individual is.docxThe assumptions of mainstream market economics are- The individual is.docx
The assumptions of mainstream market economics are- The individual is.docx
 
The analysis and redesign of workilow The management of information fl.docx
The analysis and redesign of workilow The management of information fl.docxThe analysis and redesign of workilow The management of information fl.docx
The analysis and redesign of workilow The management of information fl.docx
 
The age distribution of students at a community college is given below.docx
The age distribution of students at a community college is given below.docxThe age distribution of students at a community college is given below.docx
The age distribution of students at a community college is given below.docx
 
The additional growth of plants in one week are recorded for 11 plants.docx
The additional growth of plants in one week are recorded for 11 plants.docxThe additional growth of plants in one week are recorded for 11 plants.docx
The additional growth of plants in one week are recorded for 11 plants.docx
 
Thanks for the help- The process is correct- For part b for those who.docx
Thanks for the help- The process is correct- For part b for those who.docxThanks for the help- The process is correct- For part b for those who.docx
Thanks for the help- The process is correct- For part b for those who.docx
 
T2- Propose code for a pseudorandom number generator function in CH+ a.docx
T2- Propose code for a pseudorandom number generator function in CH+ a.docxT2- Propose code for a pseudorandom number generator function in CH+ a.docx
T2- Propose code for a pseudorandom number generator function in CH+ a.docx
 
Telling geological time using fossils You are becoming a very good pal.docx
Telling geological time using fossils You are becoming a very good pal.docxTelling geological time using fossils You are becoming a very good pal.docx
Telling geological time using fossils You are becoming a very good pal.docx
 
Technology and economic growth- 192) What is technological progress- G.docx
Technology and economic growth- 192) What is technological progress- G.docxTechnology and economic growth- 192) What is technological progress- G.docx
Technology and economic growth- 192) What is technological progress- G.docx
 
TABLE 3- Summary of univariable logistic regression analysis of contin.docx
TABLE 3- Summary of univariable logistic regression analysis of contin.docxTABLE 3- Summary of univariable logistic regression analysis of contin.docx
TABLE 3- Summary of univariable logistic regression analysis of contin.docx
 
TABLE 2- Frequencies of various skin pigmentation alleles in different.docx
TABLE 2- Frequencies of various skin pigmentation alleles in different.docxTABLE 2- Frequencies of various skin pigmentation alleles in different.docx
TABLE 2- Frequencies of various skin pigmentation alleles in different.docx
 
Swifty Company uses a periodic inventory system- Details for the inven.docx
Swifty Company uses a periodic inventory system- Details for the inven.docxSwifty Company uses a periodic inventory system- Details for the inven.docx
Swifty Company uses a periodic inventory system- Details for the inven.docx
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 

The assignment is to solve the Travelling Salesman Problem using brute.docx

  • 1. The assignment is to solve the Travelling Salesman Problem using brute force for an arbitrary number of points in 2D space. You must design the total solution using classes for Vertex, Edge, and Path, and you must have a main.cpp which prints out the path for your solution. Solution Answer: Note: vertex and edges are defined in the 2D space array. this is the simple task, usage of classes will make it complicated. C++ code : #include<stdio.h> #include<iostream> using namespace std; int c1 = 0,cost1 = 999; //2D space int graph1[4][4] = { {0, 10, 15, 20}, {10, 0, 35, 25}, {15, 35, 0, 30}, {20, 25, 30, 0} }; //method to swap the value void swapFunc (int *x1, int *y1)
  • 2. { int temp1; temp1 = *x1; *x1 = *y1; *y1 = temp1; } //method to copy the array value void copy_arrayVal(int *a1, int n1) { int i1, sum1 = 0; for(i1 = 0; i1 <= n1; i1++) { sum1 += graph1[a1[i1 % 4]][a1[(i1 + 1) % 4]]; } if (cost1 > sum1) { cost1 = sum1; } } //method to permute the value void permuteVal(int *a1, int i1, int n1) { int j1, k1;
  • 3. if (i1 == n1) { //method to call copy_arrayVal(a1, n1); } else { for (j1 = i1; j1 <= n1; j1++) { //call of functions swapFunc((a1+ i1), (a1 + j1)); permuteVal(a1, i1 + 1, n1); swapFunc((a1+ i1), (a1 + j1)); } } } //main method int main() { int i1, j1; int a1[] = {0, 1, 2, 3}; permuteVal(a1, 0, 3); cout<<"minimum cost for Path:"<<cost1<<endl;
  • 4. }