简体   繁体   English

具有邻接表图的Dijkstra算法

[英]Dijkstra's Algorithm with adjacency list graph

I have an undirected, weighted graph implemented as an adjacency list. 我有一个实现为邻接表的无向加权图。 There is a hashmap with Node objects as keys and lists of Edge objects as values. 有一个哈希图,其中Node对象为键,Edge对象列表为值。 These Edge objects contain the weight the weight of the edges between two nodes. 这些Edge对象的权重包括两个节点之间的边缘的权重。

I'm trying to code a Dijkstra's shortest path algorithm; 我正在尝试编写Dijkstra的最短路径算法; but I fear my graph structure is too complicated to make sense of all the example/pseudo code I can find for Dijkstra's. 但是我担心我的图形结构太复杂,无法理解我能为Dijkstra找到的所有示例/伪代码。 Can anyone offer any help. 任何人都可以提供任何帮助。 Thanks in advance. 提前致谢。

How about using Boost Graph Library ,there is also python binding for it. 如何使用Boost Graph Library ,还有python绑定。 I thought Dijkstra Shortest path is one of its example. 我认为Dijkstra最短路径是其示例之一。

For Java, there are many available. 对于Java,有很多可用的。 JGraphT is simple and nice. JGraphT简单又不错。 Jung, JTS etc. If you are implementing on your own, then I would rather use a LinkedHashSet, ex: Jung,JTS等。如果您是自己实现的,那么我宁愿使用LinkedHashSet,例如:

public abstract class Graphs<T, E> 
{
final LinkedHashSet<Vertex<? extends T>> allVertex;
 ...

and Vertex would be: 顶点将是:

/**
 * 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);
  }

With Edge being: 边缘为:

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();

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM