Question: What is the Basic Operation for this code? import java.util.*; public class TSP_BruteForce { static int travllingSalesmanProblem(int graph[][],int s ,int num_nodes) { //creatig list ArrayList

What is the Basic Operation for this code?

import java.util.*; public class TSP_BruteForce { static int travllingSalesmanProblem(int graph[][],int s ,int num_nodes) { //creatig list ArrayList vertex = new ArrayList(); ArrayList bestVertex = new ArrayList();

for (int i = 0; i < num_nodes; i++) if (i != s) vertex.add(i);

int[] optimal_pathtemp = new int[num_nodes+1]; int[] optimal_path = new int[num_nodes+1];

int min_path = Integer.MAX_VALUE; do { int current_pathweight = 0; int k = s; for (int i = 0; i < vertex.size(); i++) { optimal_pathtemp[i]=k; System.out.print(" "+k +" -> "+vertex.get(i)+" "); current_pathweight += graph[k][vertex.get(i)]; k = vertex.get(i); } optimal_pathtemp[optimal_pathtemp.length-2]=k; optimal_pathtemp[optimal_pathtemp.length-1]=s; System.out.print(" "+k + " -> "+ s ); current_pathweight += graph[k][s]; System.out.println(" = "+ current_pathweight); if(min_path < current_pathweight){ for (int i = 0; i < optimal_pathtemp.length; i++) { optimal_path[i]=optimal_pathtemp[i]; } } min_path = Math.min(min_path, current_pathweight);

} while (findNextPermutation(vertex)); int c = 0 ; System.out.println("This is the optimal path with cost : "); for (int i = 0; i < optimal_path.length-1; i++) { System.out.print(optimal_path[i]+" -> "); if (optimal_path[i+1]==0 ) System.out.println(""); } System.out.println(optimal_path[optimal_path.length-1]); return min_path ; } public static ArrayList swap(ArrayList data,int left, int right){ int temp = data.get(left); data.set(left, data.get(right)); data.set(right, temp); return data; } public static ArrayList reverse(ArrayList data, int left, int right) { while (left < right) { int temp = data.get(left); data.set(left++, data.get(right)); data.set(right--, temp); } return data; } public static boolean findNextPermutation(ArrayList data) { if (data.size() <= 1) return false;

int last = data.size() - 2; while (last >= 0) { if (data.get(last) < data.get(last + 1)) { break; } last--; } if (last < 0) return false;

int nextGreater = data.size() - 1; for (int i = data.size() - 1; i > last; i--) { if (data.get(i) > data.get(last)) { nextGreater = i; break; } } data = swap(data,nextGreater, last); data = reverse(data, last + 1, data.size() - 1); return true; } public static void main(String args[]) { Scanner input = new Scanner (System.in); // Getting the number of nodes and number of edges as input int num_nodes , num_edges ; System.out.println("Enter the number of nodes : "); num_nodes = input.nextInt(); System.out.println("Enter the number of edges : "); num_edges = input.nextInt(); int graph[][] = AdjacentMatrix(input , num_nodes , num_edges);

int s = 0; System.out.println(travllingSalesmanProblem(graph, s , num_nodes)); }

//-------------------------------------------------------------------------------------- // Time Complexity - O(V^2), space complexity - O(V^2), where V is the number of nodes public static int[][] AdjacentMatrix(Scanner input , int num_nodes , int num_edges){ // creating a multi-dimensional array int[][] edges_list = new int[num_nodes][num_nodes]; for(int i=0;i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!