csc 2300 data structures & algorithms

25
CSC 2300 Data Structures & Algorithms April 17, 2007 Chapter 9. Graph Algorithms

Upload: pabla

Post on 06-Jan-2016

35 views

Category:

Documents


0 download

DESCRIPTION

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 . - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSC 2300 Data Structures & Algorithms

CSC 2300Data Structures & Algorithms

April 17, 2007

Chapter 9. Graph Algorithms

Page 2: CSC 2300 Data Structures & Algorithms

Today

Network Flow Minimum Spanning Tree

Prim’s Algorithm

Page 3: CSC 2300 Data Structures & Algorithms

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.

Page 4: CSC 2300 Data Structures & Algorithms

Example

A graph and its maximum flow:

Page 5: CSC 2300 Data Structures & Algorithms

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.

Page 6: CSC 2300 Data Structures & Algorithms

Initial Stage

Graphs G, Gf, and Gr:

Page 7: CSC 2300 Data Structures & Algorithms

Stage 2

Two units of flow are added along s, b, d, t:

Page 8: CSC 2300 Data Structures & Algorithms

Stage 3

Two units of flow are added along s, a, c, t:

Page 9: CSC 2300 Data Structures & Algorithms

Stage 4

One unit of flow added along s, a, d, t:

Algorithm terminates with correct solution.

Page 10: CSC 2300 Data Structures & Algorithms

Greedy Algorithm

We have described a greedy algorithm, which does not always work.

Here is an example:

Algorithm terminates with suboptimal solution.

Page 11: CSC 2300 Data Structures & Algorithms

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.

Page 12: CSC 2300 Data Structures & Algorithms

Augmenting Path

Old:

New:

Page 13: CSC 2300 Data Structures & Algorithms

Improved Algorithm

Two units of flow are added along s, b, d, a, c, t:

Algorithm terminates with correct solution.

Page 14: CSC 2300 Data Structures & Algorithms

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?

Page 15: CSC 2300 Data Structures & Algorithms

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| ).

Page 16: CSC 2300 Data Structures & Algorithms

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.

Page 17: CSC 2300 Data Structures & Algorithms

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.

Page 18: CSC 2300 Data Structures & Algorithms

Minimum Spanning Tree

Page 19: CSC 2300 Data Structures & Algorithms

Example

Page 20: CSC 2300 Data Structures & Algorithms

Second Example

A graph and its minimum spanning tree:

Page 21: CSC 2300 Data Structures & Algorithms

Prim’s Algorithm

Outline:

Page 22: CSC 2300 Data Structures & Algorithms

Prim’s Algorithm

Page 23: CSC 2300 Data Structures & Algorithms

Prim’s Algorithm – Tables

Page 24: CSC 2300 Data Structures & Algorithms

Another Algorithm

Can you name another algorithm that is very similar to Prim’s algorithm?

Dijkstra’s algorithm. What is the major difference?

Page 25: CSC 2300 Data Structures & Algorithms

Running Time

Without heaps? O( |V|2 ). Optimal for dense graphs. With heaps? O( |E| log|V| ). Good for sparse graphs.