I have to create an N * M matrix and fill it up with values between 0 and 9. One of the values should be "A" which is the starting point of the graph, and I should find the shortest path to the value "B" (both of these are generated at a random position of the matrix). If the value is 0 it counts as an obstacle, and 2 < N, M < 100.
I have to print out the exact route of the shortest graph and the total cost of it. Also, the problem has to be solved by Dijkstra's algorithm.
I've haven't gotten past filling up the Matrix. I store the values in a 2D String array, but I think I should use different arrays or maybe Maps for storing the positions of key values such as the start and endpoint. I've been thinking on this for 2 days now because I'm a total noob in Java and not much better at programming in general. I'm mainly looking for guidance on how to store the datas and what should I actually store in order to get to the end because I think I overcomplicate the problem.
This is the matrix generating part of the code.
int N = ThreadLocalRandom.current().nextInt(3,7);
int M = ThreadLocalRandom.current().nextInt(3,7);
int J = ThreadLocalRandom.current().nextInt(0,(Math.min(N, M))/2);
int K = 0;
int aPosX = ThreadLocalRandom.current().nextInt(0,N);
int aPosY = ThreadLocalRandom.current().nextInt(0,M);
int bPosX = ThreadLocalRandom.current().nextInt(0,N);
int bPosY = ThreadLocalRandom.current().nextInt(0,M);
String[][] matrix = new String[N][M];
int[][] map = new int[N][M];
int shortestPath = 10;
int currentPosX,currentPosY;
int shortestPosX, shortestPosY;
public void generateMatrix(){
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
K = ThreadLocalRandom.current().nextInt(0,10);
matrix[i][j] = String.valueOf(K);
}
}
}
public void createStartAndFinish(){
matrix[aPosX][aPosY] = "A";
matrix[bPosX][bPosY] = "B";
}
}
This part finds the lowest cost adjacent tiles and steps on them but id does generate an out of bounds exception. I'm also aware that it has nothing to do with Dijkstra algorithm but this is my starting point.
public void solveMatrix(){
visited[aPosX][aPosY] = true;
currentPosX = aPosX;
currentPosY = aPosY;
while (!matrix[currentPosX - 1][currentPosY].equals("B") ||
!matrix[currentPosX + 1][currentPosY].equals("B") ||
!matrix[currentPosX][currentPosY - 1].equals("B") ||
!matrix[currentPosX][currentPosY + 1].equals("B")) {
if(currentPosX > 0) {
if(!visited[currentPosX - 1][currentPosY] && Integer.parseInt(matrix[currentPosX - 1][currentPosY]) < shortestPath) {
shortestPath = Integer.parseInt(matrix[currentPosX - 1][currentPosY]);
shortestPosX = currentPosX - 1;
shortestPosY = currentPosY;
}
}
if(currentPosX + 1 < N){
if(!visited[currentPosX + 1][currentPosY] && Integer.parseInt(matrix[currentPosX + 1][currentPosY]) < shortestPath) {
shortestPath = Integer.parseInt(matrix[currentPosX + 1][currentPosY]);
shortestPosX = currentPosX + 1;
shortestPosY = currentPosY;
}
}
if(currentPosY > 0){
if(!visited[currentPosX][currentPosY - 1] && Integer.parseInt(matrix[currentPosX][currentPosY - 1]) < shortestPath) {
shortestPath = Integer.parseInt(matrix[currentPosX][currentPosY - 1]);
shortestPosX = currentPosX;
shortestPosY = currentPosY - 1;
}
}
if(currentPosY - 1 < M){
if(!visited[currentPosX][currentPosY + 1] && Integer.parseInt(matrix[currentPosX][currentPosY + 1]) < shortestPath) {
shortestPath = Integer.parseInt(matrix[currentPosX][currentPosY + 1]);
shortestPosX = currentPosX;
shortestPosY = currentPosY + 1;
}
}
visited[shortestPosX][shortestPosY] = true;
currentPosX = shortestPosX;
currentPosY = shortestPosY;
System.out.println(shortestPosX + " " + shortestPosY + " " + shortestPath);
shortestPath = 10;
}
}
Related
I am creating a program that will take a wordlist of 5 000 strings and find the shortest path from one string to another. For example abc -> bac could print "abc, bbc, bac".
I am pretty sure about what I want to do, the only thing I'm not completely sure about is what datastructure should represent my wordlist. The goal is for the search(BFS) to run as fast as possible, so to sacrifice some space is no problem. I am thinking either a BST or an adjacency list, but since I'm no expert at datastrutcutres' timecomplexity I want to be certain before I start adjusting my code. Can anyone recommend one of the structures over the other? Or have I perhaps missed a datastructure that is an obvious alternative for this?
Looks like what you are looking for is the Levenshtein distance, here is the Rosetta code implementation, you should be able to change it to suit your need:
public class Levenshtein {
public static int distance(String a, String b) {
a = a.toLowerCase();
b = b.toLowerCase();
// i == 0
int [] costs = new int [b.length() + 1];
for (int j = 0; j < costs.length; j++)
costs[j] = j;
for (int i = 1; i <= a.length(); i++) {
// j == 0; nw = lev(i - 1, j)
costs[0] = i;
int nw = i - 1;
for (int j = 1; j <= b.length(); j++) {
int cj = Math.min(1 + Math.min(costs[j], costs[j - 1]), a.charAt(i - 1) == b.charAt(j - 1) ? nw : nw + 1);
nw = costs[j];
costs[j] = cj;
}
}
return costs[b.length()];
}
public static void main(String [] args) {
String [] data = { "kitten", "sitting", "saturday", "sunday", "rosettacode", "raisethysword" };
for (int i = 0; i < data.length; i += 2)
System.out.println("distance(" + data[i] + ", " + data[i+1] + ") = " + distance(data[i], data[i+1]));
}
}
I have given a sequence of N numbers (4 ≤ N ≤ 150). One index i (0 < i < N) is picked and multiplied with the left and the right number, in other words with i-1 and i+1. Then the i-th number is removed. This is done until the sequence has only two numbers left over. The goal is to find the smallest sum of these products which obviously depends on the order in which the indices are picked.
E.g. for the sequence 44, 45, 5, 39, 15, 22, 10 the smallest sum would be 17775
using the indices in the following order: 1->3->4->5->2 which is the sum:
44*45*5 + 5*39*15 + 5*15*22 + 5*22*10 + 44*5*10 = 9900 + 2925 + 1650 + 1100 + 2200 = 17775
I have found a solution using a recursive function:
public static int smallestSum(List<Integer> values) {
if (values.size() == 3)
return values.get(0) * values.get(1) * values.get(2);
else {
int ret = Integer.MAX_VALUE;
for (int i = 1; i < values.size() - 1; i++) {
List<Integer> copy = new ArrayList<Integer>(values);
copy.remove(i);
int val = smallestSum(copy) + values.get(i - 1) * values.get(i) * values.get(i + 1);
if (val < ret) ret = val;
}
return ret;
}
}
However, this solution is only feasible for small N but not for a bigger amount of numbers. What I am looking for is a way to do this using an iterative Dynamic Programming approach.
The optimal substructure needed for a DP is that, given the identity of the last element removed, the elimination strategy for the elements to the left is independent of the elimination strategy for the elements to the right. Here's a new recursive function (smallestSumA, together with the version from the question and a test harness comparing the two) incorporating this observation:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Foo {
public static void main(String[] args) {
Random r = new Random();
for (int i = 0; i < 10000; i++) {
List<Integer> values = new ArrayList<Integer>();
for (int n = 3 + r.nextInt(8); n > 0; n--) {
values.add(r.nextInt(100));
}
int a = smallestSumA(values, 0, values.size() - 1);
int q = smallestSumQ(values);
if (q != a) {
System.err.println("oops");
System.err.println(q);
System.err.println(a);
System.err.println(values);
}
}
}
public static int smallestSumA(List<Integer> values, int first, int last) {
if (first + 2 > last)
return 0;
int ret = Integer.MAX_VALUE;
for (int i = first + 1; i <= last - 1; i++) {
int val = (smallestSumA(values, first, i)
+ values.get(first) * values.get(i) * values.get(last) + smallestSumA(values, i, last));
if (val < ret)
ret = val;
}
return ret;
}
public static int smallestSumQ(List<Integer> values) {
if (values.size() == 3)
return values.get(0) * values.get(1) * values.get(2);
else {
int ret = Integer.MAX_VALUE;
for (int i = 1; i < values.size() - 1; i++) {
List<Integer> copy = new ArrayList<Integer>(values);
copy.remove(i);
int val = smallestSumQ(copy) + values.get(i - 1) * values.get(i) * values.get(i + 1);
if (val < ret)
ret = val;
}
return ret;
}
}
}
Invoke as smallestSum(values, 0, values.size() - 1).
To get the DP, observe that there are only N choose 2 different settings for first and last, and memoize. The running time is O(N^3).
If anyone is interested in a DP solution, based on David Eisenstat's recursive solution, here is an iterative one using DP (for many big numbers it's useful to replace int's with long's):
public static int smallestSum(List<Integer> values) {
int[][] table = new int[values.size()][values.size()];
for (int i = 2; i < values.size(); i++) {
for (int j = 0; j + i < values.size(); j++) {
int ret = Integer.MAX_VALUE;
for (int k = j + 1; k <= j + i - 1; k++) {
int val = table[j][k] + values.get(j) * values.get(k) * values.get(j + i) + table[k][j + i];
if (val < ret) ret = val;
}
table[j][j + i] = ret;
}
}
return table[0][values.size() - 1];
}
I was asked this question on a technical interview. Question is: given a target, and an array of strings, return an array that contains all strings with ONLY one difference to the target.
For example if target is cat, catt, caT, caa, ca, at <-- are all only one difference. And conversely, cat, cattt, dog, flower, c <-- are not one difference and should not be returned.
oneDiff(String target, String[] a) ...
My approach was:
ans = []
for all element e in the array
count -> 0
if absoulte(e's length - target length) > 1
continue
endif
for all character c in e
scan through, increment count if difference is found.
endfor
if (count == 1)
continue
else
add e to ans
endfor
return ans
But the interviewer wasnt happy with what's above. Anyone has any efficient/clever ideas?
Thanks
As mentioned by zubergu Levenshtein distance will solve your problem. You can find Levenshtein distance in java here.
Edit: Since you tagged it as java you can run the following java code:
public class Levenshtein {
public static int distance(String a, String b) {
a = a.toLowerCase();
b = b.toLowerCase();
// i == 0
int [] costs = new int [b.length() + 1];
for (int j = 0; j < costs.length; j++)
costs[j] = j;
for (int i = 1; i <= a.length(); i++) {
// j == 0; nw = lev(i - 1, j)
costs[0] = i;
int nw = i - 1;
for (int j = 1; j <= b.length(); j++) {
int cj = Math.min(1 + Math.min(costs[j], costs[j - 1]), a.charAt(i - 1) == b.charAt(j - 1) ? nw : nw + 1);
nw = costs[j];
costs[j] = cj;
}
}
return costs[b.length()];
}
public static void main(String [] args) {
String comparison = "cat";
String [] data = { "cattt", "catt", "caT", "caa", "ca", "at" };
for (int i = 0; i < data.length; i++)
System.out.println("distance(" + comparison + ", " + data[i] + ") = " + distance(comparison, data[i]));
}
}
If you run the code you will see the following output:
distance(cat, cattt) = 2
distance(cat, catt) = 1
distance(cat, caT) = 0
distance(cat, caa) = 1
distance(cat, ca) = 1
distance(cat, at) = 1
If the distance is 0 or 1 then its acceptable.
I'm a beginner, and I'm trying to write a working travelling salesman problem using dynamic programming approach.
This is the code for my compute function:
public static int compute(int[] unvisitedSet, int dest) {
if (unvisitedSet.length == 1)
return distMtx[dest][unvisitedSet[0]];
int[] newSet = new int[unvisitedSet.length-1];
int distMin = Integer.MAX_VALUE;
for (int i = 0; i < unvisitedSet.length; i++) {
for (int j = 0; j < newSet.length; j++) {
if (j < i) newSet[j] = unvisitedSet[j];
else newSet[j] = unvisitedSet[j+1];
}
int distCur;
if (distMtx[dest][unvisitedSet[i]] != -1) {
distCur = compute(newSet, unvisitedSet[i]) + distMtx[unvisitedSet[i]][dest];
if (distMin > distCur)
distMin = distCur;
}
else {
System.out.println("No path between " + dest + " and " + unvisitedSet[i]);
}
}
return distMin;
}
The code is not giving me the correct answers, and I'm trying to figure out where the error is occurring. I think my error occurs when I add:
distCur = compute(newSet, unvisitedSet[i]) + distMtx[unvisitedSet[i]][dest];
So I've been messing around with that part, moving the addition to the very end right before I return distMin and so on... But I couldn't figure it out.
Although I'm sure it can be inferred from the code, I will state the following facts to clarify.
distMtx stores all the intercity distances, and distances are symmetric, meaning if distance from city A to city B is 3, then the distance from city B to city A is also 3. Also, if two cities don't have any direct paths, the distance value is -1.
Any help would be very much appreciated!
Thanks!
Edit:
The main function reads the intercity distances from a text file. Because I'm assuming the number of cities will always be less than 100, global int variable distMtx is [100][100].
Once the matrix is filled with the necessary information, an array of all the cities are created. The names of the cities are basically numbers. So if I have 4 cities, set[4] = {0, 1, 2, 3}.
In the main function, after distMtx and set is created, first call to compute() is called:
int optRoute = compute(set, 0);
System.out.println(optRoute);
Sample input:
-1 3 2 7
3 -1 10 1
2 10 -1 4
7 1 4 -1
Expected output:
10
Here's a working iterative solution to the TSP with dynamic programming. What would make your life easier is to store the current state as a bitmask instead of in an array. This has the advantage that the state representation is compact and can be cached easily.
I made a video detailing the solution to this problem on Youtube, please enjoy! Code was taken from my github repo
/**
* An implementation of the traveling salesman problem in Java using dynamic
* programming to improve the time complexity from O(n!) to O(n^2 * 2^n).
*
* Time Complexity: O(n^2 * 2^n)
* Space Complexity: O(n * 2^n)
*
**/
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class TspDynamicProgrammingIterative {
private final int N, start;
private final double[][] distance;
private List<Integer> tour = new ArrayList<>();
private double minTourCost = Double.POSITIVE_INFINITY;
private boolean ranSolver = false;
public TspDynamicProgrammingIterative(double[][] distance) {
this(0, distance);
}
public TspDynamicProgrammingIterative(int start, double[][] distance) {
N = distance.length;
if (N <= 2) throw new IllegalStateException("N <= 2 not yet supported.");
if (N != distance[0].length) throw new IllegalStateException("Matrix must be square (n x n)");
if (start < 0 || start >= N) throw new IllegalArgumentException("Invalid start node.");
this.start = start;
this.distance = distance;
}
// Returns the optimal tour for the traveling salesman problem.
public List<Integer> getTour() {
if (!ranSolver) solve();
return tour;
}
// Returns the minimal tour cost.
public double getTourCost() {
if (!ranSolver) solve();
return minTourCost;
}
// Solves the traveling salesman problem and caches solution.
public void solve() {
if (ranSolver) return;
final int END_STATE = (1 << N) - 1;
Double[][] memo = new Double[N][1 << N];
// Add all outgoing edges from the starting node to memo table.
for (int end = 0; end < N; end++) {
if (end == start) continue;
memo[end][(1 << start) | (1 << end)] = distance[start][end];
}
for (int r = 3; r <= N; r++) {
for (int subset : combinations(r, N)) {
if (notIn(start, subset)) continue;
for (int next = 0; next < N; next++) {
if (next == start || notIn(next, subset)) continue;
int subsetWithoutNext = subset ^ (1 << next);
double minDist = Double.POSITIVE_INFINITY;
for (int end = 0; end < N; end++) {
if (end == start || end == next || notIn(end, subset)) continue;
double newDistance = memo[end][subsetWithoutNext] + distance[end][next];
if (newDistance < minDist) {
minDist = newDistance;
}
}
memo[next][subset] = minDist;
}
}
}
// Connect tour back to starting node and minimize cost.
for (int i = 0; i < N; i++) {
if (i == start) continue;
double tourCost = memo[i][END_STATE] + distance[i][start];
if (tourCost < minTourCost) {
minTourCost = tourCost;
}
}
int lastIndex = start;
int state = END_STATE;
tour.add(start);
// Reconstruct TSP path from memo table.
for (int i = 1; i < N; i++) {
int index = -1;
for (int j = 0; j < N; j++) {
if (j == start || notIn(j, state)) continue;
if (index == -1) index = j;
double prevDist = memo[index][state] + distance[index][lastIndex];
double newDist = memo[j][state] + distance[j][lastIndex];
if (newDist < prevDist) {
index = j;
}
}
tour.add(index);
state = state ^ (1 << index);
lastIndex = index;
}
tour.add(start);
Collections.reverse(tour);
ranSolver = true;
}
private static boolean notIn(int elem, int subset) {
return ((1 << elem) & subset) == 0;
}
// This method generates all bit sets of size n where r bits
// are set to one. The result is returned as a list of integer masks.
public static List<Integer> combinations(int r, int n) {
List<Integer> subsets = new ArrayList<>();
combinations(0, 0, r, n, subsets);
return subsets;
}
// To find all the combinations of size r we need to recurse until we have
// selected r elements (aka r = 0), otherwise if r != 0 then we still need to select
// an element which is found after the position of our last selected element
private static void combinations(int set, int at, int r, int n, List<Integer> subsets) {
// Return early if there are more elements left to select than what is available.
int elementsLeftToPick = n - at;
if (elementsLeftToPick < r) return;
// We selected 'r' elements so we found a valid subset!
if (r == 0) {
subsets.add(set);
} else {
for (int i = at; i < n; i++) {
// Try including this element
set |= 1 << i;
combinations(set, i + 1, r - 1, n, subsets);
// Backtrack and try the instance where we did not include this element
set &= ~(1 << i);
}
}
}
public static void main(String[] args) {
// Create adjacency matrix
int n = 6;
double[][] distanceMatrix = new double[n][n];
for (double[] row : distanceMatrix) java.util.Arrays.fill(row, 10000);
distanceMatrix[5][0] = 10;
distanceMatrix[1][5] = 12;
distanceMatrix[4][1] = 2;
distanceMatrix[2][4] = 4;
distanceMatrix[3][2] = 6;
distanceMatrix[0][3] = 8;
int startNode = 0;
TspDynamicProgrammingIterative solver = new TspDynamicProgrammingIterative(startNode, distanceMatrix);
// Prints: [0, 3, 2, 4, 1, 5, 0]
System.out.println("Tour: " + solver.getTour());
// Print: 42.0
System.out.println("Tour cost: " + solver.getTourCost());
}
}
I know this is pretty old question but it might help somebody in the future.
Here is very well written paper on TSP with dynamic programming approach
https://github.com/evandrix/SPOJ/blob/master/DP_Main112/Solving-Traveling-Salesman-Problem-by-Dynamic-Programming-Approach-in-Java.pdf
I think you have to make some changes in your program.
Here there is an implementation
http://www.sanfoundry.com/java-program-implement-traveling-salesman-problem-using-nearest-neighbour-algorithm/
I'm trying to implement the CYK algorithm based on wikipedia pseudocode. When I test the string "a b" for the grammar input:
S->A B
A->a
B->b
It gives me false, and I think it should be true. I have an arraylist called AllGrammar that contains all the rules. For the example above it would contain:
[0]: S->A B[1]: A->a[2]: B->bFor the example S->hello and the input string hello it gives me true as it should. More complex tests (more productions) gives me false :S
public static boolean cyk(String entrada) {
int n = entrada.length();
int r = AllGrammar.size();
//Vector<String> startingsymbols = getSymbols(AllGrammar);
String[] ent = entrada.split("\\s");
n = ent.length;
System.out.println("length of entry" + n);
//let P[n,n,r] be an array of booleans. Initialize all elements of P to false.
boolean P[][][] = initialize3DVector(n, r);
//n-> number of words of string entrada,
//r-> number of nonterminal symbols
//This grammar contains the subset Rs which is the set of start symbols
for (int i = 1; i < n; i++) {
for(int j = 0; j < r; j++) {
String[] rule = (String[]) AllGrammar.get(j);
if (rule.length == 2) {
if (rule[1].equals(ent[i])) {
System.out.println("entrou");
System.out.println(rule[1]);
P[i][1][j + 1] = true;
}
}
}
}
for(int i = 2; i < n; i++) {
System.out.println("FIRST:" + i);
for(int j = 1; j < n - i + 1; j++) {
System.out.println("SECOND:" + j);
for(int k = 1; k < i - 1; k++) {
System.out.println("THIRD:" + k);
for(int g = 0; g < r; g++) {
String[] rule = (String[]) AllGrammar.get(g);
if (rule.length > 2) {
int A = returnPos(rule[0]);
int B = returnPos(rule[1]);
int C = returnPos(rule[2]);
System.out.println("A" + A);
System.out.println("B" + B);
System.out.println("C" + C);
if (A!=-1 && B!=-1 && C!=-1) {
if (P[j][k][B] && P[j + k][i - k][C]) {
System.out.println("entrou2");
P[j][i][A] = true;
}
}
}
}
}
}
}
for(int x = 0; x < r; x++) {
if(P[1][n][x]) return true;
}
return false;
}
As compared to the CYK algorithm:
you have indexing starting at 1, but the arrays would appear to start at 0
the function returnpos() is not defined, and it's not obvious what it does.
It would seem the problems could be fairly basic in the the use of indexes. If you are new to the language, you might want to get a refresher.