So I have a program written so far that reads in a csv file of cities and distances in the following format:
Alaska Mileage Chart,Anchorage,Anderson,Cantwell,
Anchorage,0,284,210,
Anderson,284,0,74,
Cantwell,210,74,0,
So the algorithm works and outputs the cities in the order they should be visited following the shortest path using the nearest neighbor algorithm always starting with Anchorage as the city of origin or starting city.
Using this data, the example output for the algorithm is: 1,3,2. I have ran this with a 27 element chart and had good results as well. I am using this small one for writing and debugging purposes.
Ideally the output I am looking for is the Name of the City and a cumulative milage.
Right now I am having working on trying to get the cities into an array that I can print out. Help with both parts would be appreciated or help keeping in mind that is the end goal is appreciated as well.
My thought was that ultimately I may want to create an array of {string, int}
so my output would look something like this..
Anchorage 0
Cantwell 210
Anderson 284
I am able to set the first element of the array to 1, but can not get the 2nd and 3rd element of the new output array to correct
This is the code I am having a problem with:
public class TSPNearestNeighbor {
private int numberOfNodes;
private Stack<Integer> stack;
public TSPNearestNeighbor()
{
stack = new Stack<>();
}
public void tsp(int adjacencyMatrix[][])
{
numberOfNodes = adjacencyMatrix[1].length;
// System.out.print(numberOfNodes);
// System.out.print(Arrays.deepToString(adjacencyMatrix));
int[] visited = new int[numberOfNodes];
// System.out.print(Arrays.toString(visited));
visited[1] = 1;
// System.out.print(Arrays.toString(visited));
stack.push(1);
int element, dst = 0, i;
int min = Integer.MAX_VALUE;
boolean minFlag = false;
System.out.print(1 + "\n");
//System.arraycopy(arr_cities, 0, arr_final, 0, 1); // Copies Anchorage to Pos 1 always
//System.out.print(Arrays.deepToString(arr_final)+ "\n");
while (!stack.isEmpty())
{
element = stack.peek();
i = 1;
min = Integer.MAX_VALUE;
while (i <= numberOfNodes-1)
{
if (adjacencyMatrix[element][i] > 1 && visited[i] == 0)
{
if (min > adjacencyMatrix[element][i])
{
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag)
{
visited[dst] = 1;
stack.push(dst);
System.out.print(dst + "\n");
minFlag = false;
continue;
}
stack.pop();
}
}
Given the existing structure you are using, you can output the cities in the path using:
public void printCities(Stack<Integer> path, int[][] distances, List<String> names) {
int cumulativeDistance = 0;
int previous = -1;
for (int city: path) {
if (previous != -1)
cumulativeDistance += distances[previous][city];
System.out.println(names.get(city) + " " + cumulativeDistance);
previous = city;
}
}
I'd like to answer your question slightly indirectly. You are making life hard for yourself by using arrays of objects. They make the code difficult to read and are hard to access. Things would become easier if you create a City class with appropriate methods to help you with the output.
For example:
class City {
private final String name;
private final Map<City,Integer> connections = new HashMap<>();
public static addConnection(City from, City to, int distance) {
from.connections.put(to, distance);
to.connections.put(from, distance);
}
public int getDistanceTo(City other) {
if (connections.containsKey(other))
return connections.get(other);
else
throw new IllegalArgumentException("Non connection error");
}
}
I've left out constructor, getters, setters for clarity.
Now outputting your path becomes quite a bit simpler:
public void outputPath(List<City> cities) {
int cumulativeDistance = 0;
City previous = null;
for (City current: cities) {
if (previous != null)
cumulativeDistance += previous.getDistanceTo(current);
System.out.println(current.getName + " " + cumulativeDistance);
previous = current;
}
}
Related
I have to find all the cycles in a directed graph where every node has to only go out to 1 node but it can have more than one come in towards it and print all the nodes that are in a cycle.
Is there anyway I can make [my code][1] run faster?
right now it runs 100k nodes at about 4s but the time-limit is 1.5s
import java.io.*;
import java.util.*;
public class Main {
public static void main (String[] args) throws IOException {
long startTime = 0;
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int numOfPeople = Integer.parseInt(input.readLine());
StringTokenizer following = new StringTokenizer(input.readLine(), " ");
startTime = System.nanoTime();
int[] people = new int[numOfPeople], // index -> personID, value -> personID's friend
winningPotentials = new int[numOfPeople]; // index -> personID, value -> personID's winningPotential
Arrays.fill(winningPotentials, 50);
// adding followings of people
for (int i = 0 ; i < numOfPeople ; i++) {
people[i] = Integer.parseInt(following.nextToken()) - 1;
}
/*
SETTING WINNER POTENTIALS
*/
int numOfWinners = 0;
for (int person : people) {
if (winningPotentials[person] == 50) {
Deque<Integer> path = new ArrayDeque<>();
path.addLast(person);
while (true) {
int friend = people[person];
if (path.contains(friend)) {
// all those in a friend group are winningPot = 100
while (path.getLast() != friend) {
if (winningPotentials[path.peekLast()] != 100) {
numOfWinners++;
winningPotentials[path.peekLast()] = 100;
}
path.removeLast();
}
if (winningPotentials[path.peekLast()] != 100) {
numOfWinners++;
winningPotentials[path.peekLast()] = 100;
}
path.removeLast();
break;
}
// if friend hasn't been checked before, repeat
else {
path.addLast(friend);
person = friend;
}
}
// anyone in the path that wasnt in a friend group is winnerPot=0
for (int person2 : path)
winningPotentials[person2] = 0;
}
}
/*
PRINTING THE RESULTS
*/
StringBuilder sb = new StringBuilder();
sb.append(numOfWinners + "\n");
// print each winner
for (int i = 0 ; i < winningPotentials.length ; i++)
if (winningPotentials[i] == 100)
sb.append((i + 1) + " ");
sb.append("\nExecution Time ->\t" + ((System.nanoTime() - startTime) / 1000000) + "ms");
output.write(sb.toString());
output.flush();
output.close();
}
}
Why do you need a BufferedWriter ? Can you just not do a System.out.println(sb.toString()) ?
This can be implemented as a modified BFS algorithm.
Difference is what whenever you see a point that has already been added to the queue, and isn't the point before the one you were just at, you have found a cycle. So when you add points to the queue you add the current path to that point instead of just the point, but you also add the adjacent point (the last on the path) to a list of already found points.
I would probably wait to calculate the winning Potentials until you have found all the cycles.
I implemented an experimental OOP language and now benchmark garbage collection using a Storage benchmark. Now I want to check/print the following benchmark for small depths (n=2, 3, 4,..).
The tree (forest with 4 subnode) is generated by the buildTreeDepth method. The code is as follows:
import java.util.Arrays;
public final class StorageSimple {
private int count;
private int seed = 74755;
public int randomNext() {
seed = ((seed * 1309) + 13849) & 65535;
return seed;
}
private Object buildTreeDepth(final int depth) {
count++;
if (depth == 1) {
return new Object[randomNext() % 10 + 1];
} else {
Object[] arr = new Object[4];
Arrays.setAll(arr, v -> buildTreeDepth(depth - 1));
return arr;
}
}
public Object benchmark() {
count = 0;
buildTreeDepth(7);
return count;
}
public boolean verifyResult(final Object result) {
return 5461 == (int) result;
}
public static void main(String[] args) {
StorageSimple store = new StorageSimple();
System.out.println("Result: " + store.verifyResult(store.benchmark()));
}
}
Is there a somewhat simple/straight forward way to print the tree generated by buildTreeDepth? Just the short trees of n=3, 4, 5.
As other has already suggested, you may choose some lib to do so. But if you just want a simple algo to test in command line, you may do the following, which I always use when printing tree in command line (write by handle, may have some bug. Believe you can get what this BFS algo works):
queue.add(root);
queue.add(empty);
int count = 1;
while (queue.size() != 1) {
Node poll = queue.poll();
if (poll == empty) {
count = 1;
queue.add(empty);
}
for (Node n : poll.getChildNodes()) {
n.setNodeName(poll.getNodeName(), count++);
queue.add(n);
}
System.out.println(poll.getNodeName());
}
Sample output:
1
1-1 1-2 1-3 1-4
1-1-1 1-1-2 1-1-3 1-2-1 1-2-2 1-3-1 1-3-2 1-4-1
...
And in your case you use array, which seems even easier to print.
Instead of using object arrays, use a List implementation like ArrayList. For an improved better result subclass ArrayList to also hold a 'level' value and add indentation to the toString() method.
I am trying to add marker New direction to the arrayList mergeArray when I found the second 3 value in the buffer arrayList but I am always getting the first 3 value in the ArrayList. How can I get the second one after Amsterdam?
I appreciate any help.
output:
paris
3
water
ball
money
Amsterdam
3
door
output should looks like this:
paris
3
water
ball
money
New direction
Amsterdam
3
door
Code:
public static void main(String[] args) {
ArrayList<String> buffer = new ArrayList<String>();
ArrayList<String> mergeArray = new ArrayList<String>();
String route = "3";
String direction = "paris";
String start = "Amsterdam";
buffer.add("paris");
buffer.add("3");
buffer.add("water");
buffer.add("ball");
buffer.add("money");
buffer.add("Amsterdam");
buffer.add("3");
buffer.add("door");
for (String line : buffer) {
if (line.equals(route)) {
mergeArray.add(line);
int index = buffer.indexOf(line);
String prevElement = buffer.get(index - 1);
if (prevElement == direction) {
String addElem = buffer.get(index + 1);
mergeArray.add(addElem);
} else if (prevElement == start) {
mergeArray.add("New direction");
}
}
}
for (String key : mergeArray) {
System.out.println(key);
}
}
Do not use indexOf as it will always retrieve the index of the first appearance.
Keep an auxiliary index variable and use it in your loop:
int auxIndex = 0;
for (String line : buffer) {
if (line.equals(route)) {
mergeArray.add(line);
String prevElement = buffer.get(auxIndex - 1);
if (prevElement.equals(direction)) {
String addElem = buffer.get(auxIndex + 1);
mergeArray.add(addElem);
} else if (prevElement.equals(start)) {
mergeArray.add("New direction");
}
}
auxIndex++
}
also add safety checks so the index will not under/over-flow
Try using a clasic for, instead of one that uses Iterable
for (int i=0; i<buffer.size(); i++) {
String line = buffer.get(i);
if (line.equals(route)) {
mergeArray.add(line);
String prevElement = buffer.get(i - 1);
if (prevElement == direction) {
String addElem = buffer.get(i + 1);
mergeArray.add(addElem);
} else if (prevElement == start) {
mergeArray.add("New direction");
}
}
}
Also, consider checking for over/underflow problems. You refer to index-1 and index+1, which will cause trouble if the appearance is on the first or last position.
I have implemented two algorithms in Java and when testing depth first search it seems to be taking an incredible amount of time when there are 12 nodes, when using A* it completes it in seconds, I was just wondering if this is to be expected or am I doing something wrong? Its running the search in the background now as I type this and has been going for a few minutes.
I wouldnt normally mind but ive got to test up to 500 nodes which could take days at this rate, is this something I should expect or am I doing something wrong?
Thanks!
import java.util.*;
#SuppressWarnings({ "rawtypes", "unchecked" })
public class DepthFirstSearch {
Routes distances;
static Routes routes;
int firstNode;
String result = new String();
ArrayList firstRoute, bestRoute;
int nodes = 0;
int routeCost = 0;
int bestCost = Integer.MAX_VALUE;
public DepthFirstSearch(Routes matrix, int firstNode) { //new instance
distances = matrix;
this.firstNode = firstNode;
}
public void run () { //run algorithm
long startTime = System.nanoTime();
firstRoute = new ArrayList();
firstRoute.add(firstNode);
bestRoute = new ArrayList();
nodes++;
long endTime = System.nanoTime();
System.out.println("Depth First Search\n");
search(firstNode, firstRoute);
System.out.println(result);
System.out.println("Visited Nodes: "+nodes);
System.out.println("\nBest solution: "+bestRoute.toString() + "\nCost: "+bestCost);
System.out.println("\nElapsed Time: "+(endTime-startTime)+" ns\n");
}
/**
* #param from node where we start the search.
* #param route followed route for arriving to node "from".
*/
public void search (int from, ArrayList chosenRoute) {
// we've found a new solution
if (chosenRoute.size() == distances.getCitiesCount()) {
chosenRoute.add(firstNode);
nodes++;
// update the route's cost
routeCost += distances.getCost(from, firstNode);
if (routeCost < bestCost) {
bestCost = routeCost;
bestRoute = (ArrayList)chosenRoute.clone();
}
result += chosenRoute.toString() + " - Cost: "+routeCost + "\n";
// update the route's cost (back to the previous value)
routeCost -= distances.getCost(from, firstNode);
}
else {
for (int to=0; to<distances.getCitiesCount(); to++){
if (!chosenRoute.contains(to)) {
ArrayList increasedRoute = (ArrayList)chosenRoute.clone();
increasedRoute.add(to);
nodes++;
// update the route's cost
routeCost += distances.getCost(from, to);
search(to, increasedRoute);
// update the route's cost (back to the previous value)
routeCost -= distances.getCost(from, to);
}
}
}
}
}
you are not updating chosenRoute correctly; you always add "firstNode" with the same value to your arraylist, I think you should add the visited node.
I will try to check that later
I have an algorithm that recursively makes change in the following manner:
public static int makeChange(int amount, int currentCoin) {
//if amount = zero, we are at the bottom of a successful recursion
if (amount == 0){
//return 1 to add this successful solution
return 1;
//check to see if we went too far
}else if(amount < 0){
//don't count this try if we went too far
return 0;
//if we have exhausted our list of coin values
}else if(currentCoin < 0){
return 0;
}else{
int firstWay = makeChange(amount, currentCoin-1);
int secondWay = makeChange(amount - availableCoins[currentCoin], currentCoin);
return firstWay + secondWay;
}
}
However, I'd like to add the capability to store or print each combination as they successfully return. I'm having a bit of a hard time wrapping my head around how to do this. The original algorithm was pretty easy, but now I am frustrated. Any suggestions?
CB
Without getting into the specifics of your code, one pattern is to carry a mutable container for your results in the arguments
public static int makeChange(int amount, int currentCoin, List<Integer>results) {
// ....
if (valid_result) {
results.add(result);
makeChange(...);
}
// ....
}
And call the function like this
List<Integer> results = new LinkedList<Integer>();
makeChange(amount, currentCoin, results);
// after makeChange has executed your results are saved in the variable "results"
I don't understand logic or purpose of above code but this is how you can have each combination stored and then printed.
public class MakeChange {
private static int[] availableCoins = {
1, 2, 5, 10, 20, 25, 50, 100 };
public static void main(String[] args) {
Collection<CombinationResult> results = makeChange(5, 7);
for (CombinationResult r : results) {
System.out.println(
"firstWay=" + r.getFirstWay() + " : secondWay="
+ r.getSecondWay() + " --- Sum=" + r.getSum());
}
}
public static class CombinationResult {
int firstWay;
int secondWay;
CombinationResult(int firstWay, int secondWay) {
this.firstWay = firstWay;
this.secondWay = secondWay;
}
public int getFirstWay() {
return this.firstWay;
}
public int getSecondWay() {
return this.secondWay;
}
public int getSum() {
return this.firstWay + this.secondWay;
}
public boolean equals(Object o) {
boolean flag = false;
if (o instanceof CombinationResult) {
CombinationResult r = (CombinationResult) o;
flag = this.firstWay == r.firstWay
&& this.secondWay == r.secondWay;
}
return flag;
}
public int hashCode() {
return this.firstWay + this.secondWay;
}
}
public static Collection<CombinationResult> makeChange(
int amount, int currentCoin) {
Collection<CombinationResult> results =
new ArrayList<CombinationResult>();
makeChange(amount, currentCoin, results);
return results;
}
public static int makeChange(int amount, int currentCoin,
Collection<CombinationResult> results) {
// if amount = zero, we are at the bottom of a successful recursion
if (amount == 0) {
// return 1 to add this successful solution
return 1;
// check to see if we went too far
} else if (amount < 0) {
// don't count this try if we went too far
return 0;
// if we have exhausted our list of coin values
} else if (currentCoin < 0) {
return 0;
} else {
int firstWay = makeChange(
amount, currentCoin - 1, results);
int secondWay = makeChange(
amount - availableCoins[currentCoin],
currentCoin, results);
CombinationResult resultEntry = new CombinationResult(
firstWay, secondWay);
results.add(resultEntry);
return firstWay + secondWay;
}
}
}
I used the following:
/**
* This is a recursive method that calculates and displays the combinations of the coins included in
* coinAmounts that sum to amountToBeChanged.
*
* #param coinsUsed is a list of each coin used so far in the total. If this branch is successful, we will add another coin on it.
* #param largestCoinUsed is used in the recursion to indicate at which coin we should start trying to add additional ones.
* #param amountSoFar is used in the recursion to indicate what sum we are currently at.
* #param amountToChange is the original amount that we are making change for.
* #return the number of successful attempts that this branch has calculated.
*/private static int change(List<Integer> coinsUsed, Integer currentCoin, Integer amountSoFar, Integer amountToChange)
{
//if last added coin took us to the correct sum, we have a winner!
if (amountSoFar == amountToChange)
{
//output
System.out.print("Change for "+amountToChange+" = ");
//run through the list of coins that we have and display each.
for(Integer count: coinsUsed){
System.out.print(count + " ");
}
System.out.println();
//pass this back to be tallied
return 1;
}
/*
* Check to see if we overshot the amountToBeChanged
*/
if (amountSoFar > amountToChange)
{
//this branch was unsuccessful
return 0;
}
//this holds the sum of the branches that we send below it
int successes=0;
// Pass through each coin to be used
for (Integer coin:coinAmounts)
{
//we only want to work on currentCoin and the coins after it
if (coin >= currentCoin)
{
//copy the list so we can branch from it
List<Integer> copyOfCoinsUsed = new ArrayList<Integer>(coinsUsed);
//add on one of our current coins
copyOfCoinsUsed.add(coin);
//branch and then collect successful attempts
successes += change(copyOfCoinsUsed, coin, amountSoFar + coin, amountToChange);
}
}
//pass back the current
return successes;
}