Discrete Math for CS Chapter 8: Directed Graphs. Discrete Math for CS digraph: A digraph is a graph...

Preview:

Citation preview

Discrete Math for CS

Chapter 8: Directed Graphs

Discrete Math for CS

digraph:

A digraph is a graph G = (V,E) where V is a finite set of vertices and E is a relation on V.

If a, b ∈ V and aEb then there is a directed edge of E from a to b called an arc.

If u, v ∈ V and (u,v) is an arc we write uv as the arc name.

A simple digraph has no loops and no multiple edges.

If uv is an arc then u is the antecedent of v.

Discrete Math for CS

Example:

a b

dc

V = { a, b, c, d }E = { ab, cb, dc, bd, db }

a b c da 0 1 0 0

b 0 0 0 1

c 0 1 0 0

d 0 1 1 0

Discrete Math for CS

More Notation:

A path of length k in a digraph is a sequence of distinct vertices v

1, v

2, v

3, ..., v

k where v

i-1v

i is an arc for i = 1, ... k.

A cycle is a path where v1 = v

k and no other vertices are the

same.

A graph without cycles is called acyclic.

Directed acyclic graphs (DAGs) are some of the most important graphs.

In task-scheduling problems a DAG is called a PERT chart.

Discrete Math for CS

Example:

A student needs to take 8 courses to satisfy a major. The courses and their prerequisites are given below. Draw a PERT chart showing the order in which the courses can be taken.

Course PrerequisiteBCHCD, GECNone

Advanced biotechnologyBiotechnologyCell biologyDNA structuresEnzyme activityFood scienceGenetic engineeringHuman biology

Discrete Math for CS

Answer:

A

B

C

D E

F

G

H

Discrete Math for CS

Topological Sort Algorithm

A topological sort algorithm produces a consistent labeling of the edges of the above graph.

A labeling 1, 2, 3, ... , n is consisent if uv is an arc, u has label i, v has label j and i < j.

Discrete Math for CS

TSA:

G = (V,E) is a digraph.

Let A(v) = { all antecedents of v }.

begin for v V do calculate A(v); label := 0; while unlabeled vertices v remain for which A(v) = ∅ do begin label := label + 1; u := a vertex with A(u) = ∅ ; assign label to u; for each unlabeled vertex v ∈ V do a(v) := A(v) \ {u} end endend

Discrete Math for CS

Example:

Find a consistent labeling of the previous graph.

Step 0: A(A) = {B}, A(B) = {C}, A(C) = {H}, A(D) = {C}, A(E) = {D, G}, A(F) = {E}, A(G) = {C}, A(H) = ∅.Step 1: Assign label 1 to H since A(H) = ∅. A(A) = {B}, A(B) = {C}, A(C) = ∅, A(D) = {C}, A(E) = {D, G}, A(F) = {E}, A(G) = {C}, A(H) = ∅.Step 2: Assign label 2 to C since A(C) = ∅. A(A) = {B}, A(B) =∅, A(C) = ∅, A(D) =∅, A(E) = {D, G}, A(F) = {E}, A(G) =∅, A(H) = ∅.Step 3: Choose one of the possibilities> Assign label 3 to B. A(A) = ∅, A(B) =∅, A(C) = ∅, A(D) =∅, A(E) = {D, G}, A(F) = {E}, A(G) =∅, A(H) = ∅.Step 4: Assign label 4 to A. A(A) = ∅, A(B) =∅, A(C) = ∅, A(D) =∅, A(E) = {D, G}, A(F) = {E}, A(G) =∅, A(H) = ∅.Step 5: Assign label 5 to D. A(A) = ∅, A(B) =∅, A(C) = ∅, A(D) =∅, A(E) = {G}, A(F) = {E}, A(G) =∅, A(H) = ∅.Step 6: Assign label 6 to G. A(A) = ∅, A(B) =∅, A(C) = ∅, A(D) =∅, A(E) = ∅, A(F) = {E}, A(G) =∅, A(H) = ∅.Step 7: Assign label 7 to E. A(A) = ∅, A(B) =∅, A(C) = ∅, A(D) =∅, A(E) = ∅, A(F) = ∅, A(G) =∅, A(H) = ∅.Step 8: Assign label 8 to F. Consistent Labeling is H, C, B, A, D, G, E, F

Discrete Math for CS

Paths in Digraphs

Directed paths can represent things line airline routes or networked computers.

We may need to know alternative routes if a link goes down.

So we need to know if there exists a path between two vertices of a digraph.

G = (V,E) with n vertices. Let M be the adjacency matrix. If m

ij == 1 then there is an arc from vertex v

i to vertex v

j. An arc is

a path of length 1.

Consider M2. The boolean product of M by M yields a matrix which shows paths of length 2 in the original graph.

Discrete Math for CS

Example:

a b

dc

V = { a, b, c, d }E = { ab, cb, dc, bd, db }

a b c da 0 1 0 0

b 0 0 0 1

c 0 1 0 0

d 0 1 1 0

a b c da 0 0 0 1

b 0 1 1 0

c 0 0 0 1

d 0 1 0 1

M

M2

Discrete Math for CS

Reachability Matrix

Calculate M* = M or M2 or M3 or ... or Mn

M* contains all paths of all lengths so shows what vertices are reachable from what vertices.

The reachability matrix of a graph is the graph of the transitive closure of its adjacency matrix.

Discrete Math for CS

Example:

a b

dc

a b c da 0 1 0 0

b 0 0 1 1

c 0 0 0 0

d 0 0 1 0

a b c da 0 0 1 1

b 0 0 1 0

c 0 0 0 0

d 0 0 0 0

M

M2

a b c da 0 0 1 0

b 0 0 0 0

c 0 0 0 0

d 0 0 0 0

M3

a b c da 0 0 0 0

b 0 0 0 0

c 0 0 0 0

d 0 0 0 0

M4

Discrete Math for CS

Example:

a b

dc

a b c da 0 1 1 1

b 0 0 1 1

c 0 0 0 0

d 0 0 1 0

M*

Discrete Math for CS

Large Matrices

This calculation is labourious for big matrices.

Warshall's Algorithm calculates M* more efficiently.

G = (V,E). with vertices v1, v

2, ..., v

n. Warshall's Algorithm

generates matrices W0 = M, W

1, W

2, ..., W

n.

For k >= 1, Wk(i,j) = 1 iff there is a path of any length from v

i to v

j

where the intermediary vertices in the path lie in the set {v

1, ... v

k}.

Wn = M*.

Warshall's Algorithm is efficient by a clever use of for-loops.

Successive passes of the outer loop calculate W1, W

2, ..., W

n.

Discrete Math for CS

Warshall's Algorithm

G = (V,E). M is the adjacency matrix. Calculates W = M*.

begin W := M; for k = 1 to n do for i = 1 to n do for j = 1 to n do W(i,j) = W(i,j) or W(i,k) and W(k,j);end

Note: On each pass of outer loop the algorithm generates Wk. This is done by

updating entries in Wk-1

. To find ith row of W

k we evaluate

W(i,j) = W(i,j) or W(i,k) and W(k,j); (*)for various values of j. If W(i,k) = 0 then (W(i,k) and W(k,j)) = 0 and so (*) reduces to W(i,j); ie, row i ofthe matrix remains unchanged.Otherwise W(i,k) = 1 and (*) reduces to W(i,j) or W(k,j). In this case row i becomes the logical or of the current row i and current row k.

Discrete Math for CS

Warshall's Algorithm:

So Warshall's Algorithm reduces to calculating Wk from W

k-1 as

follows:

Consider the kth column of Wk.

For each row with a 0 entry in this column, copy the row from W

k-1.

For each row with a 1 entry form the logical or of that row with row k and write the resulting row in W

k.

Discrete Math for CS

Example (Warshall's Algorithm):

1

2 3

4

5

0 1 0 0 00 0 1 0 01 0 0 1 00 0 0 0 01 0 1 0 0

W0

0 1 0 0 00 0 1 0 0

0 0 0 0 0

W1

copy rows 1,2,4

0 1 0 0 00 0 1 0 01 1 0 1 00 0 0 0 0

W1 row 3 or row 1

0 1 0 0 00 0 1 0 01 1 0 1 00 0 0 0 01 1 1 0 0

W1

row 5 or row 1

Discrete Math for CS

Example (Warshall's Algorithm):

1

2 3

4

5

0 1 0 0 00 0 1 0 01 1 0 1 00 0 0 0 01 1 1 0 0

W1

0 0 1 0 0

0 0 0 0 0

W2

0 1 1 0 00 0 1 0 01 1 1 1 00 0 0 0 01 1 1 0 0

W2

Discrete Math for CS

Example (Warshall's Algorithm):

1

2 3

4

5

0 1 1 0 00 0 1 0 01 1 1 1 00 0 0 0 01 1 1 0 0

W2

0 0 0 0 0

W3

1 1 1 1 01 1 1 1 01 1 1 1 00 0 0 0 01 1 1 1 0

W3

Note: W4 = W

3 so we are done.

Discrete Math for CS

Shortest Paths

Find the shortest path between two vertices in a weighted digraph.

Typical situations – transportation networks, communications networks

Discrete Math for CS

Shortest Paths:

A

B C

F

ED

2

1

5

4

3

2

1

Find the shortest path from A to any other vertex.

weight matrix:

w(u,v) = 0 if u = v∞ if uv is not an arcd if uv is an arc of weight d A B C D E F

A 0 2 ∞ 3 ∞ ∞B ∞ 0 1 ∞ 4 ∞C ∞ ∞ 0 ∞ ∞ 5D ∞ ∞ ∞ 0 2 ∞E ∞ ∞ ∞ ∞ 0 1F ∞ ∞ ∞ ∞ ∞ O

Discrete Math for CS

Idea:

Initially define d[v] to be the weight of an arc from A to v.

d[v] = ∞ if there is no arc.

We traverse the vertices and improve d[v] as we go.

We mark a value for d[u] once we know for sure the shortest route to u from A.

For the remaining vertices, w, we assign the min of the current value of d[w] and the distance to w via the last marked vertex, u.

The algorithm terminates once all vertices that can be marked are marked.

Discrete Math for CS

Step Vertex to mark A B C D E F Unmarked0 A 0 2 inf 3 inf inf B,C,D,E,F1 B 0 2 3 3 6 inf C,D,E,F2 D 0 2 3 3 5 inf C,E,F3 C 0 2 3 3 5 8 E,F4 E 0 2 3 3 5 6 F5 F 0 2 3 3 5 6

Step 0: Mark A and let the first row represent the initial values of d[v].

Step 1: Mark B since it is closest to A. Calculate the distances to unmarked vertices via B. If a shorter distance is found, use it.. Vertices not adjacent to the last marked have their d[v] values unchanged.

Step 2: Next mark D (we could mark C too). Calculate the remaining distances

Step 3: Mark C. F can now be accessed.

Step 4 and 5: Mark E and F.

Discrete Math for CS

Dijkstra's Algorithm

G = (V,E) is a weighted digraph, A is a vertex. The algorithm finds the shortest path from A to v as well as d[v].

w(u,v) is the weight of arc uv PATHTO(v) lists the vertices on the shortest path to v from

A. begin for each v in V do begin d[v] = w(A,v); PATHTO(v) := A; end Mark vertex A while unmarked vertices remain do begin u:= unmarked vertex closest to A Mark u; <for-loop> endend

for each unmarked vertex v with uv in E do begin d' := d[u] + w(u,v); if d' < d[v] then d[v] := v'; PATHTO(v) := PATHTO(u), v end end

Discrete Math for CS

Exercise:

Use Dijkstra's Algorithm with the following graph:B E

F

DA

C

10

3

4

12

11 8

2 6

5

Discrete Math for CS

Answer

Step Marked A B C D E F Unmarked0 A 0 3 10 inf inf inf B C D E F1 B 0 3 10 15 inf inf C D E F2 C 0 3 10 15 inf inf D E F3 D 0 3 10 15 17 23 E F4 E 0 3 10 15 17 23 F5 F 0 3 10 15 17 23

Discrete Math for CS