Undirected Graph G = (V,E)
CSE 421: Introduction to 1 2 Algorithms 10 3 11 Graph Traversal 12 4 8 Paul Beame 13 5 9 6
1 7 2
Directed Graph G = (V,E) Graph Traversal
1 Learn the basic structure of a graph Walk from a fixed starting vertex s to 2 10 find all vertices reachable from s 3
11 Three states of vertices 12 4 8 unvisited visited/discovered 13 fully-explored 5 9 6
7 3 4
Generic Graph Traversal Algorithm Generic Traversal Always Works
Claim: At termination R is the set of nodes Find: set R of vertices reachable from s∈∈∈V reachable from s Proof ⊆ ∈∈∈ Reachable( s): : For every node v R there is a path from s to v ⊇: Suppose there is a node w∉∉∉R reachable from s R←←← {s} via a path P ∉∉∉ While there is a ( u,v )∈∈∈E where u∈∈∈R and v∉∉∉R Take first node v on P such that v R Predecessor u of v in P satisfies Add v to R u ∈∈∈ R
(u,v) ∈∈∈E
But this contradicts the fact that the algorithm exited the while loop.
5 6
1 Breadth-First Search BFS(s)
Completely explore the vertices in order Global initialization: mark all vertices “unvisited” of their distance from s BFS( s) ← ← mark s “visited”; R {s}; layer L 0 {s}
while Li not empty ←←←∅∅∅ Li+1 ∈∈∈ For each u Li Naturally implemented using a queue for each edge { u,v} if ( v is “unvisited”) mark v “visited”
Add v to set R and to layer Li+1 mark u “fully-explored” i ←←← i+1
7 8
Properties of BFS (v) Properties of BFS
BFS( s) visits x if and only if there is a path in G from On undirected graphs s to x. All non-tree edges join vertices on the same or adjacent layers Edges followed to undiscovered vertices define a “breadth first spanning tree" of G Suppose not Layer i in this tree, L Then there would be vertices (x,y) such that i ∈∈∈ ∈∈∈ >>> x Li and y Lj and j i+1 those vertices u such that the shortest path in G from the root s is of length i. Then, when vertices incident to x are considered in BFS y would be added to Li+1 On undirected graphs and not to Lj All non-tree edges join vertices on the same or adjacent layers
9 10
Graph Search Application: BFS Application: Shortest Paths Connected Components
Tree gives shortest 1 0 Want to answer questions of the paths from start vertex form: 2 3 1 Given : vertices u and v in G
4 Is there a path from u to v? 6 Q: Why 7 not create 2 Idea: create array A such that 9 5 an array A[u] = smallest numbered vertex Path [u,v]? 11 3 that is connected to u 12 10 question reduces to whether A[u]= A[v]? 8 can label by distances from start 4 13 11 12
2 Graph Search Application: Connected Components DFS(u) – Recursive version
initial state: all v unvisited ← for s 1 to n do Global Initialization: mark all vertices "unvisited" if state( s) ≠ “fully-explored” then BFS( s): setting A[u] ←s for each u found DFS( u) (and marking u visited/fully-explored) mark u “visited” and add u to R endif for each edge { u,v} endfor if ( v is “unvisited”) DFS( v) Total cost: O(n+m) end for each vertex is touched once in this outer mark u “fully-explored” procedure and the edges examined in the different BFS runs are disjoint
works also with Depth First Search
13 14
Properties of DFS(s) Non-tree edges
Like BFS( s): All non-tree edges join a vertex and one DFS( s) visits x if and only if there is a path in G from s to x of its descendents/ancestors in the DFS
Edges into undiscovered vertices define a "depth tree first spanning tree" of G Unlike the BFS tree:
the DFS spanning tree isn't minimum depth its levels don't reflect min distance from the root No cross edges. non-tree edges never join vertices on the same or adjacent levels BUT…
15 16
No cross edges in DFS on undirected Applications of Graph Traversal: graphs Bipartiteness Testing
Claim: During DFS(x) every vertex marked visited is Easy: A graph G is not bipartite if it contains a descendant of x in the DFS tree T an odd length cycle Claim: For every x,y in the DFS tree T, if (x,y) is an WLOG: G is connected edge not in T then one of x or y is an ancestor of the Otherwise run on each component other in T Simple idea: start coloring nodes starting at a Proof: given node s One of x or y is visited first, suppose WLOG that x is visited first and therefore DFS(x) was called before DFS(y) Color s red Color all neighbors of s blue During DFS(x), the edge (x,y) is examined
Since (x,y) is a not an edge of T, y was visited when the Color all their neighbors red edge (x,y) was examined during DFS(x) If you ever hit a node that was already colored
Therefore y was visited during the call to DFS(x) so y is a the same color as you want to color it, ignore it descendant of x. the opposite color, output error
17 18
3 BFS gives Bipartiteness Why does it work?
s Run BFS assigning all vertices from
layer Li the color i mod 2 u and v have a common ancestor i.e. red if they are in an even layer, blue if L in an odd layer i
If there is an edge joining two vertices Cycle length 2(j-i)+1 from the same layer then output “Not Bipartite”
Lj Lj u v
19 20
DFS(v) for a directed graph DFS(v)
1 1 tree edges 2 2 10 10 3 3 forward edges 11 11 12 back edges 12 4 8 4 8 13 13 5 9 5 9 ← cross edges 6 6 NO → cross edges 7 21 7 22
Properties of Directed DFS Directed Acyclic Graphs
Before DFS(s) returns, it visits all A directed graph G=(V,E) is acyclic if it previously unvisited vertices reachable has no directed cycles via directed paths from s
Terminology: A directed acyclic graph is
Every cycle contains a back edge in the also called a DAG DFS tree
23 24
4 Topological Sort Directed Acyclic Graph
Given: a directed acyclic graph (DAG) G=( V,E) Output: numbering of the vertices of G with distinct numbers from 1 to n so edges only go from lower number to higher numbered vertices
Applications nodes represent tasks edges represent precedence between tasks topological sort gives a sequential schedule for solving them
25 26
In-degree 0 vertices Topological Sort
Every DAG has a vertex of in-degree 0 Can do using DFS Proof: By contradiction
Suppose every vertex has some incoming edge
Consider following procedure: Alternative simpler idea:
while (true) do Any vertex of in-degree 0 can be given v←some predecessor of v number 1 to start
Remove it from the graph and then give a After n+1 steps where n=| V| there will be a repeated vertex vertex of in-degree 0 number 2, etc.
This yields a cycle, contradicting that it is a DAG
27 28
Topological Sort Topological Sort
1 1 2
29 30
5 Topological Sort Topological Sort
1 2 1 2
4 3 3
31 32
Topological Sort Topological Sort
1 2 1 2
4 4 3 3
5 5 6
33 34
Topological Sort Topological Sort
1 2 1 2
4 4 3 3 8 5 5 6 6
7 7
35 36
6 Topological Sort Topological Sort
1 2 1 2
4 4 3 3 8 8 5 5 6 6 9 9 10 7 7
37 38
Topological Sort Topological Sort
1 2 1 2
4 4 3 3 8 8 5 5 6 6 9 10 9 10 7 7 11 11 12
39 40
Topological Sort Topological Sort
1 2 1 2
4 4 3 3 8 8 5 5 6 6 9 10 9 10 7 7 11 12 11 12 13 13
41 14 42
7 Implementing Topological Sort
Go through all edges, computing in-degree for each vertex O(m+n)
Maintain a queue (or stack) of vertices of in-degree 0
Remove any vertex in queue and number it
When a vertex is removed, decrease in- degree of each of its neighbors by 1 and add them to the queue if their degree drops to 0
Total cost O(m+n)
43
8