SlideShare a Scribd company logo
For any help regarding C++ Homework Help
Visit :- https://www.cpphomeworkhelp.com/ ,
Email :- info@cpphomeworkhelp.com or
Travelling Salesman Assignment Solution with C++
Write a multi-threaded program for solving the traveling salesman problem using
the branch-and-bound technique. You may use C++ or Java to implement this
multi-threaded program.
Task assignments to the threads may be static or dynamic. Since the multi-thread
program executes in a shared memory platform, both task assignment strategies can
easily be adopted. Experiment with the different number of threads for both task
assignment strategies, observe the execution times, and report your observations.
Recall in employing the branch and bound technique for solving the traveling
salesman problem, certain rules of inference would be useful in helping reduce the
size of the state space tree:
1. Use a heuristic to compute a lower bound for each node.
2. When considering the inclusion of an edge in a branch step, if the inclusion
will lead to more than 2 incident edges to a city, the branch can be terminated there.
cpphomeworkhelp.com
3. When considering the inclusion of an edge in a branch step, if the inclusion will
result in a premature cycle (i.e., a cycle of size < the number of cities in the map), the
branch can be terminated there.
4. When considering the exclusion of an edge in a branch step, if the exclusion will
lead to a city not having 2 edges incident to it in the end, the branch can be terminated
there.
5.As soon as the search identifies a tour, record it as the best-cost-so-far. Update this cost
when a new tour with a better cost is found. This cost can be used in pruning those tree
nodes with lower bound >= this cost.
cpphomeworkhelp.com
#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <fstream>
#include <pthread.h>
using namespace std;
#define maximum_of_city 100
int number_city;
int
inputgraph[maximum_of_city]
[maximum_of_city];
int first = 0;
int threads = 2;
int flag;
cpphomeworkhelp.com
Solution
struct tspSearchParams {
int curr;
bool
visited_city[maximum_of_city];
int City[maximum_of_city];
float Lower_Bound = 0;
float edge_cost = 0;
float minimumcost = 0;
int level = 0;
int from = 0;
int to = 0;
bool multithread = false;
};
tspSearchParams*
copyParams(tspSearchParams* origin) {
tspSearchParams* copy = new
tspSearchParams();
cpphomeworkhelp.com
for (int i = 0; i<number_city; i++) {
copy->visited_city[i] = origin->visited_city[i];
copy->City[i] = origin->City[i];
}
copy->Lower_Bound =
origin->Lower_Bound;
copy->edge_cost =
origin->edge_cost;
copy->minimumcost =
origin->minimumcost;
copy->level = origin-
>level;
copy->from = 0;
copy->to = number_city;
copy->multithread =
false;
return copy;
}
cpphomeworkhelp.com
void input_print_Graph() {
cout << "Insert number of cities: ";
cin >> number_city;
cout << "Insert the graph: " << endl;
for(int i = 0; i < number_city; i++) {
for(int j = 0; j < number_city; j++) {
cin >> inputgraph[i][j];
}
}
cout << "The graph you input: " << endl;
for(int i = 0; i < number_city; i++) {
for(int j = 0; j < number_city; j++) {
cout << inputgraph[i][j] << " ";
}
cout << endl;
}
}
cpphomeworkhelp.com
void readFile() {
fstream file("weightmatrix2.txt");
file >> number_city;
for(int i = 0; i < number_city; i++) {
for(int j = 0; j < number_city; j++){
file >> inputgraph[i][j];
}
}
file.close();
cout << "The City Matrix you input: " <<
endl;
for(int i = 0; i < number_city; i++) {
for(int j = 0; j < number_city; j++){
cout << inputgraph[i][j] << " ";
}
cout << endl;
}
}
cpphomeworkhelp.com
//find smallest number in each row for input graph
float smallestnum(int v) {
int smallest = INT_MAX;
for (int j = 0; j < number_city; j++) {
if(v != j) {
if (inputgraph[v][j] < smallest) {
smallest = inputgraph[v][j];
}
}
}
return smallest;
}
//find second smallest number in each row for input graph
float secondsmallestnum(int v) {
int smallest = INT_MAX;
int secondsmallest = INT_MAX;
for (int j = 0; j < number_city; j++) {
if(v == j) {
continue;
}
cpphomeworkhelp.com
if (inputgraph[v][j] <= smallest) {
secondsmallest = smallest;
smallest = inputgraph[v][j];
}
else if(inputgraph[v][j] <= secondsmallest && inputgraph[v][j] !=
smallest) {
secondsmallest = inputgraph[v][j];
}
}
return secondsmallest;
}
void compute_lowerbound(tspSearchParams* root) {
for (int i = 0; i < number_city; i++) {
root->Lower_Bound += (smallestnum(i) +
secondsmallestnum(i)) / 2;
root->visited_city[i] = false;
}
}
cpphomeworkhelp.com
void* TSPBnB(void* args) {
tspSearchParams* params =
(tspSearchParams*)args;
int v = params->curr;
params->City[params->level] = v;
if (params->level + 1 <
number_city) {
int level = params->level;
params->visited_city[v] = true;
if (params->level == 0 && !params->multithread) {
int div = number_city / threads;
int mod = number_city % threads;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,
PTHREAD_CREATE_JOINABLE);
cpphomeworkhelp.com
pthread_t pthreads[threads];
for (int j = 0; j < threads; j++) {
int from = div * j + ((j >= mod) ? mod : j);
int to = div * (j+1) + ((j+1 >= mod) ? mod :
(j+1));
tspSearchParams* copy = copyParams(params);
copy->from = from;
copy->to = to;
copy->multithread = true;
pthread_create(&pthreads[j], &attr, TSPBnB,
(void *)copy);
}
for (int j = 0; j<threads; j++) {
void* status;
pthread_join(pthreads[j], &status);
tspSearchParams* result =
(tspSearchParams*)status;
cpphomeworkhelp.com
if (result->minimumcost > 0 && (result -
>minimumcost < params->minimumcost || params->minimumcost < 1)) {
params->minimumcost = result->minimumcost;
for (int k = 0; k<number_city; k++) {
params->City[k] = result->City[k];
}
}
delete result;
}
pthread_attr_destroy(&attr);
}
else {
for (int i = params->from; i < params->to; i++) {
if (params->visited_city[i] == false) {
float temp = params->Lower_Bound;
cpphomeworkhelp.com
tspSearchParams* copy = copyParams(params);
copy->level++;
copy->curr = i;
copy->edge_cost += inputgraph[v][i];
if (copy->level == 1) {
copy->Lower_Bound -= ((smallestnum(copy->City[level]) +
smallestnum(i)) / 2);
}
else {
copy->Lower_Bound -= ((secondsmallestnum(copy->City[level]) +
smallestnum(i))/ 2);
}
if(copy->Lower_Bound + copy->edge_cost <
copy->minimumcost || params->minimumcost < 1) {
TSPBnB(copy);
cpphomeworkhelp.com
if (copy->minimumcost > 0 && (copy-
>minimumcost < params->minimumcost || params->minimumcost < 1)) {
params-
>minimumcost = copy->minimumcost;
for (int k = 0;
k<number_city; k++) {
params->City[k] = copy->City[k];
}
}
}
if(flag == 1) {
if(copy->Lower_Bound + copy->edge_cost >= copy->minimumcost) {
cout << copy->Lower_Bound + copy->edge_cost << " been pruned" << endl;
}
}
delete copy;
}
}
}
}
cpphomeworkhelp.com
else {
params->edge_cost += inputgraph[v][first];
params->minimumcost = params->edge_cost;
if(flag == 1) {
cout << "Current minimum cost: " << params->minimumcost << endl;
}
}
return (void*)params;
}
int main() {
readFile();
cout << endl;
cout << endl;
cout << "Please insert 1 if you want verbose output (1/0): ";
cin >> flag;
clock_t start, end;
double total_time;
cpphomeworkhelp.com
start = clock();
tspSearchParams* params = new tspSearchParams();
compute_lowerbound(params);
if(flag == 1) {
cout << "Root's lower bound: " << params->Lower_Bound << endl;
}
params->curr = first;
params->from = 0;
params->to = number_city;
params->multithread = false;
TSPBnB(params);
end = clock();
total_time = end - start;
cout << endl;
cout << "Final Path: ";
for (int i = 0; i < number_city; i++) {
cout << params->City[i] + 1 << " -> ";
}
cpphomeworkhelp.com
cout << params->City[0] + 1 << endl;
cout << "Cost of the Final Path: " << params->minimumcost << endl;
cout << "Execution time: " << total_time << endl;
delete params;
return 0;
}
cpphomeworkhelp.com

More Related Content

PPTX
ExploringPrimsAlgorithmforMinimumSpanningTreesinC.pptx
PPTX
CPP Homework Help
PDF
Hi,you covered mostly things.there are issue to point and link poi.pdf
PPTX
Computer Science Assignment Help
PDF
Dynamic programming burglar_problem
PPT
Unit-2 Branch & Bound Design of Algorithms.ppt
PPTX
DS Assignment Presentation 20242024.pptx
ExploringPrimsAlgorithmforMinimumSpanningTreesinC.pptx
CPP Homework Help
Hi,you covered mostly things.there are issue to point and link poi.pdf
Computer Science Assignment Help
Dynamic programming burglar_problem
Unit-2 Branch & Bound Design of Algorithms.ppt
DS Assignment Presentation 20242024.pptx

Similar to CPP Homework Help (20)

PPTX
Part 3 : building a network and supporting applications
PDF
Write CC++ a program that inputs a weighted undirected graph and fi.pdf
PPTX
Dynamic Programming in design and analysis .pptx
PPTX
Travelling Salesman
PPT
Prim's Algorithm on minimum spanning tree
PDF
#include iostream #include fstream #include cstdlib #.pdf
PPT
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
PPTX
Traveling salesman problem
PPTX
Traveling salesman problem
PDF
travelingsalesmanproblem-170122053648.pdf
DOCX
CS3491-AI and ML lab manual cs3491 r2021
PPTX
Branch and bound method
PPT
Mixed Integer Programming (MIP) is a type of mathematical optimization Branch...
PDF
Training at AI Frontiers 2018 - LaiOffer Self-Driving-Car-lecture 2: Incremen...
PPT
Branch and bound
PPT
3 network-transport
PDF
COMPUTER NETWORKS AND SECURITY PRACTICAL
PPT
8783733
PPTX
TSP_Branch_and_Bound___Presentation.pptx
PDF
Dynamic programming
Part 3 : building a network and supporting applications
Write CC++ a program that inputs a weighted undirected graph and fi.pdf
Dynamic Programming in design and analysis .pptx
Travelling Salesman
Prim's Algorithm on minimum spanning tree
#include iostream #include fstream #include cstdlib #.pdf
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
Traveling salesman problem
Traveling salesman problem
travelingsalesmanproblem-170122053648.pdf
CS3491-AI and ML lab manual cs3491 r2021
Branch and bound method
Mixed Integer Programming (MIP) is a type of mathematical optimization Branch...
Training at AI Frontiers 2018 - LaiOffer Self-Driving-Car-lecture 2: Incremen...
Branch and bound
3 network-transport
COMPUTER NETWORKS AND SECURITY PRACTICAL
8783733
TSP_Branch_and_Bound___Presentation.pptx
Dynamic programming
Ad

More from C++ Homework Help (19)

PPTX
cpp promo ppt.pptx
PPTX
CPP Homework Help
PPT
CPP homework help
PPTX
CPP Programming Homework Help
PPTX
C++ Programming Homework Help
PPTX
Online CPP Homework Help
PPTX
CPP Homework Help
PPTX
C++ Homework Help
PPTX
C++ Programming Homework Help
PPTX
Get Fast C++ Homework Help
PPTX
Best C++ Programming Homework Help
PPTX
CPP Programming Homework Help
PPTX
Online CPP Homework Help
PPTX
CPP Assignment Help
PPTX
CPP Homework help
PPTX
CPP homework help
PPTX
CPP Homework Help
PPTX
CPP Homework Help
PPTX
Cpp Homework Help
cpp promo ppt.pptx
CPP Homework Help
CPP homework help
CPP Programming Homework Help
C++ Programming Homework Help
Online CPP Homework Help
CPP Homework Help
C++ Homework Help
C++ Programming Homework Help
Get Fast C++ Homework Help
Best C++ Programming Homework Help
CPP Programming Homework Help
Online CPP Homework Help
CPP Assignment Help
CPP Homework help
CPP homework help
CPP Homework Help
CPP Homework Help
Cpp Homework Help
Ad

Recently uploaded (20)

PDF
01-Introduction-to-Information-Management.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Basic Mud Logging Guide for educational purpose
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Pre independence Education in Inndia.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Cell Types and Its function , kingdom of life
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Lesson notes of climatology university.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Anesthesia in Laparoscopic Surgery in India
01-Introduction-to-Information-Management.pdf
Final Presentation General Medicine 03-08-2024.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
RMMM.pdf make it easy to upload and study
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Abdominal Access Techniques with Prof. Dr. R K Mishra
Basic Mud Logging Guide for educational purpose
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Supply Chain Operations Speaking Notes -ICLT Program
GDM (1) (1).pptx small presentation for students
Pre independence Education in Inndia.pdf
Sports Quiz easy sports quiz sports quiz
Cell Types and Its function , kingdom of life
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Module 4: Burden of Disease Tutorial Slides S2 2025
Lesson notes of climatology university.
Microbial diseases, their pathogenesis and prophylaxis
Anesthesia in Laparoscopic Surgery in India

CPP Homework Help

  • 1. For any help regarding C++ Homework Help Visit :- https://www.cpphomeworkhelp.com/ , Email :- info@cpphomeworkhelp.com or
  • 2. Travelling Salesman Assignment Solution with C++ Write a multi-threaded program for solving the traveling salesman problem using the branch-and-bound technique. You may use C++ or Java to implement this multi-threaded program. Task assignments to the threads may be static or dynamic. Since the multi-thread program executes in a shared memory platform, both task assignment strategies can easily be adopted. Experiment with the different number of threads for both task assignment strategies, observe the execution times, and report your observations. Recall in employing the branch and bound technique for solving the traveling salesman problem, certain rules of inference would be useful in helping reduce the size of the state space tree: 1. Use a heuristic to compute a lower bound for each node. 2. When considering the inclusion of an edge in a branch step, if the inclusion will lead to more than 2 incident edges to a city, the branch can be terminated there. cpphomeworkhelp.com
  • 3. 3. When considering the inclusion of an edge in a branch step, if the inclusion will result in a premature cycle (i.e., a cycle of size < the number of cities in the map), the branch can be terminated there. 4. When considering the exclusion of an edge in a branch step, if the exclusion will lead to a city not having 2 edges incident to it in the end, the branch can be terminated there. 5.As soon as the search identifies a tour, record it as the best-cost-so-far. Update this cost when a new tour with a better cost is found. This cost can be used in pruning those tree nodes with lower bound >= this cost. cpphomeworkhelp.com
  • 4. #include <bits/stdc++.h> #include <iostream> #include <iomanip> #include <ctime> #include <fstream> #include <pthread.h> using namespace std; #define maximum_of_city 100 int number_city; int inputgraph[maximum_of_city] [maximum_of_city]; int first = 0; int threads = 2; int flag; cpphomeworkhelp.com Solution
  • 5. struct tspSearchParams { int curr; bool visited_city[maximum_of_city]; int City[maximum_of_city]; float Lower_Bound = 0; float edge_cost = 0; float minimumcost = 0; int level = 0; int from = 0; int to = 0; bool multithread = false; }; tspSearchParams* copyParams(tspSearchParams* origin) { tspSearchParams* copy = new tspSearchParams(); cpphomeworkhelp.com
  • 6. for (int i = 0; i<number_city; i++) { copy->visited_city[i] = origin->visited_city[i]; copy->City[i] = origin->City[i]; } copy->Lower_Bound = origin->Lower_Bound; copy->edge_cost = origin->edge_cost; copy->minimumcost = origin->minimumcost; copy->level = origin- >level; copy->from = 0; copy->to = number_city; copy->multithread = false; return copy; } cpphomeworkhelp.com
  • 7. void input_print_Graph() { cout << "Insert number of cities: "; cin >> number_city; cout << "Insert the graph: " << endl; for(int i = 0; i < number_city; i++) { for(int j = 0; j < number_city; j++) { cin >> inputgraph[i][j]; } } cout << "The graph you input: " << endl; for(int i = 0; i < number_city; i++) { for(int j = 0; j < number_city; j++) { cout << inputgraph[i][j] << " "; } cout << endl; } } cpphomeworkhelp.com
  • 8. void readFile() { fstream file("weightmatrix2.txt"); file >> number_city; for(int i = 0; i < number_city; i++) { for(int j = 0; j < number_city; j++){ file >> inputgraph[i][j]; } } file.close(); cout << "The City Matrix you input: " << endl; for(int i = 0; i < number_city; i++) { for(int j = 0; j < number_city; j++){ cout << inputgraph[i][j] << " "; } cout << endl; } } cpphomeworkhelp.com
  • 9. //find smallest number in each row for input graph float smallestnum(int v) { int smallest = INT_MAX; for (int j = 0; j < number_city; j++) { if(v != j) { if (inputgraph[v][j] < smallest) { smallest = inputgraph[v][j]; } } } return smallest; } //find second smallest number in each row for input graph float secondsmallestnum(int v) { int smallest = INT_MAX; int secondsmallest = INT_MAX; for (int j = 0; j < number_city; j++) { if(v == j) { continue; } cpphomeworkhelp.com
  • 10. if (inputgraph[v][j] <= smallest) { secondsmallest = smallest; smallest = inputgraph[v][j]; } else if(inputgraph[v][j] <= secondsmallest && inputgraph[v][j] != smallest) { secondsmallest = inputgraph[v][j]; } } return secondsmallest; } void compute_lowerbound(tspSearchParams* root) { for (int i = 0; i < number_city; i++) { root->Lower_Bound += (smallestnum(i) + secondsmallestnum(i)) / 2; root->visited_city[i] = false; } } cpphomeworkhelp.com
  • 11. void* TSPBnB(void* args) { tspSearchParams* params = (tspSearchParams*)args; int v = params->curr; params->City[params->level] = v; if (params->level + 1 < number_city) { int level = params->level; params->visited_city[v] = true; if (params->level == 0 && !params->multithread) { int div = number_city / threads; int mod = number_city % threads; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); cpphomeworkhelp.com
  • 12. pthread_t pthreads[threads]; for (int j = 0; j < threads; j++) { int from = div * j + ((j >= mod) ? mod : j); int to = div * (j+1) + ((j+1 >= mod) ? mod : (j+1)); tspSearchParams* copy = copyParams(params); copy->from = from; copy->to = to; copy->multithread = true; pthread_create(&pthreads[j], &attr, TSPBnB, (void *)copy); } for (int j = 0; j<threads; j++) { void* status; pthread_join(pthreads[j], &status); tspSearchParams* result = (tspSearchParams*)status; cpphomeworkhelp.com
  • 13. if (result->minimumcost > 0 && (result - >minimumcost < params->minimumcost || params->minimumcost < 1)) { params->minimumcost = result->minimumcost; for (int k = 0; k<number_city; k++) { params->City[k] = result->City[k]; } } delete result; } pthread_attr_destroy(&attr); } else { for (int i = params->from; i < params->to; i++) { if (params->visited_city[i] == false) { float temp = params->Lower_Bound; cpphomeworkhelp.com
  • 14. tspSearchParams* copy = copyParams(params); copy->level++; copy->curr = i; copy->edge_cost += inputgraph[v][i]; if (copy->level == 1) { copy->Lower_Bound -= ((smallestnum(copy->City[level]) + smallestnum(i)) / 2); } else { copy->Lower_Bound -= ((secondsmallestnum(copy->City[level]) + smallestnum(i))/ 2); } if(copy->Lower_Bound + copy->edge_cost < copy->minimumcost || params->minimumcost < 1) { TSPBnB(copy); cpphomeworkhelp.com
  • 15. if (copy->minimumcost > 0 && (copy- >minimumcost < params->minimumcost || params->minimumcost < 1)) { params- >minimumcost = copy->minimumcost; for (int k = 0; k<number_city; k++) { params->City[k] = copy->City[k]; } } } if(flag == 1) { if(copy->Lower_Bound + copy->edge_cost >= copy->minimumcost) { cout << copy->Lower_Bound + copy->edge_cost << " been pruned" << endl; } } delete copy; } } } } cpphomeworkhelp.com
  • 16. else { params->edge_cost += inputgraph[v][first]; params->minimumcost = params->edge_cost; if(flag == 1) { cout << "Current minimum cost: " << params->minimumcost << endl; } } return (void*)params; } int main() { readFile(); cout << endl; cout << endl; cout << "Please insert 1 if you want verbose output (1/0): "; cin >> flag; clock_t start, end; double total_time; cpphomeworkhelp.com
  • 17. start = clock(); tspSearchParams* params = new tspSearchParams(); compute_lowerbound(params); if(flag == 1) { cout << "Root's lower bound: " << params->Lower_Bound << endl; } params->curr = first; params->from = 0; params->to = number_city; params->multithread = false; TSPBnB(params); end = clock(); total_time = end - start; cout << endl; cout << "Final Path: "; for (int i = 0; i < number_city; i++) { cout << params->City[i] + 1 << " -> "; } cpphomeworkhelp.com
  • 18. cout << params->City[0] + 1 << endl; cout << "Cost of the Final Path: " << params->minimumcost << endl; cout << "Execution time: " << total_time << endl; delete params; return 0; } cpphomeworkhelp.com