Algorithms and Data StructuresFabian Kuhn
Lecture 8
Graph Algorithms I: BFS and DFS Traversal
Algorithms and Data Structures
Fabian Kuhn
Algorithms and Complexity
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)
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
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: ๐
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
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
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
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
Algorithms and Data StructuresFabian Kuhn
Examples from [CLRS]:
9
Examples
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
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):
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
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
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
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
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
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
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
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.
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
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
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
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
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
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
Algorithms and Data StructuresFabian Kuhn 26
DFS Traversal: Example
1 2
4 5
3
6
7
8๐
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
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
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๐
๐๐,๐ ๐๐,๐ ๐๐,๐๐๐ ,๐ ๐๐,๐ ๐๐,๐๐๐,๐ ๐๐,๐๐๐ ,๐ ๐๐,๐ ๐๐,๐ ๐๐,๐
[ [ [ [ [ ] [ ] ] ] ] ]
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
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
๐ ๐๐ ๐ ๐ ๐
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
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
๐ ๐๐ ๐
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
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)
๐ ๐
๐ ๐
๐ ๐
๐ ๐
๐๐,๐ < ๐๐,๐ < ๐๐,๐ < ๐๐,๐
๐๐,๐ < ๐๐,๐ < ๐๐,๐ < ๐๐,๐
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
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.
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
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}
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.
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
๐ ๐
๐ ๐
๐ ๐โน
๐ ๐โน
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