0

I would like to write an algorithm, which finds the shortest path between two specific vertices - source and destination - in a directed and undirected graph.

I know dijkstra's algorithm, which is used to find all the shortest paths graph. But would you modify this algorithm to find the shortest path between two vertices only?

1
  • 1
    The answer to your question is: No, you would not modify it. You just stop execution once you pull the source node from the queue. Commented Jan 11, 2018 at 8:55

1 Answer 1

1

Just used the A* algorithm with no heuristic information. That would give you the same shortest path between the source and goal vertices that you would obtain from Dijkstra (Dijkstra is a specific case of A* when h = 0).

Regarding the implementation of the algorithm in C, there are tons of implementations available online: one, two or three.

Sign up to request clarification or add additional context in comments.

2 Comments

but that algorithm gives me all the shortest paths between all the vertices in the graph. How should it be modified?
No, A* will stop when it finds the shortest path from the source to the destination vertices. Just provide them along with the graph and the costs to traverse each pair of vertices.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.