0

Given a directed graph G , weights, s source vertex and d[v] for each vertex in the graph (the distance from s to v) I need to find an algorithm that builds the shortest paths graph.

I was thinking of going on edges by BFS but than how can I know which edge should be in the tree and how to check that the d[v] for each vertex is true.

5
  • By "shortest paths tree", do you mean the minimum spanning tree? Commented Dec 23, 2012 at 20:00
  • @VaughnCato: I think he means Dijkstra, which gives the shorted path from the source vertex to every other vertex. Though I'm not sure what makes it a "tree"... Commented Dec 23, 2012 at 20:02
  • What is the measurement for the length of the path? The sum of the edge weights? If so, then d[v] seems redundant. Commented Dec 23, 2012 at 20:06
  • @VaughnCato question says - d[v] is the distance (in weight) of every vertex v from s Commented Dec 23, 2012 at 20:08
  • I need to use that and than to also check that its values are true according to the given weights Commented Dec 23, 2012 at 20:09

2 Answers 2

3

Run Dijkstra. If an edge e connecting {u,v} has the property that d[u]+w[e]=d[v] then that edge is part of the tree you are looking for.

This way, you may not actually end up with a tree, but any MST has the properties you are looking for

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

2 Comments

and if im just going through the edges using BFS and checking if d[u]+w[e]=d[v] ? why do I need to use Dijkstra?
@Bobbbaa A BFS may use a node as a source node before finding the lowest weight path to it. Remember an indirect path via several other nodes may have lower total weight than a single edge.
3

The BFS algorithm helps you find the shortest path between a given root node and all other nodes only for an unweighted graph (or all weights are the same). If each edge has different weights, then the Dijkstra algorithm is a good choice. However, the Dijkstra algorithm does not work if there are are negative weights in the graph. If there are negative weights, then you should use the Bellman ford algorithm.

Comments

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.