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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:08:57+00:00 2026-06-05T01:08:57+00:00

I’ve a requirement to define Data Structure and Algorithm for Circular Data Graph for

  • 0

I’ve a requirement to define Data Structure and Algorithm for Circular Data Graph for web client.
At server, data will provided in a 2 column CSV format (e.g. Sender, Receiver).
Final output will be rendered in JSON format and sent to web request.
I have seen some Tree examples which can help in Parent Child relationships. But In my case, I have a recursive relationship i.e. A Parent's grand child can also be used as a Parent; which make life bit difficult as I run in to infinite loop.

Data:

Sender,Receiver
A,B
A,H
B,C
B,D
D,E
E,F
F,G
G,C
H,I
H,J
J,K
K,L
L,M
M,K
L,N
N,O
N,P
P,A
N,Q

Client may render like this (I only care about Java Structure):
Client can request any Node and I have to generate the whole Tree and send the response i.e. A, K or N.

enter image description here

Questions:

  1. What will be the best Data Structure for this requirement? For
    example Tree like or any other?
  2. Should I write my own logic to read
    the data and set in Tree or are there any standard algorithms out
    there?
  3. What’s the best way of avoiding the recursion?

Any working example will really help here 🙂

Please also see my working solution below.

  • 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-05T01:08:59+00:00Added an answer on June 5, 2026 at 1:08 am

    I’m sure the tree examples you’ve found already are correct on how to implement a tree like structure. In your case you have the added complication that it is possible for recursive loops to exist as certain children will be exact object references to one of their ancestors. (right?)

    Because of this complication any process that attempts to traverse your tree by iterating over the children of each node will loop around these recursive connections until stack overflow occurs.

    In this case you are no longer really dealing with a tree. Mathematically, a tree is defined as a Graph without cycles. In your case you have cycles, and therefore not a tree but a circular graph.

    I have dealt with such situations in the past, and I think you can you can deal with this in two ways.

    1. Break the cycles (at an object level), to return to a tree. Where one of these recursive connections happens, do not place the real object reference to the ancestor, but a stub that indicates which object it connects to without being the object reference to that item.

    2. Accept you have a circular graph, and ensure your code can cope with this when traversing the graph. Ensure that any client code interacting with your graph can detect when it is in a recursive branch and deal with it appropriately.

    IMHO Option 2 is not very attractive as you may find it hard to guarantee the constraint and it often leads to bugs. As long as you can allocate each item in the tree a unique identifier, option 1 works well, although clients will still need an awareness of this possibility occurring so they can process the de-coupled link and represent it correctly (for instance in a tree view UI). You are still wanting to model a circular graph, but are going to use a tree to represent it at an object level as it simplifies the code (and presentation).

    Full Example of Option 1:

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    
    public class CyclicGraphTest 
    {   
        public static void main(String[] args) 
        {       
            new CyclicGraphTest().test();           
        }
    
        public void test()
        {
            NodeManager manager = new NodeManager();
            Node root = manager.processNode("ZZZ");
            root.add(manager.processNode("AAA"));
            manager.get("AAA").add(manager.processNode("BBB"));
            manager.get("AAA").add(manager.processNode("CCC"));
            manager.get("AAA").add(manager.processNode("DDD")); 
            manager.get("DDD").add(manager.processNode("EEE"));
            manager.get("EEE").add(manager.processNode("FFF"));
            manager.get("FFF").add(manager.processNode("AAA"));
            manager.get("AAA").add(manager.processNode("JJJ")); 
            root.add(manager.processNode("EEE"));
            GraphWalker walker = new GraphWalker(manager, root, 1);
            System.out.println(walker.printGraph());        
        }
    
        /**
         * Basic Node
         */
        public class Node implements Iterable<Node>
        {
            private String id;
            private List<Node> children = new ArrayList<Node>();
    
            public Node(String id) {            
                this.id = id;
            }
    
            public boolean add(Node e) {
                return children.add(e);
            }
    
            public String getId() {
                return id;
            }
    
            @Override
            public Iterator<Node> iterator() {          
                return children.iterator();
            }           
        }
    
        /**
         * Cyclical Reference
         * 
         */
        public class ReferenceNode extends Node
        {
            private String refId;
    
            public ReferenceNode(String id, String refId) {
                super(id);
                this.refId = refId;
            }
    
            @Override
            public boolean add(Node e) {
                throw new UnsupportedOperationException("Cannot add children to a reference");
            }
    
            public String getRefId() {
                return refId;
            }           
        }   
    
        /**
         * Keeps track of all our nodes. Handles creating reference nodes for existing
         * nodes.
         */
        public class NodeManager
        {
            private Map<String, Node> map = new HashMap<String, Node>();
    
            public Node get(String key) {
                return map.get(key);
            }
    
            public Node processNode(String id)
            {
                Node node = null;
                if(map.containsKey(id))
                {
                    node = new ReferenceNode(getRefId(id), id);
                    map.put(node.getId(), node);                
                }
                else
                {
                    node = new Node(id);
                    map.put(id, node);
                }
                return node;
            }
    
            private String getRefId(String id) {
                int i = 0;
                String refId = null;
                while(map.containsKey(refId = id + "###" + i)) { i++; }
                return refId;
            }
    
            public Node resolve(ReferenceNode node) {
                return map.get(node.getRefId());
            }
        }
    
        /**
         * Walks a tree representing a cyclical graph down to a specified level of recursion
         */
        public class GraphWalker
        {
            private NodeManager manager;
            private Node root;
            private int maxRecursiveLevel;
    
            public GraphWalker(NodeManager manager, Node root, int recursiveLevel) {
                super();
                this.manager = manager;
                this.root = root;
                this.maxRecursiveLevel = recursiveLevel;
            }
    
            public String printGraph()
            {
                return printNode(root, 0, "   ").toString();
            }
    
            private StringBuilder printNode(Node node, int recursionDepth, String prefix) {
                Node resolvedNode = resolveNode(node, recursionDepth);
                if(resolvedNode != node) {
                    recursionDepth ++;
                    node = resolvedNode;
                }
                StringBuilder sb = new StringBuilder(node.getId());
                int i = 0;
                for(Node child : node)
                {
                    if(i != 0) sb.append("\n").append(prefix);
                    sb.append(" -> ").append(printNode(child, recursionDepth, prefix + "       "));             
                    i++;
                }
                return sb;
            }
    
            /**
             * Returns a resolved reference to another node for reference nodes when the 
             * recursion depth is less than the maximum allowed
             * @param node
             * @param recursionDepth
             * @return
             */
            private Node resolveNode(Node node, int recursionDepth) 
            {           
                return (node instanceof ReferenceNode) && (recursionDepth < maxRecursiveLevel) ? 
                        manager.resolve((ReferenceNode) node) : node;
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Requirement is this ... We have our 3 web-applications deployed in RHEL-5 server, we
Requirement : Should update MySQL database with that of MS Sql Server updates which
I'm supporting a multi-tenant system that allows users to define custom forms. The data
As per the requirement, I have parsed an XML file and set data into
When designing a specialized structured-data document format (perhaps upon XML): part of the requirements
I'm writing a GUI remoting client where the server defines and executes screen and
I am a starter in web development. My requirement in a bit unique here.
I have a requirement to pass bulk data between a .NET (c#) application and
I'm in the process of designing a embedded C data storage module. It will
I have a column in my oracle db as character and data stored in

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.