120
Graphs 3

Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

Graphs

3

Page 2: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

6

Objects & Relationships

Facebook friends:Obj: PeopleRel: Two are related if they are friends

Cities and Roads:Obj: CitiesRel: Two are related if they have a road between them

Data flow in programs:Obj: Lines of the programRel: Two are related if one line depends on the other

Page 3: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

7

Graphs

Objects: "vertices," aka "nodes"Relationships between pairs: "edges”Formally, a graph G = (V, E) is a pair of sets, V the vertices and E the edges. Each edge is a set or tuple of two vertices.

Page 4: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

8

Undirected Graph G = (V,E)1

210

9

8

3

4

56

7

1112

13

Page 5: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

9

Undirected Graph G = (V,E)1

210

9

8

3

4

56

7

1112

13

Page 6: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

10

Undirected Graph G = (V,E)1

210

9

8

3

4

56

7

1112

13

Page 7: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

11

Undirected Graph G = (V,E)1

210

9

8

3

4

56

7

1112

13

"self-loop"

"multi-edge"

Page 8: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

12

Undirected Graph G = (V,E)1

210

9

8

3

4

56

7

1112

13

"self-loop"

"multi-edge"

Page 9: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

13

Graphs don't live in FlatlandGeometrical drawing is mentally convenient, but mathematicallyirrelevant: 4 drawings, 1 graph.

A

7 4

3A

74

3

A

74

3

A

7 4

3

Page 10: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

14

Directed Graph G = (V,E)1

210

9

8

3

4

56

7

1112

13

Page 11: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

15

Directed Graph G = (V,E)1

210

9

8

3

4

56

7

1112

13

Page 12: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

16

Directed Graph G = (V,E)1

210

9

8

3

4

56

7

1112

13

Page 13: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

17

Directed Graph G = (V,E)1

210

9

8

3

4

56

7

1112

13

"self-loop"

"multi-edge"

Page 14: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

18

Directed Graph G = (V,E)1

210

9

8

3

4

56

7

1112

13

"self-loop"

"multi-edge"

Page 15: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

Graphs

Degree of a vertex, deg(v): # edges that touch that vertex

deg(6) = 3.

Path: sequence of distinct vertices s.t. each vertex is connected to the next vertex with an edge

Eg: 3,6,5,419

34

56

72

10

1

Page 16: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

Connected: Graph is connected if there is a path between every two vertices

Connected component: Maximal set of connected vertices

Cycle: Path of length > 1 that has the same start and end. Eg: 6,5,7

Tree: A connected graph with no cycles20

346

7

5

Page 17: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

21

Let G be an undirected graph with n vertices and medges. How are n and m related?

# Vertices vs # Edges

Page 18: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

22

Let G be an undirected graph with n vertices and medges. How are n and m related?

Since every edge connects two different vertices (no loops), and no two edges connect the same two vertices (no multi-edges),

it must be true that:

0 ≤ m ≤ n(n-1)/2 = O(n2)

# Vertices vs # Edges

Page 19: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

23

More Cool Graph Lingo

A graph is called sparse if m ≪ n2, otherwise it is dense

Boundary is somewhat fuzzy; O(n) edges is certainly sparse, Ω(n2) edges is dense.

Sparse graphs are common in practiceE.g., all planar graphs are sparse (m ≤ 3n-6, for n ≥ 3)

Q: which is a better run time, O(n+m) or O(n2)?

A: n+m = O(n2), but n+m usually way better!

Page 20: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

24

A

7 4

3

Specifying undirected graphs as input

What are the vertices?Explicitly list them: "A", "7", "3", "4"

What are the edges?Either, set of edges A,3, 7,4, 4,3, 4,AOr, (symmetric) adjacency matrix:

A 7 3 4A 0 0 1 17 0 0 0 13 1 0 0 14 1 1 1 0

Page 21: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

25

A

7 4

3

Specifying directed graphs as input

What are the vertices?Explicitly list them: "A", "7", "3", "4"

What are the edges?Either, set of directed edges: (A,4), (4,7), (4,3), (4,A), (A,3)

Or, (nonsymmetric) adjacency matrix:

A 7 3 4A 0 0 1 17 0 0 0 03 0 0 0 04 1 1 1 0

Page 22: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

26

Representing Graph G = (V,E)

Vertex set V = v1, …, vnAdjacency Matrix A

A[i,j] = 1 iff (vi,vj) ∊ E

Space is n2 bits

Advantages: O(1) test for presence or absence of edges.

Disadvantages: inefficient for sparse graphs, both in storage and access

m ≪ n2

A 7 3 4A 0 0 1 17 0 0 0 13 1 0 0 14 1 1 1 0

A

743

internally, indp of input format

Page 23: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

27

Representing Graph G=(V,E)n vertices, m edges

Adjacency List:O(n+m) words

Advantages:Compact for sparse graphs

Easily see all edges

DisadvantagesMore complex data structure no O(1) edge test

7

7

v3

v2

v1

vn

2 6

2 4

3

5

1

Page 24: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

28

Representing Graph G=(V,E)n vertices, m edges

Adjacency List:O(n+m) words

Back- and cross pointers more work to build, but allow easier traversal and deletion of edges, if needed, (don't bother if not)

1

7

v3

v2

v1

v7

2 6

2 4

3

5

1

Page 25: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

29

Graph Traversal

Learn the basic structure of a graph"Walk," via edges, from a fixed starting vertex s to all vertices reachable from s

Being orderly helps. Two common ways:Breadth-First Search: order the nodes in successive layers based on distance from s

Depth-First Search: more natural approach for exploring a maze; many efficient algs build on it.

Page 26: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

30

Breadth-First Search

Completely explore the vertices in order of their distance from s

Naturally implemented using a queue

Page 27: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

31

Graph Traversal: Implementation

Learn the basic structure of a graph"Walk," via edges, from a fixed starting vertex s to all vertices reachable from s

Three states of verticesundiscovereddiscoveredfully-explored

Page 28: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

32

BFS(s) Implementation

Global initialization: mark all vertices "undiscovered"BFS(s)

mark s "discovered"queue = s while queue not empty

u = remove_first(queue)for each edge u,x

if (x is undiscovered) mark x discoveredappend x on queue

mark u fully explored

Page 29: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

33

BFS(v)1

2 3

10

5

4

9

128

13

67

11

Queue:1

Page 30: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

34

BFS(v)1

2 3

10

5

4

9

128

13

67

11

Queue:2 3

Page 31: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

35

BFS(v)1

2 3

10

5

4

9

128

13

67

11

Queue:3 4

Page 32: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

36

BFS(v)1

2 3

10

5

4

9

128

13

67

11

Queue:4 5 6 7

Page 33: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

37

BFS(v)1

2 3

10

5

4

9

128

13

67

11

Queue:5 6 7 8 9

Page 34: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

38

BFS(v)1

2 3

10

5

4

9

128

13

67

11

Queue:8 9 10 11

Page 35: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

39

BFS(v)1

2 3

10

5

4

9

128

13

67

11

Queue:10 11 12 13

Page 36: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

40

BFS(v)1

2 3

10

5

4

9

128

13

67

11

Queue:

Page 37: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

41

BFS: Analysis, IGlobal initialization: mark all vertices "undiscovered"BFS(s)

mark s "discovered"queue = s while queue not empty

u = remove_first(queue)for each edge u,x

if (x is undiscovered) mark x discoveredappend x on queue

mark u fully explored

Simple analysis: 2 nested loops. Get worst-case number of iterations of each; multiply.

O(n)+

O(1)+

O(n)x

O(n)

=O(n2)

Page 38: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

42

BFS: Analysis, II

Above analysis correct, but pessimistic (can't have Ω(n) edges incident to each of Ω(n) distinct "u" vertices if G is sparse). Alt, more global analysis:

Each edge is explored once from each end-point, so totalruntime of inner loop is O(m).

Total O(n+m), n = # nodes, m = # edges

Exercise: extend algorithm and analysis to non-connected graphs

Page 39: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

43

Properties of (Undirected) BFS(v)

BFS(v) visits x if and only if there is a path in G from v to x.Edges into then-undiscovered vertices define a tree– the "breadth first spanning tree" of GLevel i in this tree are exactly those vertices u such that the shortest path (in G, not just the

tree) from the root v is of length i.All non-tree edges join vertices on the same or adjacent levels

not true of every spanning tree!

Page 40: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

44

Proof of correctness

Lemma 1: Every vertex at level i is explored after every vertex at level i-1.

Proof is by induction on i.Base case: i = 1. True.Induction step: Let a be at level i, and b be at level i-1. Since we use a queue, it is enough to prove that a is added to the queue after b. But awas added when a vertex at level i-1 was explored, and b is added when a vertex of level i-2 was explored. So a is added after b by induction.

Page 41: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

45

Proof of correctness

Lemma 2: Level i in this tree are exactly those vertices u such that the shortest path (in G, not just the

tree) from the root is of length i.

Proof is by induction on i.Base case: i = 0. True.Induction step: Every vertex u at level i certainly has distance at most i,because we discover a path of length i from u to v. If the distance from the root is less than i, and u was discovered when exploring a (at level i-1), then u is a neighbor of a vertex b at distance (and level) < i-1. But then, by Lemma 1, b would have been explored before a, and u would have been added in level i-1.

Page 42: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

46

BFS Application: Shortest Paths1

2 3

10

5

4

9

128

13

67

11

0

1

2

3

4can label by distances from start

all edges connect same/adjacent levels

Tree (solid edges) gives shortest paths from start vertex

Page 43: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

47

Tree (solid edges) gives shortest paths from start vertex

BFS Application: Shortest Paths1

2 3

10

5

4

9

8

13

6 7

11

0

1

2

3

4can label by distances from start

all edges connect same/adjacent levels

12

Page 44: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

48

Tree (solid edges) gives shortest paths from start vertex

BFS Application: Shortest Paths1

2 3

10

54

9

8

13

6 7

11

0

1

2

3

4 can label by distances from startall edges connect same/adjacent levels

12

Page 45: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

49

Tree (solid edges) gives shortest paths from start vertex

BFS Application: Shortest Paths1

2 3

10

54

98

13

6 7

11

0

1

2

3

4 Lemma:all edges connect same/adjacent levels

12

Page 46: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

50

Why fuss about trees?

Trees are simpler than graphsDitto for algorithms on trees vs algs on graphsSo, this is often a good way to approach a graph problem: find a "nice" tree in the graph, i.e., one such that non-tree edges have some simplifying structureE.g., BFS finds a tree s.t. level-jumps are minimizedDFS (below) finds a different tree, but it also has interesting structure…

Page 47: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

51

Graph Search Application: Connected Components

Want to answer questions of the form:given vertices u and v, is there a path from u to v?

Set up one-time data structure to answer such questions efficiently.

Page 48: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

52

Graph Search Application: Connected Components

Want to answer questions of the form:given vertices u and v, is there a path from u to v?

Idea: create array A such that A[u] = smallest numbered vertex thatis connected to u. Question reduces to whether A[u]=A[v]?

Page 49: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

53

Graph Search Application: Connected Components

initial state: all v undiscoveredfor v = 1 to n do

if state(v) != fully-explored then BFS(v): setting A[u] = v for each u found (and marking u discovered/fully-explored)

endifendfor

Total cost: O(n+m)each edge is touched a constant number of times (twice)works also with DFS

Page 50: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

3.4 Testing Bipartiteness

Page 51: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

55

Bipartite Graphs

Def. An undirected graph G = (V, E) is bipartite (2-colorable) if the nodes can becolored red or blue such that no edge has both ends the same color.

Applications.Stable marriage: men = red, women = blueScheduling: machines = red, jobs = blue

a bipartite graph

"bi-partite" means "two parts." An equivalent definition: G is bipartite if you can partition the node set into 2 parts (say, blue/red or left/right) so that all edges join nodes in different parts/no edge has both ends in the same part.

Page 52: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

56

Testing Bipartiteness

Testing bipartiteness. Given a graph G, is it bipartite?Many graph problems become:

easier if the underlying graph is bipartite (matching)tractable if the underlying graph is bipartite (independent set)

Before attempting to design an algorithm, we need to understand structure of bipartite graphs.

v1

v2 v3

v6 v5 v4

v7

v2

v4

v5

v7

v1

v3

v6

a bipartite graph G another drawing of G

Page 53: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

57

An Obstruction to Bipartiteness

Lemma. If a graph G is bipartite, it cannot contain an odd length cycle.

Pf. Impossible to 2-color the odd cycle, let alone G.

bipartite(2-colorable)

not bipartite(not 2-colorable)

not bipartite(not 2-colorable)

Page 54: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

58Case (i)

L1 L2 L3

Case (ii)

L1 L2 L3

Bipartite Graphs

Lemma. Let G be a connected graph, and let L0, …, Lk be the layers produced by BFS starting at node s. Exactly one of the following holds.

(i) No edge of G joins two nodes of the same layer, and G is bipartite.(ii) An edge of G joins two nodes of the same layer, and G contains an odd-length cycle (and hence is not bipartite).

Page 55: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

59

Case (i)

L1 L2 L3

Bipartite Graphs

Lemma. Let G be a connected graph, and let L0, …, Lk be the layers produced by BFS starting at node s. Exactly one of the following holds.

(i) No edge of G joins two nodes of the same layer, and G is bipartite.(ii) An edge of G joins two nodes of the same layer, and G contains an odd-length cycle (and hence is not bipartite).

Pf. (i)Suppose no edge joins two nodes in the same layer.By previous lemma, all edges join nodes on adjacent levels.

Bipartition: red = nodes on odd levels, blue = nodes on even levels.

Page 56: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

60

z = lca(x, y)

(x, y) path fromy to z

path fromz to x

Bipartite Graphs

Lemma. Let G be a connected graph, and let L0, …, Lk be the layers produced by BFS starting at node s. Exactly one of the following holds.

(i) No edge of G joins two nodes of the same layer, and G is bipartite.(ii) An edge of G joins two nodes of the same layer, and G contains an odd-length cycle (and hence is not bipartite).

Pf. (ii)Suppose (x, y) is an edge & x, y in same level Lj.Let z = their lowest common ancestor in BFS tree.Let Li be level containing z.Consider cycle that takes edge from x to y,then tree from y to z, then tree from z to x.Its length is 1 + (j-i) + (j-i), which is odd.

Page 57: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

61

Obstruction to Bipartiteness

Cor: A graph G is bipartite iff it contains no odd length cycle.

5-cycle C

bipartite(2-colorable)

not bipartite(not 2-colorable)

NB: the proof is algorithmic–it finds a coloring or odd cycle.

Page 58: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

3.6 DAGs and Topological Ordering

Page 59: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

63

Precedence Constraints

Precedence constraints. Edge (vi, vj) means task vimust occur before vj.

Applications

Course prerequisites: course vi must be taken before vj

Compilation: must compile module vi before vj

Computing workflow: output of job vi is input to job vj

Manufacturing or assembly: sand it before you paint it…

Spreadsheet evaluation order: if A7 is "=A6+A5+A4", evaluate them first

Page 60: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

64

Directed Acyclic Graphs

Def. A DAG is a directed acyclic graph, i.e., one that contains no directed cycles.

Ex. Precedence constraints: edge (vi, vj) means vi must precede vj.

Def. A topological order of a directed graph G = (V, E) is an ordering of its nodes as v1, v2, …, vn so that for every edge (vi, vj) we have i < j.

a DAGa topological ordering of that DAG–all edges left-to-right

v2 v3

v6 v5 v4

v7 v1

v1 v2 v3 v4 v5 v6 v7

E.g., ∀edge (vi, vj), finishvi before starting vj

Page 61: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

65

Directed Acyclic Graphs

Lemma. If G has a topological order, then G is a DAG.

Pf. (by contradiction)Suppose that G has a topological order v1, …, vn

and that G also has a directed cycle C.Let vi be the lowest-indexed node in C, and let vj be the node just before vi; thus (vj, vi) is an edge.By our choice of i, we have i < j.On the other hand, since (vj, vi) is an edge and v1, …, vn is a topological order, we must have j < i, a contradiction.

v1 vi vj vn

the supposed topological order: v1, …, vn

the directed cycle C

if all edges go L→R, you can't loop back to close a cycle

Page 62: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

66

Directed Acyclic Graphs

Lemma. If G has a topological order, then G is a DAG.

Q. Does every DAG have a topological ordering?

Q. If so, how do we compute one?

Page 63: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

67

Directed Acyclic Graphs

Lemma. If G is a DAG, then G has a node with no incoming edges.

Pf. (by contradiction)Suppose that G is a DAG and every node has at least one incoming edge. Let's see what happens.Pick any node v, and begin following edges backward from v. Since v has at least one incoming edge (u, v) we can walk backward to u.Then, since u has at least one incoming edge (x, u), we can walk backward to x.Repeat until we visit a node, say w, twice.Let C be the sequence of nodes encountered between successive visits to w. C is a cycle.

w x u v

Why must this happen?

C

Page 64: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

68

Directed Acyclic Graphs

Lemma. If G is a DAG, then G has a topological ordering.

Pf. (by induction on n)Base case: true if n = 1.Given DAG on n > 1 nodes, find a node v with no incoming edges.G - v is a DAG, since deleting v cannot create cycles.By inductive hypothesis, G - v has a topological ordering.Place v first in topological ordering; then append nodes of G - v in topological order. This is valid since v has no incoming edges.

DAG

v

Page 65: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

69

v1

Topological Ordering Algorithm: Example

Topological order:

v2 v3

v6 v5 v4

v7 v1

Page 66: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

70

v2

Topological Ordering Algorithm: Example

Topological order: v1

v2 v3

v6 v5 v4

v7

Page 67: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

71

v3

Topological Ordering Algorithm: Example

Topological order: v1, v2

v3

v6 v5 v4

v7

Page 68: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

72

v4

Topological Ordering Algorithm: Example

Topological order: v1, v2, v3

v6 v5 v4

v7

Page 69: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

73

v5

Topological Ordering Algorithm: Example

Topological order: v1, v2, v3, v4

v6 v5

v7

Page 70: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

74

v6

Topological Ordering Algorithm: Example

Topological order: v1, v2, v3, v4, v5

v6

v7

Page 71: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

75

v7

Topological Ordering Algorithm: Example

Topological order: v1, v2, v3, v4, v5, v6

v7

Page 72: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

76

Topological order: v1, v2, v3, v4, v5, v6, v7.

v2 v3

v6 v5 v4

v7 v1

v1 v2 v3 v4 v5 v6 v7

Topological Ordering Algorithm: Example

Page 73: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

77

Topological Sorting AlgorithmMaintain the following:

count[w] = (remaining) number of incoming edges to node wS = set of (remaining) nodes with no incoming edges

Initialization: count[w] = 0 for all wcount[w]++ for all edges (v,w) O(m + n)S = S È w for all w with count[w]==0

Main loop: while S not empty

remove some v from Smake v next in topo order O(1) per nodefor all edges from v to some w O(1) per edgedecrement count[w]add w to S if count[w] hits 0

Correctness: clear, I hopeTime: O(m + n) (assuming edge-list representation of graph)

Page 74: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

78

Depth-First Search

Follow the first path you find as far as you can goBack up to last unexplored edge when you reach a dead end, then go as far you can

Naturally implemented using recursive calls or a stack

Page 75: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

80

DFS(v) – Recursive versionGlobal Initialization:

for all nodes v, v.dfs# = -1 // mark v "undiscovered"dfscounter = 0

DFS(v) v.dfs# = dfscounter++ // v "discovered", number itfor each edge (v,x)

if (x.dfs# = -1) // tree edge (x previously undiscovered)

DFS(x)else … // code for back-, fwd-, parent,

// edges, if needed// mark v "completed," if needed

Page 76: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

81

Why fuss about trees (again)?

BFS tree ≠ DFS tree, but, as with BFS, DFS has found a tree in the graph s.t.non-tree edges are "simple”

Page 77: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

82

DFS(A)A,1

B J

I

H

C

G

FD

E

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack(Edge list):

A (B,J)

Page 78: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

83

DFS(A)A,1

B,2 J

I

H

C

G

FD

E

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)

Page 79: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

84

DFS(A)A,1

B,2 J

I

H

C,3

G

FD

E

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)

Page 80: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

85

DFS(A)A,1

B,2 J

I

H

C,3

G

FD,4

E

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)D (C,E,F)

Page 81: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

86

DFS(A)A,1

B,2 J

I

H

C,3

G

FD,4

E,5

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)D (C,E,F)E (D,F)

Page 82: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

87

DFS(A)A,1

B,2 J

I

H

C,3

G

F,6D,4

E,5

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)D (C,E,F)E (D,F)F (D,E,G)

Page 83: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

88

DFS(A)A,1

B,2 J

I

H

C,3

G,7

F,6D,4

E,5

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)D (C,E,F)E (D,F)F (D,E,G)G(C,F)

Page 84: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

89

DFS(A)A,1

B,2 J

I

H

C,3

G,7

F,6D,4

E,5

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)D (C,E,F)E (D,F)F (D,E,G)G(C,F)

Page 85: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

90

DFS(A)A,1

B,2 J

I

H

C,3

G,7

F,6D,4

E,5

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)D (C,E,F)E (D,F)F (D,E,G)

Page 86: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

91

DFS(A)A,1

B,2 J

I

H

C,3

G,7

F,6D,4

E,5

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)D (C,E,F)E (D,F)

Page 87: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

92

DFS(A)A,1

B,2 J

I

H

C,3

G,7

F,6D,4

E,5

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)D (C,E,F)

Page 88: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

93

DFS(A)A,1

B,2 J

I

H

C,3

G,7

F,6D,4

E,5

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)

Page 89: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

94

DFS(A)A,1

B,2 J

I

H

C,3

G,7

F,6D,4

E,5

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)

Page 90: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

95

DFS(A)A,1

B,2 J

I

H,8

C,3

G,7

F,6D,4

E,5

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)H (C,I,J)

Page 91: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

96

DFS(A)A,1

B,2 J

I,9

H,8

C,3

G,7

F,6D,4

E,5

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)H (C,I,J)I (H)

Page 92: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

97

DFS(A)A,1

B,2 J

I,9

H,8

C,3

G,7

F,6D,4

E,5

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)H (C,I,J)I (H)

Page 93: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

98

DFS(A)A,1

B,2 J

I,9

H,8

C,3

G,7

F,6D,4

E,5

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)H (C,I,J)

Page 94: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

99

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)H (C,I,J)J (A,B,H,K,L)

Page 95: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

100

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)H (C,I,J)J (A,B,H,K,L)K (J,L)

Page 96: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

101

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L,12

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)H (C,I,J)J (A,B,H,K,L)K (J,L)L (J,K,M)

Page 97: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

102

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L,12

M,13

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)H (C,I,J)J (A,B,H,K,L)K (J,L)L (J,K,M)M(L)

Page 98: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

103

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L,12

M,13

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)H (C,I,J)J (A,B,H,K,L)K (J,L)L (J,K,M)

Page 99: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

104

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L,12

M,13

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)H (C,I,J)J (A,B,H,K,L)K (J,L)

Page 100: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

105

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L,12

M,13

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)H (C,I,J)J (A,B,H,K,L)

Page 101: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

106

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L,12

M,13

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)H (C,I,J)J (A,B,H,K,L)

Page 102: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

107

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L,12

M,13

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)H (C,I,J)

Page 103: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

108

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L,12

M,13

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)C (B,D,G,H)

Page 104: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

109

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L,12

M,13

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)

Page 105: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

110

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L,12

M,13

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)B (A,C,J)

Page 106: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

111

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L,12

M,13

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)

Page 107: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

112

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L,12

M,13

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

A (B,J)

Page 108: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

113

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L,12

M,13

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

TA-DA!!

Page 109: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

114

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L,12

M,13

Edge code:Tree edgeBack edge

Page 110: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

115

DFS(A) A,1

B,2J,10

I,9

H,8

C,3

G,7

F,6

D,4

E,5

K,11 L,12

M,13

Edge code:Tree edgeBack edge

Page 111: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

116

DFS(A) A,1

B,2

J,10

I,9

H,8

C,3

G,7

F,6

D,4

E,5

K,11L,12

M,13

Edge code:Tree edgeBack edge

Page 112: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

117

DFS(A)A,1

B,2

J,10

I,9

H,8

C,3

G,7

F,6

D,4

E,5

K,11L,12

M,13

Edge code:Tree edgeBack edge

Page 113: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

118

DFS(A)A,1

B,2

J,10

I,9

H,8

C,3

G,7

F,6

D,4

E,5

K,11

L,12

M,13

Edge code:Tree edgeBack edge

Page 114: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

119

DFS(A)A,1

B,2

J,10

I,9

H,8

C,3

G,7F,6

D,4

E,5 K,11

L,12

M,13

Edge code:Tree edgeBack edge

Page 115: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

120

DFS(A)A,1

B,2

J,10

I,9

H,8

C,3

G,7

F,6

D,4

E,5

K,11L,12

M,13

Edge code:Tree edgeBack edgeNo Cross Edges!

Page 116: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

121

Properties of (Undirected) DFS(v)

Like BFS(v):DFS(v) visits x if and only if there is a path in G from v to x (through previously unvisited vertices)

Edges into then-undiscovered vertices define a tree –the "depth first spanning tree" of G

Unlike the BFS tree: the DF spanning tree isn't minimum depthits levels don't reflect min distance from the rootnon-tree edges never join vertices on the same or adjacent levels

BUT…

Page 117: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

122

Non-tree edges

All non-tree edges join a vertex and one of its descendents/ancestors in the DFS tree

No cross edges!

Page 118: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

123

Why fuss about trees (again)?

As with BFS, DFS has found a tree in the graph s.t. non-tree edges are "simple"--only descendant/ancestor

Page 119: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

124

A simple problem on trees

Given: tree T, a value L(v) defined for every vertex v in TGoal: find M(v), the min value of L(v) anywhere in the subtree rooted at v(including v itself).How?

Page 120: Graphs - homes.cs.washington.eduGraphs Degree of a vertex, deg(v): # edges that touch that vertex deg(6) = 3. Path: sequence of distinct vertices s.t.each vertex is connected to the

125

DFS(v) – Recursive versionGlobal Initialization:

for all nodes v, v.dfs# = -1 // mark v "undiscovered"dfscounter = 0

DFS(v) v.dfs# = dfscounter++ // v "discovered", number itfor each edge (v,x)

if (x.dfs# = -1) // tree edge (x previously undiscovered)

DFS(x)else … // code for back-, fwd-, parent,

// edges, if needed// mark v "completed," if needed