SlideShare a Scribd company logo
1 of 18
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

Similar to CPP Homework Help

Computer Networks Lab File
Computer Networks Lab FileComputer Networks Lab File
Computer Networks Lab FileKandarp Tiwari
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyGrejoJoby1
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptkinan keshkeh
 
Billing in a supermarket c++
Billing in a supermarket c++Billing in a supermarket c++
Billing in a supermarket c++varun arora
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptkinan keshkeh
 
Router Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditionsRouter Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditionsMorteza Mahdilar
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statementsMomenMostafa
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docxeugeniadean34240
 
Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたAkira Maruoka
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101premrings
 

Similar to CPP Homework Help (20)

Computer Networks Lab File
Computer Networks Lab FileComputer Networks Lab File
Computer Networks Lab File
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
C++ TUTORIAL 8
C++ TUTORIAL 8C++ TUTORIAL 8
C++ TUTORIAL 8
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
Billing in a supermarket c++
Billing in a supermarket c++Billing in a supermarket c++
Billing in a supermarket c++
 
CppTutorial.ppt
CppTutorial.pptCppTutorial.ppt
CppTutorial.ppt
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
Router Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditionsRouter Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditions
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
C++ file
C++ fileC++ file
C++ file
 
C++ file
C++ fileC++ file
C++ file
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
 
Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみた
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
C programming
C programmingC programming
C programming
 
10 template code program
10 template code program10 template code program
10 template code program
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 

More from C++ Homework Help (19)

cpp promo ppt.pptx
cpp promo ppt.pptxcpp promo ppt.pptx
cpp promo ppt.pptx
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Get Fast C++ Homework Help
Get Fast C++ Homework HelpGet Fast C++ Homework Help
Get Fast C++ Homework Help
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
 
CPP Assignment Help
CPP Assignment HelpCPP Assignment Help
CPP Assignment Help
 
CPP Homework help
CPP Homework helpCPP Homework help
CPP Homework help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
 

Recently uploaded

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 

Recently uploaded (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of 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