4

对于任何 Java 开发人员来说,这个问题都应该是相当容易的。我发誓我花了大约 2 个小时才查到它,但我真的不明白这段代码有什么问题。

基本上,我正在实施 Karger 的最小切割算法。它要求我不断合并图中的节点,然后计算最后的交叉边数(一个 int 值)。这个算法必须重复 n 次,总是从起始图开始。我的问题是我无法创建 Graph 对象的深层副本,并且找不到错误。

我已经裁剪了代码以仅显示问题而没有更多,但我仍然无法弄清楚出了什么问题。代码在这里。

类节点:

public class Node {
public Integer Data;


public Node() {
    Data = 0;
}

public Node(Node rhs) {
    Data = rhs.Data.intValue();
}

public Node(Integer rhs) {
    Data = rhs.intValue();
}

public void setNode(Integer rhs) {
    Data = rhs;
}

类图:

public class Graph {

public ArrayList<ArrayList<Node>> AdjList;
public ArrayList<Node> NodeSet; // This contains all the nodes

public Graph() {
    AdjList = new ArrayList<ArrayList<Node>>();
    NodeSet = new ArrayList<Node>();
}

public Graph(Graph G) {
    AdjList = new ArrayList<ArrayList<Node>>();
    for (ArrayList<Node> L : G.AdjList) {
        ArrayList<Node> Lcopy = new ArrayList<Node>();
        for (Node N : L) {
            Node copy = new Node(N);
            Lcopy.add(copy);
        }
        AdjList.add(L);
    }
}
    public void addNewAdjList(ArrayList<Node> NodeAdjList) {
    // Input is the adjacency list of a new node
    // The first element in the NodeAdjList is the node itself, the rest is the adj nodes
    AdjList.add(NodeAdjList);
}
public static void printAdjList(ArrayList<Node> Adjlist) {
    Node start = Adjlist.get(0);
    System.out.print(start.Data + " : ");
    for (int j=1; j < Adjlist.size(); ++j) {
        System.out.print(Adjlist.get(j).Data + ", ");
    }
    System.out.print("\n");
}

主要的:

public class Main {

/**
 * @param args
 */
public static void main(String[] args) {
    Node Five = new Node(5);
    Node Seven = new Node(7);
    Node One = new Node(1);
    
    Graph G = new Graph();
    
    ArrayList<Node> L = new ArrayList<Node>();
    L.add(Five);
    L.add(Seven);
    L.add(One);
    
    G.addNewAdjList(L);
    
    Graph R = new Graph(G);
    R.AdjList.get(0).get(1).setNode(19); // Gets node #1 in the first adj list, i.e. 7
    
    Graph.printAdjList(G.AdjList.get(0));
    Graph.printAdjList(R.AdjList.get(0));

}

}

输出:

5 : 19, 1,

5 : 19, 1,

This kind of puzzles me to be honest. I understand that Java is pass by value only, but objects are always represented by their reference. As far as I understand, my copy constructor for G should always make a deep copy: I am moving through every adjacency list and then I am making a deep copy of the Node. I don't understand why invoking .setNode() on the copied object modifies also the original object (that has a different reference).

Previous answers like 1 seem to go the same direction I am going, what am I missing here? :S

4

1 回答 1

5

Your error is here:

ArrayList<Node> Lcopy = new ArrayList<Node>();
for (Node N : L) {
    Node copy = new Node(N);
    Lcopy.add(copy);
}
AdjList.add(L);

You created a copy of L (called Lcopy) but then you added the original L to your cloned graph. To fix it the last line should be this:

AdjList.add(Lcopy);

Note: If you have used a sensible name for your variable instead of L this error would probably never have happened!

于 2012-07-29T22:11:32.070 回答