cs 361 data structures & algs lecture 14hayes/teaching/cs361-fall... · identifying bfs &...
TRANSCRIPT
![Page 1: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/1.jpg)
CS 361Data Structures & Algs
Lecture 14
Prof. Tom HayesUniversity of New Mexico
10-7-2010
1
![Page 2: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/2.jpg)
Last Time
Inside BFS
Running Time
Implementation
Degrees & Degree sums
Properties of BFS and DFS trees
Identifying trees in the wild
2
![Page 3: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/3.jpg)
Today
More tree forensics
READING: Finish reading Chapter 3
3
![Page 4: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/4.jpg)
identifying BFS & DFS trees
Where is the root?
Key: Edges left out of the tree:
in BFS, level difference is {-1, 0, 1}
not incident with the root.
in DFS, join a node to its ancestor
4
![Page 5: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/5.jpg)
Welcome to the morgue!
Somebody constructed this tree. Please help me identify it.
5
![Page 6: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/6.jpg)
BFS or DFS tree?
6
![Page 7: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/7.jpg)
BFS or DFS tree?
Not BFS
7
![Page 8: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/8.jpg)
Where is the root (DFS)?
8
![Page 9: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/9.jpg)
Where is the root (DFS)?
Not here. Why?
9
![Page 10: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/10.jpg)
Where is the root (DFS)?
Not here. Why?
edge not to ancestor
10
![Page 11: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/11.jpg)
Where is the root (DFS)?
None of these.Why?
11
![Page 12: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/12.jpg)
Where is the root (DFS)?
None of these.Why?
edge not to ancestor
12
![Page 13: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/13.jpg)
Where is the root (DFS)?
Not these.Why?
13
![Page 14: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/14.jpg)
Where is the root (DFS)?
Not these.Why?
edge not to ancestor
14
![Page 15: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/15.jpg)
The root?
omitted edges go to an ancestor
Where is the root (DFS)?
15
![Page 16: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/16.jpg)
The root
Where is the root (DFS)?
What order were the nodes found?
16
![Page 17: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/17.jpg)
The root
Where is the root (DFS)?
What order were the nodes found?
1
2 3
4
5
6
17
![Page 18: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/18.jpg)
The root
Where is the root (DFS)?
What order were the nodes found?
1
2 3
4
5
6
18
![Page 19: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/19.jpg)
Problem 3.6
Given: Connected graph G and vertex u. DFS and BFS, both started from u, give us the same tree, T.
Prove: G = T.
19
![Page 20: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/20.jpg)
Problem 3.6
Given: Connected graph G and vertex u. DFS and BFS, both started from u, give us the same tree, T.
Prove: G = T.
Proof by contradiction: Suppose G≠T.
20
![Page 21: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/21.jpg)
Problem 3.6
Given: Connected graph G and vertex u. DFS and BFS, both started from u, give us the same tree, T.
Prove: G = T.
Proof by contradiction: Suppose G≠T. Let e = {v,w} be an edge in G, not in T.
21
![Page 22: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/22.jpg)
Problem 3.6
Given: Connected graph G and vertex u. DFS and BFS, both started from u, give us the same tree, T.
Prove: G = T.
Proof by contradiction: Suppose G≠T. Let e = {v,w} be an edge in G, not in T. Since T is a BFST, we know: ...................
Since T is a DFST, we know: ...................
22
![Page 23: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/23.jpg)
Problem 3.6Given: Connected graph G and vertex u. DFS and BFS, both started from u, give us the same tree, T.
Prove: G = T.
Proof by contradiction: Suppose G≠T. Let e = {v,w} be an edge in G, not in T. Since T is a BFST, we know: ...................
Since T is a DFST, we know: ...................
Homework: Finish this proof.23
![Page 24: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/24.jpg)
Problem 3.9
Given: Graph G with nodes s and t such that distance(s,t) = (n/2 +1). (n = #nodes)
Prove: We can disconnect this graph if we are allowed to delete one node.
24
![Page 25: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/25.jpg)
Problem 3.9
Given: Graph G with nodes s and t such that distance(s,t) ≥ n/2. (n = #nodes)
Prove: We can disconnect this graph if we are allowed to delete one node.
Proof: Let T be a BFS tree for G, rooted at s. What can we say about the levels?
25
![Page 26: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/26.jpg)
Problem 3.9
Given: Graph G with nodes s and t such that distance(s,t) ≥ n/2. (n = #nodes)
Prove: We can disconnect this graph if we are allowed to delete one node.
Proof: Let T be a BFS tree for G, rooted at s. What can we say about the levels?
Level 0 = {s}.
26
![Page 27: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/27.jpg)
Problem 3.9
Given: Graph G with nodes s and t such that distance(s,t) = n/2 + 1. (n = #nodes)
Prove: We can disconnect this graph if we are allowed to delete one node.
Proof: Let T be a BFS tree for G, rooted at s. What can we say about the levels?
Level 0 = {s}. Level(n/2 + 1) contains t.
27
![Page 28: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/28.jpg)
Problem 3.9Given: Graph G with nodes s and t such that distance(s,t) = n/2 + 1. (n = #nodes)
Prove: We can disconnect this graph if we are allowed to delete one node.
Proof: Let T be a BFS tree for G, rooted at s. What can we say about the levels?
Level 0 = {s}. Level(n/2 + 1) contains t.
No two levels overlap.
28
![Page 29: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/29.jpg)
Problem 3.9Given: Graph G with nodes s and t such that distance(s,t) = n/2 + 1. (n = #nodes)
Prove: We can disconnect this graph if we are allowed to delete one node.
Proof: Let T be a BFS tree for G, rooted at s. What can we say about the levels?
Level 0 = {s}. Level(n/2 + 1) contains t.
No two levels overlap. Some level i has size 1!
29
![Page 30: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/30.jpg)
Problem 3.9Given: Graph G with nodes s and t such that distance(s,t) = n/2 + 1. (n = #nodes)
Prove: We can disconnect this graph if we are allowed to delete one node.
Proof: Let T be a BFS tree for G, rooted at s. What can we say about the levels?
Level 0 = {s}. Level(n/2 + 1) contains t.
No two levels overlap. Some level i has size 1! (1 ≤ i ≤ n/2) HW: Finish this proof.
30
![Page 31: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/31.jpg)
Problem 3.2
Input: An undirected graph G.
Output: A cycle in G, if one exists, else throw new UnexpectedTreeException( )
31
![Page 32: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/32.jpg)
Problem 3.2
Input: An undirected graph G.
Output: A cycle in G, if one exists, else throw new UnexpectedTreeException( )
BFS or DFS?
32
![Page 33: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/33.jpg)
Problem 3.2
Input: An undirected graph G.
Output: A cycle in G, if one exists, else throw new UnexpectedTreeException( )
Let T be a DFS tree for G, rooted at u. Now what?
33
![Page 34: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/34.jpg)
Problem 3.2
Input: An undirected graph G.
Output: A cycle in G, if one exists, else throw new UnexpectedTreeException( )
Let T be a DFS tree for G, rooted at u. Now what? Suppose {v,w} is a back-edge for T.
34
![Page 35: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/35.jpg)
Problem 3.2
Input: An undirected graph G.
Output: A cycle in G, if one exists, else throw new UnexpectedTreeException( )
Let T be a DFS tree for G, rooted at u. Now what? Suppose {v,w} is a back-edge for T. w or v is ancestor of the other
35
![Page 36: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/36.jpg)
Problem 3.2
Input: An undirected graph G.
Output: A cycle in G, if one exists, else throw new UnexpectedTreeException( )
Let T be a DFS tree for G, rooted at u. Now what? Suppose {v,w} is a back-edge for T. Say w is ancestor of v.
36
![Page 37: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/37.jpg)
Problem 3.2
Input: An undirected graph G.
Output: A cycle in G, if one exists, else throw new UnexpectedTreeException( )
Let T be a DFS tree for G, rooted at u. Now what? Suppose {v,w} is a back-edge for T. Say w is ancestor of v. Found cycle: v, p(v), p(p(v)), ..., w, v.
37
![Page 38: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/38.jpg)
Problem 3.2
Input: An undirected graph G.
Output: A cycle in G, if one exists, else throw new UnexpectedTreeException( )
Let T be a DFS tree for G, rooted at u. Now what? Suppose {v,w} is a back-edge for T. Say w is ancestor of v. Found cycle: v, p(v), p(p(v)), ..., w, v.
What’s left?
38
![Page 39: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/39.jpg)
Problem 3.2
Input: An undirected graph G.
Output: A cycle in G, if one exists, else throw new UnexpectedTreeException( )
Let T be a DFS tree for G, rooted at u. Now what? Suppose {v,w} is a back-edge for T. Say w is ancestor of v. Found cycle: v, p(v), p(p(v)), ..., w, v.
What’s left? 1) Why is this cycle?
39
![Page 40: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/40.jpg)
Problem 3.2Input: An undirected graph G.
Output: A cycle in G, if one exists, else throw new UnexpectedTreeException( )
Let T be a DFS tree for G, rooted at u. Now what? Suppose {v,w} is a back-edge for T. Say w is ancestor of v. Found cycle: v, p(v), p(p(v)), ..., w, v.
What’s left? 1) Why is this cycle? 2) What if there is no back-edge?
40
![Page 41: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/41.jpg)
Problem 3.2Input: An undirected graph G.
Output: A cycle in G, if one exists, else throw new UnexpectedTreeException( )
Let T be a DFS tree for G, rooted at u. Now what? Suppose {v,w} is a back-edge for T. Say w is ancestor of v. Found cycle: v, p(v), p(p(v)), ..., w, v.
What’s left? 1) Why is this cycle? 2) What if there is no back-edge? 3) What if G is not connected?
41
![Page 42: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/42.jpg)
Problem 3.2
Input: An undirected graph G.
Output: A cycle in G, if one exists, else throw new UnexpectedTreeException( )
Let T be a DFS tree for G, rooted at u. Now what? Suppose {v,w} is a back-edge for T. Say w is ancestor of v. Found cycle: v, p(v), p(p(v)), ..., w, v.
HW: Finish proof
42
![Page 43: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/43.jpg)
DFS: recursive version
Initialize: Mark all nodes Unfound
DFS(v):
Mark v as Found
For (w in Adj[v]):
If (w is Unfound)
{parent(w) = v; DFS(w);}
43
![Page 44: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/44.jpg)
DFS: iterative versionInitialize:Active = new stack with just s.Mark all nodes Unfoundwhile (Active is nonempty) {
v = Active.pop( ); if (v is Unfound) {
Mark v Found;For (w in Adj[v]):
Active.push(w);}
44
![Page 45: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/45.jpg)
Compare: BFSInitialize:Active = new queue with just s.Mark all nodes Unfoundwhile (Active is nonempty) {
v = Active.remove( ); if (v is Unfound) {
Mark v Found;For (w in Adj[v]):
Active.add(w);}
45
![Page 46: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/46.jpg)
Both versions of DFS are implemented using a Stack!
46
![Page 47: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/47.jpg)
Both versions of DFS are implemented using a Stack!
(Recursive version uses the
function-call stack)
47
![Page 48: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/48.jpg)
Next Topic: Directed Graphs (aka “digraphs”)
48
![Page 49: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/49.jpg)
Directed GraphsA directed graph is like a graph, but the edges are ORDERED pairs of vertices, rather than unordered pairs of vertices.
For normal graphs, we denote edges in curly braces: {u,v} (unordered)
For directed graphs, parentheses: (u,v) (ordered: “from u to v”). Directed edges are also called “arcs”
49
![Page 50: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/50.jpg)
Directed Graphs
Example: V = {1,2,3,4}
E = { (1,2), (2,3), (3,4), (4,1)}.
This example is known as a “directed cycle” or “oriented cycle” (of length 4)
1
2
3
450
![Page 51: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/51.jpg)
Representing DiGraphsFor directed graphs, we need 2 adjacency lists for each node. edges INTO v, and OUT FROM v.
2 arrays of Lists:
in-neighbors[2] = {1}
out-neighbors[2] = {1,3}1
2
3
4
51
![Page 52: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/52.jpg)
DAGsA directed graph is called acyclic if it has no oriented cycles.
Meaning: you can’t get back where you start if you always follow arrows.
The “underlying graph” (just lose all the orientation info) may have cycles.
52
![Page 53: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/53.jpg)
DAGsQ: How do we tell if a graph is a DAG? Alternatively, how do we find an oriented cycle if there is one?
Look at the Left example. The nodes are in a line, all the edges go left-to-right. This is called a topological sort.
53
![Page 54: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/54.jpg)
DAGsQ: How do we tell if a graph is a DAG? Alternatively, how do we find an oriented cycle if there is one?
Look at the Left example. The nodes are in a line, all the edges go left-to-right. This is called a topological sort.
Thm: G has a topological sort iff G is a DAG
54
![Page 55: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/55.jpg)
Topological Sorting
Q: How do we tell if a graph has a topo. sort?
Look at the left example. The leftmost node has no in-edges. Is this always the case?
55
![Page 56: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/56.jpg)
Topological Sorting
Q: How do we tell if a graph has a topo. sort?
Look at the left example. The leftmost node has no in-edges. Is this always the case? Yes.
Idea: Find the leftmost node. Recurse!
56
![Page 57: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/57.jpg)
Topological Sorting
Q: How do we tell if a graph has a topo. sort?
Look at the left example. The leftmost node has no in-edges. Is this always the case? Yes.
Idea: Find the leftmost node. Recurse!
Implementation issues?
57
![Page 58: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/58.jpg)
Searching DiGraphs
Question: Find all nodes reachable from s in a directed graph G.
58
![Page 59: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/59.jpg)
Searching DiGraphs
Question: Find all nodes reachable from s in a directed graph G.
Idea: Slightly modify BFS and DFS algorithms: they should only “find” nodes along out-edges.
59
![Page 60: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/60.jpg)
DFS and topo sort
Suppose we run DFS on a directed graph. What can we say about a node v when we mark it as INACTIVE?
60
![Page 61: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/61.jpg)
DFS and topo sort
Suppose we run DFS on a directed graph. What can we say about a node v when we mark it as INACTIVE?
Did we fully explore all nodes reachable from v?
61
![Page 62: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/62.jpg)
DFS and topo sort
Suppose we run DFS on a directed graph. What can we say about a node v when we mark it as INACTIVE?
Did we fully explore all nodes reachable from v? (to whiteboard)
62
![Page 63: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/63.jpg)
DFS and topo sort
Suppose we run DFS on a directed graph. What can we say about a node v when we mark it as INACTIVE?
Did we fully explore all nodes reachable from v? YES, unless there is a back-edge to an active node (an ancestor of the current node). In this case there is an oriented cycle!.
63
![Page 64: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/64.jpg)
Suppose we run DFS on a directed graph. What can we say about a node v when we mark it as INACTIVE?
Did we fully explore all nodes reachable from v? YES, unless there is a back-edge to an active node (an ancestor of the current node). In this case there is an oriented cycle!
So: IF we ever find a back-edge to an active node, output the cycle. Otherwise, we know all nodes reachable from v were marked INACTIVE before v was.
DFS and topo sort
64
![Page 65: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/65.jpg)
Start at a node s. Begin the DFS algorithm, following only out-edges.
Algorithm
65
![Page 66: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/66.jpg)
Start at a node s. Begin the DFS algorithm, following only out-edges.
If we ever find an edge to an active node, there is a cycle. Each time we mark a node inactive, pre-pend it to the output.
Algorithm
66
![Page 67: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/67.jpg)
Start at a node s. Begin the DFS algorithm, following only out-edges.
If we ever find an edge to an active node, there is a cycle. Each time we mark a node inactive, pre-pend it to the output.
If graph is a DAG, output will be a topological sort of the nodes reachable from s.
Algorithm
67
![Page 68: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/68.jpg)
Start at a node s. Begin the DFS algorithm, following only out-edges.
If we ever find an edge to an active node, there is a cycle. Each time we mark a node inactive, pre-pend it to the output.
If graph is a DAG, output will be a topological sort of the nodes reachable from s.
If not given s: start by finding a source (node with no in-edges). A DAG always has one.
Algorithm
68
![Page 69: CS 361 Data Structures & Algs Lecture 14hayes/teaching/cs361-fall... · identifying BFS & DFS trees Where is the root? Key: Edges left out of the tree: in BFS, level difference is](https://reader036.vdocuments.net/reader036/viewer/2022071016/5fcf6401bdb8e23c3339380b/html5/thumbnails/69.jpg)
Start at a node s. Begin the DFS algorithm, following only out-edges.
If we ever find an edge to an active node, there is a cycle. Each time we mark a node inactive, pre-pend it to the output.
If graph is a DAG, output will be a topological sort of the nodes reachable from s.
If not given s: start by finding a source (node with no in-edges). A DAG always has one.
If not all nodes reached, go to next component.
Algorithm
69