Reputation: 115
I have been working on a project and I have been facing an issue with Arraylist in java. Problem is prim's algorithm 1. Randomly generating graph 2. Making an arraylist of neighbours for every vertex 3. passing the arraylist to the 2 different functions
Problem :- when i make changes in Arraylist in one class it is reflecting is other class as well. How to fix it ? Thanks in advance.Here when i call mst.java g.getneighbourlist() , it works fine. But now when i call in mstFheap.java with same g.getbeighbourlist() its differnt neibourlist i mean all the edges selected by first removed from second .
//main.java
public class main{
GenGraph g = new GenGraph(size, density);
g.makeGraph();
g.print();
// calling for finding mst
mst m= new mst(g.getNeighbourlist(),size);
mstFheap m1= new mstFheap(g.getNeighbourlist(), size);
m.start(); // starts the algo
m.print(); // print mst
m1.print(); // In second class just printing the neibhour list
}
//mst.java
public class mst {
private List<LinkedList<edge>> neighbour =null;
private LinkedList<edge> mst = new LinkedList<edge>();
private int [] traker = null;
private int totalCost =0;
private int size=0;
private boolean path=false;
public mst(List<LinkedList<edge>> list,int x)
{
this.neighbour=new ArrayList<LinkedList<edge>>(list);;
this.size=x;
this.traker= new int[size];
for(int i=0;i<size;i++){traker[i]=0;}
}
public void start() {
List<Integer> subGroup = new ArrayList<Integer>();
Random ran= new Random();
int val= ran.nextInt(size);
subGroup.add(val);
traker[val]=1;
while(!path){
edge e= minCost(subGroup);
totalCost+= e.weight;
subGroup.add(e.v2);
neighbour.get(e.v1).remove(e);
edge temp = new edge(e.v2,e.v1,e.weight);
neighbour.get(e.v2).remove(temp);
traker[e.v1]=1;
traker[e.v2]=1;
mst.add(e);
if(subGroup.size()==size) path=true;
}
}
//mstFheap.java
public class mstFheap {
private fHeap f;
private double totalCost =0;
private double [] keyList= null;
//Graph elements
private int size=0;
private List<LinkedList<edge>> neighbour =null;
public double cost(){return totalCost;}
public mstFheap(List<LinkedList<edge>> list,int size){
f=new fHeap();
neighbour=new ArrayList<LinkedList<edge>>(list);
this.size=size;
keyList=new double[size];
for(int i=0;i<size;i++){
keyList[i]= Double.POSITIVE_INFINITY;
fHeapNode temp= new fHeapNode(i, keyList[i]);
f.insert(temp, keyList[i]);
}
}
public void print(){
System.out.print(" Keylist:- ");
for(int i=0;i<neighbour.size();i++){
System.out.print(neighbour.get(i).size()+" ");
}
}
Upvotes: 0
Views: 209
Reputation: 16059
Try:
mst m= new mst(new ArrayList(g.getNeighbourlist()),size);
mstFheap m1= new mstFheap(new ArrayList(g.getNeighbourlist()), size);
That will make a copy of the list for each algorithm.
SideNote: Please name classes starting with a capital letter. And consider using human readable names. "g" is Graph, so why not call it "graph"?
EDIT: I just saw, that it is a "List of Lists". So you may adjust your getNeighbourlist()
Method, so it returns a "deep copy". That means: Instead of a copy of the list, you'll have to return a new list of copies of the entry-lists.
Upvotes: 2