Recursion to print all paths in a matrix - java

I came across a solution HERE. Can someone please explain how is this working. In particular, one of things that I am not able to understand is the recursive call. In one of them new ArrayList<>(path) is passed while in other simply path is passed. Why ? Between the solution is working all right.
public class Main {
public static void getPaths(int[][]A, int i, int j, ArrayList<Integer> path, ArrayList<ArrayList<Integer>> allPaths) {
int n = A.length;
if (i>=n || j>=n) return;
path.add(A[i][j]);
if (i==n-1 && j==n-1) {
allPaths.add(path);
return;
}
getPaths(A, i, j+1, new ArrayList<>(path), allPaths);
getPaths(A, i+1, j, path, allPaths);
}
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> allPaths = new ArrayList<>();
getPaths(new int[][] { {1,2,3},{4,5,6},{7,8,9}}, 0,0, new ArrayList<Integer>(), allPaths );
System.out.println(allPaths);
}
}

A copy of the path so far is created and passed in the first recursive call so that further entries can be added to the path. We do not need to pass it in the second call as whatever entries will be added as a result of second call are part of the first call's path.

They represent two different paths from the current one. So, new ArrayList<>(path) is used to create a copy of the current path in one direction and just path is passed to complete the current path on the other direction.
Essentially because you want to complete two different pahts, you can't use the current one to insert two different paths in the same array. So you pass the copy in one of the calls, to have that path in a different memory area so both paths that separate on the current point can be calculated independently.

Related

text based undo function in java

I'm trying to figure out how to get a undo function for a small maze game. First I worked out a way to do this by checking what the last direction was and just going back in the opposite direction. But this code was getting way too long since I also had to track back possible item pickups or hidden walls etc.
Background info on the code: I use a String[][] to store the maze as this was the easiest. I use an Arraylist<String[][]> to store all the strings.
After each step the player takes I save the String[][] array to the arraylist. When the player say undo I look at the second last String[][] in the arraylist and want to set the String[][] back to this. But the currentPos never seems to get updated. I'm not sure where the problem lies.
if (direction.equals("north")) {
if (currentPos[i - 1][j].equals("---")) {
continue;
} else {
currentPos[i][j] = " ";
currentPos[i - 2][j] = "P";
break;
}
}
if (direction.equals("undo")) {
currentPos = history.get(history.size()-2);
history.remove(history.size()-1);
break;
}
Without understanding the way you are setting history, I've made the assumption from your question that you are simply adding the current map to the history list. If you aren't careful, you will be simply adding the same Object, populating the history with multiply Object references to the current map state. This would have the effect you are observing with the state not changing, because you the history only contains a reference to the most recent map (Not storing any actual history).
To obtain the value from an Object, you typically need to clone the object (invoking the clone() method). However, cloning a 2-dimensional array is somewhat problematic. Invoking the clone() method on a 2-dimensional array "shallow" clones the object, essentially only cloning the first dimension while leaving the second as a reference to the same object (The reason for this is that the first 1-dimension of the array holds a reference to the second 1-dimension). Changing the value on a shallow copied object will change the value of the original and vice-versa, not what you want if you want to keep the Objects distinct.
To create two distinct objects, you will need to perform a "deep" clone, which can be easily implemented in a helper method. The below code illustrates the importance of ensuring you fully clone the object before storing it in the history list.
public static void main (String args[]) throws Exception {
ArrayList<String[][]> list = new ArrayList<>();
String[][] shallowClonedMap = new String[1][1];
String[][] deepClonedMap = new String[1][1];
shallowClonedMap[0][0] = "Old";
deepClonedMap[0][0] = "Old";
list.add(shallowClonedMap.clone());
list.add(deepClone(deepClonedMap));
shallowClonedMap[0][0] = "New";
deepClonedMap[0][0] = "New";
list.add(shallowClonedMap.clone());
list.add(deepClone(deepClonedMap));
for (String[][] item : list) {
System.out.print(item[0][0]);
}
}
public static String[][] deepClone(String[][] arry) {
if (arry == null) {
return null;
}
String[][] clone = new String[arry.length][];
for (int i = 0; i < arry.length; i++) {
clone[i] = arry[i].clone();
}
return clone;
}
The output for executing this code is : NewOldNewNew whereas the "intended" output is "OldOldNewNew". From this you can see the shallowClonedMap was updated to "New" even after being cloned and added to the list.

Sorting a randomized array in Java

I am trying to make an app for sorting a randomized array
I made some code and I can not see what is wrong with it that it returns wrong values
Notes: I am trying to learn programming. So don't suggest whole different ways of solving the problem. I just want to see what is wrong with this code so I can get better.
What RandomArrayCreator.create() returns is just an array of numbers in randomized order.
public class ArraySorter
{
public static void main(String[] args)
{
int[] siyahi = RandomArrayCreator.create();
int[] siralanmish = new int[siyahi.length];
for (int i=0;i<siyahi.length;i++)
{
for (int j=0;j<siyahi.length;j++)
{
for (int k=j+1;k<siyahi.length;k++)
{
if (siyahi[k]<siyahi[j]) j=k;
}
siralanmish[i]=siyahi[j];
siyahi[j]=siyahi.length+1;
}
System.out.println(siralanmish[i]);
}
}
}
I know you did not want suggestions but I'm going to offer one anyway.
Hopefully this will help guide you along the way, but still allow you to come up with your own solution.
Sort smallest to biggest.
did I have swap an element?
while I swapped an element
assume I did not swap an element
for element i in the array
is i > i+1?
if yes
swap the elements
I did swap an element
else
do nothing
Given that you mentioned you wanted to learn how to improve your current program, here are minimalist changes that will have your code produce a sorted array.
A few notes on the changes:
1.
if (siyahi[k]<siyahi[j]) j=k;
This I assume is for trying to swap the values at each indexes. Instead you are assigning the value of k to j which will cause problems with the entire for loop. I replaced this with the following:
if (siyahi[k]<siyahi[j])
{
int temp = siyahi[j];
siyahi[j] = siyahi[k];
siyahi[k] = temp;
}
This creates a temporary variable to store one of the values so that you can swap the value at each index without losing one of your values.
2.
siralanmish[i]=siyahi[j];
This was replaced with:
siralanmish[j]=siyahi[j];
This allows you to directly copy the values from the same index from the source array to the target array.
3.
siyahi[j]=siyahi.length+1;
This code will just fill up your array with the value of length+1 for your original array and you will lose your other values.
Your code with the fixes are below:
public class ArraySorter
{
public static void main(String[] args)
{
int[] siyahi = RandomArrayCreator.create();
int[] siralanmish = new int[siyahi.length];
for (int i=0;i<siyahi.length;i++)
{
for (int j=0;j<siyahi.length;j++)
{
for (int k=j+1;k<siyahi.length;k++)
{
if (siyahi[k]<siyahi[j])
{
int temp = siyahi[j];
siyahi[j] = siyahi[k];
siyahi[k] = temp;
}
}
siralanmish[j]=siyahi[j];
}
System.out.println(siralanmish[i]);
}
}

Lexicographic quicksort algorithm

My professor gave me a code for the methods to be used in sorting an array of names lexicographically, but I have no idea how what to write inside the main class to show that the program works. I am very new to java, so please if you know how to do this could you write it as simple as possible for me to understand it. Thanks in advance.
This is are the classes
public class quicksort_class {
int[] array1 = new int[11];
public quicksort_class(int[] w)
{
array1 = w;
}
private static void sort(String[] string, int leftlimit, int rightlimit) {
if (rightlimit > leftlimit)
{
int midpoint = partitionstep(string, leftlimit, rightlimit);
sort(string, leftlimit, midpoint - 1);
sort(string, midpoint, rightlimit);
}
}
public static int partitionstep(String[] string, int leftlimit, int rightlimit)
{
String midpoint = string[rightlimit];
int lpointer = leftlimit;
int rpointer = rightlimit;
String temp = "";
while(string[lpointer].compareTo(midpoint) <= 0)
{
lpointer = lpointer ++;
}
while(string[rpointer].compareTo(midpoint) > 0)
{
rpointer = rpointer --;
}
if(lpointer > rpointer)
{
temp = string[lpointer];
string[lpointer] = string[rightlimit];
string[rpointer] = temp;
System.out.println(string);
}
while(lpointer < rpointer)
{
temp = string[lpointer];
string[lpointer] = string[rightlimit];
string[rightlimit] = temp;
}
return lpointer;
}
}
This is the main class (as you can see I have no idea what to write)
package quicksort;
public class Quicksort {
public static void main(String[] args) {
}
}
Write something that sets up an array of strings and calls sort against it, then prints out the results or checks them against a known good result.
Ideally, write something which does this repeatedly, with particular emphasis on checking unusual combinations (already sorted or sorted in reverse, null in the array, same value appearing several times or all values being identical...)
If you want to go beyond that, you need to dig into the code to understand its edge cases and specifically test those, and/or do a "code coverage" analysis (there are tools to help with that) to make sure all parts of the code have been exercised.
Assume the algorithm of sort method is correct:
1. If the main method is within the body of quicksort_class, you can directly call the sort method as sort(arrayToBeSorted, 0 , arrayToBeSorted.length-1). And the arrayToBeSorted should ordered lexicographically after your call. You can check that to confirm.
2. If the main method is in other class, as your main method currently, you need to at least change the private prefix of sort method to public, and call quicksort_class.sort(arrayToBeSorted, 0 , arrayToBeSorted.length-1).
Some tips:
1. Private prefix of method definition means this method can only be called inside current class body.
2. Static prefix of method definition means you should call this method via class name directly, instead of via a instance of class.
By the way, can you provide what the array1 attribute stands for? I don't get why it's there.

Adding to a stack from a map

I am working on a project for school that requires us to find the shortest path between two points. Basically I use a breadth first search to traverse the graph and then use a map to keep track of each cities predecessor. My idea is then that when I reach the end I will then use the edges map to find out how a city was gotten to and essentially work backwards. However when I attempt to pull values from the map all I get is null, even though when I print out the contents it shows that there is something there. If somebody could help me track down the problem I would appreciate it.
Contents of input file with each city and its neighbor:
basic
Bismark Fargo
Minneapolis Chicago
StPaul Chicago
Minneapolis StPaul
Minneapolis Fargo
Fargo GrandForks
The code (corrected version, so this code won't exhibit the described problem any more):
import java.util.*;
import java.io.*;
public class BFSBasics {
public static void main(String[] args) throws FileNotFoundException {
Map<String, List<String>> graph = new HashMap<>();
openFile(graph, args[0]);
String start = args[1];
String end = args[2];
BFS(graph, start, end);
}
public static void openFile(Map<String,List<String>> graph,
String file)
throws FileNotFoundException{
Map<String,List<String>> aGraph = new HashMap<>();
try (Scanner scan = new Scanner(new File(file))){
if(!scan.next().equals("basic")){
System.err.println("File cannot be read.");
System.exit(1);
}else{
while(scan.hasNext()){
String city1 = scan.next();
String city2 = scan.next();
addEdge(graph, city1, city2);
addEdge(graph, city2, city1);
}
}
}
}
private static void addEdge(Map<String, List<String>> graph, String city1,
String city2){
List<String> adjacent = graph.get(city1);
if(adjacent == null){
adjacent = new ArrayList<>();
graph.put(city1, adjacent);
}
adjacent.add(city2);
}
public static void BFS(Map<String, List<String>> graph, String start,
String end) {
boolean done = false;
//cities that still need to be worked on
Queue<String> work = new ArrayDeque<>();
//cities that have already been seen
Set<String> seen = new HashSet<>();
//cities predecessor i.e. how it was gotten to
Map<String, String> edges = new HashMap<>();
LinkedList<String> path = new LinkedList<>();
String city = start;
work.add(start);
while (!done && !work.isEmpty()) {
city = work.remove();
for (String s : graph.get(city)) {
if (!seen.contains(s)) {
edges.put(s, city);
work.add(s);
seen.add(s);
if (s.equals(end)) {
done = true;
}
}
}
}
//Work backwards through the edges map and push onto the path stack
path.push(end);
String temp = edges.get(end);
while(!temp.equals(start)){
path.push(temp);
temp = edges.get(path.peek()};
}
path.push(start);
//print out the path
while(!path.isEmpty()){
System.out.println(path.pop());
}
}
}
There is something wrong with your path building code:
path.push(end); // push node (n - 1)
String temp = edges.get(end); // temp = node (n - 2)
while(!temp.equals(start)){
path.push(edges.get(temp)); // push node (n - 3) down to and including node 0
temp = path.peek(); // temp = node (n - 3) down to and including node 0
}
path.push(start); // push node 0
So the node (n - 2) will never be pushed to the path, whereas the node 0 will be pushed twice.
But except for this, the program works for me. So perheaps you really have an unreachable target, as Hbcdev suggests. You should check whether or not you actually reached the end node. Note that your graph datra structure models a directed graph, so if you want to interpret your input as undirected edges, you'll have to insert two directed edges for each line of input.
Also note that you don't mark the initial node as seen, whereas all other nodes will get marked as seen when you add them to the queue. You should mark the first as well.
Edit:
After you pasted your (almost) complete code, I fixed it in the following ways:
added two wildcard imports, for java.util.* and java.io.*. Wildcard imports are quick and dirty.
Added a closing } at the very end to close the class definition.
Added a line with the word basic to your input data. You really should System.exit(1) in case of that keyword missing, instead of continuing with inconsistent state.
With those modifications, I tested all possible combinations of two cities, always in both orders, and including paths form a city to itself. No evidence of null values anywhere, neither in output nor as a cause of printed exceptions.
I see a couple of possible problems here. The immediate cause may be this: Your logic implicitly assumes that there is only one way to reach any given node. While this may be true, I doubt it. If there are two ways to reach the same node, you overwrite the first with the second in your map.
For example, suppose the input was:
A->B
C->B
B->E
D->E
You want to get from A to E. It could be done by going A, B, E. But when you build your map, you'll create an edge entry for B, A. Then you'll write B, C, overwriting B, A. Then you write E, B. Fine. Then you write E, D, overwriting E, B. So when you're done, all that's in the edge map is (B, C) and (E, D). You then try to walk backwards from E. You find E, D. But this is wrong: you wanted E, B, but that got overwritten. When you try to find an entry for D, there is none, so it's impossible to get back to A.
A second problem is that you said that the goal was to find the SHORTEST path from start to end. But you're doing nothing to find the shortest path: you stop looking once you find any path. You really need to, in principle, find all possible paths and then select the shortest from that list. (Or hopefully, eliminate longer paths as you go, one way or another.)

Null Pointer Exception on first element add to my Generic ChunkList

So I have a null pointer exception when run. I am supposed to create a generic class that implements a list with chunks of arrays added as needed. Each time I add an element it is to check if there is space in the tail chunk array and if so add the element. Else it needs to add a chunk, adjust the pointers and add the element. My problem so far is that when I go to add the first element it is throwing a null pointer exception. I believe I have instantiated and object and assigned it where needed. If anyone has any insight please feel free to let me know what I am doing wrong or maybe its right in front of my face.
"myChunk.chunk_.add(element);////////////error" is where I am getting the error.
package ChunkList;
import java.util.*;
import java.io.*;
public class chunkList<T> {
public static void main(String[] args) {
chunkList<Integer> myList=new chunkList<Integer>();
for(int i=1; i<24; i++)
{
myList.add(i);//////////////////////////////////
System.out.println("Adding number: "+ i);
}
System.out.println("");
myList.display();
}
private chunk head;//changed T to chunk
private chunk tail;//changed T to chunk
private int array_size=8;
private int list_size;
public chunkList()//Default Constructor
{
head=null;
tail=null;
list_size=0;
}
//public chunkList(chunkList copy){}// a copy constructor.... don't think I need.
class chunk// added <T>
{
//T[] chunk_arr = new T[array_size];// illegal operation
//ArrayList<T> chunk_ = new ArrayList<T>(array_size);
ArrayList<T> chunk_;
private int chunk_size; //may need to change to public
chunk nextChunk;//changed T to chunk
chunk prevChunk;//changed T to chunk
public chunk()//default constructor
{
chunk_ = new ArrayList<T>(array_size);
chunk_size=0;
nextChunk=null;
prevChunk=null;
}
}
public void add(T element)
{
if(this.tail==null)//empty chunk list
{
chunk myChunk=new chunk();//instantiate
//myChunk.prevChunk=null;//changed from head to null
//myChunk.nextChunk=null;//changed from tail to null
head=myChunk;
tail=myChunk;
//head.nextChunk=null;
//head.prevChunk=null;
myChunk.chunk_.add(element);////////////error
list_size++;
myChunk.chunk_size=1;
}
else if (this.tail.chunk_size<array_size)//adds the element to the last chunk in list
{
this.tail.chunk_.add(element);//add element
list_size++;
this.tail.chunk_size++;//increase individual chunk array size
}
else// create new chunk, relink chunks, add element
{
chunk myChunk=new chunk();
myChunk.chunk_size=1;
list_size++;
myChunk.chunk_.add(element);
tail.nextChunk=myChunk;
myChunk.prevChunk=tail;
tail=myChunk;
}}
public int size()
{return list_size;}
public void display()
{
chunk my_chunk=head;
if(my_chunk==null)
{
System.out.print("Empty Chunk List");
return;
}
for(int i=0;i<list_size; )
{
for(int j=0; j<my_chunk.chunk_size; j++)
{
System.out.println(my_chunk.chunk_.get(j));
i++;
}
if(my_chunk.nextChunk!=null)
my_chunk=my_chunk.nextChunk;
}
}
}
So thanks Olivier Jacot-Descombes , I fixed one problem with the code and it now adds the first chunk BUT it is throwing NPE when it tries to create the next chunk. I will look at it and be back if i need more help. Thanks All.
P.S. The add method on this was incorrectly linked together in the last else statement.
Your code is very strange
There is a public static void main(String[] args) inside the class chunkList<T>. This makes no sense.
You declare a chunkList<Integer> instead of chunkList<int>.
You re-declare a chunk<T> head and chunk<T> tail in the constructor. The code should simply be head = null; without chunk<T>.
In the constructor of chunk you do the same thing again with ArrayList<T> chunk_ = ....
I could tell more things; however, I think that you should start by fixing these things to begin with.
NAMING STANDARDS PLEASE!
You will immediately benefit from adhering to industry naming standards, because your code will be more easily read by others when you ask for their help. Also, you'll be able to read other's code more easily too.
In java, convention is that:
all names are camelCase
all names start with a lowercase letter, except class names start with a capital letter
constants are all-capitals with underscore separation (eg MY_CONSTANT)
tend not to abbreviate names
don't use hungarian notation
To apply this to your code, make the following changes:
Rename chunkList to ChunkList
Rename array_size to arraySize
Rename my_chunk to myChunk
Rename chunk_ tochunk`
Now, to answer your question, just use a java.util.LinkedList and stop trying to reinvent the wheel!

Categories