Computer Programming & Principles

Biotechnology

Oct 23, 2013 (4 years and 6 months ago)

83 views

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

2

CS1022

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

Edges are directed

Finding paths require following a direction

3

CS1022

1

5

6

2

3

4

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

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

4

CS1022

a

b

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

5

CS1022

Simple digraphs (2)

Example: digraph
G

(
V
,
E
) where

Vertex set
V

a
,
b
,
c
,
d

Arc set
E

ab
,
bd
,
cb
,
db
,

dc

Graphically:

6

CS1022

a

c

b

d

a

b

c

d

a

b

c

d

Simple digraphs (3)

E

ab
,
bd
,
cb
,
db
,

dc

)
:

7

CS1022

a

b

c

d

a

F

b

c

d

a

b

c

d

a

F

T

F

F

b

c

d

a

b

c

d

a

F

T

F

F

b

F

F

F

T

c

d

a

b

c

d

a

F

T

F

F

b

F

F

F

T

c

F

T

F

F

d

a

b

c

d

a

F

T

F

F

b

F

F

F

T

c

F

T

F

F

d

F

T

T

F

Paths and cycles in digraphs

A
path of length

k

is a

Sequence of vertices
v
0
,
v
1
,

,
v
k

Such that
v
i

1
v
i

is an arc, 1

i

k

Example:
a
,
b
,
d
,
c

is a path

A cycle is a

Sequence of vertices
v
0
,
v
1
,

,
v
k

Such that
v
i

1
v
i

is an arc, 1

i

k

v
0

v
k

(first and last vertices are the same)

v
i

v
j
, 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 graph

8

CS1022

a

c

b

d

PERT chart (1)

Acyclic graphs useful to model situations in which
tasks have to be carried out in a certain order

-
scheduling problems the corresponding
acyclic digraph is known as
PERT chart

P
roject
E
valuation and
R
eview
T
echnique (PERT)

9

CS1022

PERT chart (2)

Suppose (partial) degree programme below

Pre
-
requisites, so order is important

10

CS1022

Module

Pre
-
requisites

A
dvanced

biotechnology

B

B
iotechnology

C

C
ell biology

H

D
NA structures

C

E
nzyme

activities

D
,

G

F
ood science

E

G
enetic

engineering

C

H
uman biology

None

PERT chart (3)

PERT chart shows interdependence of modules

11

CS1022

Module

Pre
-
requisites

A
dvanced

biotechnology

B

B
iotechnology

C

C
ell biology

H

D
NA structures

C

E
nzyme

activities

D
,

G

F
ood science

E

G
enetic

engineering

C

H
uman biology

None

A

B

C

D

E

H

G

F

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

12

CS1022

Topological sort algorithm (2)

Gives consistent labelling of acyclic digraph
G

(
V
,
E
)

Antecedents of each vertex stored in
A
(
v
)

13

CS1022

begin

for

v

V

do

calculate

A
(
v
)
;

label

:= 0;

while

unlabelled vertices
v

remain for which
A
(
v
)

do

begin

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
v
s

end

end

Find consistent labelling for digraph of modules

Step 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
)

begin

for

v

V

do

calculate

A
(
v
)
;

label

:= 0;

while

unl abel l ed verti ces
v

remai n for whi ch
A
(
v
)

do

begin

label

:=

label

+ 1
;

u

:=
a vertex wi th
A
(
u
)

;

assi gn l abel to
u
;

for

each unl abel l ed vertex
v

V

do

A
(
v
) :=

A
(
v
)



{
u
}
;

end

end

Topological sort algorithm (3)

14

CS1022

for

v

V

do

calculate

A
(
v
)
;

A

B

C

D

E

H

G

F

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
}

15

CS1022

...

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
}
;

...

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
)

16

CS1022

...

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
}
;

...

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
)

17

CS1022

...

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
}
;

...

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
)

18

CS1022

...

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
}
;

...

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
)

19

CS1022

...

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
}
;

...

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
}

20

CS1022

...

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
}
;

...

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
)

21

CS1022

...

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
}
;

...

Topological sort algorithm (11)

Step 7

final pass through while loop:

Assign label 8 to
F

a

There are no remaining
v
s

to delete
E

from

22

CS1022

...

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
}
;

...

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

23

CS1022

A

B

C

D

E

H

G

F

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

24

CS1022

R.
Haggarty
. “Discrete Mathematics for
Computing”. Pearson Education Ltd.
2002. (Chapter 8)

Wikipedia’s entry on directed graphs

Wikibooks entry on graph theory

25

CS1022