• No results found

graph algorithm

N/A
N/A
Protected

Academic year: 2020

Share "graph algorithm"

Copied!
56
0
0

Loading.... (view fulltext now)

Full text

(1)

Single Source Shortest-Path:

The General Case (with negative edges)

 Bellman-Ford algorithm.

Iteratively relax all edges |V|-1 times

If no negative cycles, then: 1) d(v)= (v)

2) triangle inequality should hold

(2)

COSC 3101N J. Elder

(3)

All-Pairs Shortest Paths

 Given a directed graph G = (V, E), weight function w : E R, |V| = n.→  Assume no negative weight cycles.

 Goal: create an n × n matrix of shortest-path distances δ(u, v).  Could run BELLMAN-FORD once from each vertex:

 O(V2E)—which is O(V4) if the graph is dense (E = (V2)).

 If no negative-weight edges, could run Dijkstra’s algorithm once from

each vertex:

 O(V E lg V) with binary heap—O(V3 lg V) if dense,

(4)

All Pairs Shortest Path – Floyd-Warshall Algorithm

 Dynamic programming approach.

 Use optimal substructure of shortest paths: Any subpath of

a shortest path is a shortest path.

 Create a 3-dimensional table:

 Let dij(k) –shortest path weight of any path from i to j where all

intermediate vertices are from the set {1,2, …, k}.

(5)

Computing d

ij(k)

 Base condition: dij(0) = ?

 dij(0) = wij.

 For k>0:

 Let p=<vi, . . . , vj> be a shortest path from vertex i to vertex j

with all intermediate vertices in {1,2, …, k}.

 If k is not an intermediate vertex, then all intermediate

vertices are in {1,2, …, k-1}.

 If k is an intermediate vertex, then p is composed of 2

(6)
(7)

Algorithm

 Running time = ?

 O(n3).

 Memory required = ?

(8)
(9)
(10)
(11)
(12)
(13)
(14)

All-Pairs Shortest Path: Johnson’s Algorithm

 Idea: If the graph is sparse (|E|<<|V|2), it pays to run

Dijkstra’s algorithm once from each vertex.

 O(VE log V) using binary heap, O(V2 log V + V E) using

Fibonacci heap.

 But Dijkstra’s algorithm does not handle negative edges.

 Johnson’s Algorithm: reweight edges to form equivalent

graph with non-negative edges.

 Floyd-Warshall still has advantages:

 very simple implementation

 no fancy data structures

(15)
(16)

The Problem

 Use a graph to model material that flows through conduits.

 Each edge represents one conduit, and has a capacity, which

is an upper bound on the flow rate = units/time.

 Can think of edges as pipes of different sizes. But flows

don’t have to be of liquids.

 Want to compute max rate that we can ship material from a

(17)

What is a Flow Network?

 Each edge (u,v) has a nonnegative capacity c(u,v).  If (u,v) is not in E, assume c(u,v)=0.

 We have a source s, and a sink t.

 Assume that every vertex v in V is on some path from s to t.

(18)

What is a Flow in a Network?

 For each edge (u,v), the flow f(u,v) is a real-valued function

that must satisfy 3 conditions:

 Note that skew symmetry condition implies that f(u,u)=0.

  

Capacity constraint: u v V f u v, , ( , ) c u v( , )

   

Skew symmetry: u v V f u v, , ( , ) f v u( , )

  

Flow conservation: { , }, ( , ) 0 v V

(19)

Example of a Flow:

 f(v2, v1) = 1, c(v2, v1) = 4.

 f(v1, v2) = -1, c(v1, v2) = 10.

 f(v3, s) + f(v3, v1) + f(v3, v2) + f(v3, v4) + f(v3, t) =

0 + (-12) + 4 + (-7) + 15 = 0

flow

(20)

The Value of a flow

 The value of a flow is given by

V

v

V

v

t

v

f

v

s

f

f

|

(

,

)

(

,

)

|

(21)

Example:

|f| = f(s, v1) + f(s, v2) + f(s, v3) + f(s, v4) + f(s, t) = 11 + 8 + 0 + 0 + 0 = 19

|f|= f(s, t) + f(v1, t) + f(v2, t) + f(v3, t) + f(v4, t) =

(22)

A flow in a network

 We assume that there is only flow in one direction at a time.

 Sending 7 trucks from Edmonton to Calgary and 3 trucks from

(23)

Multiple Sources Network

 We have several sources and several targets.

(24)

The Ford-Fulkerson Method

 Try to improve the flow, until we reach the maximum.

 The residual capacity of the network with a flow f is given by:

)

,

(

)

,

(

)

,

(

u

v

c

u

v

f

u

v

c

f

(25)

Example of residual capacities

Network:

Residual Network:

(26)

The residual network

 The edges of the residual network are the edges on which

(27)

Augmenting Paths

 An augmenting path p is a simple path from s to t on the

residual network.

 We can put more flow from s to t through p.

 We call the maximum capacity by which we can increase the

flow on p the residual capacity of p.

}

on

is

)

,

(

:

)

,

(

min{

)

(

p

c

u

v

u

v

p

(28)

Augmenting Paths - example

 The residual capacity of p = 4.

(29)
(30)

Example

Flow(1)

Residual(1)

Flow(2)

Residual(2)

(31)

Augmenting Paths – example

 The maximum possible flow through the cut = 12 + 7 + 4 = 23

The network has a capacity of at most 23.

This is called a

minimum cut

.

(32)

Cuts of Flow Networks

 A cut in a network is a partition of V into S and T=V-S so

(33)

The net flow through a cut (S,T)

 f(S,T) = 12 – 4 + 11 = 19

(34)

The capacity of (S,T)

 c(S,T)= 12+ 0 + 14 = 26

(35)

Lemma 26.5

(36)

Corollary 26.6

 The value of any flow f in a flow network G is bounded from

(37)

Theorem 26.7 (Max-flow min-cut theorem)

 If f is a flow in a flow network G=(V,E), with source s and

sink t, then the following conditions are equivalent:

1. f is a maximum flow in G.

2. The residual network Gf contains no augmented paths.

(38)
(39)

Example

Resulting

Flow =

Original Network

augmenting path

(40)

Example

Resulting

Flow = 4

Residual Network

(41)

Example

Residual Network

(42)

Example

Resulting

Flow = 11

Residual Network

(43)

Example

Residual Network

(44)

Example

Resulting

Flow = 19

Residual Network

(45)

Example

Residual Network

(46)

Residual Network

Example

Resulting

Flow = 23

(47)

Analysis

O(E)

(48)

Analysis

 If capacities are all integer, then each augmenting path

raises |f| by ≥ 1.

 If max flow is f*, then need ≤ |f*| iterations time is O(E|⇒

f*|).

 Note that this running time is not polynomial in input size. It

depends on |f*|, which is not a function of |V| or |E|.

 If capacities are rational, can scale them to integers.

(49)

The basic Ford-Fulkerson Algorithm

 With time O ( E |f*|), the algorithm is not polynomial.

 This problem is real: Ford-Fulkerson may perform very badly

if we are unlucky:

(50)

Run Ford-Fulkerson on this example

Augmenting Path

(51)

Run Ford-Fulkerson on this example

Augmenting Path

(52)

Run Ford-Fulkerson on this example

(53)

The Edmonds-Karp Algorithm

 A small fix to the Ford-Fulkerson algorithm makes it work in

polynomial time.

(54)

The Edmonds-Karp Algorithm

 Compute the path in line 4 using breadth-first search on

residual network.

 The augmenting path p is the shortest path from s to t in

the residual network (treating all edge weights as 1).

(55)

The Edmonds-Karp Algorithm - example

(56)

Further Improvements

 Push-relabel algorithm ([CLRS, 26.4]) – O(V2 E).

References

Related documents

This methods block included the social studies methods course, which met one day a week for two hours and 45 minutes for 16 weeks and a middle school (6-8)

Figure 6.3: European call price matrix with mapped state space and approximation error to analytic Black-Scholes using large b value.. Chapter

Importantly, the candidate will have the opportunity to continue to develop and foster strong translational and disease-impacting programs in the department and with CWRU faculty at

An ASM-SPS is proposed in this paper to enhance the system reliability by depriving the dominating position of power electronics in MVDC IPS. The models of SSG, ASM,

Ma et al [14] introduced a comprehensive evaluation of the strength of business model by combine with entropy method and BP neural network and a specific algorithm was given.

Risk is characterized in this research by the fuzzy seismic risk index (FSRi) representing the multidimensional aspect various risk attributes such as hazard

(a) Traditional networks with distributed control and device-specific management contrast with (b) the flexible and logically-centralized control and management in

Initially, a Rectangular Microstrip Patch Antenna (RMPA) without DGS which resonates at 13 GHz in Ku-band range is considered, and to change the position of the resonant frequency