Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use Mathematica to code the following Edit the given code so that it will solve

ID: 3699929 • Letter: U

Question

Use Mathematica to code the following

Edit the given code so that it will solve the Taveling Salesman Problem using the Brute Force method. Don’t forget to return to the starting point. Be sure to print (one of) the cheapest path(s) and its corresponding cost. Then using the Timing command in Mathematica, compute the speed of the brute force algorithm for n = 1,2,3,4. Include these times as comments in your code. Recall that numNodes = n + 1.

Brute Force Method: List every Hamiltonian cycle (a Hamiltonian cycle is a closed loop passing through each vertx exactly once), calculate the weight of each, then calculate the minimal (or lowest cost) cycle.

The Traveling Salesman Problem: Given a complete network with n+1 verticies, what is the shortest path through the network which starts at some vertex, visists every vertex exactly once, then returns to the starting vertex?

The given code (this code outputs all possible paths that could be taken. Your job is to edit this code to compute the weight of each path and determine the path with the least weight):
SP = {{1}};
LP = {};

numNodes = 5;

LPpathLengths = 0;

While[LPpathLengths < numNodes,
(
   (* Cycling through the short paths of SP *)
   For[i = 1, i <= Length[SP], i++,
    (
   
     (* We want to append to SP[[i]] the numbers not in SP[[i]].*)
   
     For[j = 2, j <= numNodes, j++,
       (
      
        (* See if j is in SP[[i]]. Use counter
        k to cycle through SP[[i]]*)
        doAppend = True;
        For[k = 1, k <= Length[SP[[i]]], k++,
         (
          If[j == SP[[i, k]] (*OR getCost is too high*),
            (
             doAppend = False;
             Break[];
             )];
          )]; (* end k loop *)
      
        If[doAppend == True,
         (
          AppendTo[LP, Append[SP[[i]], j]];
          )];
      
        )]; (* end j loop *)
   
     )]; (* end i loop *)

   LPpathLengths = Length[ LP[[1]] ];
   Print["SP = ", SP];
   Print["LP = ", LP];
   Print[];

   SP = LP;
   LP = {};


   )]; (* end while loop *)
Print["Final Output = "];
SP

SP = {{1}}
LP = {{1,2},{1,3},{1,4},{1,5}}

SP = {{1,2},{1,3},{1,4},{1,5}}
LP = {{1,2,3},{1,2,4},{1,2,5},{1,3,2},{1,3,4},{1,3,5},{1,4,2},{1,4,3},{1,4,5},{1,5,2},{1,5,3},{1,5,4}}

SP = {{1,2,3},{1,2,4},{1,2,5},{1,3,2},{1,3,4},{1,3,5},{1,4,2},{1,4,3},{1,4,5},{1,5,2},{1,5,3},{1,5,4}}
LP = {{1,2,3,4},{1,2,3,5},{1,2,4,3},{1,2,4,5},{1,2,5,3},{1,2,5,4},{1,3,2,4},{1,3,2,5},{1,3,4,2},{1,3,4,5},{1,3,5,2},{1,3,5,4},{1,4,2,3},{1,4,2,5},{1,4,3,2},{1,4,3,5},{1,4,5,2},{1,4,5,3},{1,5,2,3},{1,5,2,4},{1,5,3,2},{1,5,3,4},{1,5,4,2},{1,5,4,3}}

SP = {{1,2,3,4},{1,2,3,5},{1,2,4,3},{1,2,4,5},{1,2,5,3},{1,2,5,4},{1,3,2,4},{1,3,2,5},{1,3,4,2},{1,3,4,5},{1,3,5,2},{1,3,5,4},{1,4,2,3},{1,4,2,5},{1,4,3,2},{1,4,3,5},{1,4,5,2},{1,4,5,3},{1,5,2,3},{1,5,2,4},{1,5,3,2},{1,5,3,4},{1,5,4,2},{1,5,4,3}}
LP = {{1,2,3,4,5},{1,2,3,5,4},{1,2,4,3,5},{1,2,4,5,3},{1,2,5,3,4},{1,2,5,4,3},{1,3,2,4,5},{1,3,2,5,4},{1,3,4,2,5},{1,3,4,5,2},{1,3,5,2,4},{1,3,5,4,2},{1,4,2,3,5},{1,4,2,5,3},{1,4,3,2,5},{1,4,3,5,2},{1,4,5,2,3},{1,4,5,3,2},{1,5,2,3,4},{1,5,2,4,3},{1,5,3,2,4},{1,5,3,4,2},{1,5,4,2,3},{1,5,4,3,2}}

Final Output =
{{1, 2, 3, 4, 5}, {1, 2, 3, 5, 4}, {1, 2, 4, 3, 5}, {1, 2, 4, 5,
3}, {1, 2, 5, 3, 4}, {1, 2, 5, 4, 3}, {1, 3, 2, 4, 5}, {1, 3, 2, 5,
4}, {1, 3, 4, 2, 5}, {1, 3, 4, 5, 2}, {1, 3, 5, 2, 4}, {1, 3, 5, 4,
2}, {1, 4, 2, 3, 5}, {1, 4, 2, 5, 3}, {1, 4, 3, 2, 5}, {1, 4, 3, 5,
2}, {1, 4, 5, 2, 3}, {1, 4, 5, 3, 2}, {1, 5, 2, 3, 4}, {1, 5, 2, 4,
3}, {1, 5, 3, 2, 4}, {1, 5, 3, 4, 2}, {1, 5, 4, 2, 3}, {1, 5, 4, 3,
2}}

52016 Vi 2 0 2 9-1 W02543

Explanation / Answer


#include <bits/stdc++.h>
using namespace std;
#define V 5

// implementation of traveling Salesman Problem
int travllingSalesmanProblem(int graph[][V], int s)
{
// store all vertex apart from source vertex
vector<int> vertex;
for (int i = 0; i < V; i++)
if (i != s)
vertex.push_back(i);
// store minimum weight Hamiltonian Cycle.
int min_path = INT_MAX;
do {

// store current Path weight(cost)
int current_pathweight = 0;

// compute current path weight
int k = s;
cout <<"{";
for (int i = 0; i < vertex.size(); i++) {
current_pathweight += graph[k][vertex[i]];
if(i==vertex.size()-1)
cout << vertex[i];
else
cout << vertex[i]<<",";//printing the all paths
k = vertex[i];
}
cout <<"},";
cout << endl;
current_pathweight += graph[k][s];

// update minimum
min_path = min(min_path, current_pathweight);
  
} while (next_permutation(vertex.begin(), vertex.end()));
cout <<"}";
return min_path;
}

// driver program to test above function
int main()
{
// matrix representation of graph
int graph[][V] = { { 0,2, 5, 4, 3 },
{ 2, 0, 2,9, 1 },
{ 5,2, 0,1, 6 },
{ 4,9, 1,0, 6 },
{ 3, 1, 16, 6,0 } };
int s = 0;//suppose starting point
cout<<"For each path , one vertex is trimmed i.e in this case 5"<<endl;
cout<<"Final Output = {";
cout << "Min Path cost= "<<travllingSalesmanProblem(graph, s) << endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote