简体   繁体   中英

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. These Edge objects contain the weight the weight of the edges between two nodes.

I'm trying to code a Dijkstra's shortest path algorithm; but I fear my graph structure is too complicated to make sense of all the example/pseudo code I can find for Dijkstra's. Can anyone offer any help. Thanks in advance.

How about using Boost Graph Library ,there is also python binding for it. I thought Dijkstra Shortest path is one of its example.

For Java, there are many available. JGraphT is simple and nice. Jung, JTS etc. If you are implementing on your own, then I would rather use a LinkedHashSet, ex:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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