The Bellman-Ford algorithm uses relaxation to find single source shortest paths on directed
graphs that may contain negative weight edges. The algorithm will also detect if there are
any negative weight cycles(such that there is no
DFS can be used directly, but is more often used as an intermediate technique within another
algorithm. This lecture examines three applications of DFS.
DFS can be applied to parenthetical expressions by considering the final d's and f's by observing
Given a source vertex s, follow a path to maximal depth and then backtrack to follow
any remaining paths available from preceeding vertices. If after exploring the source
any vertices remain undiscovered, begin another depth first search at an undiscovere
A path of length k from u to u' is a sequence of vertices <v0, v1, ., vk> with u = v0, u'
= vk, and (vi-1, vi) E.
If a path p from u to u' exists, then u' is reachable from u (denoted u u' if G is a
The path is simple if all the ver
Given a source vertex s, find the shortest paths (in terms of number of edges) to every reachable
vertex from s.
The procedure we will use will find all vertices reachable at a distance k before discovering
reachable vertices at a distance k+1. Ultimately
Just as Prim's algorithm improved on Kruskal's algorithm for MST's through the use
of a priority queue, Dijkstra's algorithm improves on Bellman-Ford for single source
shortest path also through the use of a priority queue. Unlike Bellman-Ford, however,
Bellman-Ford and Dijkstra's algorithms provide a means to find the shortest path from
a given source. However often we may wish to find the shortest paths between all pairs of
vertices. One way to accomplish this would be to simply run Bellman-Ford or Dij
An alternative algorithm for finding MST's is Prim's algorithm which runs
asymptotically faster than Kruskal's algorithm, but at the price of requiring a queue
Prim's algorithm finds MST's by growing the MST by adding ligh
The last type of graph problem we will investigate is maximum flow problems. Intuitively, these
problems can be thought of as starting with a source faucet that can provide as much water as
possible. The faucet is connected to a sink drain that can handle
Another graph problem is given a set of vertices and (weighted) edges, find a subset of edges that
connects all the vertices and has minimum total weight giving a minimum spanning tree (MST).
An example of such a problem is determining the minimum amount
Functions that Return a Value
So far we have looked at functions that do not compute a value. We declare these to
have the void return type.
In many cases, we WILL want to define functions that can be used to compute values.
By declaring a function as hav