CIS 3223. Data Structures and Algorithms

Paths in graphs

 

1. Shortest path

Search for path: single-source, multiple-destinations. The result is a path tree, with the source as root.

Other possibilities: one pair (stop in the middle), all-pairs (to be covered later)

Unweighted graph: BFS

Weighted graph: Dijkstra's algorithm, in a slightly different form

Similarity between the two algorithms: single-edge extension of the path tree, queue and priority queue

When the priority queue in Dijkstra's algorithm is implemented by a heap, its complexity is O((|V| + |E|) log |V|)

 

2. Shortest path with negative edges

Why Dijkstra's algorithm cannot be used if some weight values are negative?

Shortest path and cycle (with positive or negative total weight), in directed and undirected graphs

Incremental improvement: use the weight of an edge to update the distance to its destination

  procedure update((u, v) in E)
    d(v) = min{d(v), d(u) + w(u,v)}
Optimization by trying all possibilities. Number of edges in the shortest path

Bellman-Ford algorithm: repeat |V|-1 times to check every path that contains no cycle. Complexity: O(|V|*|E|).

The process can be terminated when no update happens in a whole loop. One extra loop (the |V|th) can be used to detect negative cycle.

 

3. Shortest path in dags

To avoid repeated distance updating: following a certain order

If a dag is linearized (topologically sorted), the shortest paths can be determined in that order, since there won't be any back edge.

The algorithm has complexity O(|V| + |E|). Example

Comparing the three algorithms for single-source shortest paths