250 likes | 387 Views
CSC 2300 Data Structures & Algorithms. April 17, 2007 Chapter 9. Graph Algorithms. Today. Network Flow Minimum Spanning Tree Prim’s Algorithm. Network Flow. Given a directed graph G=(V,E) with edge capacities c vw .
E N D
CSC 2300Data Structures & Algorithms April 17, 2007 Chapter 9. Graph Algorithms
Today • Network Flow • Minimum Spanning Tree • Prim’s Algorithm
Network Flow • Given a directed graph G=(V,E) with edge capacities cvw. • Examples: amount of water that flow through a pipe, or amount of traffic on a street between two intersections. • Also given two vertices: source s and sink t. • Goal: determine the maximum amount of flow that can pass from s to t.
Example • A graph and its maximum flow:
Procedure • Start with our graph G. • Construct a flow graph Gf, which gives the flow that has been attained at the current stage. • Initially, all edges in Gf have no flow. • Also construct a graph Gr, called the residual graph. • Each edge of Gr tells us how much more flow can be added.
Initial Stage • Graphs G, Gf, and Gr:
Stage 2 • Two units of flow are added along s, b, d, t:
Stage 3 • Two units of flow are added along s, a, c, t:
Stage 4 • One unit of flow added along s, a, d, t: • Algorithm terminates with correct solution.
Greedy Algorithm • We have described a greedy algorithm, which does not always work. • Here is an example: • Algorithm terminates with suboptimal solution.
Improve Algorithm • How to make the algorithm work? • Allow it to change its mind! • For every edge (v,w) with flow fvw in the flow graph, we will add an edge in the residual graph (w,v) of capacity fvw. • What are we doing? • We are allowing the algorithm to undo its decisions by sending flow back in the opposite direction.
Augmenting Path • Old: • New:
Improved Algorithm • Two units of flow are added along s, b, d, a, c, t: • Algorithm terminates with correct solution.
Algorithm Always Works? • Theorem. If the edge capacities are rational numbers, then the improved algorithm always terminates with a maximal flow. • What are rational numbers?
Running Time • Say that the capacities are all integers and the maximal flow is f. • Each augmenting flow increases the flow value by at least one. • How many stages? • Augmenting path can be found by which algorithm? • Unweighted shortest path. • Total time? • O( f |E| ).
Bad Case • This is a classic bad case for augmenting: • The maximum flow is easily seen by inspection. • Random augmentations could go along a path that includes (a,b). • What is the worst case for number of augmentations? • 2,000,000.
Remedy • How to get around problem on previous slide? • Choose the augmenting path that gives the largest increase in flow. • Which algorithm? • Modify the Dijkstra’s algorithm that solves a weighted shortest-path problem.
Second Example • A graph and its minimum spanning tree:
Prim’s Algorithm • Outline:
Another Algorithm • Can you name another algorithm that is very similar to Prim’s algorithm? • Dijkstra’s algorithm. • What is the major difference?
Running Time • Without heaps? • O( |V|2 ). • Optimal for dense graphs. • With heaps? • O( |E| log|V| ). • Good for sparse graphs.