CS1022 Computer Programming &amp; Principles

1 / 25

# CS1022 Computer Programming &amp; Principles - PowerPoint PPT Presentation

CS1022 Computer Programming &amp; Principles. Lecture 8.1 Digraphs (1). Plan of lecture. Digraphs (definition and terminology) Simple digraphs Paths and cycles PERT charts Topological sort algorithm. Digraphs, again. Directed graphs = digraphs We have used digraphs to represent relations

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'CS1022 Computer Programming &amp; Principles' - shona

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### CS1022Computer Programming & Principles

Lecture 8.1

Digraphs (1)

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

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

1

5

6

2

3

4

CS1022

Directed graphs
• 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

a

b

CS1022

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

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:

b

a

d

c

CS1022

Simple digraphs (3)

Adjacency matrix (set E ab, bd, cb, db, dc):

CS1022

Paths and cycles in digraphs
• A path of lengthk 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, bis a cycle; a, b, d, c, b, ais not a cycle
• A graph with no cycles in it is an acyclic graph

b

a

d

c

CS1022

PERT chart (1)
• Acyclic graphs useful to model situations in which tasks have to be carried out in a certain order
• In task-scheduling problems the corresponding acyclic digraph is known as PERT chart
• Project Evaluation and Review Technique (PERT)

CS1022

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

CS1022

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

A

H

G

F

B

C

D

E

CS1022

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

CS1022

Topological sort algorithm (2)

Gives consistent labelling of acyclic digraph G (V, E)

• Antecedents of each vertex stored in A(v)

CS1022

Topological sort algorithm (3)

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)  

A

H

G

F

B

C

D

E

CS1022

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}

CS1022

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)  

CS1022

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)  

CS1022

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)  

CS1022

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)  

CS1022

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}

CS1022

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)  

CS1022

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

CS1022

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

A

H

G

F

B

C

D

E

CS1022

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

CS1022