directed graphs
play

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


  1. DFS in a Directed Graph - Example visited discovery edge D None D A (D,A) F (D,F) E C (F,C) A F B (C,B) E (B,E) C G (E,G) B G Current vertex: C Edges to consider: E Directed Graphs | 31

  2. DFS in a Directed Graph - Example visited discovery edge D None D A (D,A) F (D,F) E C (F,C) A F B (C,B) E (B,E) C G (E,G) B G Current vertex: C Edges to consider: - Finished C, backtracking to F Directed Graphs | 32

  3. DFS in a Directed Graph - Example visited discovery edge D None D A (D,A) F (D,F) E C (F,C) A F B (C,B) E (B,E) C G (E,G) B G Current vertex: F Edges to consider: to D, G Directed Graphs | 33

  4. DFS in a Directed Graph - Example visited discovery edge D None D A (D,A) F (D,F) E C (F,C) A F B (C,B) E (B,E) C G (E,G) B G Current vertex: F Edges to consider: to G Directed Graphs | 34

  5. DFS in a Directed Graph - Example visited discovery edge D None D A (D,A) F (D,F) E C (F,C) A F B (C,B) E (B,E) C G (E,G) B G Current vertex: F Edges to consider: - Finished F, backtracking to D Directed Graphs | 35

  6. DFS in a Directed Graph - Example visited discovery edge D None D A (D,A) F (D,F) E C (F,C) A F B (C,B) E (B,E) C G (E,G) B G Current vertex: D Edges to consider: to G Directed Graphs | 36

  7. DFS in a Directed Graph - Example visited discovery edge D None D A (D,A) F (D,F) E C (F,C) A F B (C,B) E (B,E) C G (E,G) B G Current vertex: D Edges to consider: - Finished D – start vertex – stop. Directed Graphs | 37

  8. DFS Traversal – discovery edges D visited discovery edge D None E A F A (D,A) F (D,F) C (F,C) C B (C,B) B E (B,E) G G (E,G) Directed Graphs | 38

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

  10. 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 # . % to be the subset of vertices of ⃗ • Justification. Consider $ " 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

  11. Graph Class, part 1 Graphs | 41

  12. Graph Class, part 2 Graphs | 42

  13. Depth-First Search in a Directed Graph – Python Implementation Directed Graphs | 43

  14. 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

  15. 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 Testing whether ⃗ 2. $ is strongly connected Computing the set of vertices of ⃗ 3. $ that are reachable from a given vertex % Computing a directed cycle in ⃗ $ , or reporting that ⃗ $ is acyclic 4. Computing the transitive closure of ⃗ $ 5. Directed Graphs | 45

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 4. Compute a Directed Cycle in ⃗ " , or Report that ⃗ " is Acyclic D D A F E A F C C B B G E discovery edge G back edge forward edge cross edge Directed Graphs | 51

  22. 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 " ∗ such that • A transitive closure of a directed graph ⃗ " is itself a directed graph ⃗ " ∗ are the same vertices of ⃗ - the 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

  23. 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

  24. 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

  25. 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 ⃗ " , & ' , & ) , … , & + i - Compute the rounds, starting with round 1 - At round , , construct a directed graph ⃗ " - starting j with ⃗ " - = ⃗ " -/' , and adding to ⃗ " - the directed edge (& 1 , & 2 ) if ⃗ " -/' contains both edges (& 1 , & - ) and (& - , & 2 ) . k • This method of computing the transitive closure of a digraph is known as the Floyd-Warshall algorithm Directed Graphs | 55

  26. Floyd-Warshall Algorithm - Pseudocode Directed Graphs | 56

  27. 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 of repeated DFS when the graph is dense Directed Graphs | 57

  28. Floyd-Warshall Algorithm - Example i k j ! ( G ! % D B F ! # ! ' C A ! $ E ! " ! & * = ⃗ ⃗ * , Directed Graphs | 58

  29. Floyd-Warshall Algorithm - Example i k j 1 1 1 i=j, continue ! ( G ! % D B F ! # ! ' C A ! $ E ! " ! & ⃗ * " Directed Graphs | 59

  30. Floyd-Warshall Algorithm - Example i k j 1 1 1 2 j=k, continue ! ( G ! % D B F ! # ! ' C A ! $ E ! " ! & ⃗ * " Directed Graphs | 60

  31. Floyd-Warshall Algorithm - Example i k j 1 1 1 2 2 i=j, continue ! ( G ! % D B F ! # ! ' C A ! $ E ! " ! & ⃗ * " Directed Graphs | 61

  32. Floyd-Warshall Algorithm - Example i k j 1 1 1 2 2 ! ( + ≠ - ≠ . 3 G ! % D B F ! # Does ⃗ * have the edges (! # , ! " ) ! ' and (! " , ! $ ) ? No, it doesn’t have either, continue. C A ! $ E ! " ! & ⃗ * " Directed Graphs | 62

  33. Floyd-Warshall Algorithm - Example i k j 1 1 1 2 2 ! ( 3 G 4 + ≠ - ≠ . ! % D B F ! # Does ⃗ * have: ! ' - (! # , ! " ) - no - (! " , ! % ) – yes C Continue. A ! $ E ! " ! & ⃗ * " Directed Graphs | 63

  34. Floyd-Warshall Algorithm - Example i k j 1 1 1 2 2 ! ( 3 G 4 ! % + ≠ - ≠ . 5 D B F ! # ! ' Does ⃗ * have: C - (! # , ! " ) - no - (! " , ! & ) - no A ! $ Continue. E ! " ! & 2-1-6, 2-1-7 will also not add edges because (! # , ! " ) does not exist. ⃗ * " Directed Graphs | 64

  35. Floyd-Warshall Algorithm - Example i k j 1 1 1 + ≠ - ≠ . 2 2 ! ( 3 G ! % D B F ! # ! ' Does ⃗ * have: C - (! $ , ! " ) - yes - (! " , ! # ) - no A ! $ Continue. E ! " ! & ⃗ * " Directed Graphs | 65

  36. Floyd-Warshall Algorithm - Example i k j 1 1 1 2 2 ! ( + = -, 3 3 G continue ! % D B F ! # ! ' C A ! $ E ! " ! & ⃗ * " Directed Graphs | 66

  37. Floyd-Warshall Algorithm - Example i k j 1 1 1 2 2 ! ( 3 3 G / ≠ 1 ≠ 2 4 ! % D B F ! # ! ' Does ⃗ * have: C - (! $ , ! " ) - yes - (! " , ! % ) - yes A ! $ E ! " A direct edge (! $ , ! % ) already ! & exists, continue. ⃗ * " Directed Graphs | 67

  38. Floyd-Warshall Algorithm - Example i k j 1 1 1 2 2 ! ( 3 3 G 4 ! % 5 / ≠ 1 ≠ 2 D B F ! # ! ' Does ⃗ * have: C - (! $ , ! " ) - yes - (! " , ! & ) - no A ! $ E ! " Continue. 3-1-6 and 3-1-7 wont ! & add edges, since 1-6 and 1-7 do not exist. ⃗ * " Directed Graphs | 68

  39. Floyd-Warshall Algorithm - Example i k j 1 1 1 2 2 ! ( 3 3 G 4 4 ! % 5 D B F 6 ! # ! ' 7 No edges added starting with C 4-1. A ! $ E ! " ! & ⃗ * " Directed Graphs | 69

  40. Floyd-Warshall Algorithm - Example i k j 1 1 1 2 2 ! ( 3 3 G 4 4 ! % 5 5 D B F 6 ! # ! ' 7 The edge 5-1-4 can be added, C a direct edge from 5 to 4 does not exist. A ! $ E ! " ! & ⃗ * " Directed Graphs | 70

  41. Floyd-Warshall Algorithm - Example i k j 1 1 1 2 2 ! ( 3 3 G 4 4 ! % 5 5 D B F 6 6 ! # ! ' 7 No edges added starting with C 6-1. A ! $ E ! " ! & ⃗ * " Directed Graphs | 71

  42. Floyd-Warshall Algorithm - Example i k j 1 1 1 2 2 ! ( 3 3 G 4 4 ! % 5 5 D B F 6 6 ! # ! ' 7 7 No edges added starting with C 7-1. A ! $ E ! " ! & ⃗ * " Directed Graphs | 72

  43. Floyd-Warshall Algorithm - Example i k j 1 1 1 2 2 ! ( 3 G 4 ! % 5 D B F 6 ! # ! ' 7 The are no outgoing edges for C ! # - so no edges are added to the transitive closure. A ! $ E ! " ! & ⃗ * # Directed Graphs | 73

  44. Floyd-Warshall Algorithm - Example i k j 1 1 1 2 2 2 ! ( 3 3 3 G 4 4 ! % 5 5 D B F 6 6 ! # ! ' 7 7 4-3-1, add edge from 4 to 1 C 4-3-2, add edge from 4 to 2 5-3-1, a direct 5-1 edge exists A ! $ 5-3-2, add edge from 5 to 2 E ! " 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 ⃗ * $ Directed Graphs | 74

  45. Floyd-Warshall Algorithm - Example i k j 1 1 1 2 2 2 ! ( 3 3 3 G 4 4 4 ! % 5 5 D B F 6 6 ! # ! ' 7 7 1-4-2, add edge from 1 to 2 C 1-4-3, add edge from 1 to 3 3-4-1, a direct 3-1 edge exists A ! $ 3-4-2, a direct 3-2 edge exists E 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 Directed Graphs | 75

  46. Floyd-Warshall Algorithm - Example i k j 1 1 1 2 2 2 ! ( 3 3 3 G 4 4 4 ! % 5 5 5 D B F 6 6 ! # ! ' 7 7 6-5-1, a direct 6-1 edge exists C 6-5-2, a direct 6-2 edge exists 6-5-3, a direct 6-3 edge exists A ! $ 6-5-4, a direct 6-4 edge exists E 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. Directed Graphs | 76

  47. Floyd-Warshall Algorithm – Python Implementation Directed Graphs | 77

  48. BFS in a Directed Graph Directed Graphs | 78

  49. BFS in a Directed Graph - Example visited discovery edge D None D E A F C B G Current vertex: D Edges to consider: to A, F, 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 Directed Graphs | 79

  50. BFS in a Directed Graph - Example visited discovery edge Level 0 D None D A (D,A) F (D,F) E G (D,G) A F C B G Current level: D Edges to consider: to A, F, 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 Directed Graphs | 80

  51. BFS in a Directed Graph - Example visited discovery edge Level 0 D None D A (D,A) F (D,F) E G (D,G) A F C (F,C) B (G,B) C B G Current level: A, F, G Edges to consider: (F, A), (F,C), (F,D), Level 1 (F,G), (G,B), (G,C) Directed Graphs | 81

  52. BFS in a Directed Graph - Example visited discovery edge Level 0 D None D A (D,A) F (D,F) E G (D,G) A F C (F,C) B (G,B) C E (B, E) B G Current level: B, C Level 2 Edges to consider: (B,E), (C, A), (C,B), Level 1 (C,E) Directed Graphs | 82

  53. BFS in a Directed Graph - Example visited discovery edge Level 0 D None D A (D,A) F (D,F) E G (D,G) A F C (F,C) Level 3 B (G,B) C E (B, E) B G Current level: E Level 2 Edges to consider: (E,C), (E,G) Level 1 No new nodes for next level, BFS stop. Directed Graphs | 83

  54. Topological Ordering in Directed Acyclic Graphs (DAGs) Directed Graphs | 84

  55. 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

  56. https://www.xkcd.com/754/ Directed Graphs | 86

  57. Introduction to Introduction to Computational Mathematics Linguistics for Linguists Text Programming 1 Technology Grammar Formalisms Programming 2 Data Structures and Algorithms for CL Parsing Directed Graphs | 87

  58. Introduction to Introduction to Computational Mathematics Linguistics for Linguists A B C Text Programming 1 Technology D Grammar Formalisms F E Programming 2 G H Data Structures and Algorithms for CL Parsing Directed Graphs | 88

  59. 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

  60. 1 4 Introduction to Alterante Topological Orderings (1) Introduction to Computational Mathematics Linguistics for Linguists 2 Text Programming 1 Technology 5 Grammar Formalisms 3 6 Programming 2 Data Structures and Algorithms for CL 7 8 Parsing Directed Graphs | 90

  61. 2 1 Introduction to Alterante Topological Orderings (2) Introduction to Computational Mathematics Linguistics for Linguists 3 Text Programming 1 Technology 4 Grammar Formalisms 6 5 Programming 2 Data Structures and Algorithms for CL 7 8 Parsing Directed Graphs | 91

  62. 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

  63. 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

  64. • topological sorting is an Topological Sorting 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 of vertices that have not been ordered must contain a cycle Directed Graphs | 94

  65. Topological Sorting - Example • in the left box, the current incount of 0 0 each of the vertices in the graph A B • in the right box, the index of the vertex in the topological ordering 1 C D 3 F E 1 2 G 2 H 3 Directed Graphs | 95

  66. Topological Sorting - Example topo ready 0 0 B A B A 1 C D 3 F E 1 2 • len(ready) > 0 == True G 2 H 3 Directed Graphs | 96

  67. Topological Sorting - Example topo ready 0 0 A B A B 1 C D 3 F E 1 2 • pop A from ready, append it to topo • decrease the incount of all G 2 neighbours of A (on outgoing edges): C, D H 3 Directed Graphs | 97

  68. Topological Sorting - Example topo ready 0 1 0 A B A B C 0 C D 2 F E 1 2 • if after the decrease any of the vertices have an incount of 0, add it to ready G 2 • pop C, add it to topo H 3 Directed Graphs | 98

  69. Topological Sorting - Example topo ready 0 1 0 A B A B C E 0 2 C D 1 F E 0 2 • decrease the incount of D, E and H • add E to ready, since its incount is 0 G 2 H 2 Directed Graphs | 99

  70. Topological Sorting - Example topo ready 0 1 0 A B A B C E 0 2 C D 1 F E 0 3 2 • pop E from ready, add it to topo G 2 H 2 Directed Graphs | 100

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend