Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)

Similar presentations


Presentation on theme: "CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)"— Presentation transcript:

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

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

3 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 3 CS

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 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 4 CS1022 ab

5 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

6 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

7 abcd a b c d Simple digraphs (3) Adjacency matrix (set E   ab, bd, cb, db, dc  ): 7 CS1022 abcd aF b c d abcd aFTFF b c d abcd aFTFF bFFFT c d abcd aFTFF bFFFT cFTFF d abcd aFTFF bFFFT cFTFF dFTTF

8 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

9 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) 9 CS1022

10 PERT chart (2) Suppose (partial) degree programme below – Pre-requisites, so order is important 10 CS1022 ModulePre-requisites Advanced biotechnologyB BiotechnologyC Cell biologyH DNA structuresC Enzyme activitiesD, G Food scienceE Genetic engineeringC Human biologyNone

11 PERT chart (3) PERT chart shows interdependence of modules 11 CS1022 ModulePre-requisites Advanced biotechnologyB BiotechnologyC Cell biologyH DNA structuresC Enzyme activitiesD, G Food scienceE Genetic engineeringC Human biologyNone A B C DE H G F

12 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

13 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 vs end

14 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 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}; end Topological sort algorithm (3) 14 CS1022 for v  V do calculate A(v); A B C DE H G F

15 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 CS 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};...

16 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 CS 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};...

17 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 CS 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};...

18 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 CS 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};...

19 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 CS 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};...

20 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 CS 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};...

21 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 CS 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};...

22 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 22 CS 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};...

23 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 DE H G F

24 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

25 Further reading R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd (Chapter 8) Wikipedia’s entry on directed graphs Wikibooks entry on graph theory 25 CS1022


Download ppt "CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)"

Similar presentations


Ads by Google