1

The objective of this program to implement a graph in JAVA without using any libraries. This is not a homework assignment, just some practice. I am trying to implement a unidirected weighted graph which can be later passed in as a parameter to a Kruskal's or a Prim's algorithm to implement Minimum Spanning Tree. Since I am new to datastructures, I am having a hard time to figure how to go about implementing a graph. Adjacency Matrix/List is something I would want to avoid, can I go forward with the following approach:

/**
 * Graph.java: This is the main file.
 */ 
public class Graph {

    public static void main(String[] args)
    {
        Node n1 = new Node("A");
        Node n2 = new Node("B");
        Node n3 = new Node("C");
        Node n4 = new Node("D");
        Node n5 = new Node("E");
        Node n6 = new Node("F");

        Edges e1 = new Edges(n1, n2, 5);
        Edges e2 = new Edges(n1, n3, 3);
        Edges e3 = new Edges(n2, n4, 5);
        Edges e4 = new Edges(n2, n5, 2);
        Edges e5 = new Edges(n3, n6, 7);
    }
}


/**
 * Node.java class used to represent vertices
 */
public class Node {
    private String name;
    public Node(String name)
    {
        this.name = name;
    }
}

/**
 * Edges.java class used to represent edges.
 */
public class Edges {
    private int weight;
    private Node sNode;
    private Node dNode;
    public Edges(Node sNode, Node dNode, int weight)
    {
        this.sNode = sNode;
        this.dNode = dNode;
        this.weight = weight;
    }
}
3
  • 1
    This looks about right for a graph of nodes, what is it that you need help with? Also, you may want to have some way to access the data from the Nodes and Edges, either by making the fields public (and final if you don't want them changed), or by adding get methods Commented Nov 12, 2015 at 19:05
  • This might get better responses on Code Review SE. You've got a good start, as phflack mentions you want get/sets on some of your Node and Edge properties. I'm not sure why you want to avoid Graph being a Collection (List or Set or ...) of Node and Edge, it seems a perfectly logical way to represent a weighted digraph. Commented Nov 12, 2015 at 19:52
  • I don't want to avoid other suggestions, just want to avoid use of any libraries as I need to get a basic understanding of graph implementation. I implemented a graph using adjacency matrix once while implementing DFS, A* and uniform cost search, I had a hard time storing all the data such as the edge weights and path traversed. Thanks for the suggestions. Also, wouldn't the space complexity increases if adjacency matrix is used. Commented Nov 12, 2015 at 21:47

1 Answer 1

5

Personally I would keep the edges attached to the nodes. I realize it doubles the number of edges (because they are bidirectional in your case) but it makes traversing nodes a lot faster because you don't have to iterate through your edges to find where you can go next. I realize you mentioned you're not interested in adjacency lists but it seems like a better approach in most cases in terms of performance, although I'm sure you have your reasons. So I'm posting the code anyway, just so it's there for others.

(no get/set on purpose to keep the code succinct)

public class Node {
    private String name;
    private List<Edge> neighbors;

    public Node(String name) {
        this.name = name;
    }

    public void AddUndirectedEdge(Node destination, int weight) {
        neighbors.Add(new Edge(destination, weight));
        destination.neighbors.Add(new Edge(this, weight));
    }

    private static class Edge {
        public Node destination;
        public int weight;

        public Edge(Node destination, int weight) {
            this.destination = destination;
            this.weight = weight;
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Didn't quite get what you mean by saying "edges attached to the nodes". Also, how is this code different than mine. Aren't the 2 doing the same thing. Do you mean nesting Edge class inside Node class when you say "keeping edges attached to the nodes". I guess these are naive questions and I am sorry for that. I am new to the concept of data structures so trying to understand.
Sorry, I guess I was unclear. Here's an example of why it's different. If you want to traverse your graph starting from node n. With your implementation, you have to go look at all the Edges (for the entire graph) and look for the ones that involve n to find which one you can go to next (n's neighbors). In my proposed implementation, if you have 'n', all you have to do is look at its list of neighbors. They all pertain to that node. It's much much faster, especially on a large graph.
Oh ok, got your point, yeah it makes sense and now I understand how thats much more fast. Thanks for the explanation.
How to traverse these type of graphs when we have node and Edge as different classes? any help @mprivat
I don't understand the question I'm afraid.

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.