Java Recursion, calling it with Objects - How To Copy The Objects? - java

The old value/reference things. Im getting ConcurrentModificationException
for this adaptation of the Bron-Kerbosch.
public int[] bk(ArrayList<Integer> R, ArrayList<Integer> P, ArrayList<Integer> X) {
int count[] = new int[n];
int u=0, c = 0;
ArrayList<Integer> tempPX = new ArrayList<Integer>();
ArrayList<Integer> newP = P;
ArrayList<Integer> newX = X;
ArrayList<Integer> newR = R;
if (P.isEmpty() && X.isEmpty()) {
count[R.size()]++;
} else {
u = 0; c = 0; // find vertex with largest degree
tempPX.addAll(P); tempPX.addAll(X); // P ⋃ X
for (Integer v : tempPX) {
if (neighbours[v].size() > neighbours[u].size()) {
u = c;
}
c++;
}
P.removeAll(neighbours[u]); // P \ neighbours[u]
for (Integer v : newP) {
newR.add(v); // R ⋃ v
newP.retainAll(neighbours[v]); // P â‹‚ neighbours[v]
newX.retainAll(neighbours[v]); // X â‹‚ neighbours[v]
bk(newR, newP, newX);
P.remove(v); // removing object
X.add(v); // X ⋃ v
}
}
return count;
}
The exception occurs at line for (Integer v : newP), and the recursive call in there.
I need to P.removeAll(neighbours[u]); then loop over that resulting list, inside doing the things in the comments, AND PASS COPIES in the recursive call so it wont complain and work not pass the references and keep modifying the same object P/X/R. So how and WHEN do i copy them?? Those first lines.. I'm making copies of the references aren't i...
(yes i know i "modify" newP then loop over the old P, they just point to the same object it seems)
--- new code after reading the replies -
public int[] bk(List<Integer> r, List<Integer> p, List<Integer> x) {
int count[] = new int[n];
int u = 1;
List<Integer> tempPX = new ArrayList<Integer>();
List<Integer> newR, newP, newX;
if (p.isEmpty() && x.isEmpty()) {
count[r.size()]++;
} else {
// find vertex with largest degree in P U X
tempPX.addAll(p);
tempPX.addAll(x);
for (Integer v : tempPX) {
if (neighbours[v].size() > neighbours[u].size()) {
u = v;
}
}
p.removeAll(neighbours[u]); // P \ neighbours[u]
newP = new ArrayList<Integer>(p);
for (Integer v : newP) {
r.add(v); // R U v
newR = new ArrayList<Integer>(r);
p.retainAll(neighbours[v]); // P /\ neighbours[v]
newP = new ArrayList<Integer>(p);
x.retainAll(neighbours[v]); // X /\ neighbours[v]
newX = new ArrayList<Integer>(x);
bk(newR, newP, newX);
p.remove(v); // removing object
x.add(v); // X U v
}
}
return count;
}

As you've identified, you're copying the reference, not the list. You need to instantiate a new ArrayList object with the entries in the old list.
e.g.
List<Integer> newList = new ArrayList<Integer>(oldList);
So this explicitly creates a new object containing references to the same elements.
Note as an aside that I pass around a reference to the interface List rather than the implementation - generally good practise since you're not exposing implementation throughout the codebase.

ArrayList newP = P;
This only creates a second reference to the same ArrayList. To copy the arraylist use
ArrayList newP = new ArrayList(P);

Your first && check is redundant, originally the check was with the X argument?
Also, your for loop at the start of the else block doesn't make any sense, what are you trying to do there? The thing is that you are both using a counter (c) and the contents of the tempPX list (v). You use v to do the check but c as the assignment. The result is totally dependent on data ordering and doesn't produce anything meaningful.
Normally you'd use one or the other and use it both in the check and the assignment.
if (p.isEmpty() && p.isEmpty()) {
count[r.size()]++;
} else {
u = 0; c = 0; // find vertex with largest degree
tempPX.addAll(p); tempPX.addAll(x); // P U X
for (Integer v : tempPX) {
if (neighbours[v].size() > neighbours[u].size()) {
u = c;
}
c++;
}
Either your goal is to find the index in the temppx list with the largest amount of neighbours (drop the c variable and the c++ line, use v in the assignment) OR to just find the index with the largest amount of neighbours without any restriction on the index, in that case you would use a for loop like this:
for (int c = 0; c < neighbours.size(); ++c)
and drop any mention of the temppx list. My guess is your trying to do the first case.

Related

Changing a variable changes the previously assigned variable as well

I'm having what I'm sure is a basic logic error but I can't seem to fix it. I'm sorting a GenericSimpleArrayList before creating a tree using both the unsorted (postorder) and sorted (inorder) lists. When I sort one of the lists it sorts them both? I'm not sure why.
public static <AnyType extends Comparable<? super AnyType>> BinaryNode<AnyType>constructBST ( GenericSimpleArrayList<AnyType> postorder ){
GenericSimpleArrayList <AnyType> g = postorder;
quicksort(postorder, new SortFunctor()); //this is where i sort the list.
GenericSimpleArrayList <AnyType> inorder = postorder;
return constructTree(inorder, g);
}
Can anyone help me fix this? Why does it sort both g and postorder when I only sort postorder? Thanks.
Edit: Added constructTree.
public static <AnyType> BinaryNode<AnyType> constructTree(GenericSimpleArrayList<AnyType> inorder, GenericSimpleArrayList<AnyType> postorder) {
int nodes = postorder.size();
AnyType root = postorder.get(nodes-1);
BinaryNode<AnyType> left = null;
BinaryNode<AnyType> right = null;
if (nodes > 1) {
int rootPos = 0;
for (int loop = 0; loop <= nodes-1; loop++) {
if (inorder.get(loop).equals(root)) {
rootPos = loop;
//System.out.println(loop);
} else {
//System.out.println("Not found at pos: " + loop);
}
}
if (rootPos != 0) {
GenericSimpleArrayList <AnyType> leftInorder = new GenericSimpleArrayList();//(AnyType) new Object[rootPos];
GenericSimpleArrayList <AnyType> leftPostorder = new GenericSimpleArrayList();//(AnyType[]) new Object[rootPos];
for (int loop = 0; loop < rootPos; loop++) {
leftInorder.add(inorder.get(loop));
leftPostorder.add(postorder.get(loop));
}
left = constructTree(leftInorder, leftPostorder );
}
if (rootPos < nodes-1){
GenericSimpleArrayList <AnyType> rightInorder = new GenericSimpleArrayList();//(AnyType[]) new Object[nodes - rootPos - 1];
GenericSimpleArrayList <AnyType> rightPostorder = new GenericSimpleArrayList();//(AnyType[]) new Object[rightInorder.length];
for (int loop = 0; loop < nodes-rootPos-1; loop++){
rightInorder.add(inorder.get(rootPos + loop + 1));
rightPostorder.add(postorder.get(rootPos + loop));
}
right = constructTree(rightInorder, rightPostorder);
}
}
return new BinaryNode<AnyType>(root, left, right);
}
Why does it sort both g and postorder when I only sort postorder?
GenericSimpleArrayList <AnyType> g = postorder;
postorder is a reference to an object. When you copy this reference you now have two references to an object. However there is still only one object.
I don't know the API to your custom ArrayList but I assume you can do
GenericSimpleArrayList<AnyType> g = new GenericSimpleArrayList<AnyType>(postorder);
or
GenericSimpleArrayList<AnyType> g = new GenericSimpleArrayList<AnyType>();
for(AnyType at: postorder)
g.add(at);
Most likely your sorting utility sorts the collection in place. This is very common for sort functions to alter the original list.
If you want to preserve both the original order and sort the list, I suggest first taking a copy of the list.
An alternetive is to use a different structures like a TreeMap to record the String in sorted order and the index of the original position.
GenericSimpleArrayList <AnyType> g = postorder;
GenericSimpleArrayList <AnyType> inorder = postorder;
In these two statements, all the three variable will be referring to the same reference hold by postorder initially. Because you are doing reference assignment and update in one object state will reflect into all the reference variable.
constructTree(inorder, g);
So when you make the above call then you are passing the same same reference.
In this line:
GenericSimpleArrayList <AnyType> g = postorder;
you only define a new name g for the same object. If you want g to be a different List, you need to create it newly (that is really call the constructor) and copy postorder's contents into it.

How to choose in Java few elements from set without repetition?

An example:
I have a set
[a, b, c, d, e, f, g, h]
and I want to choose three of them but without repetition,
so
[e, g, a]
is a correct subset and
[h, c, h]
is incorrect.
Any idea how to make it easily and clearly?
For the sake of clarity:
I have a set of questions for a quiz. It's not about generate a subset of letters.
What I want to have:
Either a subset of String array or a subset of indexes of array e.g. from 0 to 10.
Because it is for a quiz the elements can not be repeated.
You can try with Collections.shuffle() on List and get the first three values if there is no concern of extra memory uses.
Set<Character> set = new TreeSet<Character>();
for (char c = 'a'; c < 'i'; c++) {
set.add(c);
}
List<Character> list = new ArrayList<Character>(set);
Collections.shuffle(list);
System.out.println(list.subList(0, 3));
You can try this one as well
Random random = new Random();
int index = 0;
List<Character> list = new ArrayList<Character>(set);
Set<Character> newSet = new HashSet<Character>();
while (newSet.size() < 3) {
index = random.nextInt(set.size());
newSet.add(list.get(index));
}
If O(n) is ok for you, you can iterate through the items. For each item, you randomly select it or not.
Set<Character> newSet = ...;
for (Char elem : set) {
if (randomFunction()) {
newSet.add(elem);
}
}
If this is for school, you may need to avoid convenience methods. Here is a simple way to do it:
ArrayList<String> al = new ArrayList<String>();
al.add("a");
al.add("b");
al.add("c");
al.add("d");
al.add("e");
al.add("f");
al.add("g");
al.add("h");
int chooseNum = 3;
Random rand = new Random();
int index;
String val;
while (chooseNum > 0 && chooseNum <= al.size()) {
index = rand.nextInt(al.size());
val = al.remove(index);
System.out.print(val);
System.out.print(" ");
chooseNum--;
}
I think Braj's answer is the most elegant, but if performance matters to you, here are a couple alternatives:
Partial shuffle
This is like Braj's answer. Except it is O(k) instead of O(n) (where k is the number of elements picked and n the size of the input).
/**
* Randomly picks elements from a set.
*
* #param k Number of elements to choose.
* #return A list containg k elements from the set.
*/
public Set<T> randomPick(int k) {
T[] a = (T[]) new Object[k];
a = this.inputSet.toArray(a);
int n = a.length;
if (k < 0 || k > n) {
String msg = String.format(
"Cannot pick %0 elements from a set that contains %1 elements.",
k, n);
throw new IllegalArgumentException(msg);
}
Random random = new Random();
for (int i = 0; i < k; i++) {
// Swap a[i] with a random element in the unswapped elements
int r = i + random.nextInt(n - i);
T temp = a[i];
a[i] = a[r];
a[r] = temp;
}
T[] output = Arrays.copyOfRange(a, 0, k);
return new HashSet<T>(Arrays.asList(output));
}
Tries to pick again until number of picked elements is as expected
This is quite straightforward.
I suppose it is faster if k is small, and becomes less efficient when k gets close to n.
/**
* Randomly picks elements from a set.
*
* #param k Number of elements to choose.
* #return A list containg k elements from the set.
*/
public Set<T> randomPick2(int k) {
List<T> list = new ArrayList<T>(this.inputSet);
int n = list.size();
if (k < 0 || k > n) {
String msg = String.format(
"Cannot pick %0 elements from a set that contains %1 elements.",
k, n);
throw new IllegalArgumentException(msg);
}
T[] a = (T[]) new Object[k];
a = this.inputSet.toArray(a);
Random random = new Random();
Set<T> output = new HashSet<T>(k);
while (output.size() < k) {
int r = random.nextInt(n);
output.add(a[r]);
}
return output;
}

Restart iterator when finished

I have a list and I'd like to get the values at i-1, i and i+1 positions. When i is the first or the last index, it would throw an IndexOutOfBoundsException. To prevent that, I would write a few if-statements and hardcode it like that:
if (i == 0){
a = list.get(list.size()-1);
b = list.get(0);
c = list.get(1);
} else if (i == list.size()-1){
a = list.get(i-1);
b = list.get(i);
c = list.get(0);
} else {
a = list.get(i-1);
b = list.get(i);
c = list.get(i+1);
}
I find this way is a littlebit static. Let's say I want to get n entries from the list in this way, how would you do that?
You can use (i-1+list.size()) % list.size() and (i+1) % list.size().
This will also handle lists of length 1.
Heavier options:
Write a method
<T> T get(List<T> list, int i)
{
i %= list.size();
return list.get(i >= 0 ? i : i + list.size());
}
Subclass and override get()
Make a wrapper class which wraps around indices
You could use the ternary operator to shorten the code a little, and factor out the get calls to shorten the code further.
int prev, i, next;
//start loop here
prev = (i == 0 ? list.size()-1 : i-1);
next = (i == list.size()-1 ? 0 : i+1);
a = list.get(prev);
b = list.get(i);
c = list.get(next);
// end loop here
You will have to handle small lists, (size() <= 2) to stop repeating elements.
Why you can't just iterate with foreach and reassign old values like this:
List<Integer> list = Arrays.asList(1, 5, 7, 3, 4);
int n = 3; // how much entries we take
int a = 0, b = 0, c;
for (int i = 0; i < n; i++) {
c = b;
b = a;
a = list.get(i);
// do some stuff
}

Prim's MST algorithm implementation with Java

I'm trying to write a program that'll find the MST of a given undirected weighted graph with Kruskal's and Prim's algorithms. I've successfully implemented Kruskal's algorithm in the program, but I'm having trouble with Prim's. To be more precise, I can't figure out how to actually build the Prim function so that it'll iterate through all the vertices in the graph. I'm getting some IndexOutOfBoundsException errors during program execution. I'm not sure how much information is needed for others to get the idea of what I have done so far, but hopefully there won't be too much useless information.
This is what I have so far:
I have a Graph, Edge and a Vertex class.
Vertex class mostly just an information storage that contains the name (number) of the vertex.
Edge class can create a new Edge that has gets parameters (Vertex start, Vertex end, int edgeWeight). The class has methods to return the usual info like start vertex, end vertex and the weight.
Graph class reads data from a text file and adds new Edges to an ArrayList. The text file also tells us how many vertecis the graph has, and that gets stored too.
In the Graph class, I have a Prim() -method that's supposed to calculate the MST:
public ArrayList<Edge> Prim(Graph G) {
ArrayList<Edge> edges = G.graph; // Copies the ArrayList with all edges in it.
ArrayList<Edge> MST = new ArrayList<Edge>();
Random rnd = new Random();
Vertex startingVertex = edges.get(rnd.nextInt(G.returnVertexCount())).returnStartingVertex(); // This is just to randomize the starting vertex.
// This is supposed to be the main loop to find the MST, but this is probably horribly wrong..
while (MST.size() < returnVertexCount()) {
Edge e = findClosestNeighbour(startingVertex);
MST.add(e);
visited.add(e.returnStartingVertex());
visited.add(e.returnEndingVertex());
edges.remove(e);
}
return MST;
}
The method findClosesNeighbour() looks like this:
public Edge findClosestNeighbour(Vertex v) {
ArrayList<Edge> neighbours = new ArrayList<Edge>();
ArrayList<Edge> edges = graph;
for (int i = 0; i < edges.size() -1; ++i) {
if (edges.get(i).endPoint() == s.returnVertexID() && !visited(edges.get(i).returnEndingVertex())) {
neighbours.add(edges.get(i));
}
}
return neighbours.get(0); // This is the minimum weight edge in the list.
}
ArrayList<Vertex> visited and ArrayList<Edges> graph get constructed when creating a new graph.
Visited() -method is simply a boolean check to see if ArrayList visited contains the Vertex we're thinking about moving to. I tested the findClosestNeighbour() independantly and it seemed to be working but if someone finds something wrong with it then that feedback is welcome also.
Mainly though as I mentioned my problem is with actually building the main loop in the Prim() -method, and if there's any additional info needed I'm happy to provide it.
Thank you.
Edit: To clarify what my train of thought with the Prim() method is. What I want to do is first randomize the starting point in the graph. After that, I will find the closest neighbor to that starting point. Then we'll add the edge connecting those two points to the MST, and also add the vertices to the visited list for checking later, so that we won't form any loops in the graph.
Here's the error that gets thrown:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Graph.findClosestNeighbour(graph.java:203)
at Graph.Prim(graph.java:179)
at MST.main(MST.java:49)
Line 203: return neighbour.get(0); in findClosestNeighbour()
Line 179: Edge e = findClosestNeighbour(startingVertex); in Prim()
Vertex startingVertex = edges.get(rnd.nextInt(G.returnVertexCount())).returnStartingVertex();
This uses the vertex count to index an edge list, mixing up vertices and edges.
// This is supposed to be the main loop to find the MST, but this is probably horribly wrong..
while (MST.size() < returnVertexCount()) {
Edge e = findClosestNeighbour(startingVertex);
MST.add(e);
visited.add(e.returnStartingVertex());
visited.add(e.returnEndingVertex());
edges.remove(e);
}
This shouldn't be passing the same startingVertex to findClosestNeighbour each time.
public Edge findClosestNeighbour(Vertex v) {
ArrayList<Edge> neighbours = new ArrayList<Edge>();
ArrayList<Edge> edges = graph;
for (int i = 0; i < edges.size() -1; ++i) {
if (edges.get(i).endPoint() == s.returnVertexID() && !visited(edges.get(i).returnEndingVertex())) {
neighbours.add(edges.get(i));
}
}
return neighbours.get(0); // This is the minimum weight edge in the list.
}
What is s here? This doesn't look like it's taking the edge weights into account. It's skipping the last edge, and it's only checking the ending vertex, when the edges are non-directional.
// Simple weighted graph representation
// Uses an Adjacency Linked Lists, suitable for sparse graphs /*undirected
9
A
B
C
D
E
F
G
H
I
A B 1
B C 2
C E 7
E G 1
G H 8
F H 3
F D 4
D E 5
I F 9
I A 3
A D 1
This is the graph i used saved as graph.txt
*/
import java.io.*;
import java.util.Scanner;
class Heap
{
private int[] h; // heap array
private int[] hPos; // hPos[h[k]] == k
private int[] dist; // dist[v] = priority of v
private int MAX;
private int N; // heap size
// The heap constructor gets passed from the Graph:
// 1. maximum heap size
// 2. reference to the dist[] array
// 3. reference to the hPos[] array
public Heap(int maxSize, int[] _dist, int[] _hPos)
{
N = 0;
MAX = maxSize;
h = new int[maxSize + 1];
dist = _dist;
hPos = _hPos;
}
public boolean isEmpty()
{
return N == 0;
}
public void siftUp( int k)
{
int v = h[k];
h[0] = 0;
dist[0] = Integer.MIN_VALUE;
//vertex using dist moved up heap
while(dist[v] < dist[h[k/2]]){
h[k] = h[k/2]; //parent vertex is assigned pos of child vertex
hPos[h[k]] = k;//hpos modified for siftup
k = k/2;// index of child assigned last parent to continue siftup
}
h[k] = v;//resting pos of vertex assigned to heap
hPos[v] = k;//index of resting pos of vertex updated in hpos
//display hpos array
/* System.out.println("\nThe following is the hpos array after siftup: \n");
for(int i = 0; i < MAX; i ++){
System.out.println("%d", hPos[i]);
}
System.out.println("\n Following is heap array after siftup: \n");
for (int i = 0; i < MAX; i ++ ){
System.out.println("%d" , h[i]);
}*/
}
//removing the vertex at top of heap
//passed the index of the smallest value in heap
//siftdown resizes and resorts heap
public void siftDown( int k)
{
int v, j;
v = h[k];
while(k <= N/2){
j = 2 * k;
if(j < N && dist[h[j]] > dist[h[j + 1]]) ++j; //if node is > left increment j child
if(dist[v] <= dist[h[j]]) break;//if sizeof parent vertex is less than child stop.
h[k] = h[j];//if parent is greater than child then child assigned parent pos
hPos[h[k]] = k;//update new pos of last child
k = j;//assign vertex new pos
}
h[k] = v;//assign rest place of vertex to heap
hPos[v] = k;//update pos of the vertex in hpos array
}
public void insert( int x)
{
h[++N] = x;//assign new vertex to end of heap
siftUp( N);//pass index at end of heap to siftup
}
public int remove()
{
int v = h[1];
hPos[v] = 0; // v is no longer in heap
h[N+1] = 0; // put null node into empty spot
h[1] = h[N--];//last node of heap moved to top
siftDown(1);//pass index at top to siftdown
return v;//return vertex at top of heap
}
}
class Graph {
class Node {
public int vert;
public int wgt;
public Node next;
}
// V = number of vertices
// E = number of edges
// adj[] is the adjacency lists array
private int V, E;
private Node[] adj;
private Node z;
private int[] mst;
// used for traversing graph
private int[] visited;
private int id;
// default constructor
public Graph(String graphFile) throws IOException
{
int u, v;
int e, wgt;
Node t;
FileReader fr = new FileReader(graphFile);
BufferedReader reader = new BufferedReader(fr);
String splits = " +"; // multiple whitespace as delimiter
String line = reader.readLine();
String[] parts = line.split(splits);
System.out.println("Parts[] = " + parts[0] + " " + parts[1]);
V = Integer.parseInt(parts[0]);
E = Integer.parseInt(parts[1]);
// create sentinel node
z = new Node();
z.next = z;
// create adjacency lists, initialised to sentinel node z
adj = new Node[V+1];
for(v = 1; v <= V; ++v)
adj[v] = z;
// read the edges
System.out.println("Reading edges from text file");
for(e = 1; e <= E; ++e)
{
line = reader.readLine();
parts = line.split(splits);
u = Integer.parseInt(parts[0]);
v = Integer.parseInt(parts[1]);
wgt = Integer.parseInt(parts[2]);
System.out.println("Edge " + toChar(u) + "--(" + wgt + ")--" + toChar(v));
// write code to put edge into adjacency matrix
t = new Node(); t.vert = v; t.wgt = wgt; t.next = adj[u]; adj[u] = t;
t = new Node(); t.vert = u; t.wgt = wgt; t.next = adj[v]; adj[v] = t;
}
}
// convert vertex into char for pretty printing
private char toChar(int u)
{
return (char)(u + 64);
}
// method to display the graph representation
public void display() {
int v;
Node n;
for(v=1; v<=V; ++v){
System.out.print("\nadj[" + toChar(v) + "] ->" );
for(n = adj[v]; n != z; n = n.next)
System.out.print(" |" + toChar(n.vert) + " | " + n.wgt + "| ->");
}
System.out.println("");
}
//use the breath first approach to add verts from the adj list to heap
//uses 3 arrays where array = # of verts in graph
//parent array to keep track of parent verts
// a dist matrix to keep track of dist between it and parent
//hpos array to track pos of vert in the heap
public void MST_Prim(int s)
{
int v, u;
int wgt, wgt_sum = 0;
int[] dist, parent, hPos;
Node t;
//declare 3 arrays
dist = new int[V + 1];
parent = new int[V + 1];
hPos = new int[V +1];
//initialise arrays
for(v = 0; v <= V; ++v){
dist[v] = Integer.MAX_VALUE;
parent[v] = 0;
hPos[v] = 0;
}
dist[s] = 0;
//d.dequeue is pq.remove
Heap pq = new Heap(V, dist, hPos);
pq.insert(s);
while (! pq.isEmpty())
{
// most of alg here
v = pq.remove();
wgt_sum += dist[v];//add the dist/wgt of vert removed to mean spanning tree
//System.out.println("\nAdding to MST edge {0} -- ({1}) -- {2}", toChar(parent[v]), dist[v], toChar[v]);
dist[v] = -dist[v];//mark it as done by making it negative
for(t = adj[v]; t != z; t = t.next){
u = t.vert;
wgt = t.wgt;
if(wgt < dist[u]){ //weight less than current value
dist[u] = wgt;
parent[u] = v;
if(hPos[u] == 0)// not in heap insert
pq.insert(u);
else
pq.siftUp(hPos[u]);//if already in heap siftup the modified heap node
}
}
}
System.out.print("\n\nWeight of MST = " + wgt_sum + "\n");
//display hPos array
/*System.out.println("\nhPos array after siftUp: \n");
for(int i = 0; i < V; i ++){
System.out.println("%d", hPos[i]);
}*/
mst = parent;
}
public void showMST()
{
System.out.print("\n\nMinimum Spanning tree parent array is:\n");
for(int v = 1; v <= V; ++v)
System.out.println(toChar(v) + " -> " + toChar(mst[v]));
System.out.println("");
}
}
public class PrimLists {
public static void main(String[] args) throws IOException
{
int s = 2;
String fname = "graph.txt";
Graph g = new Graph(fname);
g.display();
}
}

searching in n 2-dimensional arrays

Help with how to implement searching on n 2-dimenssional arrays. To be more specific:
If I have 6 tables and I am putting these into a 2-dimensional array.I will provide a value say 10 like how val=0 here. I need to search from these tables all the combination values that make up 10. The value will be computed taking values from all these tables.
public static int Main() {
int[] a = {2,1,4,7};
int[] b = {3,-3,-8,0};
int[] c = {-1,-4,-7,6};
int sum;
int i; int j; int k;
int val = 0;
for(i = 0; i < 4; i++) {
for(j = 0;j<4;j++) {
for(k = 0;k<4;k++) {
sum = a[i]* b[j]* c[k];
if(sum == val)
System.out.printf("%d %d %d\n",a[i],b[j],c[k]);
}
}
}
}
Following will be the code you require:
(The solution includes recursion making your problem go easier)
private ArrayList numbers = new ArrayList();
public void CalculateSum(int tableNumber)
{
if(!Tables.isLast(tableNumber))
{
int[][] a = Tables.Get(tableNumber);
for(int y = 0; y < a.length; y++)
{
for(int x = 0; x < a[y].length; x++)
{
numbers.add(a[y][x]);
CalculateSum(tableNumber + 1);
numbers.remove(tableNumber - 1);
}
}
}else
{
int[][] a = Tables.Get(tableNumber);
for(int y = 0; y < a.length; y++)
{
for(int x = 0; x < a[y].length; x++)
{
if((sum(numbers) + a[y][x]) == checkValue)
{
PrintNumbers(numbers);
System.out.print(a[y][x]);
System.out.println();
}
}
}
}
}
You need to implement a class ('Tables' as my solution) write methods:
boolean isLast(int tableNo): to check whether the given table is the last table your tables list
int[][] Get(int tableNo): to get the table with the specified index
Also the method sum should sum the values in the numbers ArrayList.
PrintNumbers method should print the numbers in the numbers ArrayList in a row.
checkValue is the value you want to check.
Hope this helps....
Please write if you want any clarification on this algorithm.
You can consider a table as list of values. Then, if you have N tables, your problem is to find lists of N integers (each one taken from one of the N tables) whose product is equal to a value p. You can solve the problem recursively:
given a non-empty list of tables {t1, t2, t3, ...}
given a product value p that you look for
for every value v in t1 you must look for solutions of the sub-problem with a product value p / v and and tables {t2, t3, ...} (this assumes that p % v == 0, because we're dealing with integers
etc.
Some java code below:
public class SO6026472 {
public static void main(String[] args) {
// define individual tables
Integer[] t1 = new Integer[] {2,-2,4,7};
Integer[] t2 = new Integer[] {3,-3,-8,0};
Integer[] t3 = new Integer[] {-1,-4,-7,6};
Integer[] t4 = new Integer[] {1,5};
// build list of tables
List<List<Integer>> tables = new ArrayList<List<Integer>>();
tables.add(Arrays.asList(t1));
tables.add(Arrays.asList(t2));
tables.add(Arrays.asList(t3));
tables.add(Arrays.asList(t4));
// find solutions
SO6026472 c = new SO6026472();
List<List<Integer>> solutions = c.find(36, tables);
for (List<Integer> solution : solutions) {
System.out.println(
Arrays.toString(solution.toArray(new Integer[0])));
}
}
/**
* Computes the ways of computing p as a product of elements taken from
* every table in tables.
*
* #param p the target product value
* #param tables the list of tables
* #return the list of combinations of elements (one from each table) whose
* product is equal to p
*/
public List<List<Integer>> find(int p, List<List<Integer>> tables) {
List<List<Integer>> solutions = new ArrayList<List<Integer>>();
// if we have no tables, then we are done
if (tables.size() == 0)
return solutions;
// if we have just one table, then we just have to check if it contains p
if (tables.size() == 1) {
if (tables.get(0).contains(p)) {
List<Integer> solution = new ArrayList<Integer>();
solution.add(p);
solutions.add(solution);
return solutions;
} else
return solutions;
}
// if we have several tables, then we take the first table T, and for
// every value v in T we search for (p / v) in the rest of the tables;
// we do this only if p % v is equal to 0, because we're dealing with
// ints
List<Integer> table = tables.remove(0);
for (Integer value : table) {
if (value != 0 && p % value == 0) {
List<List<Integer>> subSolutions = find(p / value, tables);
if (! subSolutions.isEmpty()) {
for (List<Integer> subSolution : subSolutions) {
subSolution.add(0, value);
}
solutions.addAll(subSolutions);
}
}
}
tables.add(0, table);
return solutions;
}
}
The code prints solutions for a slightly modified version of your example:
[2, 3, 6, 1]
[-2, -3, 6, 1]
The solutions work for any number of tables. There are ways to improve the algorithm, for example by using memoization and dynamic programming. But I think that the recursive solution is more clear.

Categories

Resources