Java - Which is the best implementation structure for Graph?

前端 未结 8 2086
我在风中等你
我在风中等你 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 17:08

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

    class Node
    {
      List 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.

提交回复
热议问题