Advanced Graph Algorithms
If all edges are equally-weighted, the breadth-first search algorithm finds shortest paths from a source vertex to all other vertices. However, it does not work if the edges have different weights.
The following algorithm initializes the distance array d and the predecessor array π.
Many shortest-path algorithm use the "relaxation" technique, which tries to improve the shortest distance to vertex v by taking vertex u into consideration, with (u, v) as the last step. The matrix w stores the weights of the edges.
Bellman-Ford algorithm processes the graph |V|-1 passes, and in each pass, tries the edges one-by-one to relax the distance. After that, if there is still possible relaxation, the graph contains negatively-weighted cycle.
The running time is O(V E). Example:
If the graph is a DAG, there are faster solutions. The following algorithm topologically sort the vertices first, then determine the shortest paths for each vertex in that order. It runs in Θ(V + E) time, which comes from topological sorting (and DFS).
Example:
Dijkstra's algorithm works for directed graphs without negative weight. It repeatedly selects the vertex with the shorted path, then use it to relax the paths to other vertices. It is similar to MST-Prim, except here the distance is to the starting vertex, not to the MST.
This is a greedy algorithm. Its running time is O((V + E) lg V). Example:
Many algorithms use the adjacency matrix to remember the shortest paths found so far, and a predecessor matrix Π, where πij is the predecessor of j on some shortest path from i to j. Given it, the following algorithm prints the shortest path recursively.
The following dynamic-programming algorithm extends shortest paths starting with edge, and in each step tries to add one more edge.

Each call to the above algorithm will extend the length of path under consideration by one. Therefore, if we start with L = W, and repeatedly call the algorithm n-1 times, we will get a matrix for the shortest paths. This solution takes Θ(n4) time.
Floyd-Warshall algorithm in each step adds one possible intermediate vertex into the shortest paths.

The running time of the above algorithm is Θ(n3).
A similar algorithm calculates the transitive closure of a graph, where T(n)ij = 1 if and only if there is a path from i to j.
The following method finds the maximum flow of a given flow network:
Ford-Fulkerson-Method(G, s, t) 1 initialize flow f to 0 2 while there exists an augmenting path p 3 do augment flow f along p 4 return fIntuitively, an augmenting path is a path from s to t that still has residual (remaining) capacity, and the additional flow is decided by the edge in the path that has the smallest difference between the flow capacity and the current flow.
Example: Figure 26.5
When an algorithm follows this method and uses BFS to scan the paths, the running time of the algorithm is O(V E2). See the textbook for details.