Scientific Programming: Part B Graphs Luca Bianco - Academic Year - - PowerPoint PPT Presentation

β–Ά
scientific programming part b
SMART_READER_LITE
LIVE PREVIEW

Scientific Programming: Part B Graphs Luca Bianco - Academic Year - - PowerPoint PPT Presentation

Scientific Programming: Part B Graphs Luca Bianco - Academic Year 2019-20 luca.bianco@fmach.it [credits: thanks to Prof. Alberto Montresor] Graphs: examples http://www.kegg.jp/ [From: Compeau et al, How to apply de Bruijn graphs to genome


slide-1
SLIDE 1

Scientific Programming: Part B

Graphs

Luca Bianco - Academic Year 2019-20 luca.bianco@fmach.it [credits: thanks to Prof. Alberto Montresor]

slide-2
SLIDE 2

Graphs: examples

http://www.kegg.jp/

[From: Compeau et al, How to apply de Bruijn graphs to genome assembly, Nature Biotech,2011]

slide-3
SLIDE 3

Graphs: examples

[From: Compeau et al, How to apply de Bruijn graphs to genome assembly, Nature Biotech,2011]

slide-4
SLIDE 4

Graphs: examples

A 10 actor social network introduced by David Krackhardt to illustrate: degree, betweenness, centrality, closeness, etc. The traditional labeling is: Andre=1, Beverley=2, Carol=3, Diane=4, Ed=5, Fernando=6, Garth=7, Heather=8, Ike=9, Jane=10. [Social Network analysis for startups, "O'Reilly Media, Inc.", 2011] The London underground system

slide-5
SLIDE 5

Graphs

slide-6
SLIDE 6

Graphs

Relations represented by edges can be symmetric (e.g. sibling_of: if π‘Œ is sibling of 𝑍 then 𝑍 is sibling of π‘Œ) and in this case the edges are just lines rather than arrows. In this case the graph is directed. In case relationships are not symmetric (i.e. π‘Œβ†’π‘ does not imply

π‘β†’π‘Œ) we put an arrow to indicate the direction of the relationship among the nodes and in this case we say the graph is undirected.

slide-7
SLIDE 7

Definitions

slide-8
SLIDE 8

Size and complexity

Undirected graph n= 4 m = 6 (=4*3/2) Ignoring self loops

slide-9
SLIDE 9

Size and complexity

Directed graph n= 4 m = 12 (=16-4) Ignoring self loops

slide-10
SLIDE 10

Some special cases

slide-11
SLIDE 11

Some special cases

slide-12
SLIDE 12

Degree

slide-13
SLIDE 13

Random graphs

ErdΓΆs-Renyi (ER) Model Create a network with n nodes connecting them with m (undirected) edges chosen randomly out of the possible n*(n-1)/2 edges. The probability of two random nodes to be connected is: p = 2m / (n *(n βˆ’ 1)) The probability of a node to have a degree k (approx. Poisson):

E-R graph with p=0.01

slide-14
SLIDE 14

Random graphs (1)

Barabasi-Albert (BA) Model Networks grow: nodes are not fixed but grow as a function of time Preferential attachment: the probability that a node gets an edge is proportional to its current degree. Start from a network with n nodes and m edges and add a node at every step, connecting it to p<= N other nodes (with probability depending on their degree). At time T the network will have n+T nodes and m+pT edges. The probability of a node to have a degree k:

slide-15
SLIDE 15

Example: scale free networks

Internet and social relationships

slide-16
SLIDE 16

Definition: Path

slide-17
SLIDE 17

Definition: Path

a,b,c,d is the shortest path from a to d

slide-18
SLIDE 18

Finding paths...

Eulerian Cycle (undirected graphs) Is it possible to walk around the graph in a way that would involve crossing each EDGE exactly

  • nce getting back to start node?

If and only if 0 or 2 nodes have an ODD number of edges YES: DABDCED NO Algorithms exist to find the path in O(n+m)

slide-19
SLIDE 19

Finding paths...

Eulerian Cycle (directed graphs) Is it possible to walk around the graph in a way that would involve crossing each EDGE exactly

  • nce getting back to start node?

If the in-degree and out-degree

  • f all nodes are EQUAL

NO YES: DCACEDABD Algorithms exist to find the path in O(n+m)

slide-20
SLIDE 20

Finding paths...

Hamiltonian Cycle (undirected graphs) Is it possible to walk around the graph in a way that would involve crossing each NODE exactly

  • nce getting back to start node?

YES, if each node has degree >=n/2 (num nodes, n >3) This is a more complex problem. No polynomial solution is currently known! YES: ACBEDA NP-complete problem:

Problems for which there are no polynomial time algorithms known. IF there was one, then all NP problems would be solved polynomially and P would be equal to NP (P=NP). Interestingly, it is easy to check if a solution is correct or not (but it is very hard to find such a solution!).

slide-21
SLIDE 21

Graph ADT

NOTE: sometimes graphs don’t change after being loaded (no delete)

INT

slide-22
SLIDE 22

How can we represent a graph?

slide-23
SLIDE 23

Adjacency matrix

slide-24
SLIDE 24

Adjacency matrix

+ : flexible, can put weights on edges + : quick to check if edge is present (both ways!) + : in undirected graphs, matrix is symmetric (saves half of the space)

  • : in general, it uses a lot of space

(matrix n x n no matter how many edges)

slide-25
SLIDE 25

Adjacency list

slide-26
SLIDE 26

Adjacency list: undirected graph

slide-27
SLIDE 27

Adjacency list

+: flexible, nodes can be complex objects (ex. node1.list_add(node2); ) +: uses less space : checking presence of an edge is in general slower (requires going through the list of source node)

  • : getting all incoming edges of a node is slow

(requires going through all nodes!) Workaround: store another list with all β€œIN”-linking nodes

slide-28
SLIDE 28

Possible implementations

Both the concepts of adjacency matrix and adjacency list can be implemented in several ways. Our simple implementation will use a dictionary

slide-29
SLIDE 29

Graph as adjacency matrix: exercise

Nodes: ['Node_1', 'Node_2', 'Node_3', 'Node_4', 'Node_5', 'Node_6'] Matrix: [[0, 0.5, 0, 0, 0, 1], [0, 0, 0.5, 0, 0, 1], [0, 0, 0, 0.5, 0, 1], [0, 0, 0, 0, 0.5, 1], [0.5, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1]]

Output of print(G):

slide-30
SLIDE 30

Weighted Graph (adj list as a dict of dicts)

for simplicity nodes are strings (can make them objects as an exercise)

slide-31
SLIDE 31

Summary

slide-32
SLIDE 32

Iterating through nodes/edges

Equivalent ways of looping through nodes and edges How much do these operations cost? (n nodes, m edges)

  • Looping through nodes is O(n)
  • Looping through edges is:

β—‹ O(m + n) with adjacency lists and variants β—‹ O(n^2) with adjacency matrices

slide-33
SLIDE 33

Graph traversal

Naive idea, just iterate through the nodes and edges with:

  • r

but this does not take into account the topology of the graph and is still O(n + m) OK in some cases, but not what we are looking for!

slide-34
SLIDE 34

Graph traversal

As in the case of trees, two possible methods:

  • Breadth first search (BFS)
  • Depth first search (DFS)
slide-35
SLIDE 35

Graph traversal

As in the case of trees, two possible methods:

  • Breadth first search (BFS)
  • Depth first search (DFS)

but graphs are more complicated that trees (these are Direct Acyclic Graphs) no matter what, beware of cycles! Hint: mark visited nodes

slide-36
SLIDE 36

Graph traversal: BFS

slide-37
SLIDE 37

BFS, goals

slide-38
SLIDE 38

Graph traversal

  • Warning. Wrong code!!!
slide-39
SLIDE 39

Graph traversal

  • Warning. Wrong code!!!
slide-40
SLIDE 40

Graph traversal

  • Warning. Wrong code!!!
slide-41
SLIDE 41

Graph traversal

  • Warning. Wrong code!!!

even though we can avoid adding elements already in the Queue, this never gets empty! β†’ infinite loop!

slide-42
SLIDE 42

Graph traversal: BFS

visiting: a DFS visit: a Q: ['a'] visited: {'a'} enqueue dequeue

slide-43
SLIDE 43

Graph traversal: BFS

visiting: c visiting: f visiting: e DFS visit: a, c, f, e Q: ['c', 'f', 'e'] visited: {'e', 'f', 'c', 'a'}

a

slide-44
SLIDE 44

Graph traversal: BFS

visiting: b visiting: d DFS visit: a, c, f, e, b, d Q: ['f', 'e', 'b', 'd'] visited: {'d', 'b', 'a', 'c', 'e', 'f'}

c

slide-45
SLIDE 45

Graph traversal: BFS

visiting: g DFS visit: a, c, f, e, b, d, g Q: ['e', 'b', 'd', 'g'] visited: {'d', 'b', 'a', 'g', 'c', 'e', 'f'}

f

slide-46
SLIDE 46

Graph traversal: BFS

visiting: h DFS visit: a, c, f, e, b, d, g, h Q: ['b', 'd', 'g', 'h'] visited: {'d', 'b', 'h', 'a', 'g', 'c', 'e', 'f'}

e

slide-47
SLIDE 47

Graph traversal: BFS

visiting: - DFS visit: a, c, f, e, b, d, g, h Q: [ 'd', 'g', 'h'] visited: {'d', 'b', 'h', 'a', 'g', 'c', 'e', 'f'}

b

slide-48
SLIDE 48

Graph traversal: BFS

visiting: - DFS visit: a, c, f, e, b, d, g, h Q: [ 'g', 'h'] visited: {'d', 'b', 'h', 'a', 'g', 'c', 'e', 'f'}

d

slide-49
SLIDE 49

Graph traversal: BFS

visiting: j DFS visit: a, c, f, e, b, d, g, h, j Q: ['h', 'j'] visited: {'d', 'b', 'j', 'h', 'a', 'g', 'c', 'e', 'f'}

g

slide-50
SLIDE 50

Graph traversal: BFS

visiting: - DFS visit: a, c, f, e, b, d, g, h, j Q: ['j'] visited: {'d', 'b', 'j', 'h', 'a', 'g', 'c', 'e', 'f'}

h

slide-51
SLIDE 51

Graph traversal: BFS

visiting: - DFS visit: a, c, f, e, b, d, g, h, j Q: [] β†’ DONE visited: {'d', 'b', 'j', 'h', 'a', 'g', 'c', 'e', 'f'} Node Dist from a a c 1 f 1 e 1 b 2 d 2 g 2 h 2 j 3

j

slide-52
SLIDE 52

Graph traversal: BFS tree of the graph

visiting: - visited: {'d', 'b', 'j', 'h', 'a', 'g', 'c', 'e', 'f'} Q: [] DONE! BFS from a: c, f, e, b, d, g, h, j

This can be done by storing a pointer to parents!

slide-53
SLIDE 53

Graph traversal: BFS complexity

slide-54
SLIDE 54

BFS: application. Shortest path

for fun: https://www.csauthors.net/distance

slide-55
SLIDE 55

BFS: application. Shortest distance/Shortest path

Initially all distances: +∞ all parents: -1 distance root <-> root = 0 parent of root = root distances is used also as β€˜visited’ if not set, distance node: distance of parent +1

slide-56
SLIDE 56

BFS: application. Shortest distance/Shortest path

Distances from 'a': {'a': 0, 'c': 1, 'f': 1, 'e': 1, 'b': 2, 'd': 2, 'g': 2, 'j': 3, 'h': 2, 'k': inf, 'l': inf} All parents: {'a': 'a', 'c': 'a', 'f': 'a', 'e': 'a', 'b': 'c', 'd': 'c', 'g': 'f', 'j': 'g', 'h': 'e', 'k': -1, 'l': -1}

slide-57
SLIDE 57

BFS: application. Shortest distance/Shortest path

Distances from 'b': {'a': 4, 'c': 5, 'f': 1, 'e': 5, 'b': 0, 'd': 4, 'g': 2, 'j': 3, 'h': 6, 'k': inf, 'l': inf} All parents: {'a': 'j', 'c': 'a', 'f': 'b', 'e': 'a', 'b': 'b', 'd': 'j', 'g': 'f', 'j': 'g', 'h': 'e', 'k': -1, 'l': -1}

slide-58
SLIDE 58

BFS: application. Shortest distance/Shortest path

printing the shortest path...

slide-59
SLIDE 59

BFS: application. Shortest distance/Shortest path

printing the shortest path...

Path from 'a' to 'j': a --> f --> g --> j Path from 'a' to 'k': Not available All parents: {'a': 'a', 'c': 'a', 'f': 'a', 'e': 'a', 'b': 'c', 'd': 'c', 'g': 'f', 'j': 'g', 'h': 'e', 'k': -1, 'l': -1} root or nodes not reached == -1

slide-60
SLIDE 60

BFS: application. Shortest distance/Shortest path

printing the shortest path...

Path from 'b' to 'c': b --> f --> g --> j --> a --> c root or nodes not reached == -1 All parents: {'a': 'j', 'c': 'a', 'f': 'b', 'e': 'a', 'b': 'b', 'd': 'j', 'g': 'f', 'j': 'g', 'h': 'e', 'k': -1, 'l': -1}

slide-61
SLIDE 61

Exercise

What if the shortest path between (a,j) is j→ a???

Shortest path from 'a' to 'j': j --> a

slide-62
SLIDE 62

Traversals: Depth First Search (DFS)

slide-63
SLIDE 63

Traversals: Depth First Search (DFS)

Idea:

Visit the first node (mark it as visited)… … then recursively all its children nodes (follow one path until it ends)

slide-64
SLIDE 64

Traversals: Depth First Search (DFS)

Idea:

Visit the first node (mark it as visited)… … then recursively all its children nodes (follow one path until it ends) Execution stack:DFS(1)

slide-65
SLIDE 65

Traversals: Depth First Search (DFS)

Idea:

Visit the first node (mark it as visited)… … then recursively all its children nodes (follow one path until it ends) Execution stack:DFS(1, DFS(2))

slide-66
SLIDE 66

Traversals: Depth First Search (DFS)

Idea:

Visit the first node (mark it as visited)… … then recursively all its children nodes (follow one path until it ends) Execution stack:DFS(1, DFS(2, DFS(3)))

slide-67
SLIDE 67

Traversals: Depth First Search (DFS)

Idea:

Visit the first node (mark it as visited)… … then recursively all its children nodes (follow one path until it ends) Execution stack:DFS(1, DFS(2, DFS(3, DFS(4))))

slide-68
SLIDE 68

Traversals: Depth First Search (DFS)

Idea:

Visit the first node (mark it as visited)… … then recursively all its children nodes (follow one path until it ends) Execution stack:DFS(1, DFS(2, DFS(3))) DFS(4): nothing to do. Done.

slide-69
SLIDE 69

Traversals: Depth First Search (DFS)

Idea:

Visit the first node (mark it as visited)… … then recursively all its children nodes (follow one path until it ends) Execution stack:DFS(1, DFS(2, DFS(3, DFS(6))))

slide-70
SLIDE 70

Traversals: Depth First Search (DFS)

Idea:

Visit the first node (mark it as visited)… … then recursively all its children nodes (follow one path until it ends) Execution stack:DFS(1, DFS(2, DFS(3)))) DFS(6): nothing to do. Done.

slide-71
SLIDE 71

Traversals: Depth First Search (DFS)

Idea:

Visit the first node (mark it as visited)… … then recursively all its children nodes (follow one path until it ends) Execution stack:DFS(1, DFS(2))) DFS(3): nothing to do. Done.

slide-72
SLIDE 72

Traversals: Depth First Search (DFS)

Idea:

Visit the first node (mark it as visited)… … then recursively all its children nodes (follow one path until it ends) Execution stack:DFS(1, DFS(2, DFS(5))))

slide-73
SLIDE 73

Traversals: Depth First Search (DFS)

Idea:

Visit the first node (mark it as visited)… … then recursively all its children nodes (follow one path until it ends) Execution stack:DFS(1, DFS(2)) DFS(5): nothing to do. Done.

slide-74
SLIDE 74

Traversals: Depth First Search (DFS)

Idea:

Visit the first node (mark it as visited)… … then recursively all its children nodes (follow one path until it ends) Execution stack:DFS(1) DFS(2): nothing to do. Done.

slide-75
SLIDE 75

Traversals: Depth First Search (DFS)

Idea:

Visit the first node (mark it as visited)… … then recursively all its children nodes (follow one path until it ends) Execution stack: DONE! DFS(1): nothing to do. Done.

slide-76
SLIDE 76

Traversals: Depth First Search (DFS)

Idea:

Visit the first node (mark it as visited)… … then recursively all its children nodes (follow one path until it ends) Execution stack: DFS(7) Done.

slide-77
SLIDE 77

Recursive Depth First Search (DFS)

DFS from a: visiting: a visiting: c visiting: b visiting: f visiting: g visiting: j visiting: d visiting: e visiting: h

slide-78
SLIDE 78

Recursive Depth First Search (DFS)

DFS from b: visiting: b visiting: f visiting: g visiting: j visiting: a visiting: c visiting: d visiting: e visiting: h

slide-79
SLIDE 79

Recursive Depth First Search (DFS)

With recursive calls, β€œunclosed” calls are memorized in the stack and with big graphs this can cause a stack

  • verflow error.
slide-80
SLIDE 80

Iterative Depth First Search (DFS)

DFS from a: visiting a visiting e visiting h visiting j visiting d visiting b visiting f visiting g visiting c DFS from b: visiting b visiting f visiting g visiting j visiting d visiting a visiting e visiting h visiting c

slide-81
SLIDE 81

Connected graphs and components

slide-82
SLIDE 82

Connected components

slide-83
SLIDE 83

Reachability

slide-84
SLIDE 84

Application of DFS

slide-85
SLIDE 85

Connected components

  • ids is a list containing the component identifiers (it is also used as

β€˜visited’ structure)

  • ids[u] is the identifier of the connected component to which u belongs
slide-86
SLIDE 86

Connected components

3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

slide-87
SLIDE 87

Connected components

3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

slide-88
SLIDE 88

Connected components

3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

slide-89
SLIDE 89

Connected components

3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

ids is != 0

slide-90
SLIDE 90

Connected components

3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

slide-91
SLIDE 91

Connected components

3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

ids is != 0

slide-92
SLIDE 92

Connected components

3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

slide-93
SLIDE 93

Connected components

3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

ids is != 0

slide-94
SLIDE 94

Connected components

3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

ids is != 0

slide-95
SLIDE 95

Connected components

3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

call on d completed

slide-96
SLIDE 96

Connected components

3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3} some steps later… component 1 is done, component 2 starts...

call on c,b,a completed in the order The algorithm tries to restart from b,c,d but nodes are visited…

slide-97
SLIDE 97

Connected components

3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

slide-98
SLIDE 98

Definitions

Ignored, trivial cycle

slide-99
SLIDE 99

Definitions

Idea: perform a DFS visit, if it finds a node already visited then there is a cycle

slide-100
SLIDE 100

Cycle detection: undirected graph

slide-101
SLIDE 101

Cycle detection: undirected graph

True

slide-102
SLIDE 102

Cycle detection: undirected graph

True

slide-103
SLIDE 103

Cycle detection: undirected graph

False

slide-104
SLIDE 104

Cycle detection: directed graph

slide-105
SLIDE 105

Directed acyclic graph (DAG)

slide-106
SLIDE 106

Cycle detection

slide-107
SLIDE 107

Cycle detection

visit a

slide-108
SLIDE 108

Cycle detection

visit b

slide-109
SLIDE 109

Cycle detection

visit c

slide-110
SLIDE 110

Cycle detection

back from a to c β†’ cycle: wrong answer

slide-111
SLIDE 111

Edge classification

edges part of the DFS visit

slide-112
SLIDE 112

Edge classification

DFS edge Forward edge Back edge Cross edge

perform a DFS visit if dt[v] == 0 β†’ equals to v NOT visited clock is increased by one at each operation increase the time and set the finish time of node

slide-113
SLIDE 113

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1

slide-114
SLIDE 114

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1 DFS edge: a --> b

slide-115
SLIDE 115

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1 DFS edge: a --> b Start time b: 2

slide-116
SLIDE 116

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1 DFS edge: a --> b Start time b: 2 DFS edge: b --> c

slide-117
SLIDE 117

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1 DFS edge: a --> b Start time b: 2 DFS edge: b --> c Start time c: 3

slide-118
SLIDE 118

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1 DFS edge: a --> b Start time b: 2 DFS edge: b --> c Start time c: 3 Finish time c: 4

slide-119
SLIDE 119

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1 DFS edge: a --> b Start time b: 2 DFS edge: b --> c Start time c: 3 Finish time c: 4 Finish time b: 5

slide-120
SLIDE 120

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1 DFS edge: a --> b Start time b: 2 DFS edge: b --> c Start time c: 3 Finish time c: 4 Finish time b: 5 Forward edge: a--> c

slide-121
SLIDE 121

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1 DFS edge: a --> b Start time b: 2 DFS edge: b --> c Start time c: 3 Finish time c: 4 Finish time b: 5 Forward edge: a--> c DFS edge: a --> d

slide-122
SLIDE 122

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1 DFS edge: a --> b Start time b: 2 DFS edge: b --> c Start time c: 3 Finish time c: 4 Finish time b: 5 Forward edge: a--> c DFS edge: a --> d Start time d: 6

slide-123
SLIDE 123

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1 DFS edge: a --> b Start time b: 2 DFS edge: b --> c Start time c: 3 Finish time c: 4 Finish time b: 5 Forward edge: a--> c DFS edge: a --> d Start time d: 6 Back edge: d--> a

slide-124
SLIDE 124

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1 DFS edge: a --> b Start time b: 2 DFS edge: b --> c Start time c: 3 Finish time c: 4 Finish time b: 5 Forward edge: a--> c DFS edge: a --> d Start time d: 6 Back edge: d--> a Cross edge: d --> b

slide-125
SLIDE 125

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1 DFS edge: a --> b Start time b: 2 DFS edge: b --> c Start time c: 3 Finish time c: 4 Finish time b: 5 Forward edge: a--> c DFS edge: a --> d Start time d: 6 Back edge: d--> a Cross edge: d --> b Finish time d: 7

slide-126
SLIDE 126

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1 DFS edge: a --> b Start time b: 2 DFS edge: b --> c Start time c: 3 Finish time c: 4 Finish time b: 5 Forward edge: a--> c DFS edge: a --> d Start time d: 6 Back edge: d--> a Cross edge: d --> b Finish time d: 7 Finish time a: 8

slide-127
SLIDE 127

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1 DFS edge: a --> b Start time b: 2 DFS edge: b --> c Start time c: 3 Finish time c: 4 Finish time b: 5 Forward edge: a--> c DFS edge: a --> d Start time d: 6 Back edge: d--> a Cross edge: d --> b Finish time d: 7 Finish time a: 8 Start time e: 9

slide-128
SLIDE 128

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1 DFS edge: a --> b Start time b: 2 DFS edge: b --> c Start time c: 3 Finish time c: 4 Finish time b: 5 Forward edge: a--> c DFS edge: a --> d Start time d: 6 Back edge: d--> a Cross edge: d --> b Finish time d: 7 Finish time a: 8 Start time e: 9 Cross edge: e --> c

slide-129
SLIDE 129

Edge classification

DFS edge Forward edge Back edge Cross edge

Start time a: 1 DFS edge: a --> b Start time b: 2 DFS edge: b --> c Start time c: 3 Finish time c: 4 Finish time b: 5 Forward edge: a--> c DFS edge: a --> d Start time d: 6 Back edge: d--> a Cross edge: d --> b Finish time d: 7 Finish time a: 8 Start time e: 9 Cross edge: e --> c Finish time e: 10 Discovery times:{'a': 1, 'b': 2, 'c': 3, 'd': 6, 'e': 9} Finish times: {'a': 8, 'b': 5, 'c': 4, 'd': 7, 'e': 10}

slide-130
SLIDE 130

Edge classification

DFS edge Forward edge Back edge Cross edge

NOTE in the DFS visit: [1,8] completely contains [2,5] β†’ B descends from A [1,8] completely contains [3,4] β†’ C descends from A [9,10] does not overlap [2,5], [6,7] β†’ E-B E-D are not descendans Intervals describe the relationship between nodes

slide-131
SLIDE 131

Cycle detection

DFS edge Forward edge Back edge Cross edge

u v

slide-132
SLIDE 132

Cycle detection

DFS edge Forward edge Back edge Cross edge

v u

slide-133
SLIDE 133

Cycle detection

DFS edge Forward edge Back edge Cross edge

NO Cycle!

slide-134
SLIDE 134

Cycle detection

DFS edge Forward edge Back edge Cross edge

Cycle!

slide-135
SLIDE 135

Cycle detection: the code

DFS edge Forward edge Back edge Cross edge

Does G have a cycle? False Back edge: c --> a Does G have a cycle? True

simplified version of the code seen before. We just care about forward and back edges

slide-136
SLIDE 136

Comment on edge classification

DFS edge Forward edge Back edge Cross edge

1. if dt[v] == 0, it is the first time we see v in the DFS search. DFS Tree edge! 2. if dt[u] > dt[v] the DFS search found u after v and since the DFS visit started from v is not complete (ft[v] = 0), v is a descendant of u. [Path: v→ X → u]. Back edge! 3. if dt[u] < dt[v] the DFS search found v after u, therefore v descends from u. Since the visit of v is complete (ft[v] != 0) this is a Forward edge! [Path: u → Y → v]

u v X Y

[X,0] [X+1,0]

slide-137
SLIDE 137

Comment on edge classification

DFS edge Forward edge Back edge Cross edge

1. if dt[v] == 0, it is the first time we see v in the DFS search. DFS Tree edge! 2. if dt[u] > dt[v] the DFS search found u after v and since the DFS visit started from v is not complete (ft[v] = 0), v is a descendant of u. [Path: v→ X → u]. Back edge! 3. if dt[u] < dt[v] the DFS search found v after u, therefore v descends from u. Since the visit of v is complete (ft[v] != 0) this is a Forward edge! [Path: u → Y → v]

u v X Y

[X+K+T,0] [X,0] [X+K,0]

slide-138
SLIDE 138

Comment on edge classification

DFS edge Forward edge Back edge Cross edge

1. if dt[v] == 0, it is the first time we see v in the DFS search. DFS Tree edge! 2. if dt[u] > dt[v] the DFS search found u after v and since the DFS visit started from v is not complete (ft[v] = 0), v is a descendant of u. [Path: v→ X → u]. Back edge! 3. if dt[u] < dt[v] the DFS search found v after u, therefore v descends from u. Since the visit of v is complete (ft[v] != 0) this is a Forward edge! [Path: u → Y → v]

u v X Y

[X,0] [X+K,W] [X+K+T,Y]

slide-139
SLIDE 139

Topological sorting

We can think at these DAGs as dependency

  • graphs. If we

have edge x-->y activity x has to be completed before y starts.

Note: Edges always from left to right: correct

  • rder!
slide-140
SLIDE 140

Topological sorting

slide-141
SLIDE 141

Topological sorting

slide-142
SLIDE 142

Topological sorting

Note: we are destroying the graph!!! We could make a copy of the graph first, but this is not a great solution...

Picking 2 or 3 is equivalent (i.e. originates equivalent topological orderings)

slide-143
SLIDE 143

Topological sorting

slide-144
SLIDE 144

Topological sorting: example

slide-145
SLIDE 145

Topological sorting: example

slide-146
SLIDE 146

Topological sorting: example

slide-147
SLIDE 147

Topological sorting: example

slide-148
SLIDE 148

Topological sorting: example

slide-149
SLIDE 149

Topological sorting: example

slide-150
SLIDE 150

Topological sorting: example

slide-151
SLIDE 151

Topological sorting: example

slide-152
SLIDE 152

Topological sorting: example

slide-153
SLIDE 153

Topological sorting: example

slide-154
SLIDE 154

Topological sorting: example

slide-155
SLIDE 155

Topological sorting: example

What happens if nodes are chosen in a different order in the DFS visit?

slide-156
SLIDE 156

Topological sorting: example

What happens if nodes are chosen in a different order in the DFS visit?

[1,4] [2,3] [8,9] [6,7] [5,10] Stack = {a, c, e, b, d}

slide-157
SLIDE 157

Topological sorting: the code

slide-158
SLIDE 158

Topological sorting: the code

slide-159
SLIDE 159

Strongly connected graphs and components

slide-160
SLIDE 160

Strongly connected graphs and components

slide-161
SLIDE 161

Strongly connected graphs and components

slide-162
SLIDE 162

Naive (and wrong!) solution

In a nutshell: perform a DSF visit, assign to each visit the same component number until all nodes visited DFS visit starting from C, then from B, then from A DFS visit starting from B, then from A DFS visit starting from A

slide-163
SLIDE 163

Strongly connected components algorithm

slide-164
SLIDE 164

Topological sorting of general graphs

NOTE: we might have cycles, so this does not necessarily mean that we obtain a topological sort!!! But the important thing is that all the nodes before the cycle(s) and after the cycles(s) are put in the correct topological sort.

slide-165
SLIDE 165

Transpose of a graph

transpose(G)

slide-166
SLIDE 166

Transpose of a graph

slide-167
SLIDE 167

Modified connected components

Instead of examining the nodes in an arbitrary order, this version of cc(G,S) examines them in the order in which they are stored in the stack S.

slide-168
SLIDE 168

Putting it all together

top_sort(G) transpose(G) cc(GT,S)

Output: Components: 3 Ids:{'b': 2, 'a': 1, 'd': 3, 'c': 3, 'e': 3, 'f': 3}

slide-169
SLIDE 169

Proof of correctness...

  • YES. Otherwise

any cycle would be a bigger SCC.

A B CD EF NO CYCLES: top_sort correctly sorts the components

slide-170
SLIDE 170

Proof of correctness...

A B CD EF

slide-171
SLIDE 171

Proof of correctness...

A B CD EF

slide-172
SLIDE 172

Proof of correctness...

A B CD EF

slide-173
SLIDE 173

Proof of correctness...

A B CD EF

slide-174
SLIDE 174

Proof of correctness...

slide-175
SLIDE 175

If you are starting to have fun...

Good news… there are at least 110+ other algorithms on graphs!