chapter 6 張啟中. kongsberg bridge problem (1736) a kneiphof a b c d g c d b f e a b c d g e f a...

67
Chapter 6 張張張

Post on 19-Dec-2015

220 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Chapter 6

張啟中

Page 2: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Kongsberg Bridge Problem (1736)

A

Kneiphof

a b

c d gC

D

B f

e

a b

c d

g

e

f

A

B

C

D

Euler’s graph

Page 3: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Applications of Graphs

Analysis of electrical circuits Finding shortest routes Project planning Identification of chemical compounds Statistical mechanics Generics Cybernetics Linguistics Social Sciences

Page 4: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Definitions

A graph G=(V,E), consists of two sets. V is a finite, nonempty set of vertices. V(G) E is a set of pairs of vertices, these pairs are call

edges. V(E) Graphs

Undirected graph (graph) edge (u,v) = (v,u) Directed graph (digraph) edge <u,v> ≠ <v,u>

Page 5: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Example: Graphs

0

3

1 2

0

1

3 4

2

5 6

0

1

2

(a) G1 (b) G2 (c) G3

V(G1) = {0, 1, 2, 3}

E(G1) = {(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)}

V(G2) = {0, 1, 2, 3, 4, 5, 6}

E(G2) = {(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)}

V(G3) = {0, 1, 2}

E(G3) = {<0, 1>, <1, 0>, <1, 2>}

Directed GraphUndirected Graph

Page 6: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Restrictions on Graphs No self edges (self loops)

A graph may not have an edge from a vertex, v, back to itself.

No multigraph A graph may not have multiple occurrences of

same edges.

0

2

1

1

2

3

0

(a) Graph with a self edge (b) Multigraph

Page 7: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Complete Graph The number of distinct unordered pairs (u, v)

with u≠v in a graph with n vertices is n(n-1)/2. Complete graph

Undirected graph an n-vertex graph with exactly n(n-1)/2 edges.

Directed graph an n-vertex graph with exactly n(n-1) edges

K4K3

Page 8: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Adjacent and Incident Undirected graph

If (u, v) is an edge in E(G), vertices u and v are adjacent and the edge (u, v) is the incident on vertices u and v.

Directed graph <u, v> indicates u is adjacent to v and v is adjacent from u. <u, v> is incident to u and v.

0

1

3 4

2

5 6

0

1

2

Page 9: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Subgraph A subgraph of G is a graph G’ such that V(G’) V

(G) and E(G’) E(G).

0

3

1 2

0

3

1 2

0

1 2

0

0

1

2

0

1

2

0

1

2

0

1

2

(5)

(6) (7) (8)

0

3

1 2 (1)(2)

(3)

(4)

Page 10: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Path and Length Path

A path from vertex u to vertex v in graph G is a sequence of vertices u, i1, i2, …, ik, v, such that (u, i1), (i1, i2), …, (ik, v) are edges in E(G).

G’ is directed graph, <u, i1>, <i1, i2>, …, <ik, v> are edges in E(G’).

Length The length of a path is the number of edges on it.

Simple Path A simple path is a path in which all vertices except possibly

the first and last are distinct. A path (0, 1), (1, 3), (3, 2) can be written as 0, 1, 3, 2.

Page 11: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Cycle

A cycle is a simple path in which the first and last vertices are the same.

Similar definitions of path and cycle can be applied to directed graphs.

Page 12: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Example: Path, Simple Path, Cycle

0

1

2

4

3

5

Path 1 (0,1) (1,3) (3,4) (4,2) (2,1) (1,4) (4,5)

Path 2 (0,1) (1,3) (3,4) (4,5)

Path 3 (1,3) (3,4) (4,2) (2,1)

Simple Path Path 2、 Path 3

Circle Path 3

Page 13: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Connected and Connected Component Two vertices u and v are connected in an

undirected graph iff there is a path from u to v (and v to u).

An undirected graph is connected iff for every pair of distinct vertices u and v in V(G) there is a path from u to v in G.

A connected component of an undirected is a maximal connected subgraph.

A tree is a connected acyclic graph

Page 14: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Strongly Connected Graph

A directed graph G is strongly connected iff for every pair of distinct vertices u and v in V(G), there is directed path from u to v and also from v to u.

A strongly connected component is a maximal subgraph that is strongly connected.

Page 15: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Example: Connected and Strongly Connected 0

3

1 2

4

7

5 6

G4

H1H2

0

1

2

0

1 2

Page 16: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Degree of A Vertex Undirected graph

The degree of a vertex is the number of edges incident to that vertex.

Directed graph In-degree

The number of edges for which vertex is the head Out-degree

The number of edges for which the vertex is the tail.

For a graph G with n vertices and e edges, if di is the degree of a vertex i in G, then the number of edges of G is

1

0

2/)(n

iide

Page 17: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Articulation Point

A vertex v of G is an articulation point iff the deletion of v, together with the deletion of all edges incident to v, leaves behind a graph that has at least two connected components.

Page 18: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Biconnected graph and Biconected Component Biconnected graph

A biconnected graph is a connected graph that has no articulation points.

Biconected component A biconnected component of a connected graph G

is a maximal biconnected subgraph H of G. Two biconnected components of the same graph can have

at most one vertex in common. No edge can be in two or more biconnected components. The biconnected components of G partition the edges of

G.

Page 19: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Example: Biconnected

0

1

4

2 3

8

7

6

5

9

(a)A connected graph1,3,5,7 is articulation point

1

4

2 3

7

6

5

0

1

3 5

8

7 7

9

(b) Its biconnected components

Page 20: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Weighted Edges

Very often the edges of a graph have weights associated with them. distance from one vertex to another cost of going from one vertex to an adjacent

vertex. To represent weight, we need additional field,

weight, in each entry. A graph with weighted edges is called a network.

Page 21: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Graph Representations

ADT (Please see book p.336) Adjacency Matrix Adjacency Lists Adjacency Multilists

Page 22: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Graph ADTclass Graph{//Objects: A nonempty set of vertices and a set of undirected edges

where each edge is a pair of verticespublic:Graph(); // Create an empty graphvoid InsertVertex(Vertex v);void InsertEdge(Vertex u, Vertex v);void DeleteVertex(Vertex v);void DeleteEdge(Vertex u, Vertex v);

//if graph has no vertices return TRUEBoolean IsEmpty();

List<List> Adjacent(Vertex v);//return a list of all vertices that are adjacent to v

};

Page 23: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Adjacency Matrix Adjacency Matrix is a two dimensional n×n

array.

0111

1011

1101

1110

3

2

1

0

3210

000

101

010

2

1

0

210

(a) G1 (b) G3

0

3

1 2

0

1

2

Page 24: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Adjacency Matrix

01000000

10100000

01010000

00100000

00000110

00001001

00001001

00000110

7

6

5

4

3

2

1

0

76543210

(c) G4

0

3

1 2

4

7

5 6

G4

H1

H2

Page 25: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Adjacency Lists

3

2

1

0

1

3

3

1

2 0

0 0

0 0

2 0

[0][1][2][3]

0

1 0

2 0 0

[0][1][2]

HeadNodes

HeadNodes

(a) G1

(b) G3

0

3

1 2

0

1

2

Page 26: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Adjacency Lists

2

3

0

1

1 0

0 0

3 0

1 0

[0][1][2][3]

HeadNodes

(c) G4

5 0

6

5

6 0

4 0

7 0

[4][5][6][7]

0

3

1 2

4

7

5 6

G4

H1

H2

Page 27: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Adjacency Lists : Sequential Representation

9 11 13 15 17 18 20 22 23 2 1 3 0 0 3 1 2 5 6 4 5 7 6

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

0

3

1 2

4

7

5 6

G4

H1 H2

Page 28: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Inverse Adjacency Lists

1 0

0 0

1 0

[0]

[1]

[2]

0

1

2

0

1 0

2 0 0

[0][1][2]

Page 29: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Adjacency Lists: Orthogonal List

0

1

2 0

1 0 0

0

0 1 0 0

1 2

1 2 0 0

head nodes (shown twice)

tail column link for headhead row link for tail

0

1

2

Page 30: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Multilists

[0][1][2][3]

0 1 N1 N3

0 2 N2 N3

0 3 0 N4

1 2 N4 N5

1 3 0 N5

2 3 0 0

N0

N1

N2

N3

N4

N5

HeadNodes

edge (0, 1)

edge (0, 2)

edge (0, 3)

edge (1, 2)

edge (1, 3)

edge (2, 3)

The lists are

Vertex 0: N0 -> N1 -> N2

Vertex 1: N0 -> N3 -> N4

Vertex 2: N1 -> N3 -> N5

Vertex 3: N2 -> N4 -> N5

m vertax2vertex1 list1 list20

3

1 2

Page 31: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Graph Operations

Graph Traversals A general operation on a graph G is to visit all

vertices in G that are reachable from a vertex v. Depth-First Search Breadth-First Search

Graph Components Connected Components Spanning Trees Biconnected Components

Page 32: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Depth-First Search0

7

1

3 4

2

5 6

1

0

0

1

1

2

2

3

02

3

5

07

07

07

07

4

04

06

5 06

[0][1][2][3][4][5][6][7]

HeadNodes

0,1,3,7,4,5,2,6

Page 33: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Breadth-First Search0

7

1

3 4

2

5 6

1

0

0

1

1

2

2

3

02

3

5

07

07

07

07

4

04

06

5 06

[0][1][2][3][4][5][6][7]

HeadNodes

0,1,2,3,4,5,6,7

Page 34: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

DFS and BFS Performance Analysis

Representation

Search

adjacency matrix adjacency lists

DFS O(n2) O(e)

BFS O(n2) O(e)

Page 35: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Connected Components

The connected components of a graph may be obtained by making repeated calls to either DFS(v) or BFS(v)

G is represented by adjacency lists, it take O(n+e) to generate all the connected components.

If adjacency matrices are used instead, the time required is O(n2)

Page 36: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Spanning Trees: Complete Graph

Page 37: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

DFS and BFS Spanning Tree

3 4

1 2

5 6

0

7

3 4

1 2

5 6

0

7

(a) DFS (0) spanning tree (b) BFS (0) spanning tree

Page 38: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Proporety of Spanning Trees Any tree consisting solely of edges in G and including all vertices

in G is called a spanning tree A spanning tree is a minimal subgraph, G’, of G such that V(G’) =

V(G), and G’ is connected. (Minimal subgraph is defined as one with the fewest number of edges).

Spanning tree can be obtained by using either a depth-first or a breath-first search.

Any connected graph with n vertices must have at least n-1 edges, and all connected graphs with n – 1 edges are trees. Therefore, a spanning tree has n – 1 edges.

When a nontree edge (v, w) is introduced into any spanning tree T, a cycle is formed.

Question: Do G have spanning trees if G is not connected ?

Page 39: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Find Biconnected Components

The root of the depth-first spanning tree is an articulation point iff it has at least two children.

Any other vertex u is an articulation point iff it has at least one child, w, such that it is not possible to reach an ancestor of u using apath composed solely of w, descendants of w, and a single back edge.

Define low(w) as the lowest depth-first number that can be reached fro w using a path of descendants followed by, at most, one back edge.

Page 40: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Find Biconnected Components

u is an articulation point iff u is either the root of the spanning tree and has two or more children or u is not the root and u has a child w such that low(w) ≥ dfn(u).

Page 41: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Find Biconnected Components

0

1

4

2 3

8

7

6

5

9

1

2

3

4

5

6

7

8

910

3

4

2

1

0

6

7

8

5

9

1

2

3

4

5

6

7

8

10 9

vertex 0 1 2 3 4 5 6 7 8 9

dfn 5 4 3 1 2 6 7 8 10 9

low 5 1 1 1 1 6 6 6 10 9

Page 42: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Minimum-Cost Spanning Trees The cost of a spanning tree of a weighted, un

directed graph is the sum of the costs (weights) of the edges in the spanning tree.

A minimum-cost spanning tree is a spanning tree of lease cost.

Greedy Algorithm Kruskal’s Algorithm Prim’s Algorithm Sollin’s Algorithm

Page 43: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Kruskal’s Algorithm

0

5

1

6

43

2

10

28

14 16

1218

22

25

(a) (b)

24

0

5

1

6

43

2

1014 16

12

22

25

Page 44: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Kruskal’s Algorithm

1. T = {};2. while ((T contains less than n-1) && (E not empty)) {3 Choose an edge (v,w) from E of lowest cost;4. Delete (v,w) from E5. If ((v,w) does not creste a cycle in T) add (v,w) to T else discard (v,w);6. }7 . If (T contains fewer than n-1 edges)8. cout << “no spanning tree” << endl;

Page 45: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Prim’s Algorithm

0

5

1

6

43

2

10

28

14 16

1218

22

2524

(a)(b)

0

5

1

6

43

2

1014 16

12

22

25

Page 46: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Prim’s Algorithm

//Assume that G has at least one vertex1. TV = {}; //start with vertex 0 and no edges2. for (T={}; T contains less than n-1; add (u,v) to T) {3 Let (v,w) be a least-cost edge such that u in TV and v not in TV;4. If (there is no such edge) break; add v to TV;5. }6 . If (T contains fewer than n-1 edges)7. cout << “no spanning tree” << endl;

Page 47: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Sollin’s Algorithm

0

5

1

6

43

2

10

28

14 16

1218

22

2524

(a)(b)

0

5

1

6

43

2

1014 16

12

22

25

Page 48: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Performance Analysis

Algorithm

ItemKruskal Prim Sollin

Time Complexity

O(eloge) O(n2)

Page 49: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Shortest Paths

Single source/all destinations: Nonnegative edges costs.

Single Source/all destinations: General Weights.

All-Pairs Shortest Paths

Page 50: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Single source/all destinations: Nonnegative edges costs

0

3 4

1 2

5

20

50

15

20

10

35

30

315

Path Length

1) 0, 3

2) 0, 3, 4

3) 0, 3, 4, 1

4) 0, 2

10

25

45

45

(a) Graph (b) Shortest paths from 0

Page 51: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Single source/all destinations: Nonnegative edges costs

0

1 2

34

5

67

300

800

1700

1000

1000

1400

1200

1500

1000 250

900

Los Angeles

San Francisco

Denver

New Orleans Miami

New York

BostonChicago

001700

100000

1400900010000

250015000

01200

08001000

0300

0

7

6

5

4

3

2

1

0

76543210

Page 52: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Single source/all destinations: Nonnegative edges costs

iteration SVertex selected

Distance

LA SF DEN CHI BOST NY MIA NO

[0] [1] [2] [3] [4] [5] [6] [7]

Initial -- --- +∞ +∞ +∞ 1500 0 250 +∞ +∞

1 {4} 5 +∞ +∞ +∞ 1250 0 250 1150 1650

2 {4,5} 6 +∞ +∞ +∞ 1250 0 250 1150 1650

3 {4,5,6} 3 +∞ +∞ 2450 1250 0 250 1150 1650

4 {4,5,6,3} 7 3350 +∞ 2450 1250 0 250 1150 1650

5 {4,5,6,3,7} 2 3350 3250 2450 1250 0 250 1150 1650

6 {4,5,6,3,7,2} 1 3350 3250 2450 1250 0 250 1150 1650

{4,5,6,3,7,2,1}

Page 53: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Single Source/All Destinations: General Weights When negative edge lengths are permitted, we

require that the graph have no cycles of negative length.

0 1 2

5

7 -5

0 1 2

-2

1 1

(a) Directed graph with a negative-length edge

(b) Directed graph with a cycle of negative length

Page 54: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Single Source/All Destinations: General Weights

0

1

2

3

4

5

6

(a) A directed graph

kdistk[7]

0 1 2 3 4 5 6

1 0 6 5 5 ∞ ∞ ∞

2 0 3 3 5 5 4 ∞

3 0 1 3 5 2 4 7

4 0 1 3 5 0 4 5

5 0 1 3 5 0 4 3

6 0 1 3 5 0 4 3

(b) distk

6

5

5

-2

-2

-1

-1

1

3

3

Page 55: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

All-Pairs Shortest Paths

A-1 0 1 2

0 0 4 11

1 6 0 2

2 3 ∞ 0

A0 0 1 2

0 0 4 11

1 6 0 2

2 3 7 0

A1 0 1 2

0 0 4 6

1 6 0 2

2 3 7 0

2

0 1

6

2

4

11

3A2 0 1 2

0 0 4 6

1 5 0 2

2 3 7 0

(b) A-1 (c) A0

(d) A1 (e) A2

Page 56: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Transitive Closure

Definition The transitive closure matrix, denoted A+, of a gra

ph G, is a matrix such that A+[i][j] = 1 if there is a path of length > 0 fromi to j; otherwise, A*[i][j] = 0.

Definition The reflexive transitive closure matrix, denoted A*,

of a graph G, is a matrix such that A*[i][j] = 1 if there is a path of length 0 from i to j; otherwise, A*[i][j] = 0.

Page 57: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Transitive Closure

11100

11100

11100

11100

11110

4

3

2

1

0

43210

00000

10000

01000

00100

00010

4

3

2

1

0

43210

0 1 2 3 4

(a) Digraph G

11100

11100

11100

11110

11111

4

3

2

1

0

43210

(b) Adjacency matrix A

(c) A+(d) A*

Page 58: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Activity Networks

Activity-on-Vertex (AOV) Networks A directed graph G in which the vertices represent

tasks or activities and the edges represent precedence relations between tasks is an activity-on-vertex network or AOV network.

Activity-on-Edge (AOE) Networks A directed graph G in which the edges represent

tasks or activities and the vertices represent events. Events signal the completion of certain activities.

Page 59: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Activity-on-Vertex (AOV) Networks Vertex i in an AOV network G is a predecess

or of vertex j iff there is a directed path from vertex i to vertex j. i is an immediate predecessor of j iff <i, j> is an edge in G. If i is a predecessor of j, then j is an successor of i. If i is an immediate predecessor of j, then j is an immediate successor of i.

Page 60: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Partial Order and Topological Order Partial Order

A relation · is transitive iff it is the case that for all triples, i, j, k, i.j and j·k => i·k. A relation · is irreflexive on a set S if for no element x in S it is the case that x·x. A precedence relation that is both transitive and irreflexive is a partial order.

Topological Order A topological order is a linear ordering of the vertic

es of a graph such that, for any two vertices I and j, if I is a predecessor of j in the network, then i precedes j in the linear ordering.

Page 61: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Example: AOV NetworkCourse number Course name Prerequisites

C1 Programming I None

C2 Discrete Mathematics None

C3 Data Structures C1, C2

C4 Calculus I None

C5 Calculus II C4

C6 Linear Algebra C5

C7 Analysis of Algorithms C3, C6

C8 Assembly Language C3

C9 Operating Systems C7, C8

C10 Programming Languages C7

C11 Compiler Design C10

C12 Artificial Intelligence C7

C13 Computational Theory C7

C14 Parallel Algorithms C13

C15 Numerical Analysis C5

Courses needed for a computer science degree

Page 62: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Example: AOV Network

C1

C2

C3

C5C4 C6 C15

C7

C8

C13

C12

C10

C9

C14

C11

C1, C2, C4, C5, C3, C6, C8, C7, C10, C13, C12, C14, C15 , C11, C9

C4, C5, C2, C1, C6, C3, C8, C15, C7, C9, C10, C11, C12 , C13, C14

Page 63: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Example: Topological Order

0

1

2

3

4

5

1

2

3

4

5

1

2 4

5

1

4

5

1

4 4

(a) Initial (b) Vertex 0 deleted (c) Vertex 3 deleted

(d) Vertex 2 deleted (e) Vertex 5 deleted (f) Vertex 1 deleted

0, 3, 2, 5,1, 4

Page 64: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Topological Sorting Algorithm with Internal Representation

1

4 0

4

5

2

5 0

4 0

[0][1][2][3][4]

0

1

1

1

3 0

2 0[5]

3 0

count first data link

Page 65: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Example: AOE

1

0 4

2

6

8

7

3 5

start finish

a1 = 6 a4 = 6

a2 = 4

a5 = 1

a7= 9

a10 = 2

a3 = 5

a6 = 2

a9 = 4

a11 = 4

event interpretation

0 Start of project

1 Completion of activity a1

4 Completion of activities a4 and a5

7 Completion of activities a8 and a9

8 Completion of project

Page 66: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

AOE Representation with Adjacency Lists

[0][1][2][3][4]

0

1

1

1

3

2[5]

count first vertex

2[6] 2[7] 2 0[8]

1 6

4 1 0

4 1 0

5 2 0

6 9

7 4 0

8 2 0

8 4 0

dur link

2 4 3 5 0

7 7 0

Page 67: Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

Computation Early and Late Activity Time ee [0] [1] [2] [3] [4] [5] [6] [7] [8] Stack

Initial 0 0 0 0 0 0 0 0 0 [0]

output 0 0 6 4 5 0 0 0 0 0 [3,2,1]

output 3 0 6 4 5 0 7 0 0 0 [5,2,1]

output 5 0 6 4 5 0 7 0 11 0 [2,1]

output 2 0 6 4 5 0 7 0 11 0 [1]

output 1 0 6 4 5 5 7 0 11 0 [4]

output 4 0 6 4 5 7 7 0 14 0 [7,6]

output 7 0 6 4 5 7 7 16 14 18 [6]

output 6 0 6 4 5 7 7 16 14 18 [8]

output 8