首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >邻接表图的Dijkstra算法

邻接表图的Dijkstra算法
EN

Stack Overflow用户
提问于 2012-11-25 05:21:58
回答 2查看 2.7K关注 0票数 0

我有一个实现为邻接表的无向加权图。有一个哈希图,以Node对象为键,以Edge对象列表为值。这些Edge对象包含权重两个节点之间的边的权重。

我正在尝试编写Dijkstra的最短路径算法;但我担心我的图结构太复杂了,无法理解我能为Dijkstra找到的所有示例/伪代码。有人能提供帮助吗?提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2012-11-25 05:28:56

使用Boost Graph Library怎么样,它也有python绑定。我认为Dijkstra最短路径就是它的一个例子。

票数 0
EN

Stack Overflow用户

发布于 2012-11-25 05:33:32

对于Java,有许多可用的。JGraphT既简单又好用。如果您是自己实现的,那么我更愿意使用LinkedHashSet,例如:

代码语言:javascript
运行
复制
public abstract class Graphs<T, E> 
{
final LinkedHashSet<Vertex<? extends T>> allVertex;
 ...

而顶点将是:

代码语言:javascript
运行
复制
/**
 * 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);
  }

边缘为:

代码语言:javascript
运行
复制
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();

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13545738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档