You are on page 1of 21

7.

Graphs and Their Applications

7-1

Chapter 7. Graphs and Their Applications


Graphs [S, Chap. 12]
Graph is a mathematical modeling tool (a data model) which has endless applications.


Degree of

where

set of vertices (nodes) set of edges

= number of edges incident with .

Theorem 1 (Degree Sum; Handshaking)

Proof : Every edge is incident with exactly 2 vertices. Walk from .

to : sequence

, where
, ,

and

Trail: walk in which all edges are distinct. (Path) Path: trail in which all vertices are distinct. (Simple path) Circuit: closed trail ( Cycle: path except Power of :

). (Cycle)

). (Simple cycle)

, where

, and iff

in

Digraph (directed graph): directions imposed on edges.  Edges (arcs) become ordered pairs of vertices. Indegree:

= number of vertices adjacent to . Outdegree: = number of vertices adjacent from .

Theorem 2

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications

7-2

Graph Representation
* Static: 1) adjacency matrix, 2) edge list, 3) edge incidence matrix. * Dynamic: adjacency list. Adjacency matrix:

if otherwise

#define MAXNODES 100; int A[MAXNODES][MAXNODES]; Storage requirement =

bits.
2 5 8 6 7 3

A(G)=?

4 Exercise 1 (a) How do you decide if

? in how much time?

(b) How do you decide what vertices are adjacent to ? in how much time? Edge list: typedef struct vertex{ ... } VERTEXTYPE; typedef struct graph{ int |V|, |E|; VERTEXTYPE edges[|E|][2]; } G;

Storage requirement =

bits.
00.5

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

7. Graphs and Their Applications 1


7-3

edges[ ][ ]=?
4

Exercise 2 (a) How do you decide if

? in how much time?

(b) How do you decide what vertices are adjacent to ? in how much time? Incidence matrix:


1 5 8 4

if is incident with otherwise


2 6 7 3

B(G)=?

Exactly two 1s in each column. Each row is the modulo-2 sum of all others. Adjacency list: #define MAXVERTICES 50 typedef struct NODE *NODEPTR; typedef struct NODE{ int vertex; struct node *link; }; NODEPTR graph[MAXVERTICES]; int n=0; /* # of vertices currently in use */ Adjacency multilist: typedef struct EDGE *EDGEPTR; typedef struct EDGE{

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications

7-4

1 2 3
NULL

NULL

4 2
NULL

NULL

5 4 5 5
NULL

short int marked; int v1; int v2; /* for edge (v1, v2) */ EDGEPTR path1; EDGEPTR path2; }; EDGEPTR graph[MAXVERTICES];

0 1 3 2

0 1 2 3

N1 N2 N3 N4 N5 N6

0 0 0 1 1 2

1 2 3 2 3 3

N2 N3
NULL

N4 (0,1) N4 (0,2) N5 (0,3) N6 (1,2) N6 (1,3)


NULL

N5
NULL

The adjacency lists are: vertex0: N1-N2-N3 vertex1: N1-N4-N5 vertex2: N2-N4-N6 vertex3: N3-N5-N6

NULL

(2,3)

Exercise 3 For an undirected graph (a) (b) is a tree.

with vertices, prove that the following are equivalent:

is connected, but if any edge is removed the resulting graph is not connected. , there is exactly one simple path from to . contains no cycles and has edges.

(c) For any two distinct vertices and in (d)

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications

7-5

Bipartite Graphs

and

where

Complete graph:

Complete bipartite graph: Matching: a matching on

is a set of edges of E(G) in which no two are adjacent. Complete matching (perfect matching): which spans .

Theorem 3 (Philip Halls Marriage Theorem) If is bipartite, then a matching spanning iff

: adjacency set of .

set of girls set of boys

Denition 1 (1) (2)

set of boys (girls) compatible with . set of boys (girls) compatible with at least one

Theorem 4 a perfect matching iff

Denition 2 An M-augmenting path (augmenting path with respect to matching M) is an alternating path with respect to M whose 1st and last vertices are free. 3 5 6 1 2 4 (free) (free) M M M-augmenting path Theorem 5 (Berge) A matching M in is maximum iff 1 2 3 4 5 M 6

M M augmented path

M-augmenting path in

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications


7-6

(free)

Augmenting path search tree: one which consists of alternating paths emanating from a free vertex (Root).

Maximum Network Flow


The maximum matching problem can be viewed as a special case of the maximum network ow problem, in which we introduce two additional nodes, the source () and the terminal (), and model the network ows with unit capacity edges. Network ow model with unit capacity edges: s

Denition 3 The cut-set of a graph

is the set of edges of

whose removal leaves

disconnected.

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications

7-7

a b c d

cut-set = a,b,c,d

Denition 4 A network is a digraph with nonnegative real weights. A capacitated network is one whose weights stand for the capacities of the edges.

: a capacitated network where

set of vertices set of edges source vertex terminal vertex capacity of

* *

set of paths from to .

set of paths in that cover edge .

* The (path) ow assignment function is dened as * The edge ow of with respect to * We call a ow on P if

is dened as

If then

is the ow on p. If then is the ow on x.


The path ow on N is represented by the quintuple The value of path ow (N,s,t,P,f) is dened as

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications Example 1 Consider the following network.

7-8

2 ()

1 2 2

()

(a) Let

, and

.
1 2


2 1 1

Therefore,

(b) Let Then

, and

, which cannot be augmented by adding additional ow paths to . Therefore, the path ow is maximal (unaugmentable) (but not maximum).
a 1 s 1 b 1 1 3 t

maximum ow = ?

set of edges entering vertex . set of edges leaving vertex .

The edge ow function of is dened as


1.

where

; (capacity constraint)

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications 2.


7-9

. (conservation constraint)

The quadruple If then

is called the edge ow on x.

is dened as a conservative ow on from to .

The total ow F is dened as

which is the net ow into . A.k.a. the value of a conservative ow


: value of a maximum possible


. on

Upper bound:

s N

t Maxow(N,s,t) cannot exceed

A cut (cut-set) Let If

be a conservative ow. , then denotes the complement of


with respect to , i.e.,

A cut is dened as

and

It is directional.

is an cut if

Capacity of the cut

There does not exist an path in

is dened as

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications


7-10

Flow of the cut

capacity of a minimum-capacity cut in .

is dened as

net ow across

Example 2 Consider the network shown below. X a 2,1 s 1,0 1,1 b 3,2 3,1 3,2 d 4,3 X c 2,1 1,1 3,1 t


Theorem 6 Let

A minimum-capacity -cut is ( ), ( ). Therefore,

be a conservative ow, and

be an cut in . Then

Proof:

Consider

, then
c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications

7-11

Let If

or .
then contributes opposite terms to the sum of the right hand side, so the terms cancel.

The only remaining terms arise from Therefore, Theorem 7


and

when

Proof:

Therefore, the value of any conservative ow

is

Corollary 7.1 Given network ow mincut.

and a cut

, if

, then

is a maxow and

is a

Theorem 8 IF is a path ow, then there exists a conservative ow such that . Conversely, if is a conservative ow, then there exists a path ow such that .

Augmenting Path Algorithm [Ford & Fulkerson]


An edge

is saturated with respect to . Spare capacity of

if

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications A.k.a. slack. Flow augmenting path (FAP) with respect to edges of ,

7-12

alternating sequence of vertices and

1. 2. 3. 4.

and

(forward edge) or (backward edge); forward edge , ; backward edge , .


s 7,2

3,2

5,2

3,0

8,6

An FAP Ford and Fulkersons maximum ow algorithm is essentially a search tree technique for nding FAPs.

minimum of spare capacities on forward edges of FAP minimum of edge ows on backward edges of FAP

; , .
c a 1,0 1,1 1,0 b 1,1 d 1,1 1,1 c

We update (FAP) by 1. 2. forward edges , backward edges


1,0 s 1,0


1,1 1,1

1,1

1,0

b Theorem 9 Let

1,1

s t 1,0 FAP = s-a-d-t M=1, m=1, inc=1 d

1,1

1,0 t

be a conservative ow. Then

FAP with respect to

00.5

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

7. Graphs and Their Applications We view the process as stufng snakes through a plumbing system. Initially, push as many snakes as possible. The algorithm does 3 things: 1. label; 2. scan; 3. ow augmenting. At any stage, we will have vertices that are (a) labeled, (b) unlabeled, or (c) labeled and being scanned (exactly 1 of them). Assume that we are scanning vertex . Label on vertex :

7-13

, where or , and is an integer. (1) The source () is labeled as . a b d

(2) Consider the edge

:
can be labeled

i.e., it can be labeled iff there is a slack (spare capacity) on the edge. We label as

.
if it can be labeled.

Repeat for every vertex adjacent from (3) Consider the edge

:
can be labeled

i.e., it can be labeled iff there is a positive ow on the edge. We label as


.
if it can be labeled.

Repeat for every vertex adjacent to

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications a 1,1 s 1,0 1,0 3,0 b Exercise 4 Consider the network shown above. Find the maxow and a min-cut. 1,0 3,1 t

7-14

Exercise 5 (a) Using breadth rst search (BFS), show that the number of FAPs needed to obtain the maximum . ow never exceeds (b) What is the worst-case running time of the above algorithm? Theorem 10 Let

be the conservative ow determined by the Ford/Fulkerson algorithm. Then


Theorem 11

Minimum Spanning Trees


Spanning tree: tree that spans (covers) all vertices, i.e., one that connects all vertices. Problem: Given a connected undirected graph

with a weight function

(assuming all weights are distinct), nd an MST such that is minimized.

Claim: An optimal tree has optimal subtrees: Let be an MST of , which includes . If we remove to partition into and , then is an MST of subgraph induced by vertices in (i.e., = vertices in , and . ), and similarly, is an MST of Proof:

, the

Therefore, there cannot be a better tree than or , or else would be suboptimal.

00.5

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

7. Graphs and Their Applications Then, greedy algorithms can be used to solve the problem.

7-15

Two of the most famous greedy algorithms are the Kruskals algorithm and the Prims algorithm. Kruskals algorithm: T = {}; while(|E(T)| < n-1 && |E(G)| > 0) { choose a least-weight edge e from E(G); delete e from E(G); if(e does not create a cycle in T) add e to T; else discard e; } b 4 a 8 h 11 7 1 8 2 i 6 g 2 f 4 14 10 e c 7 d 9

Exercise 6 (a) Show that Kruskals algorithm generates an MST for (b) Show that the running time is Prims algorithm:

. [Prove by contradiction.]

T = {}; TV = {0}; /* start with vertex 0 and no edges */ while(|E(T)| < n-1) { find a least-weight edge (u,v) such that u is in TV but v is not in TV; if(there is no such edge) exit(1); add v to TV; add (u,v) to T; }

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications b 4 a 8 h 11 7 1 8 2 i 6 g 2 f 4 14 10 e c 7 d 9

7-16

Exercise 7 (a) Show that Prims algorithm generates an MST for (b) Show that the running time is

. [Prove by contradiction.]

. [Using array.]

(c) Is it possible to implement the algorithm such that the running time is [Fibonacci heap.]

DFS
Given a connected undirected graph with an adjacency-list representation, we want to methodically explore every vertex and every edge. #define #define #define typedef TRUE 1 FALSE 0 MAXVERTICES 100 struct node { int vertex; struct node *link; } *NODEPTR; NODEPTR graph[MAXVERTICES]; int visited[MAXVERTICES]; /* initialized to FALSE */ dfs(int v) /* dfs of graph[] beginning with vertex v */ { NODEPTR w; visited[v] = TRUE; printf("%5d", v); for(w = graph[v]; w; w = w->link) if(!visited[w->vertex]) dfs(w->vertex); }

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications 2 1 u 5

7-17


Exercise 8 (a) Show that the running time is (b) Does the worst case occur on ,

. [Visits each node in the adjacency list at most once.]

, , or ?

(c) Show that the running time is

if the graph is represented by its adjacency matrix.

BFS
typedef struct queue { int vertex; QUEPTR *link; } *QUEPTR; void addq(QUEPTR *, QUEPTR *, int); int deleteq(QUEPTR *); bfs(int v) /* bfs of graph[] beginning with vertex v; */ /* visited[] is initialized to 0. */ { NODEPTR w; QUEPTR front, rear; front = rear = NULL; visited[v] = TRUE; printf("%5d", v); addq(&front, &rear, v); while(front) { v = deleteq(&front); for(w = graph[v]; w; w = w->link) if(!visited[w->vertex]) { printf("%5d", w->vertex); addq(&front, &rear, w->vertex); visited[w->vertex] = TRUE;

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications } } } 2 1 u 5

7-18


Finds distance of any vertex from the source vertex. Exercise 9 (a) Show that the running time is (b) Does the worst case occur on ,

, , or ?

(c) Show that the running time is

if the graph is represented by its adjacency matrix.

(d) What does the following segment of code do? for(i=0; i<|V|; i++) if(!visited[i]) dfs(i); (e) Can dfs() and bfs() be applied to directed graphs? Can they reach all vertices?

Single-Source Shortest Paths


A large number of optimization problems are mathematically equivalent to nding shortest paths in a graph. Problem: Given a connected directed graph

with a source vertex and a weight function nd a shortest path from to every other vertex (or to a single prespecied vertex). Generalization of BFS to handle weighted graphs. In BFS, for all .
For example, road map with distances on roads between cities.

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications Weight of a path


7-19

is dened as

Shortest path

path of minimum weight.

Claim: Subpaths of a shortest path are shortest paths. Proof [By contradiction]: If some subpath were not a shortest path, we could substitute the shortest subpath and create a shorter total path. Therefore can be solved by greedy algorithms. Dene

as the weight of a shortest path from to .


Theorem 12 (Triangle Inequality)

Note: If there is a negative-weight cycle in Dijkstras algorithm: Let

, then some shortest paths (trails) may not exist.

be the

th nearest vertex to .

Observation: The th nearest vertex to is the vertex

that minimizes


over all . #define MAXVERTICES 100 int cost[][MAXVERTICES]; /* weighted adjacency matrix */ int d[MAXVERTICES]; /* d[i] = d(s, i) */ int found[MAXVERTICES]; int n = MAXVERTICES; dijkstra(int s, int cost[][MAXVERTICES], int d[], int n, int found[]) { int i, u, w; for(i = 0; i < n; i++) { found[i] = FALSE; d[i] = cost[s][i]; }

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications

7-20

found[s] = TRUE; d[s] = 0; for(i = 0; i < n-2; i++) { u = choose(d, n, found); /* u has a min dist from s */ found[u] = TRUE; for(w = 0; w < n; w++) if(!found[w]) if(d[u] + cost[u][w] < d[w]) d[w] = d[u] + cost[u][w]; } } int choose(int d[], int n, int found[]) { int i, min, minpos; min = INFINITY; minpos = -1; for(i = 0; i < n; i++) if(d[i] < min && !found[i]) { min = d[i]; minpos = i; } return minpos; } Running time = . In addition to weights, actual paths can also be constructed. 1. keep an array pred[] with pred[i] = 0 initially; 2. whenever d[w] > d[u] + cost[u][w], set pred[w] = u; s
1

3 1 1 4

1.7

5.3

2
4

1
3

2 s
1

2.3
3

0.4

3
4

2.2

4.5

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

7. Graphs and Their Applications

7-21

All-Pairs Shortest Paths [S, 15.2.4]


Floyd-Warshall algorithm: A dynamic programming algorithm. May use Dijkstras algorithm with source 1, 2, ..., .

shortest path from to length of

time. whose internal vertices

Initially,

otherwise

Observation:

floyd_warshall(int cost[][MAXVERTICES], int d[][MAXVERTICES], int n) { int i, j, k; for(i = 0; i < n; i++) for(j = 0; j < n; j++) d[i][j] = cost[i][j]; for(k = 0; k < n; k++) for(i = 0; i < n; i++) for(j = 0; j < n; j++) if(d[i][k] + d[k][j] < d[i][j]) d[i][j] = d[i][k] + d[k][j]; } Running time = . Exercise 10 (a) Does it allow negative-weight cycles? negative weights but free of negative-weight cycles? (b) How do you construct the paths at the same time?

c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU

00.5

You might also like