【发布时间】:2019-09-21 17:39:47
【问题描述】:
我有一个有向图边的数组列表。
List<Edge> graph = new ArrayList();
我知道我可以将HashMap<Integer, List<Integer>> 用于有向图的邻接列表表示,以获得更快的 O(n) 性能,但 List<Edge> 只是澄清我对其他类似用户定义对象数组列表的问题的一个示例。
Edge 是用户定义的类如下:
public class Edge
{
int sourceVertex;
int destinationVertex;
public int getSourceVertex()
{
return sourceVertex;
}
public int getDestinationVertex()
{
return destinationVertex;
}
public void setSourceVertex(int sourceVertex)
{
this.sourceVertex = sourceVertex;
}
public void setDestinationVertex(int destinationVertex)
{
this.destinationVertex = destinationVertex;
}
}
我们可以通过创建边类的新对象来向有向图 Array List 添加新元素。
Edge edge = new Edge();
edge.setSourceVertex(1);
edge.setDestinationVertex(2);
graph.add(edge);
我的问题是,是否可以仅通过指定源顶点和目标顶点来从有向图 Array List 中搜索、修改或删除元素,而无需检查 Array List 的每个元素。我们不知道有向图 Array List 中任何特定元素的索引,以便能够通过索引在有向图 Array List 中执行这些操作。
如果我被允许仅使用有向图边的 Array List 表示,是否至少可以在 O(log n) 时间内完成这些搜索和修改操作,而无需花费 O(n) 时间?
【问题讨论】:
标签: java search arraylist graph-theory getter-setter