Question
I need the help for my code in Java in these point : 1- I want the zero that it is print in the last
I need the help for my code in Java in these point :
1- I want the zero that it is print in the last to be in the first line
2 - print the correct optimal path
3- print the cost of each path
Please Provide the solution on the code I provided down there , provide me the full code after adding your solution , thanks in advance
-
The code I have been working on :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tspbruteforce;
import java.util.*;
/**
*
* @author Alya
*/
public class TSPBruteForce {
static int V = 4;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// matrix representation of graph
System.out.println("Enter the num of city");
int n=in.nextInt();
int graph[][] = new int [n][n];
System.out.println("The matrix is : ");
Random rand = new Random(100);
for (int i = 0; i
for ( int j = 0; j
if(i==j){
graph[i][j]=0;
}else{
int value = Math.abs((50 + rand.nextInt((500 -1 ) + 1)));
graph[i][j] = value;
graph[j][i] = value;}
}
}// end matrix random
for ( int i = 0; i
for (int j = 0; j
System.out.print(graph[i][j] + " ");
}
System.out.println(); //change line on console as row comes to end in the matrix.
}
System.out.println("");
//---------------------------------------------------------------------------
long startTime = System.nanoTime();
System.out.println(" The cost : "+ travllingSalesmanProblem(graph, n));
// TODO code application logic here
long endTime = System.nanoTime();
long TotalTime = endTime-startTime;
long milli= TotalTime/100000;
System.out.println("The total time is: "+milli);
}
// implementation of traveling
// Salesman Problem
static int travllingSalesmanProblem(int graph[][],int n)
{
// store all vertex apart
// from source vertex
ArrayList
for (int i = 0; i
vertex.add(i);
// store minimum weight
// Hamiltonian Cycle.
int min_path = Integer.MAX_VALUE;
do
{
// store current Path weight(cost)
int current_pathweight = 0;
// compute current path weight
int k = 0;
int counter= 0;
for (int i = 0;i
current_pathweight += graph[k][vertex.get(i)];
k = vertex.get(i);
if(counter==n-1){
System.out.print(" ");
counter=0;
}
System.out.print(k +" ");
counter++;
}
current_pathweight += graph[k][0];
// update minimum
min_path = Math.min(min_path,current_pathweight);
} while (findNextPermutation(vertex));
System.out.println(" The optimal path is: ");
for (int i = 0; i
System.out.print(vertex.size() + " ");
}
System.out.println(" ");
return min_path;
}
// Function to swap the data
// present in the left and right indices
public static ArrayList
{
// Swap the data
int temp = data.get(left);
data.set(left, data.get(right));
data.set(right, temp);
// Return the updated array
return data;
}
//Function to reverse the sub-array
// starting from left to the right
// both inclusive
public static ArrayList
// Reverse the sub-array
while (left
{
int temp = data.get(left);
data.set(left++,
data.get(right));
data.set(right--, temp);
}
// Return the updated array
return data;
}
// Function to find the next permutation
// of the given integer array
public static boolean findNextPermutation( ArrayList
{
// If the given dataset is empty
// or contains only one element
// next_permutation is not possible
if (data.size()
return false;
int last = data.size() - 2;
// find the longest non-increasing
// suffix and find the pivot
while (last >= 0)
{
if (data.get(last) { break; } last--; } // If there is no increasing pair // there is no higher order permutation if (last return false; int nextGreater = data.size() - 1; // Find the rightmost successor // to the pivot for (int i = data.size() - 1; i > last; i--) { if (data.get(i) > data.get(last)) { nextGreater = i; break; } } // Swap the successor and // the pivot data = swap(data, nextGreater, last); // Reverse the suffix data = reverse(data, last + 1, data.size() - 1); // Return true as the // next_permutation is done return true; } } Output of the code
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started