我有一个作为邻接表实现的无向加权图。有一个以 Node 对象作为键,Edge 对象列表作为值的 hashmap。这些 Edge 对象包含两个节点之间的边的权重。
我正在尝试编写 Dijkstra 的最短路径算法;但我担心我的图形结构太复杂,无法理解我能为 Dijkstra 找到的所有示例/伪代码。任何人都可以提供任何帮助。提前致谢。
使用Boost Graph Library怎么样,还有 python 绑定。我认为 Dijkstra Shortest path 就是它的例子之一。
对于 Java,有很多可用的。JGraphT 简单又好用。Jung,JTS 等。如果您自己实现,那么我宁愿使用 LinkedHashSet,例如:
public abstract class Graphs<T, E>
{
final LinkedHashSet<Vertex<? extends T>> allVertex;
...
和顶点将是:
/**
* E is of the type: the kind of vertex I want to make. For example String
*/
public interface Vertice<E>
{
public void setAttribute(E ob);
public E getAttribute();
// public Set<Edge<?>> getConnectedVertices();
public Set<Edge<?>> getConnectedEdges();
public Edge<?> getPassageToVertice(Vertice<?> vertice);
}
边缘是:
package Graph;
public interface Edge<E>
{
/**
* Returns the attribute Associated with the Edge
* @return The Attribute associated with the Edge
*/
public E getAttribute();
public void setSource(Vertex<?> source);
public void setDestination(Vertex<?> dest);
/**
* Sets the attribute of the edge
* @param attr Sets the attribute of the Edge
*/
void setAttribute(E attr);
/**
* Get the permission to access the destination from the source.
* <p> For example:
* <li>A-------edge---------B </li>
* here A is a vertex
* B is a vertex
* edge is the edge.
* If the argument of the method is vertex A then it returns Vertex B, if it is
* legal to return (for ex will return B if the edge is intended to be Undirected)
* This method can be used to create different types of Edge's.</p>
* <p> In case of a directed edge
* <li>A----->----edge------>B</li>
* on calling getPassage(B) it will return null.
* </p>
* @param source One end of the edge
* @return The other end of the edge if the edge has access to. Or else return null;
*/
public Vertex<?> getPassage(Vertex<?> source);
public Vertex<?> getSource();
public Vertex<?> getDestination();
}