Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8297231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:20:04+00:00 2026-06-08T15:20:04+00:00

This question should be rather easy for any Java developer. I swear I looked

  • 0

This question should be rather easy for any Java developer. I swear I looked it up after spending ~2 hours on it, but I can’t really understand what’s wrong with this code.

Basically, I am implementing Karger’s minimum cuts algorithm. It requires me to keep merging nodes in a graph and then compute the number of crossing edges at the end (an int value). This algorithm must be repeated n times, always from the starting graph. My problem is that I am unable to create a deep copy of my Graph object, and I can’t find the mistake.

I have cropped the code to just show the problem and no more, but I am still unable to figure out what’s wrong. Here the code is.

Class Node:

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;
}

Class Graph:

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");
}

Main:

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));

}

}

Output:

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:20:06+00:00Added an answer on June 8, 2026 at 3:20 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK, this feels like a question that should be easy to answer, but as
I'm not 100% sure how I should word this question but I'll try my
Dear all,Now i have this question in my java program,I think it should be
This should be an easy question. I have two url patterns in Django: url(r'^wiki/page/(?P<page_title>.*)',
this is might be a very basic question, but seems like it's not easy
Edited Question: This should be clear. using System; namespace UpdateDateTimeFields { class Program {
This question may seem daft (I'm a new to 'programming' and should probably stop
I should preface this question by saying that I am new to JavaServer Pages.
I ran into this question today and thought I should post it for the
I came across this question on transcender: What should you apply to a field

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.