Java - Which is the best implementation structure for Graph?

前端 未结 8 2083
我在风中等你
我在风中等你 2021-02-02 16:47

The graph is very large but undirected. Edges are unweighted.

In my implementation, I have to find the vertex with max degree and do deletion on both vertexes and edges.

相关标签:
8条回答
  • 2021-02-02 16:58

    From the above suggested the answer would be

    Map with LinkedList...

    Your datastructure could be like this(varies according to your requirement)...

    Map<?, List<?>>
    <Node-name, List-of-nodes-connected-to-it>
    

    Basically, Graphs are best implemented with the help of HASHING and the above datastructure helps a lot in that..

    0 讨论(0)
  • 2021-02-02 16:58

    Graph implementation depends on what you going to do with it. But for most cases Adjacency list based implementation helps.

    In Java you can do it using a Map<>. Here is generic Adjacency List based Graph.Java implementation on my blog.

    0 讨论(0)
  • 2021-02-02 17:00

    You could also have a look at specifically-designed libraries, like JUNG

    0 讨论(0)
  • 2021-02-02 17:03

    My suggestion would be to store the vertexes in a priority queue. That way you can have very fast access to the vertex with the highest degree. As for how to implement the vertexes, I would store each neighboring vertex in some kind of set data-structure such as a HashSet or a TreeSet to be able to remove stuff efficiently. I wouldn't represent the edges explicitly, it's not needed.

    Code, something along the lines of:

    class Graph {
    
      PriorityQueue<Vertex> vertexes;
    
      public Graph() {
        vertexes = new PriorityQueue<Vertex>(10,new Vertex());
      }
    
      public Vertex maxDegreeVertex() {
        return vertexes.peek();
      }
    
      ...
    
    }
    
    class Vertex implements Comparator<Vertex> {
      HashSet<Vertex> edges;
    
      public Vertex() {
        edges = new HashSet<Vertex>();
      }
    
      public compare(Vertex v1, Vertex v2) {
        v2.edges.size().compareTo(v1.edges.size());
      }
    
      ...
    
    }
    

    Hope this helps.

    0 讨论(0)
  • 2021-02-02 17:08

    Depends on what other requirements you have. A naive, simple approach could be

    class Node
    {
      List<Node> edges;
      int id;
    }
    

    where you'd have a List of all the nodes in the graph. The problem is this can become inconsistent; e.g. node A might be in node B's edges list, but node B might not be in node A's list. To get around this, you could model it as such:

    class Edge
    {
      Node incidentA;
      Node incidentB;
    }
    
    class Node
    {
      int id;
    }
    

    Again, you'd have List and List of all the edges and nodes in the system. Of course analyzing this data structure would be done in a very different way than in the other approach.

    0 讨论(0)
  • 2021-02-02 17:09

    The two fundamental data structures for representing graphs are the

    • adjacency list

    • the adjacency matrix

    see http://en.wikipedia.org/wiki/Adjacency_list and http://en.wikipedia.org/wiki/Adjacency_matrix.
    The articles also discuss the pros and cons of those two structures.

    0 讨论(0)
提交回复
热议问题