GithubHelp home page GithubHelp logo

Comments (3)

Rutujaj24 avatar Rutujaj24 commented on June 1, 2024

import java.io.;
import java.util.
;

public class TSE {
// there are four nodes in example graph (graph is
// 1-based)

static int n = 4;
// give appropriate maximum to avoid overflow

static int MAX = 1000000;

// dist[i][j] represents shortest distance to go from i
// to j this matrix can be calculated for any given
// graph using all-pair shortest path algorithms
static int[][] dist = {
	{ 0, 0, 0, 0, 0 }, { 0, 0, 10, 15, 20 },
	{ 0, 10, 0, 25, 25 }, { 0, 15, 25, 0, 30 },
	{ 0, 20, 25, 30, 0 },
};

// memoization for top down recursion

static int[][] memo = new int[n + 1][1 << (n + 1)];

static int fun(int i, int mask)
{
	
	if (mask == ((1 << i) | 3))
		return dist[1][i];
	
	if (memo[i][mask] != 0)
		return memo[i][mask];

	int res = MAX;

	for (int j = 1; j <= n; j++)
		if ((mask & (1 << j)) != 0 && j != i && j != 1)
			res = Math.min(res,
						fun(j, mask & (~(1 << i)))
							+ dist[j][i]);
	return memo[i][mask] = res;
}

// Driver program to test above logic
public static void main(String[] args)
{
	int ans = MAX;
	for (int i = 1; i <= n; i++)
		// try to go from node 1 visiting all nodes in
		// between to i then return from i taking the
		// shortest route to 1
		ans = Math.min(ans, fun(i, (1 << (n + 1)) - 1)
								+ dist[i][1]);

	System.out.println(
		"The cost of most efficient tour = " + ans);
}

}

from java.

harshithsaiv avatar harshithsaiv commented on June 1, 2024

import java.io.*;

import java.util.*;

public class TSE {

// there are four nodes in example graph (graph is

// 1-based)

static int n = 4;

// give appropriate maximum to avoid overflow

static int MAX = 1000000;

// dist[i][j] represents shortest distance to go from i

// to j this matrix can be calculated for any given

// graph using all-pair shortest path algorithms

static int[][] dist = {

  { 0, 0, 0, 0, 0 }, { 0, 0, 10, 15, 20 },

  { 0, 10, 0, 25, 25 }, { 0, 15, 25, 0, 30 },

  { 0, 20, 25, 30, 0 },

};

// memoization for top down recursion

static int[][] memo = new int[n + 1][1 << (n + 1)];

static int fun(int i, int mask)

{

  if (mask == ((1 << i) | 3))

  	return dist[1][i];

  

  if (memo[i][mask] != 0)

  	return memo[i][mask];



  int res = MAX;



  for (int j = 1; j <= n; j++)

  	if ((mask & (1 << j)) != 0 && j != i && j != 1)

  		res = Math.min(res,

  					fun(j, mask & (~(1 << i)))

  						+ dist[j][i]);

  return memo[i][mask] = res;

}

// Driver program to test above logic

public static void main(String[] args)

{

  int ans = MAX;

  for (int i = 1; i <= n; i++)

  	// try to go from node 1 visiting all nodes in

  	// between to i then return from i taking the

  	// shortest route to 1

  	ans = Math.min(ans, fun(i, (1 << (n + 1)) - 1)

  							+ dist[i][1]);



  System.out.println(

  	"The cost of most efficient tour = " + ans);

}

}

In the fun function, the base case condition mask == ((1 << i) | 3) is incorrect. It should be mask == (1 << (n + 1)) - 1

from java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.