我有一个实现为邻接表的无向加权图。有一个哈希图,以Node对象为键,以Edge对象列表为值。这些Edge对象包含权重两个节点之间的边的权重。
我正在尝试编写Dijkstra的最短路径算法;但我担心我的图结构太复杂了,无法理解我能为Dijkstra找到的所有示例/伪代码。有人能提供帮助吗?提前谢谢。
发布于 2012-11-25 05:28:56
使用Boost Graph Library怎么样,它也有python绑定。我认为Dijkstra最短路径就是它的一个例子。
发布于 2012-11-25 05:33:32
对于Java,有许多可用的。JGraphT既简单又好用。如果您是自己实现的,那么我更愿意使用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();}
https://stackoverflow.com/questions/13545738
复制相似问题