Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
7-1
Degree of
where
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:
Theorem 2
00.5
7-2
Graph Representation
* Static: 1) adjacency matrix, 2) edge list, 3) edge incidence matrix. * Dynamic: adjacency list. Adjacency matrix:
if otherwise
bits.
2 5 8 6 7 3
A(G)=?
(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
7-3
edges[ ][ ]=?
4
(b) How do you decide what vertices are adjacent to ? in how much time? Incidence matrix:
1 5 8 4
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{
00.5
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
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)
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.
00.5
7-5
Bipartite Graphs
and
where
Complete graph:
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 boys (girls) compatible with . set of boys (girls) compatible with at least one
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
00.5
7-6
(free)
Augmenting path search tree: one which consists of alternating paths emanating from a free vertex (Root).
disconnected.
00.5
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.
* *
* 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
The path ow on N is represented by the quintuple The value of path ow (N,s,t,P,f) is dened as
00.5
7-8
2 ()
1 2 2
()
(a) Let
, and
.
1 2
2 1 1
Therefore,
, 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 = ?
1.
where
; (capacity constraint)
00.5
7-9
. (conservation constraint)
. on
Upper bound:
s N
A cut is dened as
and
It is directional.
is an cut if
is dened as
00.5
7-10
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
be an cut in . Then
Proof:
Consider
, then
c Cheng-Wen Wu, Lab for Reliable Computing (LaRC), EE, NTHU
00.5
7-11
Let If
or .
then contributes opposite terms to the sum of the right hand side, so the terms cancel.
and
when
Proof:
is
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 .
if
00.5
7. Graphs and Their Applications A.k.a. slack. Flow augmenting path (FAP) with respect to edges of ,
7-12
1. 2. 3. 4.
and
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
1,0 s 1,0
1,1 1,1
1,1
1,0
b Theorem 9 Let
1,1
1,1
1,0 t
00.5
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
:
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
.
if it can be labeled.
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
Theorem 11
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
00.5
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; }
00.5
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); }
00.5
7-17
Exercise 8 (a) Show that the running time is (b) Does the worst case occur on ,
, , or ?
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;
00.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 ?
(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?
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.
00.5
7-19
is dened as
Shortest path
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
be the
th nearest vertex to .
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]; }
00.5
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
00.5
7-21
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?
00.5