How to implement Java graph data-structure and class with restricted visibility?

goerlibe

I am currently learning about data structures (eg LinkedList, DoublyLinkedList, ArrayList,...) and I was wondering how to implement a (not directed) graph in Java.

I was thinking about two classes: Graph and Node<T>
Each node should know to which other nodes it is connected (is a List<Node<T>> appropriate? What kind of List would be best?) The Graph class could then provide methods like boolean contains(T element)

The Node class would have no other use so how do I restrict visibility so that only Graph has access?

EDIT: furthermore how can I weigh the connections between nodes? I guess I would need a completely different Implementation than mentioned above since a simple List of connected nodes would not be enough?

Praytic

A graph is an ordered pair G = (V, E) comprising a set V of vertices or nodes or points together with a set E of edges or arcs or lines, which are 2-element subsets

Following definition should give you a clear way to organise your graph. It consists of Set<Node> and Set<Edge> (implementation surely would be HashSet). Edge is a pair of from and to Nodes. Edge can have an attribute cost for weighted graph. If you need undirected graph, you can store either two directed Edges indicating one undirected edge or add property undirected to the Edge class.

public class Graph<T> {

    private Set<Node<T>> nodes;
    private Set<Edge<T>> edges;

    private class Node<T> {
        private T value;
    }

    private class Edge<T> {
        private Node<T> to;
        private Node<T> from;
        private Number cost;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to implement a Set Data Structure in Java?

From Dev

How to implement my own LinkedList<LinkedList> data structure in Java?

From Dev

how to implement below data structure in java using collection framework

From Dev

how to implement below data structure in java using collection framework

From Dev

How to solve no output problem when I use vectors of vector to implement a graph data structure?

From Dev

How is C++'s std::set class able to implement a binary tree for ANY type of data structure?

From Dev

How to parse JSON data without key names into a Java class structure?

From Dev

Hibernate how to implement dynamic data structure

From Dev

How to implement the data structure for "transferring ownership" of a model

From Dev

How to implement a Timer class in Java?

From Dev

How to implement this Class Diagram in JAVA?

From Dev

How to implement a java generic data structure, avoiding type erasure casting issues

From Dev

Java: how volatile guarantee visibility of "data" in this piece of code?

From Dev

Which data structure will be suitable for storing three related values? And how to implement?

From Dev

How implement data structure which is map many to one?

From Dev

Java how to implement and design an abstract class

From Dev

How to implement methods of an abstract class? (Java)

From Dev

How to map a Tinkerpop3 graph to a recursive data structure?

From Dev

How to implement a converter from string to Visibility

From Dev

How to implement a converter from string to Visibility

From Dev

How to properly structurate the visibility of this Class?

From Dev

Suggest Data Structure which implement this

From Dev

Graph data structure memory management

From Dev

Graph data structure memory management

From Dev

Dynamically adding to a graph data structure

From Dev

How to Implement indexOf function of Java in Data weave

From Dev

How do I implement restricted access to subsets in my database

From Java

Given a data structure representing a social network implement method canBeConnected on class Friend

From Dev

Implement a typed restricted property

Related Related

  1. 1

    How to implement a Set Data Structure in Java?

  2. 2

    How to implement my own LinkedList<LinkedList> data structure in Java?

  3. 3

    how to implement below data structure in java using collection framework

  4. 4

    how to implement below data structure in java using collection framework

  5. 5

    How to solve no output problem when I use vectors of vector to implement a graph data structure?

  6. 6

    How is C++'s std::set class able to implement a binary tree for ANY type of data structure?

  7. 7

    How to parse JSON data without key names into a Java class structure?

  8. 8

    Hibernate how to implement dynamic data structure

  9. 9

    How to implement the data structure for "transferring ownership" of a model

  10. 10

    How to implement a Timer class in Java?

  11. 11

    How to implement this Class Diagram in JAVA?

  12. 12

    How to implement a java generic data structure, avoiding type erasure casting issues

  13. 13

    Java: how volatile guarantee visibility of "data" in this piece of code?

  14. 14

    Which data structure will be suitable for storing three related values? And how to implement?

  15. 15

    How implement data structure which is map many to one?

  16. 16

    Java how to implement and design an abstract class

  17. 17

    How to implement methods of an abstract class? (Java)

  18. 18

    How to map a Tinkerpop3 graph to a recursive data structure?

  19. 19

    How to implement a converter from string to Visibility

  20. 20

    How to implement a converter from string to Visibility

  21. 21

    How to properly structurate the visibility of this Class?

  22. 22

    Suggest Data Structure which implement this

  23. 23

    Graph data structure memory management

  24. 24

    Graph data structure memory management

  25. 25

    Dynamically adding to a graph data structure

  26. 26

    How to Implement indexOf function of Java in Data weave

  27. 27

    How do I implement restricted access to subsets in my database

  28. 28

    Given a data structure representing a social network implement method canBeConnected on class Friend

  29. 29

    Implement a typed restricted property

HotTag

Archive