0

I want to create a class node, class Edge and class Graph. In class node, it must take some number as input to create nodes. Also, I want this class to have methods where I can change different attributes of the nodes. Also, it must contain a method to send and receive information from its adjacent nodes over Edge. These information receiving and sending methods should have provision to control information flow.

I am not sure which data structure to use for nodes to fulfill these requirements.

2
  • Create a class with the mentioned methods and the required attributes. You will need to store multiple nodes in your graph. Usually you want to be able to delete/add nodes from the graph so you need a dynamic data structure like LinkedList<Node>, if your node class is named Node. Commented Nov 22, 2013 at 1:20
  • Can you please suggest me implementation? Like with an example of how program can be written? Commented Nov 22, 2013 at 1:50

4 Answers 4

1

Create a class Node that has all the attributes that you want. Ex. If you want to create a Node for a binary Tree your Node class can be something like.

  class Node
     {
        Node left;
        Node right;
        int info;
        Node(int value)
        {
         this.info = value;
        }
        //Add more attributes or functionalities
     }

This is one representation of how you can create a Node class. Depending upon your requirements the representation might change but the underlying concept remains the same.

Sign up to request clarification or add additional context in comments.

3 Comments

what does variable info represents?
When you create a Node you need to store some information in it right? The variable info is for that purpose.
If I may add to what @Prateek said, many times if you are using a binary tree, not only do you have the "Node left" and "Node right", but also a "Node parent" so that traversal back up the tree for something like an iterator is possible. Again, just FYI
1

Create your own class node.

A node is just an object. Give it some attributes (for your example you probably want weight and value).

public class Node{

    private double weight;
    private String value;
    private ArrayList<Node> edges;


//setters and getters
}

1 Comment

Does weight suggests "to how many nodes it is connected to" ? Also, can I use Scanner to accept number of nodes from input?
0

Create a class Node which has a list of references to other Nodes (in a directed graph, this is especially usefull). An edge is not actually a "thing" to create - it's more like a connection.

Comments

0

The two most common ways to implement graphs are to use either an adjacency matrix or an adjacency list.

The matrix approach involves using a 2d array to determine if node i and node j are connected by asking if node[i][j] is true. Wasteful on space, but has constant time access.

Adjacency lists keep track of every adjacent node to the current node, which save on space but cost linear time in order to determine if a node is connected to it.

These articles explain it better:

Matrix

List

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.