CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

25
CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Transcript of CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Page 1: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

CS1022 Computer Programming &

Principles

Lecture 8.1Digraphs (1)

Page 2: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Plan of lecture• Digraphs (definition and terminology)• Simple digraphs• Paths and cycles• PERT charts• Topological sort algorithm

2CS1022

Page 3: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Digraphs, again• Directed graphs = digraphs• We have used digraphs to represent relations– We did not define them formally

• Model partial ordering– A before B, A before C, – B before C? C before B?

• Networks of dependences useful for– Data flow analysis– Task scheduling

• Edges are directed– Finding paths require following a direction

3CS1022

1

5

6

2

3

4

Page 4: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

• A digraph is a pair G (V, E) where– V is a finite set of vertices– E is a relation on V

• Visually, a digraph is – A set of labelled vertices with– Directed edges linking pairs of vertices

• Directed edges are elements of E– Pairs of vertices, where the order is important– Also called arcs

• If u, v V are vertices and (u, v) E is an arc– We write simply uv

Directed graphs

4CS1022

a b

Page 5: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Simple digraphs (1)• A simple digraph has no loops or multiple arcs• There is at most one arc uv from u to v

and• There is at most one arc vu from v to u• If uv is an arc then we say u is an antecedent of v

5CS1022

Page 6: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Simple digraphs (2)Example: digraph G (V, E) where• Vertex set V a, b, c, d• Arc set E ab, bd, cb, db, dcGraphically:

6CS1022

a

c

b

d

Page 7: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

a b c dabcd

Simple digraphs (3)Adjacency matrix (set E ab, bd, cb, db, dc):

7CS1022

a b c da Fbcd

a b c da F T F Fbcd

a b c da F T F Fb F F F Tcd

a b c da F T F Fb F F F Tc F T F Fd

a b c da F T F Fb F F F Tc F T F Fd F T T F

Page 8: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Paths and cycles in digraphs• A path of length k is a – Sequence of vertices v0, v1, , vk

– Such that vi – 1vi is an arc, 1 i k– Example: a, b, d, c is a path

• A cycle is a – Sequence of vertices v0, v1, , vk

– Such that vi – 1vi is an arc, 1 i k

– v0 vk (first and last vertices are the same)

– vi vj, 0 i, j k, i 0 or j k (no other repetition)– Example: b, d, c, b is a cycle; a, b, d, c, b, a is not a cycle

• A graph with no cycles in it is an acyclic graph8CS1022

a

c

b

d

Page 9: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

PERT chart (1)• Acyclic graphs useful to model situations in which

tasks have to be carried out in a certain order– A cycle means that a task had to precede itself!

• In task-scheduling problems the corresponding acyclic digraph is known as PERT chart– Project Evaluation and Review Technique (PERT)

9CS1022

Page 10: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

PERT chart (2)• Suppose (partial) degree programme below– Pre-requisites, so order is important

10CS1022

Module Pre-requisitesAdvanced biotechnology BBiotechnology CCell biology HDNA structures CEnzyme activities D, GFood science EGenetic engineering CHuman biology None

Page 11: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

PERT chart (3)• PERT chart shows interdependence of modules

11CS1022

Module Pre-requisitesAdvanced biotechnology BBiotechnology CCell biology HDNA structures CEnzyme activities D, GFood science EGenetic engineering CHuman biology None

A

B

C

D E

H

G

F

Page 12: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Topological sort algorithm (1)• We want to help students find an order of modules– Consistent with pre-requisites

• Classic solution: topological sort algorithm– Consistent labelling for vertices of acyclic digraphs

• Labelling 1, 2, 3, , n of vertices such that– If uv is an arc and– Vertex u has label i, and– Vertex v has label j, then– i j

12CS1022

Page 13: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Topological sort algorithm (2)Gives consistent labelling of acyclic digraph G (V, E) – Antecedents of each vertex stored in A(v)

13CS1022

beginfor v V do

calculate A(v); label := 0; while unlabelled vertices v remain for which A(v) dobegin

label := label + 1;u := a vertex with A(u) ; assign label to u;for each unlabelled vertex v V do

A(v) := A(v) {u}; % delete u from remaining vsend

end

Page 14: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Find consistent labelling for digraph of modulesStep 0 – Antecedent sets are:– A(A) {B}– A(B) {C}– A(C) {H}– A(D) {C}– A(E) {D, G}– A(F) {E}– A(G) {C}– A(H)

beginfor v V do

calculate A(v); label := 0; while unlabelled vertices v remain for which A(v) dobegin

label := label + 1;u := a vertex with A(u) ; assign label to u;for each unlabelled vertex v V do

A(v) := A(v) {u}; end

end

Topological sort algorithm (3)

14CS1022

for v V docalculate A(v);

A

B

C

D E

H

G

F

Page 15: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Topological sort algorithm (4)Step 1 – Enter while loop:– Assign label 1 to H– Delete H from remaining A(v)– A(A) {B}– A(B) {C}– A(C) – A(D) {C}– A(E) {D, G}– A(F) {E}– A(G) {C}

15CS1022

...label := label + 1;u := a vertex with A(u) ; assign label to u;for each unlabelled vertex v V doA(v) := A(v) {u};

...

Page 16: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Topological sort algorithm (5)Step 2 – second pass through while loop:– Assign label 2 to C– Delete C from remaining A(v)– A(A) {B}– A(B) – A(D) – A(E) {D, G}– A(F) {E}– A(G)

16CS1022

...label := label + 1;u := a vertex with A(u) ; assign label to u;for each unlabelled vertex v V doA(v) := A(v) {u};

...

Page 17: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Topological sort algorithm (6)Step 3 – third pass through while loop:– There is a choice of labels to choose from– Each choice leads to distinct consistent labelling– Assign label 3 to B and delete B from remaining A(v)– A(A) – A(D) – A(E) {D, G}– A(F) {E}– A(G)

17CS1022

...label := label + 1;u := a vertex with A(u) ; assign label to u;for each unlabelled vertex v V doA(v) := A(v) {u};

...

Page 18: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Topological sort algorithm (7)Step 4 – fourth pass through while loop:– There is again a choice of labels to choose from– Assign label 4 to A and delete A from remaining A(v)– A(D) – A(E) {D, G}– A(F) {E}– A(G)

18CS1022

...label := label + 1;u := a vertex with A(u) ; assign label to u;for each unlabelled vertex v V doA(v) := A(v) {u};

...

Page 19: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Topological sort algorithm (8)Step 5 – fifth pass through while loop:– Assign label 5 to D and delete D from remaining A(v)– A(E) {G}– A(F) {E}– A(G)

19CS1022

...label := label + 1;u := a vertex with A(u) ; assign label to u;for each unlabelled vertex v V doA(v) := A(v) {u};

...

Page 20: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Topological sort algorithm (9)Step 6 – sixth pass through while loop:– Assign label 6 to G and delete G from remaining A(v)– A(E) – A(F) {E}

20CS1022

...label := label + 1;u := a vertex with A(u) ; assign label to u;for each unlabelled vertex v V doA(v) := A(v) {u};

...

Page 21: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Topological sort algorithm (10)Step 7 – seventh pass through while loop:– Assign label 7 to E and delete E from remaining A(v)– A(F)

21CS1022

...label := label + 1;u := a vertex with A(u) ; assign label to u;for each unlabelled vertex v V doA(v) := A(v) {u};

...

Page 22: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Topological sort algorithm (11)Step 7 – final pass through while loop:– Assign label 8 to F a– There are no remaining vs to delete E from

22CS1022

...label := label + 1;u := a vertex with A(u) ; assign label to u;for each unlabelled vertex v V doA(v) := A(v) {u};

...

Page 23: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Topological sort algorithm (12)• Algorithm found one possible consistent labelling:

H, C, B, A, D, G, E, F• This gives an order in which modules can be taken– Consistent with pre-requisites

23CS1022

A

B

C

D E

H

G

F

Page 24: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Some remarks• Algorithm analysed a graph and ordered vertices– “Sort” vertices based on incidence of arcs

• Approach was exhaustive...– However, it did not try all traversals of the digraph– It relied on visiting vertices (labelling them) in some

order• Why should you care?– If you ever need to perform similar process you can (you

should!) re-use the algorithm– Algorithm can be implemented in different languages

24CS1022

Page 25: CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Further reading• R. Haggarty. “Discrete Mathematics for

Computing”. Pearson Education Ltd. 2002. (Chapter 8)

• Wikipedia’s entry on directed graphs• Wikibooks entry on graph theory

25CS1022