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 8693081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:27:45+00:00 2026-06-13T00:27:45+00:00

I have a graph class with Node’s, where each Node can connect to others:

  • 0

I have a graph class with Node’s, where each Node can connect to others:

public class Node {
  List<Node> connections;
}

I would like to make a deep copy of the entire graph. As a first attempt, I tried making a copy constructor like:

public Node(Node other) {
  connections = new ArrayList<Node>();
  for (Node n : other.connections) { 
    connections.add(new Node(n));
  }
}

So deep copying a graph would just be:

public Graph deepCopy () {
  Graph g = new Graph();
  g.nodes = new ArrayList<Node>();
  for (Node n : nodes) { 
    g.nodes.add(new Node(n));
  }
}

But that doesn’t work as that destroys the connection relationship among the nodes. I am wondering if anyone has suggestions to do this in a simple way? Thanks.

  • 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-13T00:27:47+00:00Added an answer on June 13, 2026 at 12:27 am

    The problem is that you need to copy the identities of the nodes, not just their values. Specifically, when you’re copying some node, you need to deal with the identities of the nodes it refers to; that means that a copy constructor, or some other kind of purely local copying mechanism, can’t do the job, because it only deals with one node at a time. I’m not sure that makes any sense, but I’ve typed it and my backspace key doesn’t work.

    Anyway, what you can do is pass around some other object which can tell which new node corresponds to which old node. If you wanted to be fancy (and who doesn’t?) you could refer to this as a graph isomorphism. This can be something as simple as a map. As in this completely untested code:

    // in Graph
    public Graph deepCopy () {
      Graph g = new Graph();
      g.nodes = new ArrayList<Node>();
      Map<Node, Node> isomorphism = new IdentityHashMap<Node, Node>();
      for (Node n : nodes) { 
        g.nodes.add(n.deepCopy(isomorphism));
      }
      return g;
    }
    
    // in Node
    public Node deepCopy(Map<Node, Node> isomorphism) {
        Node copy = isomorphism.get(this);
        if (copy == null) {
            copy = new Node();
            isomorphism.put(this, copy);
            for (Node connection: connections) {
                copy.connections.add(connection.deepCopy(isomorphism));
            }
        }
        return copy;
    }
    

    Sergii mentions using serialization; serialization actually does something pretty similar when it traverses an object graph.

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

Sidebar

Related Questions

I have a Node<T> (inner) class in Graph<T> class: public class Graph<T> { private
I have a graph where each node represents a java class and each one
I have the following simple Graph class, where for each Node , I store
how to represent a graph with list data structure i have three class (Graph,
Suppose we have an arbitrary graph represented by nodes and pointers like this: class
I require a tree / directed acyclic graph implementation something like this: public class
I have a graph, each node have 4 child nodes. I wrote a algorithm
I have the following class class node { public: node() { } node(const node&);
I have a Node class: public class Node { private string name; private Point3D
I have a nice graph (a list) containing 81 vertices (each vertex is an

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.