Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is the code I have been working using Java , I need help in it for 1- all path and it optimal path should

This is the code I have been working using Java , I need help in it for

1- all path and it optimal path should be print correctly

2 -Each path cost should be printed

3- I want the time in second or Millie seconds of each path before and after compute the cost

please complete the solution in the same code I wrote in question , thanks in advance

code:

/*

* 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();

for (int i = 0; i

for ( int j = 0; j

if(i==j){

graph[i][j]=0;

}else{

int value = Math.abs((1 + rand.nextInt((500 -1 ) + 1)));

graph[i][j] = value;

graph[j][i] = value;}

}

}

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.

}

//---------------------------------------------------------------------------

long sartTime = System.nanoTime();

System.out.println(" The cost : "+ travllingSalesmanProblem(graph, n));

// TODO code application logic here

long endTime = System.nanoTime();

long TotalTime = endTime-sartTime;

System.out.println("The total time is: "+TotalTime);

}

// implementation of traveling

// Salesman Problem

static int travllingSalesmanProblem(int graph[][],int n)

{

// store all vertex apart

// from source vertex

ArrayList vertex =new 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);//0

if(counter==2){

System.out.println(" ");

counter=0;}

counter++;

System.out.print(k+" -> ");

}

current_pathweight += graph[k][0];

// update minimum

min_path = Math.min(min_path,current_pathweight);

} while (findNextPermutation(vertex));

return min_path;

}

// Function to swap the data

// present in the left and right indices

public static ArrayList swap( ArrayList data,int left, int right)

{

// 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(ArrayList data,int left, int right){

// 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 data)

{

// 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;

}

}

picture of the code :

image text in transcribedimage text in transcribedimage text in transcribed

/ * 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 0 ; int graph[][] = new int [n][n]; System.out.println("The matrix is : "); Random rand = new Random (; for (int i=0;i swap( Array List data, int left, int right) \{ // 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 (ArrayList data, int left, int right) \{ // Reverse the sub-array data.size 0 - 1); // Return true as the // next_permutation is done return true; \} \}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M Ricardo, Susan D Urban

3rd Edition

1284056945, 9781284056945

More Books

Students also viewed these Databases questions