'TSP Branch And Bound Java
I am trying to implement TSP with the BranchAndBound algorithm but faced difficulties. Currently, I achieved the result of reducing the base matrix and finding the lower bound. I am using this algorithm and this matrix in my code as well.

public class BranchAndBound {
static int INF = Integer.MAX_VALUE;
static int N = 5;
static int [] col = new int[N];
static int [] row = new int[N];
static int bound = 0;
static int costMatrix[][] =// costMatrix[EILUTE][STULPELIS]
{ // 0 1 2 3 4
{INF,25,40,31,27}, // 1 1
{5,INF,17,30,25}, // 2 3
{19,15,INF,6,1}, // 3 4
{9,50,24,INF,6}, // 4
{22,8,7,10,INF}
};
public static void main(String[] args) {
printMatrix(costMatrix);
rowReduction(costMatrix, row);
System.out.println("Row reduction");
printMatrix(costMatrix);
System.out.println("Column reduction");
columnReduction(costMatrix, col);
printMatrix(costMatrix);
bound = findBound(row, col);
System.out.println("Apatinis rezis: " + bound);
}
public static int findBound(int [] redRow, int [] redCol)
{
int boundRow= 0;
int boundCol = 0;
for(int i = 0; i < redCol.length; i++)
{
if(redCol[i] != INF)
{
boundCol = boundCol + redCol[i];
}
}
for(int i = 0; i < redRow.length; i++)
{
if(redRow[i] != INF)
{
boundRow = boundRow + redRow[i];
}
}
return boundRow + boundCol;
}
public static void printReducedElements(int [] reducedArray)
{
for(int i = 0; i < N; i++)
{
System.out.println(reducedArray[i]);
}
}
public static void printMatrix(int matrix[][])
{
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix[i].length; j++)
{
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
public static void rowReduction(int reducedMatrix[][], int [] row)
{
for(int i = 0; i < N; i++)
{
row[i] = INF;
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if(reducedMatrix[i][j] < row[i])
{
row[i] = reducedMatrix[i][j];
}
}
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if(reducedMatrix[i][j] != INF)
{
reducedMatrix[i][j] = reducedMatrix[i][j] - row[i];
}
}
}
}
public static void columnReduction(int reducedMatrix[][], int [] col)
{
for(int i = 0; i < N; i++)
{
col[i] = INF;
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if(reducedMatrix[i][j] < col[j])
{
col[j] = reducedMatrix[i][j];
}
}
}
printReducedElements(col);
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if(reducedMatrix[i][j] != INF)
{
reducedMatrix[i][j] = reducedMatrix[i][j] - col[j];
}
}
}
}
The question is where should I move on from this position? As I understand, I need to consider an edge from 0 —> 1. However, I have no clue if I should create another class with edges or nodes. Trying to make it as simple as possible. Thank you.
Solution 1:[1]
Here is a version that works. Now, there are still problems. First, it doesn't catch if they enter garbage that is not a number for the bill or for the tip. Second, it doesn't round the tip, because I don't know what currency you're using. But at least you can run this and get an answer.
print("Now that your belly is full, how much is the bill?")
fee = float(input("Enter your bill: "))
print("Would you like to give us a tip?")
answer = input("yes or no?")
if answer.lower() == 'no':
why = input("Why do you not want to tip us? Also, you don't have to answer. When done, just press enter.")
print("We understand.")
elif answer.lower() == "yes":
tip = int(input("What tip percentage do you use? "))
fee += fee * tip / 100
else:
print("I do not understand.")
print("Your total bill is", fee)
Solution 2:[2]
As mentioned in the comments, you need to wrap the answers you're testing for in quotes (double quotes ", or single quotes ' will do), because otherwise python will think they are variable names and not string literals, that you want to compare against.
Also, in the last print, you have a colon (:) that doesn't belong there.
Here is your code with those two issues fixed.
print("Now that your belly is full, how much is the fee?")
fee = input("Enter your fee: ")
print("Would you like to give us a tip?")
answer = input("Yes or no?")
if answer == 'no':
why = input("Why do you not want to tip us? Also, you don't have to answer. When done, just press enter.")
print("We understand. Here is your fee:", fee)
elif answer == 'yes':
tip = input("Would you like to tip 15 percent or 20 percent? ")
if tip == 15:
cost1 = fee / 15 + fee
elif tip == 20:
cost2 = fee / 20 + fee
else:
print("I do not understand.")
else:
print("I do not understand.")
print("Have a wonderful day!")
Also, this way of hard coding options and no easy way to skip questions (once answered 'yes' for example) makes for a very frustrating user experience, but that is another issue.
Edit: You're also comparing strings to numbers, which will always be false, which saves you from the error, that trying to divide a string by a number would produce
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Roberts |
| Solution 2 |
