Directed Graphs Data Structures and Algorithms for CL III, WS - - PowerPoint PPT Presentation

directed graphs
SMART_READER_LITE
LIVE PREVIEW

Directed Graphs Data Structures and Algorithms for CL III, WS - - PowerPoint PPT Presentation

Department of General and Computational Linguistics Directed Graphs Data Structures and Algorithms for CL III, WS 2019-2020 Corina Dima corina.dima@uni-tuebingen.de M ICHAEL G OODRICH Data Structures & Algorithms in Python R OBERTO T


slide-1
SLIDE 1

Corina Dima corina.dima@uni-tuebingen.de

Department of General and Computational Linguistics

Data Structures and Algorithms for CL III, WS 2019-2020

Directed Graphs

slide-2
SLIDE 2

Directed Graphs | 2

Data Structures & Algorithms in Python

MICHAEL GOODRICH ROBERTO TAMASSIA MICHAEL GOLDWASSER

14.1-3 Digraph Properties and Traversals 14.4 Transitive Closure 14.5 Topological Sorting

slide-3
SLIDE 3

Directed Graphs

  • A directed graph or digraph ! is a set " of vertices

– together with a collection # of pairwise connections between vertices from ", called edges where all the edges in the graph are directed

  • An edge e = (', )) is directed from ' to ) if the pair

(', )) is ordered, with ' preceding )

  • The first endpoint of a directed edge is called the
  • rigin, and the second endpoint is called the

destination of the edge

  • ' is the origin, ) is the destination of edge ,

Directed Graphs | 3

u v a c b e

slide-4
SLIDE 4

Directed Graphs - Terminology

  • the outgoing edges of a vertex are the edges whose origin is

that vertex

  • The outgoing edges of vertex C are 1 and 2
  • The incoming edges of a vertex are the edges whose

destination is that vertex

  • The incoming edge of vertex C is 6
  • [ug] Two vertices ! and " are adjacent if there is an edge

whose end vertices are ! and "

  • [ug] An edge is called incident to a vertex if the vertex is one of

the edge’s endpoints

  • [ug] The degree of a vertex, deg("), is the number of incident

edges of "

  • The in-degree and out-degree of a vertex " are the number of

incoming and outgoing edges of ", - indeg("), outdeg(")

  • indeg(C) = 1, outdeg(C) = 2

Directed Graphs | 4

A C E B D

1 2 3 6 8 5 4 7

slide-5
SLIDE 5

Directed Graph – Terminology (cont’d)

  • A directed path is a path such that all edges are directed

and are traversed along their direction

  • " = (%, 7, (, 4, *, 3, ,) is a directed simple path
  • A directed cycle is a cycle such that all edges are

directed and are traversed along their direction

  • . = ,, 8, %, 6, ., 1, , is a directed simple cycle

Directed Graphs | 5

A C E B D

1 2 3 6 8 5 4 7

P C

slide-6
SLIDE 6

Directed Graph – Terminology (cont’d)

  • A directed graph is called acyclic if it has

no directed cycles

# is an acyclic graph

  • A directed graph is strongly connected if

for any two vertices $ and % of ⃗ #, $ reaches % and % reaches $

  • & is a strongly connected graph

Directed Graphs | 6

G C E F D B A ⃗ # G C E B &

slide-7
SLIDE 7

Directed Graph Properties

  • Property 1. If ⃗

" is a directed graph with # edges and vertex set $, then %

&∈(

indeg(/) = %

& ∈(

  • utdeg / = #
  • Justification. In a directed graph each edge (5, /) contributes:
  • One unit to the out-degree of its origin 5
  • One unit to the in-degree of its destination /
  • The total contribution is equal to the number of edges

Directed Graphs | 7

slide-8
SLIDE 8

Directed Graph Properties (cont’d)

  • Property 2. If ⃗

" is a simple directed graph with # vertices and $ edges, then $ ≤ #(# − 1)

  • Justification. The graph is simple → it has no parallel edges or self-loops.
  • No two edges can have the same origin and destination
  • There are no self-loops (edges with the same origin and destination)
  • Therefore the maximum degree of a vertex is n-1
  • It follows from property 1 that $ ≤ #(# − 1)

Directed Graphs | 8

slide-9
SLIDE 9

The Graph ADT

Graphs | 9

slide-10
SLIDE 10

The Graph ADT – for directed graphs

  • A graph is a collection of vertices and edges
  • Can be modelled as a combination of three data types: Vertex, Edge and Graph
  • class Vertex
  • Lightweight object storing the information provided by the user
  • The element() method provides a way to retrieve the stored information
  • class Edge
  • Another lightweight object storing an associated object - the cost
  • The element() method provides a way to retrieve the cost of the edge
  • endpoints() method: returns a tuple (", $) such that vertex " is the origin of the edge

and vertex $ is the destination

  • opposite(v) method: assuming vertex $ is one endpoint of an edge (either origin or

destination), return the other endpoint

Graphs | 10

slide-11
SLIDE 11

The Graph ADT – for directed graphs (cont’d)

  • class Graph: can be either undirected or directed – flag provided to the constuctor

Graphs | 11

vertex_count()

returns the number of vertices of the graph

vertices()

returns an iteration of all the vertices of the graph

edge_count()

returns the number of edges of the graph

edges()

returns an interation of all the edges of the graph

get_edge(u,v)

returns the edge from vertex ! to vertex ", if one exists, otherwise None

degree(v, out=True)

returns the number of outgoing/incoming edges incident to vertex ", as designated by the optional parameter out

incident_edges(v, out=True)

returns outgoing edges incident to vertex " by default; report incoming edges if out=False

insert_vertex(v, x=None)

create and return a new Vertex storing element #

insert_edge(u,v, x=None)

create and return a new Edge from vertex ! to vertex ", storing #

remove_vertex(v)

remove vertex " and all its incident edges from the graph

remove_edge(e)

remove edge $ from the graph

slide-12
SLIDE 12

Traversals in a Directed Graph

Directed Graphs | 12

slide-13
SLIDE 13

Traversals in a Directed Graph

  • The DFS and BFS techniques presented in the previous lecture for undirected graphs can

be used to perform traversals of directed graphs

  • The difference is that this time the edges can only be traversed from origin to destination,

but not in the opposite direction

  • As in the undirected graphs case, traversal algorithms can solve interesting problems

dealing with reachability in a directed graph ⃗ ":

  • Computing a directed path from vertex # to vertex $, or report that no such path exists
  • Finding all the vertices of ⃗

" that are reachable from a given vertex %

  • Determine whether ⃗

" is acyclic

  • Determine whether ⃗

" is strongly connected

Directed Graphs | 13

slide-14
SLIDE 14

DFS in a Directed Graph

Directed Graphs | 14

slide-15
SLIDE 15

DFS in a Directed Graph - Example

Directed Graphs | 15

G C E F D B A

visited discovery edge D None

  • Start from vertex D, which is marked as visited (red)
  • Assume that the outgoing edges of a vertex are considered in

alphabetical order – e.g. for D: A, F, G Current vertex: D Edges to consider: to A, F, G

slide-16
SLIDE 16

DFS in a Directed Graph - Example

Directed Graphs | 16

G C E F D B A

visited discovery edge D None A (D,A) Current vertex: D Edges to consider: to F, G

slide-17
SLIDE 17

DFS in a Directed Graph - Example

Directed Graphs | 17

G C E F D B A

visited discovery edge D None A (D,A) Current vertex: A Edges to consider: - (no

  • utgoing edges)

Finished A, backtrack to D

slide-18
SLIDE 18

DFS in a Directed Graph - Example

Directed Graphs | 18

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) Current vertex: D Edges to consider: to F, G

slide-19
SLIDE 19

DFS in a Directed Graph - Example

Directed Graphs | 19

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) Current vertex: F Edges to consider: to A, C, D, G

slide-20
SLIDE 20

DFS in a Directed Graph - Example

Directed Graphs | 20

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) Current vertex: F Edges to consider: to C, D, G

slide-21
SLIDE 21

DFS in a Directed Graph - Example

Directed Graphs | 21

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) Current vertex: C Edges to consider: to A, B, E

slide-22
SLIDE 22

DFS in a Directed Graph - Example

Directed Graphs | 22

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) Current vertex: C Edges to consider: to B, E

slide-23
SLIDE 23

DFS in a Directed Graph - Example

Directed Graphs | 23

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) Current vertex: B Edges to consider: to E

slide-24
SLIDE 24

DFS in a Directed Graph - Example

Directed Graphs | 24

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) Current vertex: E Edges to consider: to C, G

slide-25
SLIDE 25

DFS in a Directed Graph - Example

Directed Graphs | 25

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) G (E,G) Current vertex: E Edges to consider: to G

slide-26
SLIDE 26

DFS in a Directed Graph - Example

Directed Graphs | 26

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) G (E,G) Current vertex: G Edges to consider: to B, C

slide-27
SLIDE 27

DFS in a Directed Graph - Example

Directed Graphs | 27

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) G (E,G) Current vertex: G Edges to consider: to C

slide-28
SLIDE 28

DFS in a Directed Graph - Example

Directed Graphs | 28

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) G (E,G) Current vertex: G Edges to consider: - Finished G, backtracking to E

slide-29
SLIDE 29

DFS in a Directed Graph - Example

Directed Graphs | 29

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) G (E,G) Current vertex: E Edges to consider: - Finished E, backtracking to B

slide-30
SLIDE 30

DFS in a Directed Graph - Example

Directed Graphs | 30

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) G (E,G) Current vertex: B Edges to consider: - Finished B, backtracking to C

slide-31
SLIDE 31

DFS in a Directed Graph - Example

Directed Graphs | 31

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) G (E,G) Current vertex: C Edges to consider: E

slide-32
SLIDE 32

DFS in a Directed Graph - Example

Directed Graphs | 32

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) G (E,G) Current vertex: C Edges to consider: - Finished C, backtracking to F

slide-33
SLIDE 33

DFS in a Directed Graph - Example

Directed Graphs | 33

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) G (E,G) Current vertex: F Edges to consider: to D, G

slide-34
SLIDE 34

DFS in a Directed Graph - Example

Directed Graphs | 34

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) G (E,G) Current vertex: F Edges to consider: to G

slide-35
SLIDE 35

DFS in a Directed Graph - Example

Directed Graphs | 35

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) G (E,G) Current vertex: F Edges to consider: - Finished F, backtracking to D

slide-36
SLIDE 36

DFS in a Directed Graph - Example

Directed Graphs | 36

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) G (E,G) Current vertex: D Edges to consider: to G

slide-37
SLIDE 37

DFS in a Directed Graph - Example

Directed Graphs | 37

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) G (E,G) Current vertex: D Edges to consider: - Finished D – start vertex – stop.

slide-38
SLIDE 38

DFS Traversal – discovery edges

Directed Graphs | 38

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) G (E,G)

slide-39
SLIDE 39

DFS Tree

Directed Graphs | 39

G C E F D B A D A F C B E G

discovery edge back edge (connects a vertex to an ancestor in the DFS tree) forward edge (connects a vertex to a descendant in the DFS tree) cross edge (connects a vertex to another vertex that is neither its ancestor nor its descendant) Nontree edges visited discovery edge D None A (D,A) F (D,F) C (F,C) B (C,B) E (B,E) G (E,G) Tree edge

slide-40
SLIDE 40

Properties of a DFS in a Digraph

  • Proposition. A depth-first search in a directed graph ⃗

" starting at a vertex # visits all the vertices of ⃗ " that are reachable from #. Also, the DFS tree contains directed paths from # to every vertex reachable from #.

  • Justification. Consider $

% to be the subset of vertices of ⃗

" visited by a DFS starting at #. Need to show that $

% contains # and every vertex reachable from #.

  • Suppose that there is a vertex & reachable from # that is not in $

%

  • Consider a directed path from # to & and let ((, *) be the first edge on this path that

goes out of $

% → ( ∈ $ %, v ∉ $ %

  • When DFS reaches (, all outgoing edges of ( are explored – thus it must also reach *

→ then * ∈ $

% (contradiction)

  • Second property – induction: each time a discovery edge ((, *) is identified, since (

was previously discovered, there exists a directed path from # to (; by appending the discovery edge to the existing path, a directed path from # to * is obtained

Directed Graphs | 40

slide-41
SLIDE 41

Graph Class, part 1

Graphs | 41

slide-42
SLIDE 42

Graph Class, part 2

Graphs | 42

slide-43
SLIDE 43

Depth-First Search in a Directed Graph – Python Implementation

Directed Graphs | 43

slide-44
SLIDE 44

DFS in a Directed Graph – Running Time

  • Consider ⃗

", a directed graph with # vertices and $ edges. A DFS traversal of ⃗ " can be performed in %(# + $) time.

  • provided the graph is represented using a data structure where the incident edges of

a vertex (both incoming and outgoing) can be iterated in %(deg , ) time, and finding the opposite vertex takes %(1) time

  • The DFS procedure will be called at most once for every vertex of the graph
  • Each edge will be examined at most once in a directed graph, from its origin vertex

Directed Graphs | 44

slide-45
SLIDE 45

Problems Solved using a DFS Traversal in a Directed Graph

1. Computing a directed path from vertex ! to vertex ", or report that no such path exists 2. Testing whether ⃗ $ is strongly connected 3. Computing the set of vertices of ⃗ $ that are reachable from a given vertex % 4. Computing a directed cycle in ⃗ $, or reporting that ⃗ $ is acyclic 5. Computing the transitive closure of ⃗ $

Directed Graphs | 45

slide-46
SLIDE 46
  • 1. Compute a Directed Path from ! to "
  • Assume DFS was performed for the digraph
  • Exactly the same algorithm as in the undirected case - build the path from end to start

Directed Graphs | 46

slide-47
SLIDE 47
  • 2. Testing whether ! is strongly connected
  • That is, if for every pair of vertices " and #, " reaches # and # reaches "
  • Idea: start an independent DFS traversal from each vertex of ⃗

%. If the discovered dictionary of every of these independent DFS traversals has length & (the number of vertices), then ⃗ % is strongly connected

  • Running time: ?

Directed Graphs | 47

slide-48
SLIDE 48
  • 2. Testing whether ! is strongly connected
  • That is, if for every pair of vertices " and #, " reaches # and # reaches "
  • Idea: start an independent DFS traversal from each vertex of ⃗

%. If the discovered dictionary of every of these independent DFS traversals has length & (the number of vertices), then ⃗ % is strongly connected

  • Running time: '(& & + * ), not that great
  • Better idea:
  • Start with doing a DFS from an arbitrary vertex ,.
  • If the discovered dictionary does not contain all the vertices – the digraph is not

strongly connected - stop.

  • Otherwise, construct a copy of the graph ⃗

%, but where the orientation of each edge is

  • reversed. Perform a DFS on the reversed graph. If discovered contains all vertices –

the digraph is strongly connected. Otherwise it is not.

  • Runs in '(& + *) time

Directed Graphs | 48

slide-49
SLIDE 49
  • 3. Computing the Vertices Reachable from a Given Start Vertex !
  • Perform a DFS traversal ⃗

# starting from $

  • The set of vertices reachable from $ are the keys of the discovered dictionary

Directed Graphs | 49

slide-50
SLIDE 50
  • 4. Compute a Directed Cycle in ⃗

", or Report that ⃗ " is Acyclic

  • The DFS procedure was already performed for the graph "
  • A cycle exists if and only if a back edge exists with respect to the DFS traversal of that

graph

  • In a directed graph DFS traversal, there are multiple types of nontree edges: back edges,

forward edges and cross edges

  • When a directed edge is explored, leading to a previously visited vertex, keep track of

whether that vertex is an ancestor of the current vertex

  • To obtain the cycle, take the back edge from the descendant to the ancestor and then

follow DFS tree edges back to the descendant

Directed Graphs | 50

slide-51
SLIDE 51
  • 4. Compute a Directed Cycle in ⃗

", or Report that ⃗ " is Acyclic

Directed Graphs | 51

G C E F D B A D A F C B E G

discovery edge back edge forward edge cross edge

slide-52
SLIDE 52
  • 5. Computing the Transitive Closure of ⃗

"

  • Particular graph applications benefit from being able to answer reachability questions

more efficiently

  • e.g. a service that computes driving destinations from point # to point $; a first step is

to find about if $ can be reached starting from #

  • Precompute a more efficient representation for the graph that can answer such queries,

and then reuse it for all the reachability queries

  • A transitive closure of a directed graph ⃗

" is itself a directed graph ⃗ "∗ such that

  • the vertices of ⃗

"∗ are the same vertices of ⃗ " and

"∗ has an edge ((, *) whenever ⃗ " has a directed path from ( to *, including the case where ((, *) is an edge of the original graph ⃗ "

Directed Graphs | 52

slide-53
SLIDE 53
  • 5. Computing the Transitive Closure of ⃗

": Method A

  • If ⃗

" is a graph with # vertices and $ edges represented as an adjacency list or an adjacency map, then

  • Compute the transitive closure by making # sequential DFS traversals of the graph, one

starting at each vertex

  • E.g. the DFS starting at vertex % will determine all vertices reachable from % – the

transitive closure includes all the edges starting at % to each of the vertices that are reachable from %

  • Thus computing the transitive closure of a digraph using several DFS traversals can be

done in ? time

Directed Graphs | 53

slide-54
SLIDE 54
  • 5. Computing the Transitive Closure of ⃗

": Method A

  • If ⃗

" is a graph with # vertices and $ edges represented as an adjacency list or an adjacency map, then

  • Compute the transitive closure by making # sequential DFS traversals of the graph, one

starting at each vertex

  • E.g. the DFS starting at vertex % will determine all vertices reachable from % – the

transitive closure includes all the edges starting at % to each of the vertices that are reachable from %

  • Thus computing the transitive closure of a digraph using several DFS traversals can be

done in &(# # + $ ) time

  • Remember, the transitive closure is precomputed once and queried many times

Directed Graphs | 54

slide-55
SLIDE 55
  • 5. Computing the Transitive Closure of ⃗

": Method B

  • If ⃗

" is a graph with # vertices and $ edges represented by a data structure that supports O(1) lookup for

get_edge(u,v) (e.g. an adjacency matrix), then

  • Compute the transitive closure of ⃗

" in a series of rounds

  • Initially, ⃗

"%= ⃗ "

  • Define an arbitrary order over the vertices of ⃗

", &', &), … , &+

  • Compute the rounds, starting with round 1
  • At round ,, construct a directed graph ⃗

"- starting with ⃗ "- = ⃗ "-/', and adding to ⃗ "- the directed edge (&1, &2) if ⃗ "-/' contains both edges (&1, &-) and (&-, &2).

  • This method of computing the transitive closure of a

digraph is known as the Floyd-Warshall algorithm

Directed Graphs | 55

k j i

slide-56
SLIDE 56

Floyd-Warshall Algorithm - Pseudocode

Directed Graphs | 56

slide-57
SLIDE 57

Floyd-Warshall Algorithm – Running Time

  • If the data structure supports get_edge and

insert_edge in O(1) time

  • The main loop, indexed by !, is executed "

times

  • The inner loop contains of #("%) pairs of

vertices, for each of which an # 1 computation is performed

  • Total running time of the algorithm: #("()
  • Asymptotically, this is not better than running

DFS " times, which is #("% + "*)

  • Floyd-Warshall matches the asymptotic bounds
  • f repeated DFS when the graph is dense

Directed Graphs | 57

slide-58
SLIDE 58

Floyd-Warshall Algorithm - Example

Directed Graphs | 58

E C D F G A B

!" !# !$ !% !& !' !( ⃗ * = ⃗ *, i k j

slide-59
SLIDE 59

Floyd-Warshall Algorithm - Example

Directed Graphs | 59

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *" i k j 1 1 1 i=j, continue

slide-60
SLIDE 60

Floyd-Warshall Algorithm - Example

Directed Graphs | 60

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *" i k j 1 1 1 2 j=k, continue

slide-61
SLIDE 61

Floyd-Warshall Algorithm - Example

Directed Graphs | 61

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *" i k j 1 1 1 2 2 i=j, continue

slide-62
SLIDE 62

Floyd-Warshall Algorithm - Example

Directed Graphs | 62

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *" i k j 1 1 1 2 2 3 + ≠ - ≠ . Does ⃗ * have the edges (!#, !") and (!", !$)? No, it doesn’t have either, continue.

slide-63
SLIDE 63

Floyd-Warshall Algorithm - Example

Directed Graphs | 63

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *" i k j 1 1 1 2 2 3 4 + ≠ - ≠ . Does ⃗ * have:

  • (!#, !") - no
  • (!", !%) – yes

Continue.

slide-64
SLIDE 64

Floyd-Warshall Algorithm - Example

Directed Graphs | 64

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *" i k j 1 1 1 2 2 3 4 5 + ≠ - ≠ . Does ⃗ * have:

  • (!#, !") - no
  • (!", !&) - no

Continue. 2-1-6, 2-1-7 will also not add edges because (!#, !") does not exist.

slide-65
SLIDE 65

Floyd-Warshall Algorithm - Example

Directed Graphs | 65

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *" i k j 1 1 1 2 2 3 + ≠ - ≠ . Does ⃗ * have:

  • (!$, !") - yes
  • (!", !#) - no

Continue.

slide-66
SLIDE 66

Floyd-Warshall Algorithm - Example

Directed Graphs | 66

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *" i k j 1 1 1 2 2 3 3 + = -, continue

slide-67
SLIDE 67

Floyd-Warshall Algorithm - Example

Directed Graphs | 67

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *" i k j 1 1 1 2 2 3 3 4 Does ⃗ * have:

  • (!$, !") - yes
  • (!", !%) - yes

A direct edge (!$, !%) already exists, continue. / ≠ 1 ≠ 2

slide-68
SLIDE 68

Floyd-Warshall Algorithm - Example

Directed Graphs | 68

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *" i k j 1 1 1 2 2 3 3 4 5 Does ⃗ * have:

  • (!$, !") - yes
  • (!", !&) - no
  • Continue. 3-1-6 and 3-1-7 wont

add edges, since 1-6 and 1-7 do not exist. / ≠ 1 ≠ 2

slide-69
SLIDE 69

Floyd-Warshall Algorithm - Example

Directed Graphs | 69

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *" i k j 1 1 1 2 2 3 3 4 4 5 6 7 No edges added starting with 4-1.

slide-70
SLIDE 70

Floyd-Warshall Algorithm - Example

Directed Graphs | 70

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *" i k j 1 1 1 2 2 3 3 4 4 5 5 6 7 The edge 5-1-4 can be added, a direct edge from 5 to 4 does not exist.

slide-71
SLIDE 71

Floyd-Warshall Algorithm - Example

Directed Graphs | 71

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *" i k j 1 1 1 2 2 3 3 4 4 5 5 6 6 7 No edges added starting with 6-1.

slide-72
SLIDE 72

Floyd-Warshall Algorithm - Example

Directed Graphs | 72

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *" i k j 1 1 1 2 2 3 3 4 4 5 5 6 6 7 7 No edges added starting with 7-1.

slide-73
SLIDE 73

Floyd-Warshall Algorithm - Example

Directed Graphs | 73

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *# i k j 1 1 1 2 2 3 4 5 6 7 The are no outgoing edges for !# - so no edges are added to the transitive closure.

slide-74
SLIDE 74

Floyd-Warshall Algorithm - Example

Directed Graphs | 74

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *$ i k j 1 1 1 2 2 2 3 3 3 4 4 5 5 6 6 7 7 4-3-1, add edge from 4 to 1 4-3-2, add edge from 4 to 2 5-3-1, a direct 5-1 edge exists 5-3-2, add edge from 5 to 2 5-3-4, a direct 5-4 edge exists 6-3-1, add edge from 6 to 1 6-3-2, a direct 6-2 edge exists 6-3-4, add edge from 6 to 4

slide-75
SLIDE 75

Floyd-Warshall Algorithm - Example

Directed Graphs | 75

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *% i k j 1 1 1 2 2 2 3 3 3 4 4 4 5 5 6 6 7 7 1-4-2, add edge from 1 to 2 1-4-3, add edge from 1 to 3 3-4-1, a direct 3-1 edge exists 3-4-2, a direct 3-2 edge exists 5-4-1, a direct 5-1 edge exists 5-4-2, a direct 5-2 edge exists 5-4-3, a direct 5-3 edge exists 6-4-1, a direct 6-1 edge exists 6-4-2, a direct 6-2 edge exists 6-4-3, a direct 6-3 edge exists

slide-76
SLIDE 76

Floyd-Warshall Algorithm - Example

Directed Graphs | 76

E C D F G A B

!" !# !$ !% !& !' !( ⃗ *& i k j 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 7 7 6-5-1, a direct 6-1 edge exists 6-5-2, a direct 6-2 edge exists 6-5-3, a direct 6-3 edge exists 6-5-4, a direct 6-4 edge exists 7-5-1, add edge from 7 to 1 7-5-2, add edge from 7 to 2 7-5-3, add edge from 7 to 3 7-5-4, add edge from 7 to 4 ⃗ *&= ⃗ *'= ⃗ *(, stop.

slide-77
SLIDE 77

Floyd-Warshall Algorithm – Python Implementation

Directed Graphs | 77

slide-78
SLIDE 78

BFS in a Directed Graph

Directed Graphs | 78

slide-79
SLIDE 79

BFS in a Directed Graph - Example

Directed Graphs | 79

G C E F D B A

visited discovery edge D None

  • Start from vertex D, which is marked as visited (red)
  • Assume that the outgoing edges of a vertex are considered in

alphabetical order – e.g. for D: A, F, G Current vertex: D Edges to consider: to A, F, G

slide-80
SLIDE 80

BFS in a Directed Graph - Example

Directed Graphs | 80

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) G (D,G)

  • Start from vertex D, which is marked as visited (red)
  • Assume that the outgoing edges of a vertex are considered in

alphabetical order – e.g. for D: A, F, G Current level: D Edges to consider: to A, F, G Level 0

slide-81
SLIDE 81

BFS in a Directed Graph - Example

Directed Graphs | 81

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) G (D,G) C (F,C) B (G,B) Current level: A, F, G Edges to consider: (F, A), (F,C), (F,D), (F,G), (G,B), (G,C) Level 0 Level 1

slide-82
SLIDE 82

BFS in a Directed Graph - Example

Directed Graphs | 82

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) G (D,G) C (F,C) B (G,B) E (B, E) Current level: B, C Edges to consider: (B,E), (C, A), (C,B), (C,E) Level 0 Level 1 Level 2

slide-83
SLIDE 83

BFS in a Directed Graph - Example

Directed Graphs | 83

G C E F D B A

visited discovery edge D None A (D,A) F (D,F) G (D,G) C (F,C) B (G,B) E (B, E) Current level: E Edges to consider: (E,C), (E,G) No new nodes for next level, BFS stop. Level 0 Level 1 Level 2 Level 3

slide-84
SLIDE 84

Topological Ordering in Directed Acyclic Graphs (DAGs)

Directed Graphs | 84

slide-85
SLIDE 85

Directed Acyclic Graphs (DAGs)

  • directed acyclic graphs are directed graphs without directed cycles
  • DAGs are encountered in many practical applications
  • Prerequisites between the courses for a degree program
  • Inheritance between classes of an object-oriented program
  • Scheduling constrains between the tasks of a project

Directed Graphs | 85

slide-86
SLIDE 86

Directed Graphs | 86

https://www.xkcd.com/754/

slide-87
SLIDE 87

Directed Graphs | 87

Introduction to Computational Linguistics Introduction to Mathematics for Linguists Data Structures and Algorithms for CL Programming 1 Programming 2 Text Technology Grammar Formalisms Parsing

slide-88
SLIDE 88

Directed Graphs | 88

G C E F D B A H

Introduction to Computational Linguistics Introduction to Mathematics for Linguists Data Structures and Algorithms for CL Programming 1 Programming 2 Text Technology Grammar Formalisms Parsing

slide-89
SLIDE 89

Topological Ordering

# is a directed graph with $ vertices

  • A topological ordering of ⃗

# is an ordering %&, %(, … , %* of the vertices of ⃗ # such that for every edge (%,, %-) of ⃗ #, it is the case that / < 1.

  • A topological ordering is an ordering such that any directed path in ⃗

# traverses vertices in an increasing order

  • A directed graph might have more than one topological orderings

Directed Graphs | 89

slide-90
SLIDE 90

Alterante Topological Orderings (1)

Directed Graphs | 90

Introduction to Computational Linguistics Introduction to Mathematics for Linguists Data Structures and Algorithms for CL Programming 1 Programming 2 Text Technology Grammar Formalisms Parsing

1 2 3 4 5 6 7 8

slide-91
SLIDE 91

Alterante Topological Orderings (2)

Directed Graphs | 91

Introduction to Computational Linguistics Introduction to Mathematics for Linguists Data Structures and Algorithms for CL Programming 1 Programming 2 Text Technology Grammar Formalisms Parsing

2 3 6 1 4 5 7 8

slide-92
SLIDE 92

When does a Directed Graph Have a Topological Ordering?

  • Proposition. ⃗

" has a topological ordering if and only if it is acyclic.

  • Justification.
  • ⟹ Suppose ⃗

" is topologically ordered. Assume that ⃗ " has a cycle made of the edges %&', %&) , %&), %&* , … , (%&-.), %&'). But ⃗ " has a topological ordering, meaning that /0 < /2 < ⋯ < /452 < /0 - impossible, therefore ⃗ " must be acyclic.

Directed Graphs | 92

slide-93
SLIDE 93

When does a Directed Graph Have a Topological Ordering?

  • Proposition. ⃗

" has a topological ordering if and only if it is acyclic.

  • Justification.
  • ⟸ Suppose ⃗

" is acyclic. A topological ordering can be built using the following algorithm:

" is acyclic, therefore ⃗ " must have a vertex with no incoming edges, %&

  • if a vertex like %& would not exist, we would eventually encounter a vistied vertex when

tracing a path from the start index - would contradict ⃗ " being acyclic

  • thus by removing %& and its outgoing edges we obtrain another acyclic graph; this graph has,

again, a vertex %' with no incoming edges

  • repeat the process of removing the vertex with no incoming edges until ⃗

" is empty

  • %&, %', … , %* form an ordering of the vertices in ⃗

"; because of how it was constructed, if %+, %, is an edge in ⃗ ", %+ must be deleted before %, can be deleted – thus - < / and %&, %', … , %* is a topological ordering

Directed Graphs | 93

slide-94
SLIDE 94

Topological Sorting

  • topological sorting is an

algorithm for computing a topological ordering of a directed graph

  • incount is a dict, maps
  • vertex ! to
  • number of incoming

edges to ! (excluding those from vertices that have been added to the topological order)

  • also tests if ⃗

# is acyclic: if the algorithm terminates without ordering all the vertices, then the subgraph

  • f vertices that have not

been ordered must contain a cycle

Directed Graphs | 94

slide-95
SLIDE 95

Topological Sorting - Example

Directed Graphs | 95

G C E F D B A H

1 1 2 3 2 3

  • in the left box, the current incount of

each of the vertices in the graph

  • in the right box, the index of the

vertex in the topological ordering

slide-96
SLIDE 96

Topological Sorting - Example

Directed Graphs | 96

G C E F D B A H

1 1 2 3 2 3 topo ready B A

  • len(ready) > 0 == True
slide-97
SLIDE 97

Topological Sorting - Example

Directed Graphs | 97

G C E F D B A H

1 1 2 3 2 3 topo ready A B

  • pop A from ready, append it to

topo

  • decrease the incount of all

neighbours of A (on outgoing edges): C, D

slide-98
SLIDE 98

Topological Sorting - Example

Directed Graphs | 98

G C E F D B A H

0 1 1 2 2 2 3 topo ready A B C

  • if after the decrease any of the

vertices have an incount of 0, add it to ready

  • pop C, add it to topo
slide-99
SLIDE 99

Topological Sorting - Example

Directed Graphs | 99

G C E F D B A H

0 1 0 2 2 1 2 2 topo ready A B C E

  • decrease the incount of D, E and H
  • add E to ready, since its incount is 0
slide-100
SLIDE 100

Topological Sorting - Example

Directed Graphs | 100

G C E F D B A H

0 1 0 2 0 3 2 1 2 2 topo ready A B C E

  • pop E from ready, add it to topo
slide-101
SLIDE 101

Topological Sorting - Example

Directed Graphs | 101

G C E F D B A H

0 1 0 2 0 3 1 1 2 2 topo ready A B C E

  • decrease the incount of G
slide-102
SLIDE 102

Topological Sorting - Example

Directed Graphs | 102

G C E F D B A H

0 1 0 2 0 3 1 1 2 2 topo ready A B C E

  • pop B from ready, add it to topo
slide-103
SLIDE 103

Topological Sorting - Example

Directed Graphs | 103

G C E F D B A H

0 1 0 4 0 2 0 3 1 1 2 2 topo ready A C E B

  • pop B from ready, add it to topo
slide-104
SLIDE 104

Topological Sorting - Example

Directed Graphs | 104

G C E F D B A H

0 1 0 4 0 2 0 3 1 1 2 topo ready A C E B

  • decrease the incount of D and F
slide-105
SLIDE 105

Topological Sorting - Example

Directed Graphs | 105

G C E F D B A H

0 1 0 4 0 2 0 3 1 1 2 topo ready A D C E B

  • add D to ready, since its incount is 0
slide-106
SLIDE 106

Topological Sorting - Example

Directed Graphs | 106

G C E F D B A H

0 1 0 4 0 2 0 3 1 1 2 topo ready A D C E B

  • pop D from ready, add it to topo
slide-107
SLIDE 107

Topological Sorting - Example

Directed Graphs | 107

G C E F D B A H

0 1 0 4 0 2 0 3 1 0 5 1 2 topo ready A C E B D

  • decrement the incount of F
slide-108
SLIDE 108

Topological Sorting - Example

Directed Graphs | 108

G C E F D B A H

0 1 0 4 0 2 0 3 1 0 5 2 topo ready A C E B D

  • decrement the incount of F
slide-109
SLIDE 109

Topological Sorting - Example

Directed Graphs | 109

G C E F D B A H

0 1 0 4 0 2 0 3 1 0 5 2 topo ready A F C E B D

  • add F to ready, its incount is 0
slide-110
SLIDE 110

Topological Sorting - Example

Directed Graphs | 110

G C E F D B A H

0 1 0 4 0 2 0 3 1 0 5 0 6 2 topo ready A C E B D F

  • pop F from ready, add to topo
slide-111
SLIDE 111

Topological Sorting - Example

Directed Graphs | 111

G C E F D B A H

0 1 0 4 0 2 0 3 1 0 5 0 6 2 topo ready A C E B D F

  • decrement the incounts of G and H
slide-112
SLIDE 112

Topological Sorting - Example

Directed Graphs | 112

G C E F D B A H

0 1 0 4 0 2 0 3 0 5 0 6 1 topo ready A C E B D F

  • decrement the incounts of G and H
slide-113
SLIDE 113

Topological Sorting - Example

Directed Graphs | 113

G C E F D B A H

0 1 0 4 0 2 0 3 0 5 0 6 1 topo ready A G C E B D F

  • add G to ready, its incount is 0
slide-114
SLIDE 114

Topological Sorting - Example

Directed Graphs | 114

G C E F D B A H

0 1 0 4 0 2 0 3 0 7 0 5 0 6 1 topo ready A C E B D F G

  • pop G from ready, add to topo
slide-115
SLIDE 115

Topological Sorting - Example

Directed Graphs | 115

G C E F D B A H

0 1 0 4 0 2 0 3 0 7 0 5 0 6 1 topo ready A C E B D F G

  • decrement the incount of H
slide-116
SLIDE 116

Topological Sorting - Example

Directed Graphs | 116

G C E F D B A H

0 1 0 4 0 2 0 3 0 7 0 5 0 6 topo ready A C E B D F G

  • decrement the incount of H
slide-117
SLIDE 117

Topological Sorting - Example

Directed Graphs | 117

G C E F D B A H

0 1 0 4 0 2 0 3 0 7 0 5 0 6 topo ready A H C E B D F G

  • add H to ready, its incount is 0
slide-118
SLIDE 118

Topological Sorting - Example

Directed Graphs | 118

G C E F D B A H

0 1 0 4 0 2 0 3 0 7 0 5 0 6 0 8 topo ready A C E B D F G H

  • pop H from ready, add to topo
  • H has no outgoing nodes
  • ready is empty – stop.
  • the topological ordering of the

vertices is obtained in topo

slide-119
SLIDE 119

Directed Graphs | 119

Introduction to Computational Linguistics Introduction to Mathematics for Linguists Data Structures and Algorithms for CL Programming 1 Programming 2 Text Technology Grammar Formalisms Parsing

topo A C E B D F G H

slide-120
SLIDE 120

Topological Sorting - Performance

  • topological sorting runs in !(# + %) time,

using !(#) auxiliary space

  • it either computes a topological ordering of

⃗ ( or fails to include some vertices – meaning that ⃗ ( has a directed cycle

Directed Graphs | 120

slide-121
SLIDE 121

Thank you.