[英]graph implementation with adjacency list java
I have 2D coordenates for many points, for example point a = x,y我有很多点的二维坐标,例如点 a = x,y
I want to do a graph implementation using adjacency list list and connect certain points of a undirectional graph in the most efficient way (not using maps or hash table)我想使用邻接列表列表进行图形实现,并以最有效的方式连接无向图的某些点(不使用地图或 hash 表)
I tried to create a class for the points named point and did an array of Linked lists我试图为名为 point 的点创建一个 class 并做了一个链接列表数组
private List nodes[] = new LinkedList[numPoints]私有列表节点[] = new LinkedList[numPoints]
the problem is that the array will be integers and I can´t doesnt store the x and y, I need to know the x and y问题是数组将是整数,我不能不存储 x 和 y,我需要知道 x 和 y
You can try this implmentation of undirected graph您可以尝试这种无向图的实现
public class Graph {
private static final String NEW_LINE = System.lineSeparator();
private final int vertices;
private int edges;
private List<Integer>[] adj;
public Graph(int v) {
if (v < 0)
throw new IllegalArgumentException("Number of vertices must be non-negative");
this.vertices = v;
this.edges = 0;
adj = (List<Integer>[]) new LinkedList[v];
for (int i = 0; i < v; i++) {
adj[v] = new LinkedList<Integer>();
}
}
public int getVertices() {
return vertices;
}
public int getEdges() {
return edges;
}
public void addEdge(int v, int w) {
adj[v].add(w);
adj[w].add(v);
edges++;
}
public int degree(int v) {
return adj[v].size();
}
public Iterable<Integer> adj(int v) {
return adj[v];
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (int v = 0; v < vertices; v++) {
builder.append(v + ": ");
for (int w : adj[v]) {
builder.append(w + " ");
}
builder.append(NEW_LINE);
}
return builder.toString();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.