graphs - virginia techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...vlsi circuits, cellular...
TRANSCRIPT
![Page 1: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/1.jpg)
Graphs
September 16, 2014
September 16, 2014 CS4104: Graphs
![Page 2: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/2.jpg)
GraphsI Model pairwise relationships (edges) between objects (nodes).
I Useful in a large number of applications: computer networks, the World WideWeb, ecology (food webs), social networks, software systems, job scheduling,VLSI circuits, cellular networks, . . .
I Other examples: gene and protein networks, our bodies (nervous andcirculatory systems, brains), buildings, transportation networks, . . .
I Problems involving graphs have a rich history dating back to Euler.
September 16, 2014 CS4104: Graphs
![Page 3: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/3.jpg)
GraphsI Model pairwise relationships (edges) between objects (nodes).I Useful in a large number of applications:
computer networks, the World WideWeb, ecology (food webs), social networks, software systems, job scheduling,VLSI circuits, cellular networks, . . .
I Other examples: gene and protein networks, our bodies (nervous andcirculatory systems, brains), buildings, transportation networks, . . .
I Problems involving graphs have a rich history dating back to Euler.
September 16, 2014 CS4104: Graphs
![Page 4: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/4.jpg)
GraphsI Model pairwise relationships (edges) between objects (nodes).I Useful in a large number of applications: computer networks, the World Wide
Web, ecology (food webs), social networks, software systems, job scheduling,VLSI circuits, cellular networks, . . .
I Other examples: gene and protein networks, our bodies (nervous andcirculatory systems, brains), buildings, transportation networks, . . .
I Problems involving graphs have a rich history dating back to Euler.
September 16, 2014 CS4104: Graphs
![Page 5: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/5.jpg)
GraphsI Model pairwise relationships (edges) between objects (nodes).I Useful in a large number of applications: computer networks, the World Wide
Web, ecology (food webs), social networks, software systems, job scheduling,VLSI circuits, cellular networks, . . .
I Other examples: gene and protein networks, our bodies (nervous andcirculatory systems, brains), buildings, transportation networks, . . .
I Problems involving graphs have a rich history dating back to Euler.
September 16, 2014 CS4104: Graphs
![Page 6: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/6.jpg)
GraphsI Model pairwise relationships (edges) between objects (nodes).I Useful in a large number of applications: computer networks, the World Wide
Web, ecology (food webs), social networks, software systems, job scheduling,VLSI circuits, cellular networks, . . .
I Other examples: gene and protein networks, our bodies (nervous andcirculatory systems, brains), buildings, transportation networks, . . .
I Problems involving graphs have a rich history dating back to Euler.
September 16, 2014 CS4104: Graphs
![Page 7: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/7.jpg)
De�nition of a Graph
I Undirected graph G = (V ,E ): set V of nodes and set E of edges, whereE ⊆ V × V . Elements of E are unordered pairs.
I Abuse of notation: write an edge e between nodes u and v as e = (u, v) andnot as e = {u, v}.
I Say that edge e is incident on u and on v .I Exactly one edge between any pair of nodes.I G contains no self loops.
I Directed graph G = (V ,E ): set V of nodes and set E of edges, whereE ⊆ V × V . Elements of E are ordered pairs.
I e = (u, v): u is the tail of the edge e, v is its head; e leaves node u and enters
node v .I A pair of nodes {u, v} may be connected by two directed edges: (u, v) and
(v , u).I G contains no self loops.
I By default, �graph� will mean an �undirected graph�.
September 16, 2014 CS4104: Graphs
![Page 8: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/8.jpg)
De�nition of a Graph
I Undirected graph G = (V ,E ): set V of nodes and set E of edges, whereE ⊆ V × V . Elements of E are unordered pairs.
I Abuse of notation: write an edge e between nodes u and v as e = (u, v) andnot as e = {u, v}.
I Say that edge e is incident on u and on v .I Exactly one edge between any pair of nodes.I G contains no self loops.
I Directed graph G = (V ,E ): set V of nodes and set E of edges, whereE ⊆ V × V . Elements of E are ordered pairs.
I e = (u, v): u is the tail of the edge e, v is its head; e leaves node u and enters
node v .I A pair of nodes {u, v} may be connected by two directed edges: (u, v) and
(v , u).I G contains no self loops.
I By default, �graph� will mean an �undirected graph�.
September 16, 2014 CS4104: Graphs
![Page 9: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/9.jpg)
De�nition of a Graph
I Undirected graph G = (V ,E ): set V of nodes and set E of edges, whereE ⊆ V × V . Elements of E are unordered pairs.
I Abuse of notation: write an edge e between nodes u and v as e = (u, v) andnot as e = {u, v}.
I Say that edge e is incident on u and on v .I Exactly one edge between any pair of nodes.I G contains no self loops.
I Directed graph G = (V ,E ): set V of nodes and set E of edges, whereE ⊆ V × V . Elements of E are ordered pairs.
I e = (u, v): u is the tail of the edge e, v is its head; e leaves node u and enters
node v .I A pair of nodes {u, v} may be connected by two directed edges: (u, v) and
(v , u).I G contains no self loops.
I By default, �graph� will mean an �undirected graph�.
September 16, 2014 CS4104: Graphs
![Page 10: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/10.jpg)
De�nition of a Graph
I Undirected graph G = (V ,E ): set V of nodes and set E of edges, whereE ⊆ V × V . Elements of E are unordered pairs.
I Abuse of notation: write an edge e between nodes u and v as e = (u, v) andnot as e = {u, v}.
I Say that edge e is incident on u and on v .I Exactly one edge between any pair of nodes.I G contains no self loops.
I Directed graph G = (V ,E ): set V of nodes and set E of edges, whereE ⊆ V × V . Elements of E are ordered pairs.
I e = (u, v): u is the tail of the edge e, v is its head; e leaves node u and enters
node v .I A pair of nodes {u, v} may be connected by two directed edges: (u, v) and
(v , u).I G contains no self loops.
I By default, �graph� will mean an �undirected graph�.
September 16, 2014 CS4104: Graphs
![Page 11: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/11.jpg)
Paths and Connectivity1
2 3
4 5 6
7
8
9
10
11
12
13
I A path in an undirected graph G = (V ,E ) is a sequence P of nodesv1, v2, . . . , vk−1, vk ∈ V such that every consecutive pair of nodesvi , vi+1, 1 ≤ i < k is connected by an edge in E .
I P is called a path from v1 to vK or a v1-vk path.I A path is simple if all its nodes are distinct.I A cycle is a path where k > 2, the �rst i − 1 nodes are distinct, and v1 = vk .
I All de�nitions carry over to directed graphs as well.I An undirected graph G is connected if for every pair of nodes u, v ∈ V , there
is a path from u to v in G .I Directed graphs have the notion of �strong connectivity.�
I Distance between two nodes u and v is the minimum number of edges in anyu-v path.
September 16, 2014 CS4104: Graphs
![Page 12: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/12.jpg)
Paths and Connectivity1
2 3
4 5 6
7
8
9
10
11
12
13
I A path in an undirected graph G = (V ,E ) is a sequence P of nodesv1, v2, . . . , vk−1, vk ∈ V such that every consecutive pair of nodesvi , vi+1, 1 ≤ i < k is connected by an edge in E .
I P is called a path from v1 to vK or a v1-vk path.I A path is simple if all its nodes are distinct.I A cycle is a path where k > 2, the �rst i − 1 nodes are distinct, and v1 = vk .
I All de�nitions carry over to directed graphs as well.
I An undirected graph G is connected if for every pair of nodes u, v ∈ V , thereis a path from u to v in G .
I Directed graphs have the notion of �strong connectivity.�I Distance between two nodes u and v is the minimum number of edges in any
u-v path.
September 16, 2014 CS4104: Graphs
![Page 13: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/13.jpg)
Paths and Connectivity1
2 3
4 5 6
7
8
9
10
11
12
13
I A path in an undirected graph G = (V ,E ) is a sequence P of nodesv1, v2, . . . , vk−1, vk ∈ V such that every consecutive pair of nodesvi , vi+1, 1 ≤ i < k is connected by an edge in E .
I P is called a path from v1 to vK or a v1-vk path.I A path is simple if all its nodes are distinct.I A cycle is a path where k > 2, the �rst i − 1 nodes are distinct, and v1 = vk .
I All de�nitions carry over to directed graphs as well.I An undirected graph G is connected if for every pair of nodes u, v ∈ V , there
is a path from u to v in G .I Directed graphs have the notion of �strong connectivity.�
I Distance between two nodes u and v is the minimum number of edges in anyu-v path.
September 16, 2014 CS4104: Graphs
![Page 14: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/14.jpg)
Paths and Connectivity1
2 3
4 5 6
7
8
9
10
11
12
13
I A path in an undirected graph G = (V ,E ) is a sequence P of nodesv1, v2, . . . , vk−1, vk ∈ V such that every consecutive pair of nodesvi , vi+1, 1 ≤ i < k is connected by an edge in E .
I P is called a path from v1 to vK or a v1-vk path.I A path is simple if all its nodes are distinct.I A cycle is a path where k > 2, the �rst i − 1 nodes are distinct, and v1 = vk .
I All de�nitions carry over to directed graphs as well.I An undirected graph G is connected if for every pair of nodes u, v ∈ V , there
is a path from u to v in G .I Directed graphs have the notion of �strong connectivity.�
I Distance between two nodes u and v is the minimum number of edges in anyu-v path.
September 16, 2014 CS4104: Graphs
![Page 15: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/15.jpg)
Trees
I An undirected graph is a tree if it is connected and does not contain a cycle.
For any pair of nodes in a tree, there is a unique path connecting them.I Rooting a tree T : pick some node r in the tree and orient each edge of T
�away� from r , i.e., for each node v 6= r , de�ne parent of v to be the node uthat directly precedes v on the path from r to v .
I Node w is a child of node v if v is a parent of w .I Node w is a descendant of node v (or v is an ancestor of w) if v lies on the
r -w path.I Node x is a leaf if it has no descendants.
I Examples of (rooted) trees: organisational hierarchy, class hierarchies inobject-oriented languages.
September 16, 2014 CS4104: Graphs
![Page 16: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/16.jpg)
Trees
I An undirected graph is a tree if it is connected and does not contain a cycle.For any pair of nodes in a tree, there is a unique path connecting them.
I Rooting a tree T : pick some node r in the tree and orient each edge of T�away� from r , i.e., for each node v 6= r , de�ne parent of v to be the node uthat directly precedes v on the path from r to v .
I Node w is a child of node v if v is a parent of w .I Node w is a descendant of node v (or v is an ancestor of w) if v lies on the
r -w path.I Node x is a leaf if it has no descendants.
I Examples of (rooted) trees: organisational hierarchy, class hierarchies inobject-oriented languages.
September 16, 2014 CS4104: Graphs
![Page 17: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/17.jpg)
Trees
I An undirected graph is a tree if it is connected and does not contain a cycle.For any pair of nodes in a tree, there is a unique path connecting them.
I Rooting a tree T : pick some node r in the tree and orient each edge of T�away� from r , i.e., for each node v 6= r , de�ne parent of v to be the node uthat directly precedes v on the path from r to v .
I Node w is a child of node v if v is a parent of w .I Node w is a descendant of node v (or v is an ancestor of w) if v lies on the
r -w path.I Node x is a leaf if it has no descendants.
I Examples of (rooted) trees: organisational hierarchy, class hierarchies inobject-oriented languages.
September 16, 2014 CS4104: Graphs
![Page 18: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/18.jpg)
Trees
I An undirected graph is a tree if it is connected and does not contain a cycle.For any pair of nodes in a tree, there is a unique path connecting them.
I Rooting a tree T : pick some node r in the tree and orient each edge of T�away� from r , i.e., for each node v 6= r , de�ne parent of v to be the node uthat directly precedes v on the path from r to v .
I Node w is a child of node v if v is a parent of w .I Node w is a descendant of node v (or v is an ancestor of w) if v lies on the
r -w path.I Node x is a leaf if it has no descendants.
I Examples of (rooted) trees: organisational hierarchy, class hierarchies inobject-oriented languages.
September 16, 2014 CS4104: Graphs
![Page 19: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/19.jpg)
Trees
I An undirected graph is a tree if it is connected and does not contain a cycle.For any pair of nodes in a tree, there is a unique path connecting them.
I Rooting a tree T : pick some node r in the tree and orient each edge of T�away� from r , i.e., for each node v 6= r , de�ne parent of v to be the node uthat directly precedes v on the path from r to v .
I Node w is a child of node v if v is a parent of w .I Node w is a descendant of node v (or v is an ancestor of w) if v lies on the
r -w path.I Node x is a leaf if it has no descendants.
I Examples of (rooted) trees:
organisational hierarchy, class hierarchies inobject-oriented languages.
September 16, 2014 CS4104: Graphs
![Page 20: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/20.jpg)
Trees
I An undirected graph is a tree if it is connected and does not contain a cycle.For any pair of nodes in a tree, there is a unique path connecting them.
I Rooting a tree T : pick some node r in the tree and orient each edge of T�away� from r , i.e., for each node v 6= r , de�ne parent of v to be the node uthat directly precedes v on the path from r to v .
I Node w is a child of node v if v is a parent of w .I Node w is a descendant of node v (or v is an ancestor of w) if v lies on the
r -w path.I Node x is a leaf if it has no descendants.
I Examples of (rooted) trees: organisational hierarchy, class hierarchies inobject-oriented languages.
September 16, 2014 CS4104: Graphs
![Page 21: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/21.jpg)
Number of Edges in a Tree
I Claim: every n-node tree has
exactly n − 1
edges.
I Proof 1: Root the tree. Each node, except the root, has a unique parent.Each edge connects one parent to one child. Therefore, the tree has n − 1edges.
I Proof 2: (by induction) Two key pieces.I Every tree contains at least one leaf, i.e., node of degree 1. Why?I Inductive hypothesis: every tree with n − 1 nodes contains n − 2 edges.
I Stronger claim: Let G be an undirected graph on n nodes. Any two of thefollowing statements implies the third:
1. G is connected.2. G does not contain a cycle.3. G contains n − 1 edges.
I Note that none of these statements uses the word �tree�.I 1 and 2 ⇒ 3: just proved.I 2 and 3 ⇒ 1: prove by contradiction.I 3 and 1 ⇒ 2: prove yourself.
September 16, 2014 CS4104: Graphs
![Page 22: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/22.jpg)
Number of Edges in a Tree
I Claim: every n-node tree has exactly n − 1 edges.
I Proof 1:
Root the tree. Each node, except the root, has a unique parent.Each edge connects one parent to one child. Therefore, the tree has n − 1edges.
I Proof 2: (by induction) Two key pieces.I Every tree contains at least one leaf, i.e., node of degree 1. Why?I Inductive hypothesis: every tree with n − 1 nodes contains n − 2 edges.
I Stronger claim: Let G be an undirected graph on n nodes. Any two of thefollowing statements implies the third:
1. G is connected.2. G does not contain a cycle.3. G contains n − 1 edges.
I Note that none of these statements uses the word �tree�.I 1 and 2 ⇒ 3: just proved.I 2 and 3 ⇒ 1: prove by contradiction.I 3 and 1 ⇒ 2: prove yourself.
September 16, 2014 CS4104: Graphs
![Page 23: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/23.jpg)
Number of Edges in a Tree
I Claim: every n-node tree has exactly n − 1 edges.
I Proof 1: Root the tree. Each node, except the root, has a unique parent.Each edge connects one parent to one child. Therefore, the tree has n − 1edges.
I Proof 2: (by induction) Two key pieces.I Every tree contains at least one leaf, i.e., node of degree 1. Why?I Inductive hypothesis: every tree with n − 1 nodes contains n − 2 edges.
I Stronger claim: Let G be an undirected graph on n nodes. Any two of thefollowing statements implies the third:
1. G is connected.2. G does not contain a cycle.3. G contains n − 1 edges.
I Note that none of these statements uses the word �tree�.I 1 and 2 ⇒ 3: just proved.I 2 and 3 ⇒ 1: prove by contradiction.I 3 and 1 ⇒ 2: prove yourself.
September 16, 2014 CS4104: Graphs
![Page 24: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/24.jpg)
Number of Edges in a Tree
I Claim: every n-node tree has exactly n − 1 edges.
I Proof 1: Root the tree. Each node, except the root, has a unique parent.Each edge connects one parent to one child. Therefore, the tree has n − 1edges.
I Proof 2: (by induction)
Two key pieces.I Every tree contains at least one leaf, i.e., node of degree 1. Why?I Inductive hypothesis: every tree with n − 1 nodes contains n − 2 edges.
I Stronger claim: Let G be an undirected graph on n nodes. Any two of thefollowing statements implies the third:
1. G is connected.2. G does not contain a cycle.3. G contains n − 1 edges.
I Note that none of these statements uses the word �tree�.I 1 and 2 ⇒ 3: just proved.I 2 and 3 ⇒ 1: prove by contradiction.I 3 and 1 ⇒ 2: prove yourself.
September 16, 2014 CS4104: Graphs
![Page 25: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/25.jpg)
Number of Edges in a Tree
I Claim: every n-node tree has exactly n − 1 edges.
I Proof 1: Root the tree. Each node, except the root, has a unique parent.Each edge connects one parent to one child. Therefore, the tree has n − 1edges.
I Proof 2: (by induction) Two key pieces.I Every tree contains at least one leaf, i.e., node of degree 1. Why?I Inductive hypothesis: every tree with n − 1 nodes contains n − 2 edges.
I Stronger claim: Let G be an undirected graph on n nodes. Any two of thefollowing statements implies the third:
1. G is connected.2. G does not contain a cycle.3. G contains n − 1 edges.
I Note that none of these statements uses the word �tree�.I 1 and 2 ⇒ 3: just proved.I 2 and 3 ⇒ 1: prove by contradiction.I 3 and 1 ⇒ 2: prove yourself.
September 16, 2014 CS4104: Graphs
![Page 26: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/26.jpg)
Number of Edges in a Tree
I Claim: every n-node tree has exactly n − 1 edges.
I Proof 1: Root the tree. Each node, except the root, has a unique parent.Each edge connects one parent to one child. Therefore, the tree has n − 1edges.
I Proof 2: (by induction) Two key pieces.I Every tree contains at least one leaf, i.e., node of degree 1. Why?I Inductive hypothesis: every tree with n − 1 nodes contains n − 2 edges.
I Stronger claim: Let G be an undirected graph on n nodes. Any two of thefollowing statements implies the third:
1. G is connected.2. G does not contain a cycle.3. G contains n − 1 edges.
I Note that none of these statements uses the word �tree�.I 1 and 2 ⇒ 3: just proved.I 2 and 3 ⇒ 1: prove by contradiction.I 3 and 1 ⇒ 2: prove yourself.
September 16, 2014 CS4104: Graphs
![Page 27: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/27.jpg)
Number of Edges in a Tree
I Claim: every n-node tree has exactly n − 1 edges.
I Proof 1: Root the tree. Each node, except the root, has a unique parent.Each edge connects one parent to one child. Therefore, the tree has n − 1edges.
I Proof 2: (by induction) Two key pieces.I Every tree contains at least one leaf, i.e., node of degree 1. Why?I Inductive hypothesis: every tree with n − 1 nodes contains n − 2 edges.
I Stronger claim: Let G be an undirected graph on n nodes. Any two of thefollowing statements implies the third:
1. G is connected.2. G does not contain a cycle.3. G contains n − 1 edges.
I Note that none of these statements uses the word �tree�.
I 1 and 2 ⇒ 3: just proved.I 2 and 3 ⇒ 1: prove by contradiction.I 3 and 1 ⇒ 2: prove yourself.
September 16, 2014 CS4104: Graphs
![Page 28: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/28.jpg)
Number of Edges in a Tree
I Claim: every n-node tree has exactly n − 1 edges.
I Proof 1: Root the tree. Each node, except the root, has a unique parent.Each edge connects one parent to one child. Therefore, the tree has n − 1edges.
I Proof 2: (by induction) Two key pieces.I Every tree contains at least one leaf, i.e., node of degree 1. Why?I Inductive hypothesis: every tree with n − 1 nodes contains n − 2 edges.
I Stronger claim: Let G be an undirected graph on n nodes. Any two of thefollowing statements implies the third:
1. G is connected.2. G does not contain a cycle.3. G contains n − 1 edges.
I Note that none of these statements uses the word �tree�.I 1 and 2 ⇒ 3:
just proved.I 2 and 3 ⇒ 1: prove by contradiction.I 3 and 1 ⇒ 2: prove yourself.
September 16, 2014 CS4104: Graphs
![Page 29: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/29.jpg)
Number of Edges in a Tree
I Claim: every n-node tree has exactly n − 1 edges.
I Proof 1: Root the tree. Each node, except the root, has a unique parent.Each edge connects one parent to one child. Therefore, the tree has n − 1edges.
I Proof 2: (by induction) Two key pieces.I Every tree contains at least one leaf, i.e., node of degree 1. Why?I Inductive hypothesis: every tree with n − 1 nodes contains n − 2 edges.
I Stronger claim: Let G be an undirected graph on n nodes. Any two of thefollowing statements implies the third:
1. G is connected.2. G does not contain a cycle.3. G contains n − 1 edges.
I Note that none of these statements uses the word �tree�.I 1 and 2 ⇒ 3: just proved.I 2 and 3 ⇒ 1:
prove by contradiction.I 3 and 1 ⇒ 2: prove yourself.
September 16, 2014 CS4104: Graphs
![Page 30: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/30.jpg)
Number of Edges in a Tree
I Claim: every n-node tree has exactly n − 1 edges.
I Proof 1: Root the tree. Each node, except the root, has a unique parent.Each edge connects one parent to one child. Therefore, the tree has n − 1edges.
I Proof 2: (by induction) Two key pieces.I Every tree contains at least one leaf, i.e., node of degree 1. Why?I Inductive hypothesis: every tree with n − 1 nodes contains n − 2 edges.
I Stronger claim: Let G be an undirected graph on n nodes. Any two of thefollowing statements implies the third:
1. G is connected.2. G does not contain a cycle.3. G contains n − 1 edges.
I Note that none of these statements uses the word �tree�.I 1 and 2 ⇒ 3: just proved.I 2 and 3 ⇒ 1: prove by contradiction.I 3 and 1 ⇒ 2: prove yourself.
September 16, 2014 CS4104: Graphs
![Page 31: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/31.jpg)
s-t Connectivity1
2 3
4 5 6
7
8
9
10
11
12
13
s-t Connectivity
INSTANCE: An undirected graph G = (V ,E ) and two nodes s, t ∈ V .
QUESTION: Is there an s-t path in G?
I The connected component of G containing s is the set of all nodes u suchthat there is an s-u path in G .
I Algorithm for the s-t Connectivity problem: compute the connectedcomponent of G that contains s and check if t is in that component.
September 16, 2014 CS4104: Graphs
![Page 32: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/32.jpg)
s-t Connectivity1
2 3
4 5 6
7
8
9
10
11
12
13
s-t Connectivity
INSTANCE: An undirected graph G = (V ,E ) and two nodes s, t ∈ V .
QUESTION: Is there an s-t path in G?
I The connected component of G containing s is the set of all nodes u suchthat there is an s-u path in G .
I Algorithm for the s-t Connectivity problem: compute the connectedcomponent of G that contains s and check if t is in that component.
September 16, 2014 CS4104: Graphs
![Page 33: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/33.jpg)
s-t Connectivity1
2 3
4 5 6
7
8
9
10
11
12
13
s-t Connectivity
INSTANCE: An undirected graph G = (V ,E ) and two nodes s, t ∈ V .
QUESTION: Is there an s-t path in G?
I The connected component of G containing s is the set of all nodes u suchthat there is an s-u path in G .
I Algorithm for the s-t Connectivity problem: compute the connectedcomponent of G that contains s and check if t is in that component.
September 16, 2014 CS4104: Graphs
![Page 34: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/34.jpg)
Computing Connected ComponentsI �Explore� G starting from s and maintain set R of visited nodes.
1
2 3
4 5 6
7
8
9
10
11
12
13
September 16, 2014 CS4104: Graphs
![Page 35: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/35.jpg)
Computing Connected ComponentsI �Explore� G starting from s and maintain set R of visited nodes.
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
4 5 6
7
8
9
10
11
12
13
September 16, 2014 CS4104: Graphs
![Page 36: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/36.jpg)
Computing Connected ComponentsI �Explore� G starting from s and maintain set R of visited nodes.
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
4 5 6
7
8
9
10
11
12
13
September 16, 2014 CS4104: Graphs
![Page 37: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/37.jpg)
Computing Connected ComponentsI �Explore� G starting from s and maintain set R of visited nodes.
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
4 5 6
7
8
9
10
11
12
13
September 16, 2014 CS4104: Graphs
![Page 38: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/38.jpg)
Computing Connected ComponentsI �Explore� G starting from s and maintain set R of visited nodes.
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
4 5 6
7
8
9
10
11
12
13
September 16, 2014 CS4104: Graphs
![Page 39: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/39.jpg)
Computing Connected ComponentsI �Explore� G starting from s and maintain set R of visited nodes.
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
4 5 6
7
8
9
10
11
12
13
September 16, 2014 CS4104: Graphs
![Page 40: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/40.jpg)
Computing Connected ComponentsI �Explore� G starting from s and maintain set R of visited nodes.
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
4 5 6
7
8
9
10
11
12
13
September 16, 2014 CS4104: Graphs
![Page 41: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/41.jpg)
Computing Connected ComponentsI �Explore� G starting from s and maintain set R of visited nodes.
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
4 5 6
7
8
9
10
11
12
13
September 16, 2014 CS4104: Graphs
![Page 42: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/42.jpg)
Computing Connected ComponentsI �Explore� G starting from s and maintain set R of visited nodes.
1
2 3
4 5 6
7
8
9
10
11
12
13
September 16, 2014 CS4104: Graphs
![Page 43: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/43.jpg)
Issues in Computing Connected Components
1
2 3
4 5 6
7
8
9
10
11
12
13
I How do we implement the while loop?
Examine each edge in E .
I Other issues to consider:I Why does the algorithm terminate?I Does the algorithm truly compute connected component of G containing s?I What is the running time of the algorithm?
September 16, 2014 CS4104: Graphs
![Page 44: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/44.jpg)
Issues in Computing Connected Components
1
2 3
4 5 6
7
8
9
10
11
12
13
I How do we implement the while loop? Examine each edge in E .
I Other issues to consider:I Why does the algorithm terminate?I Does the algorithm truly compute connected component of G containing s?I What is the running time of the algorithm?
September 16, 2014 CS4104: Graphs
![Page 45: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/45.jpg)
Issues in Computing Connected Components
1
2 3
4 5 6
7
8
9
10
11
12
13
I How do we implement the while loop? Examine each edge in E .
I Other issues to consider:I Why does the algorithm terminate?I Does the algorithm truly compute connected component of G containing s?I What is the running time of the algorithm?
September 16, 2014 CS4104: Graphs
![Page 46: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/46.jpg)
Termination of the Algorithm
1
2 3
4 5 6
7
8
9
10
11
12
13
I How many nodes does each iteration of the while loop add to R?
Exactly 1.
I How many times is the while loop executed?
At most n times.
I What is true of R at termination?
I either R = V at the end orI in the last iteration, every edge either has both nodes in R or both nodes not
in R.
September 16, 2014 CS4104: Graphs
![Page 47: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/47.jpg)
Termination of the Algorithm
1
2 3
4 5 6
7
8
9
10
11
12
13
I How many nodes does each iteration of the while loop add to R? Exactly 1.
I How many times is the while loop executed?
At most n times.
I What is true of R at termination?
I either R = V at the end orI in the last iteration, every edge either has both nodes in R or both nodes not
in R.
September 16, 2014 CS4104: Graphs
![Page 48: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/48.jpg)
Termination of the Algorithm
1
2 3
4 5 6
7
8
9
10
11
12
13
I How many nodes does each iteration of the while loop add to R? Exactly 1.
I How many times is the while loop executed? At most n times.
I What is true of R at termination?
I either R = V at the end orI in the last iteration, every edge either has both nodes in R or both nodes not
in R.
September 16, 2014 CS4104: Graphs
![Page 49: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/49.jpg)
Termination of the Algorithm
1
2 3
4 5 6
7
8
9
10
11
12
13
I How many nodes does each iteration of the while loop add to R? Exactly 1.
I How many times is the while loop executed? At most n times.
I What is true of R at termination?
I either R = V at the end orI in the last iteration, every edge either has both nodes in R or both nodes not
in R.
September 16, 2014 CS4104: Graphs
![Page 50: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/50.jpg)
Termination of the Algorithm
1
2 3
4 5 6
7
8
9
10
11
12
13
I How many nodes does each iteration of the while loop add to R? Exactly 1.
I How many times is the while loop executed? At most n times.
I What is true of R at termination?I either R = V at the end orI in the last iteration, every edge either has both nodes in R or both nodes not
in R.
September 16, 2014 CS4104: Graphs
![Page 51: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/51.jpg)
Correctness of the Algorithm
R
s
u v
w
I Claim: at the end of the algorithm, the set R is exactly the connectedcomponent of G containing s.
I Proof: Suppose w 6∈ R but there is an s-w path P in G .I Consider �rst node v in P not in R (v 6= s).I Let u be the predecessor of v in P: u is in R.I (u, v) is an edge with u ∈ R but v 6∈ R, contradicting the stopping rule.I Note: wrong to assume that predecessor of w in P is not in R.
September 16, 2014 CS4104: Graphs
![Page 52: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/52.jpg)
Correctness of the Algorithm
R
s
u v
w
I Claim: at the end of the algorithm, the set R is exactly the connectedcomponent of G containing s.
I Proof: Suppose w 6∈ R but there is an s-w path P in G .I Consider �rst node v in P not in R (v 6= s).I Let u be the predecessor of v in P:
u is in R.I (u, v) is an edge with u ∈ R but v 6∈ R, contradicting the stopping rule.I Note: wrong to assume that predecessor of w in P is not in R.
September 16, 2014 CS4104: Graphs
![Page 53: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/53.jpg)
Correctness of the Algorithm
R
s
u v
w
I Claim: at the end of the algorithm, the set R is exactly the connectedcomponent of G containing s.
I Proof: Suppose w 6∈ R but there is an s-w path P in G .I Consider �rst node v in P not in R (v 6= s).I Let u be the predecessor of v in P: u is in R.I (u, v) is an edge with u ∈ R but v 6∈ R, contradicting the stopping rule.
I Note: wrong to assume that predecessor of w in P is not in R.
September 16, 2014 CS4104: Graphs
![Page 54: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/54.jpg)
Correctness of the Algorithm
R
s
u v
w
I Claim: at the end of the algorithm, the set R is exactly the connectedcomponent of G containing s.
I Proof: Suppose w 6∈ R but there is an s-w path P in G .I Consider �rst node v in P not in R (v 6= s).I Let u be the predecessor of v in P: u is in R.I (u, v) is an edge with u ∈ R but v 6∈ R, contradicting the stopping rule.I Note: wrong to assume that predecessor of w in P is not in R.
September 16, 2014 CS4104: Graphs
![Page 55: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/55.jpg)
Recovering Paths
1
2 3
4 5 6
7
8
9
10
11
12
13
I Given a node t ∈ R, how do we recover the s-t path?
I When adding node v to R, record the edge (u, v).
I What type of graph is formed by these edges? It is a tree! Why?
I To recover the s-t path, trace these edges backwards from t until we reach s.
September 16, 2014 CS4104: Graphs
![Page 56: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/56.jpg)
Recovering Paths
1
2 3
4 5 6
7
8
9
10
11
12
13
I Given a node t ∈ R, how do we recover the s-t path?
I When adding node v to R, record the edge (u, v).
I What type of graph is formed by these edges?
It is a tree! Why?
I To recover the s-t path, trace these edges backwards from t until we reach s.
September 16, 2014 CS4104: Graphs
![Page 57: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/57.jpg)
Recovering Paths
1
2 3
4 5 6
7
8
9
10
11
12
13
I Given a node t ∈ R, how do we recover the s-t path?
I When adding node v to R, record the edge (u, v).
I What type of graph is formed by these edges? It is a tree! Why?
I To recover the s-t path, trace these edges backwards from t until we reach s.
September 16, 2014 CS4104: Graphs
![Page 58: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/58.jpg)
Recovering Paths
1
2 3
4 5 6
7
8
9
10
11
12
13
I Given a node t ∈ R, how do we recover the s-t path?
I When adding node v to R, record the edge (u, v).
I What type of graph is formed by these edges? It is a tree! Why?
I To recover the s-t path, trace these edges backwards from t until we reach s.
September 16, 2014 CS4104: Graphs
![Page 59: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/59.jpg)
Running Time of the Algorithm
I Analyse algorithm in terms of two parameters: the number of nodes n andthe number of edges m.
I Implement the while loop by examining each edge in E . Running time ofeach loop is O(m).
I How many while loops does the algorithm execute? At most n.
I The running time is O(mn).
I Can we improve the running time by processing edges more carefully?
September 16, 2014 CS4104: Graphs
![Page 60: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/60.jpg)
Running Time of the Algorithm
I Analyse algorithm in terms of two parameters: the number of nodes n andthe number of edges m.
I Implement the while loop by examining each edge in E . Running time ofeach loop is
O(m).
I How many while loops does the algorithm execute? At most n.
I The running time is O(mn).
I Can we improve the running time by processing edges more carefully?
September 16, 2014 CS4104: Graphs
![Page 61: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/61.jpg)
Running Time of the Algorithm
I Analyse algorithm in terms of two parameters: the number of nodes n andthe number of edges m.
I Implement the while loop by examining each edge in E . Running time ofeach loop is O(m).
I How many while loops does the algorithm execute?
At most n.
I The running time is O(mn).
I Can we improve the running time by processing edges more carefully?
September 16, 2014 CS4104: Graphs
![Page 62: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/62.jpg)
Running Time of the Algorithm
I Analyse algorithm in terms of two parameters: the number of nodes n andthe number of edges m.
I Implement the while loop by examining each edge in E . Running time ofeach loop is O(m).
I How many while loops does the algorithm execute? At most n.
I The running time is
O(mn).
I Can we improve the running time by processing edges more carefully?
September 16, 2014 CS4104: Graphs
![Page 63: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/63.jpg)
Running Time of the Algorithm
I Analyse algorithm in terms of two parameters: the number of nodes n andthe number of edges m.
I Implement the while loop by examining each edge in E . Running time ofeach loop is O(m).
I How many while loops does the algorithm execute? At most n.
I The running time is O(mn).
I Can we improve the running time by processing edges more carefully?
September 16, 2014 CS4104: Graphs
![Page 64: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/64.jpg)
Running Time of the Algorithm
I Analyse algorithm in terms of two parameters: the number of nodes n andthe number of edges m.
I Implement the while loop by examining each edge in E . Running time ofeach loop is O(m).
I How many while loops does the algorithm execute? At most n.
I The running time is O(mn).
I Can we improve the running time by processing edges more carefully?
September 16, 2014 CS4104: Graphs
![Page 65: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/65.jpg)
Breadth-First Search (BFS)
1
2 3
4 5 6
7
8
9
10
11
12
13
I Idea: explore G starting at s and going �outward� in all directions, addingnodes one layer at a time.
I Layer L0 contains only s.
I Layer L1 contains all neighbours of s.
I Given layers L0, L1, . . . , Lj , layer Lj+1 contains all nodes that
1. do not belong to an earlier layer and2. are connected by an edge to a node in layer Lj .
September 16, 2014 CS4104: Graphs
![Page 66: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/66.jpg)
Breadth-First Search (BFS)
1
2 3
4 5 6
7
8
9
10
11
12
13
I Idea: explore G starting at s and going �outward� in all directions, addingnodes one layer at a time.
I Layer L0 contains only s.
I Layer L1 contains all neighbours of s.
I Given layers L0, L1, . . . , Lj , layer Lj+1 contains all nodes that
1. do not belong to an earlier layer and2. are connected by an edge to a node in layer Lj .
September 16, 2014 CS4104: Graphs
![Page 67: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/67.jpg)
Breadth-First Search (BFS)
1
2 3
4 5 6
7
8
9
10
11
12
13
I Idea: explore G starting at s and going �outward� in all directions, addingnodes one layer at a time.
I Layer L0 contains only s.
I Layer L1 contains all neighbours of s.
I Given layers L0, L1, . . . , Lj , layer Lj+1 contains all nodes that
1. do not belong to an earlier layer and2. are connected by an edge to a node in layer Lj .
September 16, 2014 CS4104: Graphs
![Page 68: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/68.jpg)
Breadth-First Search (BFS)
1
2 3
4 5 6
7
8
9
10
11
12
13
I Idea: explore G starting at s and going �outward� in all directions, addingnodes one layer at a time.
I Layer L0 contains only s.
I Layer L1 contains all neighbours of s.
I Given layers L0, L1, . . . , Lj , layer Lj+1 contains all nodes that
1. do not belong to an earlier layer and2. are connected by an edge to a node in layer Lj .
September 16, 2014 CS4104: Graphs
![Page 69: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/69.jpg)
Breadth-First Search (BFS)
1
2 3
4 5 6
7
8
9
10
11
12
13
I Idea: explore G starting at s and going �outward� in all directions, addingnodes one layer at a time.
I Layer L0 contains only s.
I Layer L1 contains all neighbours of s.
I Given layers L0, L1, . . . , Lj , layer Lj+1 contains all nodes that
1. do not belong to an earlier layer and2. are connected by an edge to a node in layer Lj .
September 16, 2014 CS4104: Graphs
![Page 70: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/70.jpg)
Properties of BFS1
2 3
4 5 6
7
8
9
10
11
12
13
I We have not yet described how to compute these layers.I Claim: For each j ≥ 1, layer Lj consists of all nodes
exactly at distance jfrom S . Proof by induction on j .
I Claim: There is a path from s to t if and only if t is a member of some layer.I Let v be a node in layer Lj+1 and u be the ��rst� node in Lj such that (u, v)
is an edge in G . Consider the graph T formed by all such edges, directedfrom u to v .
I Why is T a tree? It is connected. The number of edges in T is the number ofnodes in all the layers minus 1.
I T is called the breadth-�rst search tree.
September 16, 2014 CS4104: Graphs
![Page 71: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/71.jpg)
Properties of BFS1
2 3
4 5 6
7
8
9
10
11
12
13
I We have not yet described how to compute these layers.I Claim: For each j ≥ 1, layer Lj consists of all nodes exactly at distance j
from S . Proof
by induction on j .I Claim: There is a path from s to t if and only if t is a member of some layer.I Let v be a node in layer Lj+1 and u be the ��rst� node in Lj such that (u, v)
is an edge in G . Consider the graph T formed by all such edges, directedfrom u to v .
I Why is T a tree? It is connected. The number of edges in T is the number ofnodes in all the layers minus 1.
I T is called the breadth-�rst search tree.
September 16, 2014 CS4104: Graphs
![Page 72: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/72.jpg)
Properties of BFS1
2 3
4 5 6
7
8
9
10
11
12
13
I We have not yet described how to compute these layers.I Claim: For each j ≥ 1, layer Lj consists of all nodes exactly at distance j
from S . Proof by induction on j .I Claim: There is a path from s to t if and only if t is a member of some layer.
I Let v be a node in layer Lj+1 and u be the ��rst� node in Lj such that (u, v)is an edge in G . Consider the graph T formed by all such edges, directedfrom u to v .
I Why is T a tree? It is connected. The number of edges in T is the number ofnodes in all the layers minus 1.
I T is called the breadth-�rst search tree.
September 16, 2014 CS4104: Graphs
![Page 73: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/73.jpg)
Properties of BFS1
2 3
4 5 6
7
8
9
10
11
12
13
I We have not yet described how to compute these layers.I Claim: For each j ≥ 1, layer Lj consists of all nodes exactly at distance j
from S . Proof by induction on j .I Claim: There is a path from s to t if and only if t is a member of some layer.I Let v be a node in layer Lj+1 and u be the ��rst� node in Lj such that (u, v)
is an edge in G . Consider the graph T formed by all such edges, directedfrom u to v .
I Why is T a tree? It is connected. The number of edges in T is the number ofnodes in all the layers minus 1.
I T is called the breadth-�rst search tree.
September 16, 2014 CS4104: Graphs
![Page 74: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/74.jpg)
Properties of BFS1
2 3
4 5 6
7
8
9
10
11
12
13
I We have not yet described how to compute these layers.I Claim: For each j ≥ 1, layer Lj consists of all nodes exactly at distance j
from S . Proof by induction on j .I Claim: There is a path from s to t if and only if t is a member of some layer.I Let v be a node in layer Lj+1 and u be the ��rst� node in Lj such that (u, v)
is an edge in G . Consider the graph T formed by all such edges, directedfrom u to v .
I Why is T a tree?
It is connected. The number of edges in T is the number ofnodes in all the layers minus 1.
I T is called the breadth-�rst search tree.
September 16, 2014 CS4104: Graphs
![Page 75: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/75.jpg)
Properties of BFS1
2 3
4 5 6
7
8
9
10
11
12
13
I We have not yet described how to compute these layers.I Claim: For each j ≥ 1, layer Lj consists of all nodes exactly at distance j
from S . Proof by induction on j .I Claim: There is a path from s to t if and only if t is a member of some layer.I Let v be a node in layer Lj+1 and u be the ��rst� node in Lj such that (u, v)
is an edge in G . Consider the graph T formed by all such edges, directedfrom u to v .
I Why is T a tree? It is connected. The number of edges in T is the number ofnodes in all the layers minus 1.
I T is called the breadth-�rst search tree.
September 16, 2014 CS4104: Graphs
![Page 76: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/76.jpg)
BFS Trees
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
6
4 5 7 8
I Non-tree edge: an edge of G that does not belong to the BFS tree T .
I Claim: Let T be a BFS tree, let x and y be nodes in T belonging to layers Liand Lj , respectively, and let (x , y) be an edge of G . Then |i − j | ≤ 1.
I Proof by contradiction: Suppose i < j − 1. Node x ∈ Li ⇒ all nodes adjacentto x are in layers L1, L2, . . . Li+1. Hence y must be in layer Li+1 or earlier.
I Still unresolved: an e�cient implementation of BFS.
September 16, 2014 CS4104: Graphs
![Page 77: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/77.jpg)
BFS Trees
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
6
4 5 7 8
I Non-tree edge: an edge of G that does not belong to the BFS tree T .
I Claim: Let T be a BFS tree, let x and y be nodes in T belonging to layers Liand Lj , respectively, and let (x , y) be an edge of G . Then |i − j | ≤ 1.
I Proof by contradiction: Suppose i < j − 1. Node x ∈ Li ⇒ all nodes adjacentto x are in layers L1, L2, . . . Li+1. Hence y must be in layer Li+1 or earlier.
I Still unresolved: an e�cient implementation of BFS.
September 16, 2014 CS4104: Graphs
![Page 78: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/78.jpg)
BFS Trees
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
6
4 5 7 8
I Non-tree edge: an edge of G that does not belong to the BFS tree T .
I Claim: Let T be a BFS tree, let x and y be nodes in T belonging to layers Liand Lj , respectively, and let (x , y) be an edge of G . Then |i − j | ≤ 1.
I Proof by contradiction: Suppose i < j − 1. Node x ∈ Li ⇒ all nodes adjacentto x are in layers L1, L2, . . . Li+1. Hence y must be in layer Li+1 or earlier.
I Still unresolved: an e�cient implementation of BFS.
September 16, 2014 CS4104: Graphs
![Page 79: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/79.jpg)
Depth-First Search (DFS)
I Explore G as if it were a maze: start from s, traverse �rst edge out (to nodev), traverse �rst edge out of v , . . . , reach a dead-end, backtrack, . . . ...
1. Mark all nodes as �Unexplored�.
2. Invoke DFS(s).
I Depth-�rst search tree is a tree T : when DFS(v) is invoked directly duringthe call to DFS(v), add edge (u, v) to T .
September 16, 2014 CS4104: Graphs
![Page 80: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/80.jpg)
Depth-First Search (DFS)
I Explore G as if it were a maze: start from s, traverse �rst edge out (to nodev), traverse �rst edge out of v , . . . , reach a dead-end, backtrack, . . . ...
1. Mark all nodes as �Unexplored�.
2. Invoke DFS(s).
I Depth-�rst search tree is a tree T : when DFS(v) is invoked directly duringthe call to DFS(v), add edge (u, v) to T .
September 16, 2014 CS4104: Graphs
![Page 81: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/81.jpg)
Depth-First Search (DFS)
I Explore G as if it were a maze: start from s, traverse �rst edge out (to nodev), traverse �rst edge out of v , . . . , reach a dead-end, backtrack, . . . ...
1. Mark all nodes as �Unexplored�.
2. Invoke DFS(s).
I Depth-�rst search tree is a tree T : when DFS(v) is invoked directly duringthe call to DFS(v), add edge (u, v) to T .
September 16, 2014 CS4104: Graphs
![Page 82: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/82.jpg)
Example of DFS
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
6
4 5 7 8
1
September 16, 2014 CS4104: Graphs
![Page 83: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/83.jpg)
Example of DFS
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
6
4 5 7 8
1
2
September 16, 2014 CS4104: Graphs
![Page 84: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/84.jpg)
Example of DFS
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
6
4 5 7 8
1
2
5
September 16, 2014 CS4104: Graphs
![Page 85: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/85.jpg)
Example of DFS
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
6
4 5 7 8
6
1
2
5
September 16, 2014 CS4104: Graphs
![Page 86: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/86.jpg)
Example of DFS
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
6
4 5 7 8
36
1
2
5
September 16, 2014 CS4104: Graphs
![Page 87: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/87.jpg)
Example of DFS
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
6
4 5 7 8
36
1
2
5
7
September 16, 2014 CS4104: Graphs
![Page 88: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/88.jpg)
Example of DFS
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
6
4 5 7 8
36
1
2
5
7
8
September 16, 2014 CS4104: Graphs
![Page 89: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/89.jpg)
Example of DFS
1
2 3
4 5 6
7
8
9
10
11
12
13
1
2 3
6
4 5 7 8
36 4
1
2
5
7
8
September 16, 2014 CS4104: Graphs
![Page 90: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/90.jpg)
BFS vs. DFS1
2 3
6
4 5 7 836 4
1
2
5
7
8
I Both visit the same set of nodes but in a di�erent order.I Both traverse all the edges in the connected component but in a di�erent
order.I BFS trees have root-to-leaf paths that look as short as possible while paths in
DFS trees tend to be long and deep.I Non-tree edges
BFS within the same level or between adjacent levels.
DFS connect ancestors to descendants.
September 16, 2014 CS4104: Graphs
![Page 91: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/91.jpg)
BFS vs. DFS1
2 3
6
4 5 7 836 4
1
2
5
7
8
I Both visit the same set of nodes but in a di�erent order.I Both traverse all the edges in the connected component but in a di�erent
order.I BFS trees have root-to-leaf paths that look as short as possible while paths in
DFS trees tend to be long and deep.I Non-tree edges
BFS within the same level or between adjacent levels.DFS connect ancestors to descendants.
September 16, 2014 CS4104: Graphs
![Page 92: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/92.jpg)
Properties of DFS Trees
36 4
1
2
5
7
8
I Observation: All nodes marked as �Explored� between the start of DFS(u)and its end are descendants of u in the DFS tree T .
I Claim: Let x and y be nodes in a DFS tree T such that (x , y) is an edge ofG but not of T . Then one of x or y is an ancestor of the other in T .
I Proof: Assume, without loss of generality, that DFS(u) reached x �rst.I Since (x , y) is an edge in G , it is examined during DFS(x).I Since (x , y) 6∈ T , y must be marked as �Explored� during DFS(x) but before
(x , y) is examined.I Since y was not marked as �Explored� before DFS(x) was invoked, it must be
marked as �Explored� between the end of DFS(x).I Therefore, y must be a descendant of x in T .
September 16, 2014 CS4104: Graphs
![Page 93: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/93.jpg)
Properties of DFS Trees
36 4
1
2
5
7
8
I Observation: All nodes marked as �Explored� between the start of DFS(u)and its end are descendants of u in the DFS tree T .
I Claim: Let x and y be nodes in a DFS tree T such that (x , y) is an edge ofG but not of T . Then one of x or y is an ancestor of the other in T .
I Proof: Assume, without loss of generality, that DFS(u) reached x �rst.I Since (x , y) is an edge in G , it is examined during DFS(x).I Since (x , y) 6∈ T , y must be marked as �Explored� during DFS(x) but before
(x , y) is examined.I Since y was not marked as �Explored� before DFS(x) was invoked, it must be
marked as �Explored� between the end of DFS(x).I Therefore, y must be a descendant of x in T .
September 16, 2014 CS4104: Graphs
![Page 94: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/94.jpg)
Properties of DFS Trees
36 4
1
2
5
7
8
I Observation: All nodes marked as �Explored� between the start of DFS(u)and its end are descendants of u in the DFS tree T .
I Claim: Let x and y be nodes in a DFS tree T such that (x , y) is an edge ofG but not of T . Then one of x or y is an ancestor of the other in T .
I Proof: Assume, without loss of generality, that DFS(u) reached x �rst.I Since (x , y) is an edge in G , it is examined during DFS(x).I Since (x , y) 6∈ T , y must be marked as �Explored� during DFS(x) but before
(x , y) is examined.I Since y was not marked as �Explored� before DFS(x) was invoked, it must be
marked as �Explored� between the end of DFS(x).I Therefore, y must be a descendant of x in T .
September 16, 2014 CS4104: Graphs
![Page 95: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/95.jpg)
All Connected Components
I We have discussed the component containing a particular node s.
I Each node belongs to a component.
I What is the relationship between all these components?
I If v is in u's component, is u in v 's component?I If v is not in u's component, can u be in v 's component?
I Claim: For any two nodes s and t in a graph, their connected componentsare either equal or disjoint.
I Proof in two parts (sketch):
1. If G has an s-t path, then the connected components of s and t are the same.2. If G has no s-t path, then there cannot be a node v that is in both connected
components.
September 16, 2014 CS4104: Graphs
![Page 96: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/96.jpg)
All Connected Components
I We have discussed the component containing a particular node s.
I Each node belongs to a component.
I What is the relationship between all these components?I If v is in u's component, is u in v 's component?I If v is not in u's component, can u be in v 's component?
I Claim: For any two nodes s and t in a graph, their connected componentsare either equal or disjoint.
I Proof in two parts (sketch):
1. If G has an s-t path, then the connected components of s and t are the same.2. If G has no s-t path, then there cannot be a node v that is in both connected
components.
September 16, 2014 CS4104: Graphs
![Page 97: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/97.jpg)
All Connected Components
I We have discussed the component containing a particular node s.
I Each node belongs to a component.
I What is the relationship between all these components?I If v is in u's component, is u in v 's component?I If v is not in u's component, can u be in v 's component?
I Claim: For any two nodes s and t in a graph, their connected componentsare either equal or disjoint.
I Proof in two parts (sketch):
1. If G has an s-t path, then the connected components of s and t are the same.2. If G has no s-t path, then there cannot be a node v that is in both connected
components.
September 16, 2014 CS4104: Graphs
![Page 98: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/98.jpg)
All Connected Components
I We have discussed the component containing a particular node s.
I Each node belongs to a component.
I What is the relationship between all these components?I If v is in u's component, is u in v 's component?I If v is not in u's component, can u be in v 's component?
I Claim: For any two nodes s and t in a graph, their connected componentsare either equal or disjoint.
I Proof in two parts (sketch):
1. If G has an s-t path, then the connected components of s and t are the same.
2. If G has no s-t path, then there cannot be a node v that is in both connectedcomponents.
September 16, 2014 CS4104: Graphs
![Page 99: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/99.jpg)
All Connected Components
I We have discussed the component containing a particular node s.
I Each node belongs to a component.
I What is the relationship between all these components?I If v is in u's component, is u in v 's component?I If v is not in u's component, can u be in v 's component?
I Claim: For any two nodes s and t in a graph, their connected componentsare either equal or disjoint.
I Proof in two parts (sketch):
1. If G has an s-t path, then the connected components of s and t are the same.2. If G has no s-t path, then there cannot be a node v that is in both connected
components.
September 16, 2014 CS4104: Graphs
![Page 100: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/100.jpg)
Computing All Connected Components
1. Pick an arbitrary node s in G .
2. Compute its connected component using BFS (or DFS).
3. Find a node (say v , not already visited) and repeat the BFS from v .
4. Repeat this process until all nodes are visited.
September 16, 2014 CS4104: Graphs
![Page 101: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/101.jpg)
Representing Graphs
I Graph G = (V ,E ) has two input parameters: |V | = n, |E | = m.I Size of the graph is de�ned to be m + n.I Strive for algorithms whose running time is linear in graph size, i.e., O(m + n).
I Assume V = {1, 2, . . . , n − 1, n}.I Adjacency matrix representation: n × n Boolean matrix, where the entry in
row i and column j is 1 i� the graph contains the edge (i , j).I Space used is Θ(n2), which is optimal in the worst case.I Check if there is an edge between node i and node j in O(1) time.I Iterate over all the edges incident on node i in Θ(n) time.
I Adjacency list representation: array Adj, where Adj[v ] stores the list of allnodes adjacent to v .
I An edge e = (u, v) appears twice: in Adj[u] and Adj[v ].I nv = the number of neighbours of node v .I Space used is O(n +
∑v∈G nv ) = O(n + m), which is optimal for every graph.
I Check if there is an edge between node u and node v in O(nu) time.I Iterate over all the edges incident on node u in Θ(nu) time.
September 16, 2014 CS4104: Graphs
![Page 102: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/102.jpg)
Representing Graphs
I Graph G = (V ,E ) has two input parameters: |V | = n, |E | = m.I Size of the graph is de�ned to be m + n.I Strive for algorithms whose running time is linear in graph size, i.e., O(m + n).
I Assume V = {1, 2, . . . , n − 1, n}.I Adjacency matrix representation: n × n Boolean matrix, where the entry in
row i and column j is 1 i� the graph contains the edge (i , j).I Space used is
Θ(n2), which is optimal in the worst case.I Check if there is an edge between node i and node j in O(1) time.I Iterate over all the edges incident on node i in Θ(n) time.
I Adjacency list representation: array Adj, where Adj[v ] stores the list of allnodes adjacent to v .
I An edge e = (u, v) appears twice: in Adj[u] and Adj[v ].I nv = the number of neighbours of node v .I Space used is O(n +
∑v∈G nv ) = O(n + m), which is optimal for every graph.
I Check if there is an edge between node u and node v in O(nu) time.I Iterate over all the edges incident on node u in Θ(nu) time.
September 16, 2014 CS4104: Graphs
![Page 103: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/103.jpg)
Representing Graphs
I Graph G = (V ,E ) has two input parameters: |V | = n, |E | = m.I Size of the graph is de�ned to be m + n.I Strive for algorithms whose running time is linear in graph size, i.e., O(m + n).
I Assume V = {1, 2, . . . , n − 1, n}.I Adjacency matrix representation: n × n Boolean matrix, where the entry in
row i and column j is 1 i� the graph contains the edge (i , j).I Space used is Θ(n2), which is optimal in the worst case.I Check if there is an edge between node i and node j in
O(1) time.I Iterate over all the edges incident on node i in Θ(n) time.
I Adjacency list representation: array Adj, where Adj[v ] stores the list of allnodes adjacent to v .
I An edge e = (u, v) appears twice: in Adj[u] and Adj[v ].I nv = the number of neighbours of node v .I Space used is O(n +
∑v∈G nv ) = O(n + m), which is optimal for every graph.
I Check if there is an edge between node u and node v in O(nu) time.I Iterate over all the edges incident on node u in Θ(nu) time.
September 16, 2014 CS4104: Graphs
![Page 104: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/104.jpg)
Representing Graphs
I Graph G = (V ,E ) has two input parameters: |V | = n, |E | = m.I Size of the graph is de�ned to be m + n.I Strive for algorithms whose running time is linear in graph size, i.e., O(m + n).
I Assume V = {1, 2, . . . , n − 1, n}.I Adjacency matrix representation: n × n Boolean matrix, where the entry in
row i and column j is 1 i� the graph contains the edge (i , j).I Space used is Θ(n2), which is optimal in the worst case.I Check if there is an edge between node i and node j in O(1) time.I Iterate over all the edges incident on node i in
Θ(n) time.
I Adjacency list representation: array Adj, where Adj[v ] stores the list of allnodes adjacent to v .
I An edge e = (u, v) appears twice: in Adj[u] and Adj[v ].I nv = the number of neighbours of node v .I Space used is O(n +
∑v∈G nv ) = O(n + m), which is optimal for every graph.
I Check if there is an edge between node u and node v in O(nu) time.I Iterate over all the edges incident on node u in Θ(nu) time.
September 16, 2014 CS4104: Graphs
![Page 105: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/105.jpg)
Representing Graphs
I Graph G = (V ,E ) has two input parameters: |V | = n, |E | = m.I Size of the graph is de�ned to be m + n.I Strive for algorithms whose running time is linear in graph size, i.e., O(m + n).
I Assume V = {1, 2, . . . , n − 1, n}.I Adjacency matrix representation: n × n Boolean matrix, where the entry in
row i and column j is 1 i� the graph contains the edge (i , j).I Space used is Θ(n2), which is optimal in the worst case.I Check if there is an edge between node i and node j in O(1) time.I Iterate over all the edges incident on node i in Θ(n) time.
I Adjacency list representation: array Adj, where Adj[v ] stores the list of allnodes adjacent to v .
I An edge e = (u, v) appears twice: in Adj[u] and Adj[v ].I nv = the number of neighbours of node v .I Space used is O(n +
∑v∈G nv ) = O(n + m), which is optimal for every graph.
I Check if there is an edge between node u and node v in O(nu) time.I Iterate over all the edges incident on node u in Θ(nu) time.
September 16, 2014 CS4104: Graphs
![Page 106: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/106.jpg)
Representing Graphs
I Graph G = (V ,E ) has two input parameters: |V | = n, |E | = m.I Size of the graph is de�ned to be m + n.I Strive for algorithms whose running time is linear in graph size, i.e., O(m + n).
I Assume V = {1, 2, . . . , n − 1, n}.I Adjacency matrix representation: n × n Boolean matrix, where the entry in
row i and column j is 1 i� the graph contains the edge (i , j).I Space used is Θ(n2), which is optimal in the worst case.I Check if there is an edge between node i and node j in O(1) time.I Iterate over all the edges incident on node i in Θ(n) time.
I Adjacency list representation: array Adj, where Adj[v ] stores the list of allnodes adjacent to v .
I An edge e = (u, v) appears twice: in Adj[u] and Adj[v ].
I nv = the number of neighbours of node v .I Space used is O(n +
∑v∈G nv ) = O(n + m), which is optimal for every graph.
I Check if there is an edge between node u and node v in O(nu) time.I Iterate over all the edges incident on node u in Θ(nu) time.
September 16, 2014 CS4104: Graphs
![Page 107: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/107.jpg)
Representing Graphs
I Graph G = (V ,E ) has two input parameters: |V | = n, |E | = m.I Size of the graph is de�ned to be m + n.I Strive for algorithms whose running time is linear in graph size, i.e., O(m + n).
I Assume V = {1, 2, . . . , n − 1, n}.I Adjacency matrix representation: n × n Boolean matrix, where the entry in
row i and column j is 1 i� the graph contains the edge (i , j).I Space used is Θ(n2), which is optimal in the worst case.I Check if there is an edge between node i and node j in O(1) time.I Iterate over all the edges incident on node i in Θ(n) time.
I Adjacency list representation: array Adj, where Adj[v ] stores the list of allnodes adjacent to v .
I An edge e = (u, v) appears twice: in Adj[u] and Adj[v ].I nv = the number of neighbours of node v .I Space used is
O(n +∑
v∈G nv ) = O(n + m), which is optimal for every graph.I Check if there is an edge between node u and node v in O(nu) time.I Iterate over all the edges incident on node u in Θ(nu) time.
September 16, 2014 CS4104: Graphs
![Page 108: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/108.jpg)
Representing Graphs
I Graph G = (V ,E ) has two input parameters: |V | = n, |E | = m.I Size of the graph is de�ned to be m + n.I Strive for algorithms whose running time is linear in graph size, i.e., O(m + n).
I Assume V = {1, 2, . . . , n − 1, n}.I Adjacency matrix representation: n × n Boolean matrix, where the entry in
row i and column j is 1 i� the graph contains the edge (i , j).I Space used is Θ(n2), which is optimal in the worst case.I Check if there is an edge between node i and node j in O(1) time.I Iterate over all the edges incident on node i in Θ(n) time.
I Adjacency list representation: array Adj, where Adj[v ] stores the list of allnodes adjacent to v .
I An edge e = (u, v) appears twice: in Adj[u] and Adj[v ].I nv = the number of neighbours of node v .I Space used is O(n +
∑v∈G nv ) =
O(n + m), which is optimal for every graph.I Check if there is an edge between node u and node v in O(nu) time.I Iterate over all the edges incident on node u in Θ(nu) time.
September 16, 2014 CS4104: Graphs
![Page 109: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/109.jpg)
Representing Graphs
I Graph G = (V ,E ) has two input parameters: |V | = n, |E | = m.I Size of the graph is de�ned to be m + n.I Strive for algorithms whose running time is linear in graph size, i.e., O(m + n).
I Assume V = {1, 2, . . . , n − 1, n}.I Adjacency matrix representation: n × n Boolean matrix, where the entry in
row i and column j is 1 i� the graph contains the edge (i , j).I Space used is Θ(n2), which is optimal in the worst case.I Check if there is an edge between node i and node j in O(1) time.I Iterate over all the edges incident on node i in Θ(n) time.
I Adjacency list representation: array Adj, where Adj[v ] stores the list of allnodes adjacent to v .
I An edge e = (u, v) appears twice: in Adj[u] and Adj[v ].I nv = the number of neighbours of node v .I Space used is O(n +
∑v∈G nv ) = O(n + m), which is optimal for every graph.
I Check if there is an edge between node u and node v in
O(nu) time.I Iterate over all the edges incident on node u in Θ(nu) time.
September 16, 2014 CS4104: Graphs
![Page 110: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/110.jpg)
Representing Graphs
I Graph G = (V ,E ) has two input parameters: |V | = n, |E | = m.I Size of the graph is de�ned to be m + n.I Strive for algorithms whose running time is linear in graph size, i.e., O(m + n).
I Assume V = {1, 2, . . . , n − 1, n}.I Adjacency matrix representation: n × n Boolean matrix, where the entry in
row i and column j is 1 i� the graph contains the edge (i , j).I Space used is Θ(n2), which is optimal in the worst case.I Check if there is an edge between node i and node j in O(1) time.I Iterate over all the edges incident on node i in Θ(n) time.
I Adjacency list representation: array Adj, where Adj[v ] stores the list of allnodes adjacent to v .
I An edge e = (u, v) appears twice: in Adj[u] and Adj[v ].I nv = the number of neighbours of node v .I Space used is O(n +
∑v∈G nv ) = O(n + m), which is optimal for every graph.
I Check if there is an edge between node u and node v in O(nu) time.I Iterate over all the edges incident on node u in
Θ(nu) time.
September 16, 2014 CS4104: Graphs
![Page 111: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/111.jpg)
Representing Graphs
I Graph G = (V ,E ) has two input parameters: |V | = n, |E | = m.I Size of the graph is de�ned to be m + n.I Strive for algorithms whose running time is linear in graph size, i.e., O(m + n).
I Assume V = {1, 2, . . . , n − 1, n}.I Adjacency matrix representation: n × n Boolean matrix, where the entry in
row i and column j is 1 i� the graph contains the edge (i , j).I Space used is Θ(n2), which is optimal in the worst case.I Check if there is an edge between node i and node j in O(1) time.I Iterate over all the edges incident on node i in Θ(n) time.
I Adjacency list representation: array Adj, where Adj[v ] stores the list of allnodes adjacent to v .
I An edge e = (u, v) appears twice: in Adj[u] and Adj[v ].I nv = the number of neighbours of node v .I Space used is O(n +
∑v∈G nv ) = O(n + m), which is optimal for every graph.
I Check if there is an edge between node u and node v in O(nu) time.I Iterate over all the edges incident on node u in Θ(nu) time.
September 16, 2014 CS4104: Graphs
![Page 112: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/112.jpg)
Data Structures for Implementation
I �Implementation� of BFS and DFS: fully specify the algorithms and datastructures so that we can obtain provably e�cient times.
I Inner loop of both BFS and DFS: process the set of edges incident on a givennode and the set of visited nodes.
I How do we store the set of visited nodes? Order in which we process thenodes is crucial.
I BFS: store visited nodes in a queue (�rst-in, �rst-out).I DFS: store visited nodes in a stack (last-in, �rst-out)
September 16, 2014 CS4104: Graphs
![Page 113: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/113.jpg)
Data Structures for Implementation
I �Implementation� of BFS and DFS: fully specify the algorithms and datastructures so that we can obtain provably e�cient times.
I Inner loop of both BFS and DFS: process the set of edges incident on a givennode and the set of visited nodes.
I How do we store the set of visited nodes? Order in which we process thenodes is crucial.
I BFS: store visited nodes in a queue (�rst-in, �rst-out).I DFS: store visited nodes in a stack (last-in, �rst-out)
September 16, 2014 CS4104: Graphs
![Page 114: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/114.jpg)
Implementing BFSI Maintain an array Discovered and set
Discovered[v ] = true as soon as the algorithm sees v .
1
2 3
4 5 6
7
8
9
10
11
12
13
3 2
1
2 3
4 5 6
7
8
9
10
11
12
13
5 7 8 4
1
2 3
4 5 6
7
8
9
10
11
12
13
6
1
2 3
4 5 6
7
8
9
10
11
12
13
September 16, 2014 CS4104: Graphs
![Page 115: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/115.jpg)
Using a Queue in BFSI Instead of storing each layer in a di�erent list, maintain all the layers in a
single queue L.BFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
1
2 3
4 5 6
7
8
1
I Simple to modify this procedure to keep track of layer numbers as well.
Storethe pair (u, lu), where lu is the index of the layer containing u.
I Claim: Nodes in layer i + 1 will appear in L immediately after nodes in layeri .
More formally: If BFS(s) pops (v , lv ) from L immediately after it pops(u, lu), then either lv = lu or lv = lu + 1.
September 16, 2014 CS4104: Graphs
![Page 116: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/116.jpg)
Using a Queue in BFSI Instead of storing each layer in a di�erent list, maintain all the layers in a
single queue L.BFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
1
1
2 3
4 5 6
7
8
3
I Simple to modify this procedure to keep track of layer numbers as well.
Storethe pair (u, lu), where lu is the index of the layer containing u.
I Claim: Nodes in layer i + 1 will appear in L immediately after nodes in layeri .
More formally: If BFS(s) pops (v , lv ) from L immediately after it pops(u, lu), then either lv = lu or lv = lu + 1.
September 16, 2014 CS4104: Graphs
![Page 117: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/117.jpg)
Using a Queue in BFSI Instead of storing each layer in a di�erent list, maintain all the layers in a
single queue L.BFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
1
2 3
4 5 6
7
8
3 21
I Simple to modify this procedure to keep track of layer numbers as well.
Storethe pair (u, lu), where lu is the index of the layer containing u.
I Claim: Nodes in layer i + 1 will appear in L immediately after nodes in layeri .
More formally: If BFS(s) pops (v , lv ) from L immediately after it pops(u, lu), then either lv = lu or lv = lu + 1.
September 16, 2014 CS4104: Graphs
![Page 118: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/118.jpg)
Using a Queue in BFSI Instead of storing each layer in a di�erent list, maintain all the layers in a
single queue L.BFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
1
2 3
4 5 6
7
8
23 5
I Simple to modify this procedure to keep track of layer numbers as well.
Storethe pair (u, lu), where lu is the index of the layer containing u.
I Claim: Nodes in layer i + 1 will appear in L immediately after nodes in layeri .
More formally: If BFS(s) pops (v , lv ) from L immediately after it pops(u, lu), then either lv = lu or lv = lu + 1.
September 16, 2014 CS4104: Graphs
![Page 119: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/119.jpg)
Using a Queue in BFSI Instead of storing each layer in a di�erent list, maintain all the layers in a
single queue L.BFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
1
2 3
4 5 6
7
8
23 5 7
I Simple to modify this procedure to keep track of layer numbers as well.
Storethe pair (u, lu), where lu is the index of the layer containing u.
I Claim: Nodes in layer i + 1 will appear in L immediately after nodes in layeri .
More formally: If BFS(s) pops (v , lv ) from L immediately after it pops(u, lu), then either lv = lu or lv = lu + 1.
September 16, 2014 CS4104: Graphs
![Page 120: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/120.jpg)
Using a Queue in BFSI Instead of storing each layer in a di�erent list, maintain all the layers in a
single queue L.BFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
1
2 3
4 5 6
7
8
23 5 7 8
I Simple to modify this procedure to keep track of layer numbers as well.
Storethe pair (u, lu), where lu is the index of the layer containing u.
I Claim: Nodes in layer i + 1 will appear in L immediately after nodes in layeri .
More formally: If BFS(s) pops (v , lv ) from L immediately after it pops(u, lu), then either lv = lu or lv = lu + 1.
September 16, 2014 CS4104: Graphs
![Page 121: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/121.jpg)
Using a Queue in BFSI Instead of storing each layer in a di�erent list, maintain all the layers in a
single queue L.BFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
1
2 3
4 5 6
7
8
52 7 8 4
I Simple to modify this procedure to keep track of layer numbers as well.
Storethe pair (u, lu), where lu is the index of the layer containing u.
I Claim: Nodes in layer i + 1 will appear in L immediately after nodes in layeri .
More formally: If BFS(s) pops (v , lv ) from L immediately after it pops(u, lu), then either lv = lu or lv = lu + 1.
September 16, 2014 CS4104: Graphs
![Page 122: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/122.jpg)
Using a Queue in BFSI Instead of storing each layer in a di�erent list, maintain all the layers in a
single queue L.BFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
1
2 3
4 5 6
7
8
75 8 4 6
I Simple to modify this procedure to keep track of layer numbers as well.
Storethe pair (u, lu), where lu is the index of the layer containing u.
I Claim: Nodes in layer i + 1 will appear in L immediately after nodes in layeri .
More formally: If BFS(s) pops (v , lv ) from L immediately after it pops(u, lu), then either lv = lu or lv = lu + 1.
September 16, 2014 CS4104: Graphs
![Page 123: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/123.jpg)
Using a Queue in BFSI Instead of storing each layer in a di�erent list, maintain all the layers in a
single queue L.BFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
1
2 3
4 5 6
7
8
87 4 6
I Simple to modify this procedure to keep track of layer numbers as well.
Storethe pair (u, lu), where lu is the index of the layer containing u.
I Claim: Nodes in layer i + 1 will appear in L immediately after nodes in layeri .
More formally: If BFS(s) pops (v , lv ) from L immediately after it pops(u, lu), then either lv = lu or lv = lu + 1.
September 16, 2014 CS4104: Graphs
![Page 124: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/124.jpg)
Using a Queue in BFSI Instead of storing each layer in a di�erent list, maintain all the layers in a
single queue L.BFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
1
2 3
4 5 6
7
8
48 6
I Simple to modify this procedure to keep track of layer numbers as well.
Storethe pair (u, lu), where lu is the index of the layer containing u.
I Claim: Nodes in layer i + 1 will appear in L immediately after nodes in layeri .
More formally: If BFS(s) pops (v , lv ) from L immediately after it pops(u, lu), then either lv = lu or lv = lu + 1.
September 16, 2014 CS4104: Graphs
![Page 125: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/125.jpg)
Using a Queue in BFSI Instead of storing each layer in a di�erent list, maintain all the layers in a
single queue L.BFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
1
2 3
4 5 6
7
8
64
I Simple to modify this procedure to keep track of layer numbers as well.
Storethe pair (u, lu), where lu is the index of the layer containing u.
I Claim: Nodes in layer i + 1 will appear in L immediately after nodes in layeri .
More formally: If BFS(s) pops (v , lv ) from L immediately after it pops(u, lu), then either lv = lu or lv = lu + 1.
September 16, 2014 CS4104: Graphs
![Page 126: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/126.jpg)
Using a Queue in BFSI Instead of storing each layer in a di�erent list, maintain all the layers in a
single queue L.BFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
1
2 3
4 5 6
7
8
6
I Simple to modify this procedure to keep track of layer numbers as well.
Storethe pair (u, lu), where lu is the index of the layer containing u.
I Claim: Nodes in layer i + 1 will appear in L immediately after nodes in layeri .
More formally: If BFS(s) pops (v , lv ) from L immediately after it pops(u, lu), then either lv = lu or lv = lu + 1.
September 16, 2014 CS4104: Graphs
![Page 127: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/127.jpg)
Using a Queue in BFSI Instead of storing each layer in a di�erent list, maintain all the layers in a
single queue L.BFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
1
2 3
4 5 6
7
8
6
I Simple to modify this procedure to keep track of layer numbers as well.
Storethe pair (u, lu), where lu is the index of the layer containing u.
I Claim: Nodes in layer i + 1 will appear in L immediately after nodes in layeri .
More formally: If BFS(s) pops (v , lv ) from L immediately after it pops(u, lu), then either lv = lu or lv = lu + 1.
September 16, 2014 CS4104: Graphs
![Page 128: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/128.jpg)
Using a Queue in BFSI Instead of storing each layer in a di�erent list, maintain all the layers in a
single queue L.BFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
1
2 3
4 5 6
7
8
6
I Simple to modify this procedure to keep track of layer numbers as well. Storethe pair (u, lu), where lu is the index of the layer containing u.
I Claim: Nodes in layer i + 1 will appear in L immediately after nodes in layeri .
More formally: If BFS(s) pops (v , lv ) from L immediately after it pops(u, lu), then either lv = lu or lv = lu + 1.
September 16, 2014 CS4104: Graphs
![Page 129: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/129.jpg)
Using a Queue in BFSI Instead of storing each layer in a di�erent list, maintain all the layers in a
single queue L.BFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
1
2 3
4 5 6
7
8
6
I Simple to modify this procedure to keep track of layer numbers as well. Storethe pair (u, lu), where lu is the index of the layer containing u.
I Claim: Nodes in layer i + 1 will appear in L immediately after nodes in layeri .
More formally: If BFS(s) pops (v , lv ) from L immediately after it pops(u, lu), then either lv = lu or lv = lu + 1.
September 16, 2014 CS4104: Graphs
![Page 130: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/130.jpg)
Using a Queue in BFSI Instead of storing each layer in a di�erent list, maintain all the layers in a
single queue L.BFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
1
2 3
4 5 6
7
8
6
I Simple to modify this procedure to keep track of layer numbers as well. Storethe pair (u, lu), where lu is the index of the layer containing u.
I Claim: Nodes in layer i + 1 will appear in L immediately after nodes in layeri . More formally: If BFS(s) pops (v , lv ) from L immediately after it pops(u, lu), then either lv = lu or lv = lu + 1.
September 16, 2014 CS4104: Graphs
![Page 131: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/131.jpg)
Analysis of BFS ImplementationBFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
I Naive bound on running time is
O(n2): For each node, we spend O(n) time.I Improved bound:
I How many times is a node popped from L? Exactly once.I Time used by for loop for a node u: O(nu) time.I Total time for all for loops:
∑u∈G O(nu) = O(m) time.
I Maintaining layer information: O(1) time per node.I Total time is O(n + m).
September 16, 2014 CS4104: Graphs
![Page 132: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/132.jpg)
Analysis of BFS ImplementationBFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
I Naive bound on running time is O(n2): For each node, we spend O(n) time.
I Improved bound:I How many times is a node popped from L? Exactly once.I Time used by for loop for a node u: O(nu) time.I Total time for all for loops:
∑u∈G O(nu) = O(m) time.
I Maintaining layer information: O(1) time per node.I Total time is O(n + m).
September 16, 2014 CS4104: Graphs
![Page 133: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/133.jpg)
Analysis of BFS ImplementationBFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
I Naive bound on running time is O(n2): For each node, we spend O(n) time.I Improved bound:
I How many times is a node popped from L?
Exactly once.I Time used by for loop for a node u: O(nu) time.I Total time for all for loops:
∑u∈G O(nu) = O(m) time.
I Maintaining layer information: O(1) time per node.I Total time is O(n + m).
September 16, 2014 CS4104: Graphs
![Page 134: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/134.jpg)
Analysis of BFS ImplementationBFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
I Naive bound on running time is O(n2): For each node, we spend O(n) time.I Improved bound:
I How many times is a node popped from L? Exactly once.
I Time used by for loop for a node u: O(nu) time.I Total time for all for loops:
∑u∈G O(nu) = O(m) time.
I Maintaining layer information: O(1) time per node.I Total time is O(n + m).
September 16, 2014 CS4104: Graphs
![Page 135: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/135.jpg)
Analysis of BFS ImplementationBFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
I Naive bound on running time is O(n2): For each node, we spend O(n) time.I Improved bound:
I How many times is a node popped from L? Exactly once.I Time used by for loop for a node u:
O(nu) time.I Total time for all for loops:
∑u∈G O(nu) = O(m) time.
I Maintaining layer information: O(1) time per node.I Total time is O(n + m).
September 16, 2014 CS4104: Graphs
![Page 136: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/136.jpg)
Analysis of BFS ImplementationBFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
I Naive bound on running time is O(n2): For each node, we spend O(n) time.I Improved bound:
I How many times is a node popped from L? Exactly once.I Time used by for loop for a node u: O(nu) time.
I Total time for all for loops:∑
u∈G O(nu) = O(m) time.I Maintaining layer information: O(1) time per node.I Total time is O(n + m).
September 16, 2014 CS4104: Graphs
![Page 137: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/137.jpg)
Analysis of BFS ImplementationBFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
I Naive bound on running time is O(n2): For each node, we spend O(n) time.I Improved bound:
I How many times is a node popped from L? Exactly once.I Time used by for loop for a node u: O(nu) time.I Total time for all for loops:
∑u∈G O(nu) = O(m) time.
I Maintaining layer information:
O(1) time per node.I Total time is O(n + m).
September 16, 2014 CS4104: Graphs
![Page 138: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/138.jpg)
Analysis of BFS ImplementationBFS(s):
Set Discovered[s] = true
Set Discovered[v] = false, for all other nodes vInitialize L to consist of the single element sWhile L is not empty
Pop the node u at the head of LConsider each edge (u, v) incident on uIf Discovered[v] = false then
Set Discovered[v] = true
Add edge (u, v) to the tree TPush v to the back of L
Endif
Endwhile
I Naive bound on running time is O(n2): For each node, we spend O(n) time.I Improved bound:
I How many times is a node popped from L? Exactly once.I Time used by for loop for a node u: O(nu) time.I Total time for all for loops:
∑u∈G O(nu) = O(m) time.
I Maintaining layer information: O(1) time per node.I Total time is O(n + m).
September 16, 2014 CS4104: Graphs
![Page 139: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/139.jpg)
Recursive DFS
I Procedure has �tail recursion�: recursive call is the last step.
I Can replace the recursion by an iteration: use a stack to explicitly implementthe recursion.
September 16, 2014 CS4104: Graphs
![Page 140: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/140.jpg)
Recursive DFS
I Procedure has �tail recursion�: recursive call is the last step.
I Can replace the recursion by an iteration: use a stack to explicitly implementthe recursion.
September 16, 2014 CS4104: Graphs
![Page 141: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/141.jpg)
Implementing DFS
I Maintain a stack S to store nodes to be explored.
I Maintain an array Explored and set Explored[v ] = true when the algorithmpops v from the stack.
I Read textbook on how to construct the DFS tree.
September 16, 2014 CS4104: Graphs
![Page 142: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/142.jpg)
Implementing DFS
I Maintain a stack S to store nodes to be explored.
I Maintain an array Explored and set Explored[v ] = true when the algorithmpops v from the stack.
I Read textbook on how to construct the DFS tree.
1
2 3
4 5 6
7
8
1
September 16, 2014 CS4104: Graphs
![Page 143: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/143.jpg)
Implementing DFS
I Maintain a stack S to store nodes to be explored.
I Maintain an array Explored and set Explored[v ] = true when the algorithmpops v from the stack.
I Read textbook on how to construct the DFS tree.
1
2 3
4 5 6
7
8
2
3
1
Add parentpointer whenpushing to stack
September 16, 2014 CS4104: Graphs
![Page 144: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/144.jpg)
Implementing DFS
I Maintain a stack S to store nodes to be explored.
I Maintain an array Explored and set Explored[v ] = true when the algorithmpops v from the stack.
I Read textbook on how to construct the DFS tree.
1
2 3
4 5 6
7
8
2
2
3
5
7Overwriteparent pointer ifpushing node again
8
Node maybe on stackmore thanonce
September 16, 2014 CS4104: Graphs
![Page 145: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/145.jpg)
Implementing DFS
I Maintain a stack S to store nodes to be explored.
I Maintain an array Explored and set Explored[v ] = true when the algorithmpops v from the stack.
I Read textbook on how to construct the DFS tree.
1
2 3
4 5 6
7
8
2
2
8
5
7
7
September 16, 2014 CS4104: Graphs
![Page 146: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/146.jpg)
Implementing DFS
I Maintain a stack S to store nodes to be explored.
I Maintain an array Explored and set Explored[v ] = true when the algorithmpops v from the stack.
I Read textbook on how to construct the DFS tree.
1
2 3
4 5 6
7
8
2
2
7
5
7
September 16, 2014 CS4104: Graphs
![Page 147: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/147.jpg)
Implementing DFS
I Maintain a stack S to store nodes to be explored.
I Maintain an array Explored and set Explored[v ] = true when the algorithmpops v from the stack.
I Read textbook on how to construct the DFS tree.
1
2 3
4 5 6
7
8
2
2
7
5
September 16, 2014 CS4104: Graphs
![Page 148: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/148.jpg)
Implementing DFS
I Maintain a stack S to store nodes to be explored.
I Maintain an array Explored and set Explored[v ] = true when the algorithmpops v from the stack.
I Read textbook on how to construct the DFS tree.
1
2 3
4 5 6
7
8
2
2
5
2
4
6
September 16, 2014 CS4104: Graphs
![Page 149: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/149.jpg)
Implementing DFS
I Maintain a stack S to store nodes to be explored.
I Maintain an array Explored and set Explored[v ] = true when the algorithmpops v from the stack.
I Read textbook on how to construct the DFS tree.
1
2 3
4 5 6
7
8
2
2
6
2
4
September 16, 2014 CS4104: Graphs
![Page 150: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/150.jpg)
Implementing DFS
I Maintain a stack S to store nodes to be explored.
I Maintain an array Explored and set Explored[v ] = true when the algorithmpops v from the stack.
I Read textbook on how to construct the DFS tree.
1
2 3
4 5 6
7
8
2
2
4
2
September 16, 2014 CS4104: Graphs
![Page 151: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/151.jpg)
Implementing DFS
I Maintain a stack S to store nodes to be explored.
I Maintain an array Explored and set Explored[v ] = true when the algorithmpops v from the stack.
I Read textbook on how to construct the DFS tree.
1
2 3
4 5 6
7
8
2
2
2
September 16, 2014 CS4104: Graphs
![Page 152: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/152.jpg)
Comparing Recursion and Iteration
September 16, 2014 CS4104: Graphs
![Page 153: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/153.jpg)
Analysing DFS
I How many times is a node's adjacency list scanned?
Exactly once.
I The total amount of time to process edges incident on node u's is O(nu).
I The total running time of the algorithm is O(n +m).
September 16, 2014 CS4104: Graphs
![Page 154: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/154.jpg)
Analysing DFS
I How many times is a node's adjacency list scanned? Exactly once.
I The total amount of time to process edges incident on node u's is O(nu).
I The total running time of the algorithm is O(n +m).
September 16, 2014 CS4104: Graphs
![Page 155: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/155.jpg)
Analysing DFS
I How many times is a node's adjacency list scanned? Exactly once.
I The total amount of time to process edges incident on node u's is
O(nu).
I The total running time of the algorithm is O(n +m).
September 16, 2014 CS4104: Graphs
![Page 156: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/156.jpg)
Analysing DFS
I How many times is a node's adjacency list scanned? Exactly once.
I The total amount of time to process edges incident on node u's is O(nu).
I The total running time of the algorithm is
O(n +m).
September 16, 2014 CS4104: Graphs
![Page 157: Graphs - Virginia Techcourses.cs.vt.edu/~cs4104/sharath/fall2014/lectures...VLSI circuits, cellular networks, ... I Other examples: gene and protein networks, our bodies (nervous and](https://reader036.vdocuments.site/reader036/viewer/2022071215/6045311e79fe094d2a2b42e5/html5/thumbnails/157.jpg)
Analysing DFS
I How many times is a node's adjacency list scanned? Exactly once.
I The total amount of time to process edges incident on node u's is O(nu).
I The total running time of the algorithm is O(n +m).
September 16, 2014 CS4104: Graphs