![Page 1: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/1.jpg)
1
Chapter 3
Graphs
CS 350 Winter 2018
![Page 2: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/2.jpg)
3.0 Outline
(i) Graphs
(ii) BFS & DFS
(iii) Connectivity and Graph Traversals
(iv) Testing Bipartiteness
(v) DAGS
![Page 3: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/3.jpg)
3.1 Basic Definitions and Applications
![Page 4: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/4.jpg)
4
Undirected Graphs
Undirected graph. G = (V, E)
V = nodes.
E = edges between pairs of nodes.
Captures pairwise relationship between objects.
Graph size parameters: n = |V|, m = |E|.
V = { 1, 2, 3, 4, 5, 6, 7, 8 }
E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 }
n = 8
m = 11
![Page 5: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/5.jpg)
5
Some Graph Applications
transportation
Graph
street intersections
Nodes Edges
highways
communication computers fiber optic cables
World Wide Web web pages hyperlinks
social people relationships
food web species predator-prey
software systems functions function calls
scheduling tasks precedence constraints
circuits gates wires
![Page 6: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/6.jpg)
6
World Wide Web
Web graph.
Node: web page.
Edge: hyperlink from one page to another.
cnn.com
cnnsi.comnovell.comnetscape.com timewarner.com
hbo.com
sorpranos.com
![Page 7: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/7.jpg)
7
Social Network
Social network graph.
Node: people.
Edge: relationship between two people.
![Page 8: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/8.jpg)
8
Ecological Food Web
Food web graph.
Node = species.
Edge = from prey to predator.
Reference: http://www.twingroves.district96.k12.il.us/Wetlands/Salamander/SalGraphics/salfoodweb.giff
![Page 9: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/9.jpg)
9
Graph Representation: Adjacency Matrix
Adjacency matrix. n-by-n matrix with Auv = 1 if (u, v) is an edge.
Two representations of each edge.
Space proportional to n2.
Checking if (u, v) is an edge takes (1) time.
Identifying all edges takes (n2) time.
1 2 3 4 5 6 7 8
1 0 1 1 0 0 0 0 0
2 1 0 1 1 1 0 0 0
3 1 1 0 0 1 0 1 1
4 0 1 0 1 1 0 0 0
5 0 1 1 1 0 1 0 0
6 0 0 0 0 1 0 0 0
7 0 0 1 0 0 0 0 1
8 0 0 1 0 0 0 1 0
![Page 10: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/10.jpg)
10
Graph Representation: Adjacency List
Adjacency list. Node indexed array of lists.
Two representations of each edge.
Space proportional to m + n.
Checking if (u, v) is an edge takes O(deg(u)) time.
Identifying all edges takes (m + n) time.
1 2 3
2
3
4 2 5
5
6
7 3 8
8
1 3 4 5
1 2 5 87
2 3 4 6
5
degree = number of neighbors of u
3 7
![Page 11: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/11.jpg)
11
Paths and Connectivity
Def. A path in an undirected graph G = (V, E) is a sequence P of nodes
v1, v2, …, vk-1, vk with the property that each consecutive pair vi, vi+1 is
joined by an edge in E.
Def. A path is simple if no multi-edges or loops.
Q: What is the maximum number of edges possible in a simple graph?
Def. An undirected graph is connected if for every pair of nodes u and
v, there is a path between u and v.
![Page 12: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/12.jpg)
12
Cycles
Def. A cycle is a path v1, v2, …, vk-1, vk in which v1 = vk, k > 2, and the
first k-1 nodes are all distinct.
cycle C = 1-2-4-5-3-1
![Page 13: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/13.jpg)
13
Trees
Def. An undirected graph is a tree if it is connected and does not
contain a cycle.
Theorem. Let G be an undirected graph on n nodes. Any two of the
following statements imply the third.
G is connected.
G does not contain a cycle.
G has n-1 edges.
Q: How would we prove this
Theorem?
![Page 14: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/14.jpg)
14
Rooted Trees
Rooted tree. Given a tree T, choose a root node r and orient each edge
away from r.
Importance. Models hierarchical structure.
a tree the same tree, rooted at 1
v
parent of v
child of v
root r
![Page 15: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/15.jpg)
15
Phylogeny Trees
Phylogeny trees. Describe evolutionary history of species.
![Page 16: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/16.jpg)
16
Rooted Tree
![Page 17: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/17.jpg)
3.2 Graph Traversal
![Page 18: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/18.jpg)
18
Connectivity
s-t connectivity problem. Given two node s and t, is there a path
between s and t?
s-t shortest path problem. Given two node s and t, what is the length
of the shortest path between s and t?
Applications.
Google Maps.
Maze traversal.
Kevin Bacon number.
Fewest number of hops in a communication network.
![Page 19: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/19.jpg)
19
Breadth First Search (BFS)
BFS intuition. Explore outward from s in all possible directions, adding
nodes one "layer" at a time.
BFS algorithm.
L0 = { s }.
L1 = all neighbors of L0.
L2 = all nodes that do not belong to L0 or L1, and that have an edge
to a node in L1.
Li+1 = all nodes that do not belong to an earlier layer, and that have
an edge to a node in Li.
Theorem. For each i, Li consists of all nodes at distance exactly i
from s. There is a path from s to t iff t appears in some layer.
Q: How would we prove this?
s L1 L2 L n-1
![Page 20: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/20.jpg)
20
Breadth First Search
Property. Let T be a BFS tree of G = (V, E), and let (x, y) be an edge of
G. Then the level of x and y differ by at most 1.
(Proof by contradiction)
L0
L1
L2
L3
![Page 21: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/21.jpg)
21
Breadth First Search: Analysis
Theorem. The above implementation of BFS runs in O(m + n) time if
the graph is given by its adjacency representation. (NB: the data
structure/graph representation matters for algorithm efficiency!)
Pf.
Easy to prove O(n2) running time:
– at most n lists L[i]
– each node occurs once at most on each list; for loop runs n
times
– when we consider node u, there are n incident edges (u, v),
and we spend O(1) processing each edge
Actually runs in O(m + n) time:
– when we consider node u, there are deg(u) incident edges (u, v)
– total time processing edges is uV deg(u) = 2m
“First Theorem of Graph Theory”:
uV deg(u) = 2m
each edge (u, v) is counted exactly twicein sum: once in deg(u) and once in deg(v)
![Page 22: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/22.jpg)
22
Connected Component
Connected component. Find all nodes reachable from s.
Connected component containing node 1 = { 1, 2, 3, 4, 5, 6, 7, 8 }.
![Page 23: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/23.jpg)
23
Connected Component
Connected component. Find all nodes reachable from s.
Theorem. Upon termination, R is the connected component containing s.
BFS = explore in order of distance from s. (use stack, LIFO)
DFS = explore in a different way: explore until reaching dead-end,
then backtrack. (use queue, FIFO)
s
u v
R
it's safe to add v
![Page 24: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/24.jpg)
Breadth-first search
BFS is a simple strategy in which the root node is expanded first, then all the successors of the root node are expanded next, then their successors, etc.
BFS is an instance of the general graph-search algorithm in which the shallowest unexpanded node is chosen for expansion.
This is achieved by using a FIFO queue for the frontier. Accordingly, new nodes go to the back of the queue, and old nodes, which are shallower than the new nodes are expanded first. NB: The goal test is applied to each node when it is generated.
![Page 25: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/25.jpg)
Breadth-first search
Expand shallowest unexpanded node
Implementation:
frontier is a FIFO queue, i.e., new successors go at end
![Page 26: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/26.jpg)
26
Breadth-first search
Expand shallowest unexpanded node
Implementation:
frontier is a FIFO queue, i.e., new successors go at end
![Page 27: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/27.jpg)
27 Breadth-first search
Expand shallowest unexpanded node
Implementation:
fringe is a FIFO queue, i.e., new successors go at end
![Page 28: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/28.jpg)
28 Breadth-first search
Expand shallowest unexpanded node
Implementation:
fringe is a FIFO queue, i.e., new successors go at end
![Page 29: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/29.jpg)
14 Jan 2004
Depth-first search
Expand deepest unexpanded node
Implementation:fringe = LIFO queue, i.e., put successors at front
![Page 30: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/30.jpg)
14 Jan 2004
CS 3243 -Blind Search
30 Depth-first search
Expand deepest unexpanded node
Implementation:fringe = LIFO queue, i.e., put successors at front
![Page 31: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/31.jpg)
14 Jan 2004
CS 3243 -Blind Search
31 Depth-first search
Expand deepest unexpanded node
Implementation:fringe = LIFO queue, i.e., put successors at front
![Page 32: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/32.jpg)
14 Jan 2004
CS 3243 -Blind Search
32 Depth-first search
Expand deepest unexpanded node
Implementation:fringe = LIFO queue, i.e., put successors at front
![Page 33: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/33.jpg)
14 Jan 2004
CS 3243 -Blind Search
33 Depth-first search
Expand deepest unexpanded node
Implementation:fringe = LIFO queue, i.e., put successors at front
![Page 34: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/34.jpg)
14 Jan 2004
CS 3243 -Blind Search
34 Depth-first search
Expand deepest unexpanded node
Implementation:fringe = LIFO queue, i.e., put successors at front
![Page 35: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/35.jpg)
14 Jan 2004
CS 3243 -Blind Search
35 Depth-first search
Expand deepest unexpanded node
Implementation:fringe = LIFO queue, i.e., put successors at front
![Page 36: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/36.jpg)
14 Jan 2004
CS 3243 -Blind Search
36 Depth-first search
Expand deepest unexpanded node
Implementation:fringe = LIFO queue, i.e., put successors at front
![Page 37: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/37.jpg)
14 Jan 2004
CS 3243 -Blind Search
37 Depth-first search
Expand deepest unexpanded node
Implementation:fringe = LIFO queue, i.e., put successors at front
![Page 38: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/38.jpg)
14 Jan 2004
CS 3243 -Blind Search
38 Depth-first search
Expand deepest unexpanded node
Implementation:fringe = LIFO queue, i.e., put successors at front
![Page 39: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/39.jpg)
14 Jan 2004
CS 3243 -Blind Search
39 Depth-first search
Expand deepest unexpanded node
Implementation:fringe = LIFO queue, i.e., put successors at front
![Page 40: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/40.jpg)
14 Jan 2004
CS 3243 -Blind Search
40 Depth-first search
Expand deepest unexpanded node
Implementation:fringe = LIFO queue, i.e., put successors at front
![Page 41: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/41.jpg)
3.4 Testing Bipartiteness
![Page 42: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/42.jpg)
42
Bipartite Graphs
Def. An undirected graph G = (V, E) is bipartite if the nodes can be
colored red or blue such that every edge has one red and one blue end.
Applications.
Stable marriage: men = red, women = blue.
Scheduling: machines = red, jobs = blue.
a bipartite graph
![Page 43: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/43.jpg)
43
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 44: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/44.jpg)
44
A Structural Obstruction to Bipartiteness
Lemma. If a graph G is bipartite, it cannot contain an odd length cycle.
Pf. Not possible to 2-color the odd cycle, let alone G.
(Max number of color classes for a proper coloring of a graph is called
the chromatic number of the graph).
bipartite(2-colorable)
not bipartite(not 2-colorable)
![Page 45: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/45.jpg)
45
Obstruction to Bipartiteness
Corollary. A graph G is bipartite iff it contain no odd length cycle.
5-cycle C
bipartite(2-colorable)
not bipartite(not 2-colorable)
![Page 46: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/46.jpg)
46
A Structural Obstruction to Bipartiteness
In fact, the previous condition is even stronger than previously stated.
A graph G is bipartite iff it cannot contain an odd length cycle.
Pf. We already showed that if G is bipartite, then it cannot contain an
odd cycle.
Q: How do we show the converse?
![Page 47: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/47.jpg)
47
A Structural Obstruction to Bipartiteness
In fact, the previous condition is even stronger than previously stated.
A graph G is bipartite iff it cannot contain an odd length cycle.
Pf. We already showed that if G is bipartite, then it cannot contain an
odd cycle.
Q: How do we show the converse?
![Page 48: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/48.jpg)
48
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).
Case (i)
L1 L2 L3
Case (ii)
L1 L2 L3
![Page 49: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/49.jpg)
49
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 same layer.
By previous lemma, this implies all edges join nodes in adjacent
layers.
Bipartition: red = nodes on odd levels, blue = nodes on even levels.
Case (i)
L1 L2 L3
![Page 50: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/50.jpg)
50
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 with x, y in same level Lj.
Let z = lca(x, y) = lowest common ancestor.
Let Li be level containing z.
Consider cycle that takes edge from x to y,
then path from y to z, then path from z to x.
Its length is 1 + (j-i) + (j-i), which is odd. ▪
In Summary: Using BFS yields O(m+n) algorithm to check for
Bipartiteness.
z = lca(x, y)
(x, y) path fromy to z
path fromz to x
![Page 51: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/51.jpg)
3.5 Connectivity in Directed Graphs
![Page 52: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/52.jpg)
52
Directed Graphs
Directed graph. G = (V, E)
Edge (u, v) goes from node u to node v.
Ex. Web graph - hyperlink points from one web page to another.
Directedness of graph is crucial.
Modern web search engines exploit hyperlink structure to rank web
pages by importance.
![Page 53: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/53.jpg)
53
Graph Search
Directed reachability. Given a node s, find all nodes reachable from s.
Directed s-t shortest path problem. Given two node s and t, what is
the length of the shortest path between s and t?
Graph search. BFS extends naturally to directed graphs.
Web crawler. Start from web page s. Find all web pages linked from s,
either directly or indirectly.
![Page 54: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/54.jpg)
54
Strong Connectivity
Def. Node u and v are mutually reachable if there is a path from u to v
and also a path from v to u.
Def. A graph is strongly connected if every pair of nodes is mutually
reachable. (note that if the “underlying” graph is connected will call the
graph merely “connected”).
Lemma. Let s be any node. G is strongly connected iff every node is
reachable from s, and s is reachable from every node.
Pf. Follows from definition.
Pf. Path from u to v: concatenate u-s path with s-v path.
Path from v to u: concatenate v-s path with s-u path. ▪
s
v
u
ok if paths overlap
![Page 55: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/55.jpg)
55
Strong Connectivity: Algorithm
Theorem. Can determine if G is strongly connected in O(m + n) time.
Pf.
Pick any node s.
Run BFS from s in G.
Run BFS from s in Grev.
Return true iff all nodes reached in both BFS executions.
Correctness follows immediately from previous lemma. ▪
reverse orientation of every edge in G
strongly connected not strongly connected
![Page 56: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/56.jpg)
3.6 DAGs and Topological Ordering
![Page 57: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/57.jpg)
57
Directed Acyclic Graphs
Def. An DAG is a directed graph that contains no directed cycles.
Ex. Precedence constraints: edge (vi, vj) means vi must precede vj.
Def. A topological ordering 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 DAG a topological ordering
v2 v3
v6 v5 v4
v7 v1
v1 v2 v3 v4 v5 v6 v7
![Page 58: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/58.jpg)
58
Precedence Constraints
Precedence constraints. Edge (vi, vj) means task vi must occur before vj.
Applications.
Course prerequisite graph: course vi must be taken before vj.
Compilation: module vi must be compiled before vj. Pipeline of
computing jobs: output of job vi needed to determine input of job vj.
Markov Chains.
![Page 59: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/59.jpg)
59
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's see what happens.
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
![Page 60: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/60.jpg)
60
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 61: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/61.jpg)
61
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 denote the sequence of nodes encountered between
successive visits to w. C is a cycle. ▪
w x u v
![Page 62: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/62.jpg)
62
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 63: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/63.jpg)
63
Topological Sorting Algorithm: Running Time
Theorem. Algorithm finds a topological order in O(m + n) time.
Pf.
Maintain the following information:
– count[w] = remaining number of incoming edges
– S = set of remaining nodes with no incoming edges
Initialization: O(m + n) via single scan through graph.
Update: to delete v
– remove v from S
– decrement count[w] for all edges from v to w, and add w to S if c
count[w] hits 0
– this is O(1) per edge ▪
![Page 64: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/64.jpg)
64
v1
Topological Ordering Algorithm: Example
Topological order:
v2 v3
v6 v5 v4
v7 v1
![Page 65: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/65.jpg)
65
v2
Topological Ordering Algorithm: Example
Topological order: v1
v2 v3
v6 v5 v4
v7
![Page 66: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/66.jpg)
66
v3
Topological Ordering Algorithm: Example
Topological order: v1, v2
v3
v6 v5 v4
v7
![Page 67: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/67.jpg)
67
v4
Topological Ordering Algorithm: Example
Topological order: v1, v2, v3
v6 v5 v4
v7
![Page 68: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/68.jpg)
68
v5
Topological Ordering Algorithm: Example
Topological order: v1, v2, v3, v4
v6 v5
v7
![Page 69: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/69.jpg)
69
v6
Topological Ordering Algorithm: Example
Topological order: v1, v2, v3, v4, v5
v6
v7
![Page 70: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/70.jpg)
70
v7
Topological Ordering Algorithm: Example
Topological order: v1, v2, v3, v4, v5, v6
v7
![Page 71: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/71.jpg)
71
Topological Ordering Algorithm: Example
Topological order: v1, v2, v3, v4, v5, v6, v7.
v2 v3
v6 v5 v4
v7 v1
v1 v2 v3 v4 v5 v6 v7
![Page 72: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/72.jpg)
72
Example: HW #3.1
![Page 73: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/73.jpg)
73
Example: HW #3.1
Consider the fact that a topological ordering must start with a and end with f. Why?
![Page 74: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/74.jpg)
74
Example: HW #3.1
So, we have: a __ __ __ __ f
What can go in the middle?
![Page 75: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/75.jpg)
75
Example: HW #3.1
So, we have: a __ __ __ __ f
What can go in the middle? Note that b must precede c and d must precede e. Why?
![Page 76: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/76.jpg)
76
Example: HW #3.1
So, we have (2) cases:
a b __ __ __ f
And
a d __ __ __ f
How many valid orderings exist for each case?
![Page 77: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/77.jpg)
77
Example: HW #3.1
So, we have (2) cases:
a b __ __ __ f
And
a d __ __ __ f
How many valid orderings exist for each case?
Three. Why?
![Page 78: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/78.jpg)
78
Example: HW #3.1
Final answer: 6 topological orderings exist.
![Page 79: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/79.jpg)
79
Example: HW #3.2
Q: (Cycle Detection) Give an O(m+n) algorithm to detect whether a given undirected graph contains a cycle.
Where should we start?
![Page 80: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/80.jpg)
80
Example: HW #3.2
Q: (Cycle Detection) Give an O(m+n) algorithm to detect whether a given undirected graph contains a cycle.
Where should we start?
Start with BFS from any node, say, s in G. This process produces a tree (specifically: a tree rooted at s).
If G=T then we return false. Why?
![Page 81: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/81.jpg)
81
Example: HW #3.2
Q: (Cycle Detection) Give an O(m+n) algorithm to detect whether a given undirected graph contains a cycle.
Where should we start?
Start with BFS from any node, say, s in G. This process produces a tree (specifically: a tree rooted at s).
If G=T then we return false. Why?
Otherwise, we locate an edge in G that is not in T. How would this step be implemented efficiently?
![Page 82: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/82.jpg)
82
Example: HW #3.2
Q: (Cycle Detection) Give an O(m+n) algorithm to detect whether a given undirected graph contains a cycle.
Where should we start?
Start with BFS from any node, say, s in G. This process produces a tree (specifically: a tree rooted at s).
If G=T then we return false. Why?
Otherwise, we locate an edge in G that is not in T. How would this step be implemented efficiently?
Adding this edge to T produces a cycle (why?), and thus we returntrue in this case.
![Page 83: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/83.jpg)
83
Example: HW #3.5
Q: A binary tree is a rooted tree in which each node has at most two children.
Show by induction that in any binary tree, the number of nodes with two children is exactly one less than the number of leaves.
![Page 84: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/84.jpg)
84
Example: HW #3.5
Q: A binary tree is a rooted tree in which each node has at most two children.
Show by induction that in any binary tree, the number of nodes with two children is exactly one less than the number of leaves.
First, convince yourself that this seems intuitively correct.
![Page 85: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/85.jpg)
85
Example: HW #3.5
Q: A binary tree is a rooted tree in which each node has at most two children.
Show by induction that in any binary tree, the number of nodes with two children is exactly one less than the number of leaves.
Pf. Base case: n=1 (trivial).
Inductive Hypothesis: Suppose that n2_children(T)=nleaves(T)-1 for all trees with n=k nodes.
Consider a tree T with n=k+1 nodes.
Let v be a leaf in T (guaranteed to exist – why?)
![Page 86: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/86.jpg)
86
Example: HW #3.5
Q: A binary tree is a rooted tree in which each node has at most two children.
Show by induction that in any binary tree, the number of nodes with two children is exactly one less than the number of leaves.
Pf. Base case: n=1 (trivial).
Inductive Hypothesis: Suppose that n2_children(T)=nleaves(T)-1 for all trees with n=k nodes.
Consider a tree T with n=k+1 nodes.
Let v be a leaf in T denote u as the parent of v. Call T* the tree: T-v.
![Page 87: Graphs - Portland State Universityweb.pdx.edu/~arhodes/alg3.pdfBreadth-first search BFS is a simple strategy in which the root node is expanded first, then all the successors of the](https://reader030.vdocuments.mx/reader030/viewer/2022040410/5ec958910314b5791871cf1d/html5/thumbnails/87.jpg)
87
Example: HW #3.5Q: A binary tree is a rooted tree in which each node has at most two children.
Show by induction that in any binary tree, the number of nodes with two children is exactly one less than the number of leaves.
Pf. Base case: n=1 (trivial).
Inductive Hypothesis: Suppose that n2_children(T)=nleaves(T)-1 for all trees with n=k nodes.
Consider a tree T with n=k+1 nodes.
Let v be a leaf in T denote u as the parent of v. Call T* the tree: T-v.
Case 1: If node u had no other children, then u is now a leaf in T*, and nleaves(T*) = nleaves(T), and n2_children(T*)= n2_children(T).
By the inductive hypothesis, n2_children(T)=nleaves(T)-1, as was to be shown.
Now you finish the proof by providing case 2.