Transcript
Page 1: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Lecture 8

Graph Algorithms I: BFS and DFS Traversal

Algorithms and Data Structures

Fabian Kuhn

Algorithms and Complexity

Page 2: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Node set ๐‘ฝ, typically ๐‘› โ‰” |๐‘‰| (nodes are also called vertices)

Edge set ๐‘ฌ, typically ๐‘š โ‰” ๐ธ

โ€ข undirected graph: ๐ธ โŠ† ๐‘ข, ๐‘ฃ โˆถ ๐‘ข, ๐‘ฃ โˆˆ ๐‘‰

โ€ข directed graph: ๐ธ โŠ† ๐‘‰ ร— ๐‘‰

Examples:

2

Graphs

1

2

3

54

1

2

3

54

๐‘‰ = 1, 2, 3, 4, 5

๐ธ = 1,2 , 1,4 , 1,5 , 2,3 , 3,4 , 3,5 , {4,5}

๐‘‰ = 1, 2, 3, 4, 5๐ธ = 1,2 , 1,5 , 2,3 , 3,4 , 3,4 , 3,5 , 4,1 , (5,4)

Page 3: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Graph ๐‘ฎ = (๐‘ฝ, ๐‘ฌ) undirected:

โ€ข Degree of node ๐‘ข โˆˆ ๐‘‰: Number of edges (neighbors) of ๐‘ข

deg ๐‘ข โ‰” ๐‘ข, ๐‘ฃ โˆถ ๐‘ข, ๐‘ฃ โˆˆ ๐ธ

Graph ๐‘ฎ = (๐‘ฝ, ๐‘ฌ) directed:

โ€ข In-degree of node ๐‘ข โˆˆ ๐‘‰: Number of incoming edges

deg๐‘–๐‘›(๐‘ข) โ‰” ๐‘ฃ, ๐‘ข โˆถ ๐‘ฃ, ๐‘ข โˆˆ ๐ธ

โ€ข Out-degree of node ๐‘ข โˆˆ ๐‘‰: Number of outgoing edges

deg๐‘œ๐‘ข๐‘ก(๐‘ข) โ‰” ๐‘ข, ๐‘ฃ โˆถ ๐‘ข, ๐‘ฃ โˆˆ ๐ธ

3

Node Degrees

Page 4: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Paths in a graph ๐‘ฎ = (๐‘ฝ, ๐‘ฌ)

โ€ข Path in ๐บ : a sequence ๐‘ข1, ๐‘ข2, โ€ฆ , ๐‘ข๐‘˜ โˆˆ ๐‘‰ with ๐‘ข๐‘– โ‰  ๐‘ข๐‘— (if ๐‘– โ‰  ๐‘—)

โ€“ directed graph: ๐‘ข๐‘– , ๐‘ข๐‘–+1 โˆˆ ๐ธ for all ๐‘– โˆˆ {1, โ€ฆ , ๐‘˜ โˆ’ 1}

โ€“ undirected graph: ๐‘ข๐‘– , ๐‘ข๐‘–+1 โˆˆ ๐ธ for all ๐‘– โˆˆ {1, โ€ฆ , ๐‘˜ โˆ’ 1}

Length of a path

โ€ข Number of edges on the path

โ€ข With edge weights: sum of all edge weights

4

Paths

๐‘ข1 ๐‘ข2 ๐‘ข3 ๐‘ข7๐‘ข4 ๐‘ข5 ๐‘ข6

๐‘ข1 ๐‘ข2 ๐‘ข3 ๐‘ข7๐‘ข4 ๐‘ข5 ๐‘ข6

Path, directed:

Path, undirected:

Length of path: ๐Ÿ”

Page 5: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Paths in a graph ๐‘ฎ = (๐‘ฝ, ๐‘ฌ)

โ€ข Path in ๐บ : a sequence ๐‘ข1, ๐‘ข2, โ€ฆ , ๐‘ข๐‘˜ โˆˆ ๐‘‰ with ๐‘ข๐‘– โ‰  ๐‘ข๐‘— (if ๐‘– โ‰  ๐‘—)

โ€“ directed graph: ๐‘ข๐‘– , ๐‘ข๐‘–+1 โˆˆ ๐ธ for all ๐‘– โˆˆ {1, โ€ฆ , ๐‘˜ โˆ’ 1}

โ€“ undirected graph: ๐‘ข๐‘– , ๐‘ข๐‘–+1 โˆˆ ๐ธ for all ๐‘– โˆˆ {1, โ€ฆ , ๐‘˜ โˆ’ 1}

Length of a path

โ€ข Number of edges on the path

โ€ข With edge weights: sum of all edge weights

Shortest path between nodes ๐‘ข and ๐‘ฃ

โ€ข Path ๐‘ข,โ€ฆ , ๐‘ฃ of smallest length

โ€ข Distance ๐‘‘๐บ ๐‘ข, ๐‘ฃ : Length of a shortest path between ๐‘ข and ๐‘ฃ

Diameter ๐‘ซ โ‰” ๐ฆ๐š๐ฑ๐’–,๐’—โˆˆ๐‘ฝ

๐’…๐‘ฎ(๐’–, ๐’—)

โ€ข Length of the longest shortest path

5

Pfade

Page 6: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Two classic methods to represent a graph in a computer

โ€ข Adjacency matrix: Space usage ฮ˜ ๐‘‰ 2 = ฮ˜ ๐‘›2

โ€ข Adjacency lists: Space usage ฮ˜ ๐‘‰ + ๐ธ = ฮ˜ ๐‘› +๐‘š = ๐‘‚ ๐‘›2

Example:

6

Representation of Graphs

1 2

35

4

1 2 3 4 5

1 0 1 0 1 0

2 1 0 1 1 0

3 0 1 0 0 1

4 1 1 0 0 1

5 0 0 1 1 0

1

2

3

4

5

2 4 None

1 4 3 None

2 5 None

3 4 None

5 2 1 None

adjacency matrix adjacency lists

Page 7: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Details:

โ€ข With edge weights, matrix entries are weightes (instead of 0/1) (implicitly: weight 0 = edge does not exist)

โ€ข Directed graphs: one entry per directed edgeโ€“ Edge from ๐‘– to ๐‘—: entry in row ๐‘– and column ๐‘—

โ€ข Undirected graphs: two entries per edgeโ€“ Matrix in this case is symmetric

Properties Adjacency Matrix:

โ€ข Memory-efficient if ๐ธ = ๐‘š โˆˆ ฮ˜ ๐‘‰ 2 = ฮ˜ ๐‘›2

โ€“ In particular for unweighted graphs: only one bit per matrix entry

โ€ข Not memory-efficient for sparse graphs (๐‘š โˆˆ ๐‘œ ๐‘›2 )

โ€ข For certain algorithms, the โ€œrightโ€ data structure

โ€ข โ€œEdge between ๐‘ข and ๐‘ฃโ€ can be answered in time ๐‘‚ 1

7

Adjacency Matrix

Page 8: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Structure

โ€ข An array with all the nodes

โ€ข Entries in this node array:โ€“ Linked lists with all edges of the corresponding nodes

Properties

โ€ข Memory-efficient for sparse graphs

โ€ข Memory-usage always (almost) asymptotically optimalโ€“ but for dense graphs, still much worseโ€ฆ

โ€“ To be precise: one actually requires ๐‘‚ log๐‘› bits per node

โ€ข Queries for specific edges not very efficientโ€“ If necessary, one can use an additional data structure (e.g., a hash table)

โ€ข For many algorithms, the โ€œrightโ€ data structure

โ€ข E.g., for depth first search and breadth first search8

Adjacency Lists

Page 9: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Examples from [CLRS]:

9

Examples

Page 10: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Graph Traversal (also: graph exploration) informally

โ€ข Given: a graph ๐บ = ๐‘‰, ๐ธ and a node ๐‘  โˆˆ ๐‘‰, visit all nodes that are reachable from ๐‘  in a โ€œsystematicโ€ way.

โ€ข We have already seen this for binary trees.

โ€ข As for trees, there are two basic approaches

โ€ข Breadth First Search (BFS)โ€“ first โ€œto the breadthโ€ (nodes closer to ๐‘  first)

โ€ข Depth First Search (DFS)โ€“ first โ€œto the depthโ€ (visit everything that can be reached from some

neighbor of the current node, before going to the next neighbor)

โ€ข Graph traversal is important as it is often used as a subroutine in other algorithms.โ€“ E.g., to compute the connected components of a graph

โ€“ We will see a few examplesโ€ฆ

10

Graph Traversal

Page 11: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn 11

Traversal of a Binary Search Tree

1

2

3

4

5

6

7

8

9

10

11

1

2

3

4 5

6

7

8

9

10 11

1 2

3

4

5

6

7

8 9

10

11 1

2 3

4 5 6 7

8 9 10

11

Goal: visit all nodes of a binary search tree once

In-Order (DFS): Pre-Order (DFS):

Post-Order (DFS): Level-Order (BFS):

Page 12: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

โ€ข Solution with a FIFO queue:โ€“ If a node is visited, its children are inserted into the queue.

BFS-Traversal:

Q = new Queue()

Q.enqueue(root)

while not Q.empty() do

node = Q.dequeue()

visit(node)

if node.left != null

Q.enqueue(node.left)

if node.right != null

Q.enqueue(node.right)

12

BFS Traversal

Page 13: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Differences binary tree ๐‘ปโ‡” general graph ๐‘ฎ

โ€ข Graph ๐บ can contain cycles

โ€ข In ๐‘‡, we have a root and every node know the direction towards the root.โ€“ Such trees are very often also called โ€œrooted treesโ€

BFS Traversal in graph ๐‘ฎ (start at node ๐’” โˆˆ ๐‘ฝ)

โ€ข Cycles: mark nodes that we have already seen

โ€ข Mark node ๐‘ , then insert ๐‘  into the queue

โ€ข As before, take first node ๐‘ข from the queue:โ€“ visit node ๐’–

โ€“ Go through the neighbors ๐‘ฃ of ๐‘ขIf ๐‘ฃ is not marked, mark ๐‘ฃ and insert ๐‘ฃ into the queueIf ๐‘ฃ is marked, there is nothing to be done

13

BFS Traversal of General Graphs

Page 14: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

โ€ข At the beginning ๐‘ฃ.marked is set to ๐Ÿ๐š๐ฅ๐ฌ๐ž for all nodes ๐‘ฃ

BFS-Traversal(s):

for all u in V: u.marked = false;

Q = new Queue()

s.marked = true

Q.enqueue(s)

while not Q.empty() do

u = Q.dequeue()

visit(u)

for v in u.neighbors do

if not v.marked then

v.marked = true;

Q.enqueue(v)

14

BFS Traversal of General Graphs

Page 15: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

876 54 32 9 1

15

BFS Traversal Exmaple

1

2

3

4

56

8

7

9

FIFO Queue:

1

2

3

4

56

8

7

9

1

2

3

4

56

8

7

9

๐’” 1

9 3 2

Visiting Order:

45 8 7

6

BFS spanning tree

Page 16: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

In the following, we label nodes as follows

โ€ข white nodes: Knoten, welche der Alg. noch nicht gesehen hat

โ€ข gray (before: blue) nodes: marked nodesโ€“ Nodes become gray when they are inserted into the queue.

โ€“ Nodes are gray, as long as they are in the queue.

โ€ข black (before: red) nodes: visited nodesโ€“ Nodes become black when they are removed from the queue.

16

Analysis BFS Traversal

Page 17: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

The running time of a BFS traversal is ๐‘ถ(๐’ +๐’Ž).โ€“ Assumption: graph given as adjacency lists

โ€ข If the graph is given by an adjacency matrix, the running time is ฮ˜ ๐‘›2 .

โ€“ white nodes: nodes that the algorithm has not seen yet

โ€“ gray (before: blue) nodes: marked nodes

โ€“ black (before: red) nodes: visited nodes

โ€ข Every node is inserted at most once into the queue.โ€“ In total, there are therefore ๐‘‚ ๐‘› queue operations.

โ€ข If node ๐‘ฃ gets removed from the queue, the algorithm looks at all its nighbors.โ€“ Every directed edge is considered once.

โ€“ Adjacency lists: time cost per node = ๐‘‚ #neighbors

โ€ข One has to go through the neighbor list once.

โ€“ Adjacency matrix: time cost per node = ๐‘‚ ๐‘›

โ€ข One has to go through a whole row of the matrix.17

Analysis BFS Traversal

Page 18: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Tree:

โ€ข A connected, undirected graph without cyclesโ€“ potentially also a directed graph, but then the graph must not have cycles,

even when ignoring the directions.

Spanning Tree of a Graph ๐‘ฎ:

โ€ข A subgraph ๐‘‡ such that ๐‘‡ is a tree containing all nodes of ๐บโ€“ Subgraph: Subset of the nodes and edges of ๐บ such that they together

define a graph.

18

Trees, Spanning Trees

Page 19: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn 19

BFS Tree

12

34

5 6

8

7

9

12

34

5 6

8

7

9

If ๐‘ฎ is directed: all nodes are reachable from ๐’”

In a BFS traversal, we can construct a spanning tree as follows(if ๐บ is connected):

โ€ข Every node ๐‘ข stores, from which node ๐‘ฃ it was marked

โ€ข Node ๐‘ฃ then becomes the parent node of ๐‘ขโ€“ Because every node is marked exactly once, the parent of each node is

defined in a unique way, ๐‘  is the root and has no parent.

Page 20: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

โ€ข We additionaly store the distance from ๐‘  in the tree.

BFS-Tree(s):

Q = new Queue();for all u in V: u.marked = false;s.marked = true;s.parent = NULL;s.d = 0Q.enqueue(s)while not Q.empty() do

u = Q.dequeue()visit(u)for v in u.neighbors do

if not v.marked thenv.marked = true;

v.parent = u;

v.d = u.d + 1;

Q.enqueue(v)

20

BFS Tree: Pseudocode

๐‘ 

๐‘ข

๐‘ฃ

u.d

v.d = u.d + 1

Page 21: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

In the BFS tree of an unweighted graph, the distance from the root ๐’”to each node ๐’– is equal to ๐’…๐‘ฎ ๐’”, ๐’– .

โ€ข Tree distance from the root: ๐‘‘๐‘‡ ๐‘ , ๐‘ข = ๐‘ข. ๐‘‘

โ€ข We therefore need to show that ๐‘ข. ๐‘‘ = ๐‘‘๐บ ๐‘ , ๐‘ข

โ€ข It definitely holds that ๐’–. ๐’… โ‰ฅ ๐’…๐‘ฎ ๐’”, ๐’–โ€“ Because ๐‘ข. ๐‘‘ = ๐‘‘๐‘‡ ๐‘ , ๐‘ข , this is equivalent to ๐‘‘๐‘‡ ๐‘ , ๐‘ข โ‰ฅ ๐‘‘๐บ ๐‘ , ๐‘ข

โ€“ This of course holde because every path in ๐‘‡ is also a path in ๐บ, the distance in ๐‘‡ can therefore not be smaller than the distance in ๐บ.

21

Analysis BFS Traversal

Page 22: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Lemma: Assume that during BFS traversal, the state of the queue is๐‘„ = ๐‘ฃ1, ๐‘ฃ2, โ€ฆ , ๐‘ฃ๐‘Ÿ ๐‘ฃ1: head, ๐‘ฃ๐‘Ÿ: tail

Then, ๐‘ฃ๐‘Ÿ . ๐‘‘ โ‰ค ๐‘ฃ1. ๐‘‘ + 1 and ๐‘ฃ๐‘– . ๐‘‘ โ‰ค ๐‘ฃ๐‘–+1. ๐‘‘ (for ๐‘– = 1,โ€ฆ , ๐‘Ÿ โˆ’ 1)

Proof: By induction on the queue operations

โ€ข Base: At the beginning, only ๐‘  with ๐‘ . ๐‘‘ = 0 is in the queue.

โ€ข Step:โ€“ dequeue operation: ๐‘„ = ๐‘ฃ1, ๐‘ฃ2, โ€ฆ , ๐‘ฃ๐‘Ÿ , ๐‘ฃ๐‘Ÿ . ๐‘‘ โ‰ค ๐‘ฃ1. ๐‘‘ + 1 โ‰ค ๐‘ฃ2. ๐‘‘ + 1

โ€“ enqueue operation: ๐’– , ๐‘ฃ1, ๐‘ฃ2, โ€ฆ , ๐‘ฃ๐‘Ÿ , ๐’—

โ€“ From the induction hypothesis:

๐’—. ๐’… โ‰ค ๐’—๐Ÿ. ๐’… + ๐Ÿ: ๐‘ฃ. ๐‘‘ = ๐‘ข. ๐‘‘ + 1 โ‰ค ๐‘ฃ1. ๐‘‘ + 1

๐’—๐’“. ๐’… โ‰ค ๐’—. ๐’…: ๐‘ฃ๐‘Ÿ . ๐‘‘ โ‰ค ๐‘ข. ๐‘‘ + 1 = ๐‘ฃ. ๐‘‘

22

Analysis BFS Traversal

induction hypothesis

๐’– ๐’—

most recently deleted node

new node in the queue

When ๐‘ฃ is inserted into the queue, the last removed node

๐‘ข is getting processed(๐‘ฃ is a neighbor of ๐‘ข).

โŸน ๐‘ฃ. ๐‘‘ = ๐‘ข. ๐‘‘ + 1

Page 23: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

In the BFS tree of an unweighted graph, the distance from the root ๐’”to each node ๐’– is equal to ๐’…๐‘ฎ ๐’”, ๐’– .

โ€ข Proof by contradiction:โ€“ Assumption: ๐‘ฃ is node with smallest ๐‘‘๐บ(๐‘ , ๐‘ฃ) for which ๐’—. ๐’… > ๐’…๐‘ฎ(๐’”, ๐’—)

23

Analysis BFS Traversal

๐‘ 

๐‘ข

๐‘ฃ ๐‘‘๐บ ๐‘ , ๐‘ฃ = ๐‘‘๐บ ๐‘ , ๐‘ข + 1

๐‘ข. ๐‘‘ = ๐‘‘๐บ ๐‘ , ๐‘ข

๐’—. ๐’… > ๐‘‘๐บ ๐‘ , ๐‘ฃ = ๐‘‘๐บ ๐‘ , ๐‘ข + 1 = ๐’–. ๐’… + ๐Ÿ

Consider dequeue of ๐’–:โ€ข ๐‘ฃ will be considered as neighbor of ๐‘ข

โ€ข ๐‘ฃ is white โŸน๐‘ฃ.๐‘‘ = ๐‘ข. ๐‘‘ + 1

โ€ข ๐‘ฃ is black โŸน๐‘ฃ.๐‘‘ โ‰ค ๐‘ข. ๐‘‘

โ€ข ๐‘ฃ is gray โŸน๐‘ฃ is in the queueLemma: ๐‘ฃ. ๐‘‘ โ‰ค ๐‘ข. ๐‘‘ + 1

Page 24: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Basic idea DFS traversal in ๐‘ฎ (start at node ๐’” โˆˆ ๐‘ฝ)

โ€ข Mark node ๐’— (at the beginning ๐‘ฃ = ๐‘ )

โ€ข Visit the neighbors of ๐‘ฃ one after the other recursively

โ€ข After all neighbors are visited, visit ๐’—

โ€ข Recursively: While visiting the neighbors, visit their neighbors and while doing this their neighbors, etc.

โ€ข Cycles in G: Only visit nodes that have not been marked

โ€ข Corresponds to the post-order traversal in trees.

โ€ข The order in which the nodes are marked corresponds to thepre-order traversal in trees.

24

Depth-First Search in General Graphs

Page 25: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

DFS-Traversal(s):

for all u in V: u.color = white;

DFS-visit(s, NULL)

DFS-visit(u, p):

u.color = gray;

u.parent = p;

for all v in u.neighbors do

if v.color = white

DFS-visit(v, u)

visit node u;

u.color = black;

25

DFS Traversal: Pseusocode

Page 26: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn 26

DFS Traversal: Example

1 2

4 5

3

6

7

8๐’”

Page 27: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

In the same way as for a BFS traversal, one can also construct a spanning tree when doing a DFS traversal.

The running time of a DFS traversal is ๐‘ถ ๐’ +๐’Ž .

โ€ข We color the nodes white, gray, and black as beforeโ€“ not marked = white, marked = gray, visited = schwarz

โ€ข The recursive DFS traversal function is called at most once for every node.

โ€ข The time to process a node ๐‘ฃ is proportional to the number of(outgoing) edges of ๐‘ฃ

27

DFS Traversal: Analysis

Page 28: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

โ€ข The connected components (or simply components) of a graph are its connected parts.

Goal: Find all components of a graph.

for u in V do

if not u.marked then

start new component

explore with DFS/BFS starting at u

โ€ข The connected components of a graph can be identified in time ๐‘‚ ๐‘› +๐‘š . (by using a DFS or a BFS traversal)

28

Connected Components

1

2

35

4

9

8 10

67 13

14

12

11

Page 29: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

We define the following two times for each node ๐‘ฃ

โ€ข ๐‘ก๐‘ฃ,1: time, when ๐‘ฃ is colored gray in the DFS traversal

โ€ข ๐‘ก๐‘ฃ,2: time, when ๐‘ฃ is colored black in the DFS traversal

Theorem: In the DFS tree, a node ๐‘ฃ is in the subtree of a node ๐‘ข, if

and only if the interval ๐‘ก๐‘ฃ,1, ๐‘ก๐‘ฃ,2 is completely contained in the

interval ๐‘ก๐‘ข,1, ๐‘ก๐‘ข,2 .

Example:

29

DFS โ€œParenthesisโ€ Theorem

f e

a b

d

c๐’”

๐’•๐’‚,๐Ÿ ๐’•๐’‡,๐Ÿ ๐’•๐’†,๐Ÿ๐’•๐’…,๐Ÿ ๐’•๐’„,๐Ÿ ๐’•๐’„,๐Ÿ๐’•๐’ƒ,๐Ÿ ๐’•๐’ƒ,๐Ÿ๐’•๐’…,๐Ÿ ๐’•๐’†,๐Ÿ ๐’•๐’‡,๐Ÿ ๐’•๐’‚,๐Ÿ

[ [ [ [ [ ] [ ] ] ] ] ]

Page 30: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Theorem: In the DFS tree, a node ๐‘ฃ is in the subtree of a node ๐‘ข, if

and only if the interval ๐‘ก๐‘ฃ,1, ๐‘ก๐‘ฃ,2 is completely contained in the

interval ๐‘ก๐‘ข,1, ๐‘ก๐‘ข,2 .

Why is this useful?

โ€ข Improves our understanding of the structure of theresulting DFS tree

โ€ข We need the theorem, e.g., to prove the correctness of thealgorithm for computing a topological sort.

30

DFS โ€œParenthesisโ€ Theorem

Page 31: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Theorem: In the DFS tree, a node ๐‘ฃ is in the subtree of a node ๐‘ข, if

and only if the interval ๐‘ก๐‘ฃ,1, ๐‘ก๐‘ฃ,2 is completely contained in the

interval ๐‘ก๐‘ข,1, ๐‘ก๐‘ข,2 .

Proof:

โ€ข Gray nodes always form a path that starts at node ๐‘ .โ€“ Path starts at ๐‘ , currently active node at the end of the path

โ€“ New node ๐‘ค becomes gray โŸน๐‘ค neighbor of active node

โ€“ Node becomes black โŸน active node ends recursion

โ€ข Node ๐‘ฃ is in the subtree of u ๐‘ข if and only if ๐‘ข is part of the path, when ๐‘ฃ becomes gray and thus iff ๐‘ก๐‘ข,1 < ๐‘ก๐‘ฃ,1 < ๐‘ก๐‘ข,2.

โ€ข NOde ๐‘ฃ in this case is further to the end in the path than ๐‘ข and has to become black before node ๐‘ข, hence ๐‘ก๐‘ฃ,2 < ๐‘ก๐‘ข,2

31

DFS โ€œParenthesisโ€ Theorem

๐’” ๐’˜๐’ƒ ๐’„ ๐’… ๐’–

Page 32: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Theorem: In the DFS tree, a node ๐‘ฃ is in the subtree of a node ๐‘ข, if

and only if the interval ๐‘ก๐‘ฃ,1, ๐‘ก๐‘ฃ,2 is completely contained in the

interval ๐‘ก๐‘ข,1, ๐‘ก๐‘ข,2 .

Implications

โ€ข Two intervals are always either disjoint or one of the intervals is contained in the other.

โ€ข Why โ€œParenthesisโ€ Theorem?If for each ๐‘ก๐‘ฃ,1 we write an open parenthesis and for each ๐‘ก๐‘ฃ,2 we write a close parenthesis, one gets an expression in which the parentheses are nested properly.

โ€ข A white node ๐‘ฃ, which is discovered in the recursive traversal started at ๐‘ข becomes black before the recursion gets back to ๐‘ข.

32

DFS โ€œParenthesisโ€ Theorem

Page 33: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Theorem: In a DFS tree, a node ๐‘ฃ is in the subtree of a node ๐‘ข, if and only if immediately before marking node ๐‘ข, a completely white path from ๐‘ข to ๐‘ฃ exists.

Proof:

โ€ข Proof by contradiction: Assume that there is a node ๐‘ฃ to which there is a white path, but that node ๐‘ฃ is not in the subtree of ๐‘ข.

โ€ข Assume that ๐‘ฃ is such a node with the additional property that immediately before marking ๐‘ข, ๐‘ฃ has the shortest white path from ๐‘ข among all such nodes.

33

White Paths

at time ๐‘ก๐‘ข,1

๐’” ๐’˜๐’– ๐’—

Page 34: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Tree Edges:

โ€ข (๐‘ข, ๐‘ฃ) is a tree edge, if node๐‘ฃ is discovered from node ๐‘ข

โ€“ When considering ๐‘ข, ๐‘ฃ , ๐‘ฃ is white

Backward Edges:

โ€ข ๐‘ข, ๐‘ฃ is a backward edge if๐‘ฃ is a predecessor node of ๐‘ข

โ€“ When considering ๐‘ข, ๐‘ฃ , ๐‘ฃ is gray

Forward Edges:

โ€ข ๐‘ข, ๐‘ฃ is a forward edges if๐‘ฃ is a successor node of ๐‘ข

โ€“ When considering ๐‘ข, ๐‘ฃ , ๐‘ฃ is black

Cross Edges:

โ€ข All other edges

โ€“ When considering ๐‘ข, ๐‘ฃ , ๐‘ฃ is black34

Classification of Edges (in DFS)

1

2

3

4

5

6

8

7

9

Page 35: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Tree Edge (๐’–, ๐’—):

โ€ข Node ๐‘ฃ is โ€œdiscoveredโ€ as white neighbor of ๐‘ขโ€“ If when considering (๐‘ข, ๐‘ฃ), ๐’— is white โŸน ๐’–,๐’— tree edge

Backward Edge (๐’–, ๐’—):

โ€ข Subtree of ๐‘ข will be completely visited, before ๐‘ฃ becomes blackโ€“ If when considering (๐‘ข, ๐‘ฃ) ๐’— is gray โŸน ๐‘ข, ๐‘ฃ backward edge

Forward Edge (๐’–, ๐’—):

โ€ข ๐‘ฃ is in a subtree of ๐‘ข that has already been visited completely

โ€“ Since ๐‘ฃ is in a subtree of ๐‘ข, ๐’— is schwarz and ๐’•๐’—,๐Ÿ > ๐’•๐’–,๐Ÿ

Cross Edge (๐’–, ๐’—):

โ€ข As long as ๐‘ข is gray, all newly visited nodes are in the subtree of ๐‘ข, ๐‘ฃ was therefore visited before ๐‘ข: ๐’— is black and ๐’•๐’—,๐Ÿ < ๐’•๐’–,๐Ÿ.

35

Classification of Edges (in DFS)

๐’– ๐’—

๐’– ๐’—

๐’– ๐’—

๐’– ๐’—

๐’•๐’–,๐Ÿ < ๐’•๐’—,๐Ÿ < ๐’•๐’—,๐Ÿ < ๐’•๐’–,๐Ÿ

๐’•๐’—,๐Ÿ < ๐’•๐’—,๐Ÿ < ๐’•๐’–,๐Ÿ < ๐’•๐’–,๐Ÿ

Page 36: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

โ€ข In undirected graphs, every edge ๐‘ข, ๐‘ฃ is considered twice(once from ๐‘ข and once from ๐‘ฃ)

โ€ข We classify the edge according to the first consideration.

Theorem: In a DFS traversal in an undirected graph, every edge is either a tree edge or a backward edge.

Proof:

โ€ข W.l.o.g., we assume that ๐‘ข becomes gray before ๐‘ฃ becomes gray.

โ€ข From the theorem about white paths, we know that ๐‘ฃ is visited as long as ๐‘ข is still gray (๐‘ฃ is in the subtree of ๐‘ข).

โ€ข If the edge ๐‘ข, ๐‘ฃ is first considered from ๐‘ข, node ๐‘ฃ is still whiteโŸน ๐‘ข, ๐‘ฃ is a tree edge.

โ€ข If the edge ๐‘ข, ๐‘ฃ is first considered from ๐‘ฃ, node ๐‘ข is still grayโŸน ๐‘ข, ๐‘ฃ is a backward edge.

36

DFS โ€“ Undirected Graphs

Page 37: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Theorem: A directed graph has no cycles if and only if during a DFS traversal, there are no backward edges.

backward edge โŸน cycle:

cycle โŸน backward edge:

37

DFS โ€“ Directed Graphs

๐’–

๐’—backward edge๐‘ข must be in the

subtree of ๐‘ฃ.

๐’–

๐’—

๐’–

๐’— ๐‘ข can also be a child of ๐‘ฃ.

assumption: ๐‘ฃ isthe first considered node in the cycle .

white path:๐‘ข is in the

subtree of ๐‘ฃ.

backward edge Implication:

In directed graphs,one can test in time๐‘‚ ๐‘› +๐‘š if the graph is acyclic.

Page 38: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Directed Acyclic Graphs:

โ€ข DAG: directed acyclic graph

โ€ข E.g., models time dependencies between tasks

โ€ข Example: putting on pieces of clothes

Topological sort:

โ€ข Sort the nodes of a DAG in such a way that ๐‘ข appears before ๐‘ฃ if a directed path from ๐‘ข to ๐‘ฃ exists.

โ€ข In the example: Find a possible dressing order

38

Application: Topological Sort

T-Shirt

pants

sweater

socks

coat

shoesunderpants

glasses

Page 39: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Directed Acyclic Graphs:

โ€ข represent partial ordersโ€“ asymmetric: ๐‘Ž < ๐‘ โŸน ยฌ ๐‘ < ๐‘Ž

transitive: ๐‘Ž < ๐‘ โˆง ๐‘ < ๐‘ โŸน ๐‘Ž < ๐‘

โ€“ partial order: not all pairs need to be comparable

โ€ข Example: subset relation for sets

Topological Sort:

โ€ข Sort the nodes of a DAG in such a way that ๐‘ข appears before ๐‘ฃ if a directed path from ๐‘ข to ๐‘ฃ exists.

โ€ข Extend a partial order to a total order:

โˆ…, 1 , 2 , 3 , 1,2 , 1,3 , 2,3 , {1,2,3}

39

Topological Sort: A bit more formallyโ€ฆ

โˆ…

{1}

{2}

{3}

{1,2}

{1,3}

{2,3}

{1,2,3}

Page 40: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Do a DFS โ€ฆ

Observation:

โ€ข Nodes without successor are visited first (colored black)

โ€ข Visiting order is a reverse topological sort order

40

Topological Sort: Algorithm

1 2 3 4 5

6 7 8 9

11 12 13 14

10

๐’•๐Ÿ,๐Ÿ ๐’•๐Ÿ,๐Ÿ ๐’•๐Ÿ‘,๐Ÿ ๐’•๐Ÿ—,๐Ÿ ๐’•๐Ÿ๐ŸŽ,๐Ÿ ๐’•๐Ÿ๐ŸŽ,๐Ÿ ๐’•๐Ÿ“,๐Ÿ ๐’•๐Ÿ“,๐Ÿ ๐’•๐Ÿ—,๐Ÿ ๐’•๐Ÿ‘,๐Ÿ ๐’•๐Ÿ•,๐Ÿ ๐’•๐Ÿ–,๐Ÿ ๐’•๐Ÿ’,๐Ÿ ๐’•๐Ÿ’,๐Ÿ ๐’•๐Ÿ–,๐Ÿ

๐’•๐Ÿ•,๐Ÿ ๐’•๐Ÿ,๐Ÿ ๐’•๐Ÿ”,๐Ÿ ๐’•๐Ÿ”,๐Ÿ ๐’•๐Ÿ,๐Ÿ ๐’•๐Ÿ๐Ÿ,๐Ÿ ๐’•๐Ÿ๐Ÿ,๐Ÿ๐’•๐Ÿ๐Ÿ,๐Ÿ ๐’•๐Ÿ๐Ÿ,๐Ÿ ๐’•๐Ÿ๐Ÿ‘,๐Ÿ ๐’•๐Ÿ๐Ÿ’,๐Ÿ ๐’•๐Ÿ๐Ÿ’,๐Ÿ ๐’•๐Ÿ๐Ÿ‘,๐Ÿ

1. 2. 3.4.

5.

6.

7.

8. 9.

10.11.

12.

13.

14.

Page 41: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Theorem: The reversed โ€œvisitโ€ order (coloring black) of the nodesin a DFS traversal gives a topological sort of a directed acyclic graph.

Proof:

โ€ข We must show that for every edge ๐‘ข, ๐‘ฃ , node ๐‘ฃ becomes black before node ๐‘ข.

โ€ข Case 1: ๐’– becomes gray before ๐’—:

โ€“ Then, ๐‘ฃ is in the subtree of ๐‘ข and therefore ๐‘ก๐‘ข,1 < ๐‘ก๐‘ฃ,1 < ๐‘ก๐‘ข,2.

From the parenthesis theorem, we then also get ๐‘ก๐‘ฃ,2 < ๐‘ก๐‘ข,2.

โ€ข Case 2: ๐’— becomes gray before ๐’–:โ€“ ๐‘ข can only become gray before ๐‘ฃ becomes black, if ๐‘ข is in the subtree of ๐‘ฃ.

Then we would have a directed path from ๐‘ฃ to ๐‘ขโŸน cycle!

41

Topological Sort: Algorithm

๐’– ๐’—

๐’– ๐’—

๐’– ๐’—โŸน

๐’– ๐’—โŸน

Page 42: Algorithms and Data Structures - uni-freiburg.de

Algorithms and Data StructuresFabian Kuhn

Strongly Connected Components

โ€ข Strongly connected component of a directed graph:โ€œMaximal subset of nodes s. t. every node can reach every othernodeโ€

โ€ข Requires 2 DFS traversals (time = ๐‘‚ ๐‘š + ๐‘› )โ€“ on ๐บ and on ๐บ๐‘‡ (all edges reversed)

โ€“ ๐บ and ๐บ๐‘‡ have the same strongly connected components

โ€ข Details, e.g., in [CLRS]42

DFS Traversal: Further Application

Picture: Wikipedia