lecture 14: graph algorithms
DESCRIPTION
Lecture 14: Graph Algorithms. Shang-Hua Teng. A. B. C. D. E. F. G. H. I. J. K. L. M. N. O. P. Undirected Graphs. A graph G = (V, E) V: vertices E : edges, unordered pairs of vertices from V V (u,v) is same as (v,u) Thus |E|TRANSCRIPT
![Page 1: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/1.jpg)
Lecture 14:Graph Algorithms
Shang-Hua Teng
![Page 2: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/2.jpg)
Undirected Graphs• A graph G = (V, E)
– V: vertices– E : edges, unordered pairs of vertices from V V– (u,v) is same as (v,u)– Thus |E| <= |V| (|V|-1)/2
A
ONM
LKJ
E F G H
DCB
I
P
![Page 3: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/3.jpg)
Undirected Graphs• A graph G = (V, E)
– V: vertices– E : edges, ordered pairs of vertices from VV– (u,v) is different from (v,u)– Thus |E| <= |V| (|V|-1) A
E F G H
DCB
I
![Page 4: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/4.jpg)
Basic Graph Properties
• An undirected graph is connected if it has a path from every vertex to every other
• A directed graph is strongly connected if it has a path from every vertex to every other
• A weighted graph associates weights with either the edges or the vertices
• E.g., a road map: edges might be weighted w/ distance
• A multigraph allows multiple edges between the same vertices
![Page 5: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/5.jpg)
Dense and Sparse Graphs
• We will typically express running times in terms of |E| and |V| (often dropping the |’s)– If |E| |V|2 the graph is dense– If |E| |V| the graph is sparse
• Different data structures may be used to express sparse and dense graph
![Page 6: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/6.jpg)
Graph in Applications
• Internet: web graphs– Each page is a vertex– Each edge represent a hyperlink– Directed
• GPS: highway maps– Each city is a vertex– Each freeway segment is an undirected edge– Undirected
• Graphs are ubiquitous in computer science
![Page 7: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/7.jpg)
Representing Graphs
• Assume V = {1, 2, …, n}• An adjacency matrix represents the graph
as a n x n matrix A:– A[i, j] = 1 if edge (i, j) E (or weight of
edge)= 0 if edge (i, j) E
![Page 8: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/8.jpg)
Adjacency Matrix
• Example:1
2 4
3
a
d
b c
A 1 2 3 4
1 0 1 1 0
2 0 0 1 0
3 0 0 0 0
4 0 0 1 0
![Page 9: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/9.jpg)
Adjacency Matrix Representation• Undirected graph matrix A is symmetric• Storage requirements: O(V2)
– A dense representation• The adjacency matrix is a dense representation
– Usually too much storage for large graphs– But can be very efficient for small graphs
• Most large interesting graphs are sparse– For example, trees and planar graphs (such as highway
map) have |E| = O(|V|)– For this reason, we often needs more sparse representation
![Page 10: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/10.jpg)
Adjacency List Representation
• Adjacency list: for each vertex v V, store a list of vertices adjacent to v
• Example:– Adj[1] = {2}{3}– Adj[2] = {3}– Adj[3] = {}– Adj[4] = {3}
• Variation: can also keep a list of edges coming into vertex
1
2 4
3
![Page 11: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/11.jpg)
Adjacency List Representation• The degree of a vertex v in an undirected graph is equal to
the number of incident edges• For directed graphs, we can define in-degree, out-degree for
each vertex• For directed graphs, the total size of the adjacency lists is
out-degree(v) = |E|takes (V + E) storage
• For undirected graphs, the total size of the adjacent lists is degree(v) = 2 |E|
also (V + E) storage• So: Adjacency lists take O(V+E) storage
![Page 12: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/12.jpg)
Google Problem:Graph Searching
• How to search and explore in the Web Space?
• How to find all web-pages and all the hyperlinks?– Start from one vertex “www.google.com”– Systematically follow hyperlinks from the
discovered vertex/web page– Build a search tree along the exploration
![Page 13: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/13.jpg)
General Graph Searching
• Input: a graph G = (V, E), directed or undirected• Objective: methodically explore every vertex and
every edge• Build a tree on the graph
– Pick a vertex as the root– Choose certain edges to produce a tree– Note: might also build a forest if graph is not
connected
![Page 14: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/14.jpg)
Breadth-first Search• Objective: Traverse all vertices of a graph G that
can be reached from a starting vertex, called root• Method:
– Search for all vertices that are directly reachable from the root (called level 1 vertices)
– After mark all these vertices, visit all vertices that are directly reachable from any level 1 vertices (called level 2 vertices), and so on.
– In general level k vertices are directly reachable from a level k – 1 vertices
![Page 15: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/15.jpg)
An Example
![Page 16: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/16.jpg)
A
ONM
LKJ
E F G H
DCB
I
P
0
![Page 17: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/17.jpg)
A
ONM
LKJ
E F G H
DCB
I
P
0
1 1
1
![Page 18: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/18.jpg)
A
ONM
LKJ
E F G H
DCB
I
P
0
1 1
1
2
2
![Page 19: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/19.jpg)
A
ONM
LKJ
E F G H
DCB
I
P
0
1 1
1
2
2
3 3
3
3
3
![Page 20: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/20.jpg)
A
ONM
LKJ
E F G H
DCB
I
P
0
1 1
1
2
2
3 3
3
3
3
4 4
4
![Page 21: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/21.jpg)
A
ONM
LKJ
E F G H
DCB
I
P
0
1 1
1
2
2
3 3
3
3
3
4 4
4
55
![Page 22: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/22.jpg)
A
ONM
LKJ
E F G H
DCB
I
P
0
1 1
1
2
2
3 3
3
3
3
4 4
4
55
![Page 23: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/23.jpg)
A
ONM
LKJ
E F G H
DCB
I
P
0
1 1
1
2
2
3 3
3
3
3
4 4
4
55
![Page 24: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/24.jpg)
Breadth-First SearchBFS(G, s)1. For each vertex u in V – {s}, 2. color[u] = white; d[u] = infty; [u] = NIL3. color[s] = GRAY; d[s] = 0; [s] = NIL; Q = {}4. ENQUEUE(Q,s) // Q is a FIFO queue5. while (Q not empty)6. u = DEQUEUE(Q)7. for each v Adj[u]8. if color[v] = WHITE9. then color[v] = GREY 10. d[v] = d[u] + 1; [v] = u11. ENQUEUE(Q, v);12. color[u] = BLACK;
![Page 25: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/25.jpg)
Breadth-first Search Algorithm1. Algorithm BFS(s):
1. initialize container L to contain start vertex s.2. i 03. while Li is not empty do
1. create container Li+1 to initially be empty2. for each vertex v in Li do
1. for each edge e incident on v do1. if edge e is unexplored then2. let w be the other endpoint of e3. if vertex w is unexplored then4. label e as a discovery edge5. insert w into Li+1 6. else7. label e as a cross edge
4. i i + 1
![Page 26: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/26.jpg)
Breadth-first Search
• Visited all vertices reachable from the root • A spanning tree• For any vertex at level i, the spanning tree path
from s to i has i edges, and any other path from s to i has at least i edges (shortest path property)
• Edges not in the spanning tree are at most, 1 level apart (level by level property)
![Page 27: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/27.jpg)
Breadth-First Search: the Color Scheme
• White vertices have not been discovered– All vertices start out white
• Grey vertices are discovered but not fully explored– They may be adjacent to white vertices
• Black vertices are discovered and fully explored– They are adjacent only to black and gray vertices
• Explore vertices by scanning adjacency list of grey vertices
![Page 28: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/28.jpg)
Example
r s t u
v w x y
![Page 29: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/29.jpg)
Example
0
r s t u
v w x y
sQ:
![Page 30: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/30.jpg)
Example
1
0
1
r s t u
v w x y
wQ: r
![Page 31: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/31.jpg)
Example
1
0
1
2
2
r s t u
v w x y
rQ: t x
![Page 32: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/32.jpg)
Example
1
2
0
1
2
2
r s t u
v w x y
Q: t x v
![Page 33: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/33.jpg)
Example
1
2
0
1
2
2
3
r s t u
v w x y
Q: x v u
![Page 34: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/34.jpg)
Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: v u y
![Page 35: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/35.jpg)
Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: u y
![Page 36: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/36.jpg)
Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: y
![Page 37: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/37.jpg)
Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: Ø
![Page 38: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/38.jpg)
Properties of BFS
• BFS calculates the shortest-path distance to the source node– Shortest-path distance (s,v) = minimum number of
edges from s to v, or if v not reachable from s– Prove using blackboard
• BFS builds breadth-first tree, in which paths to root represent shortest paths in G– Thus can use BFS to calculate shortest path from one
vertex to another in O(V+E) time
![Page 39: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/39.jpg)
39
Properties of BFS• Proposition: Let G be an undirected graph on which a BFS
traversal starting at vertex s has been performed. Then– The traversal visits all vertices in the connected
component of s .– The discovery-edges form a spanning tree T, which we
call the BFS tree, of the connected component of s – For each vertex v at level i, the path of the BFS tree T
between s and v has i edges, and any other path of G between s and v has at least i edges.
– If (u, v) is an edge that is not in the BFS tree, then the level numbers of u and v differ by at most one.
![Page 40: Lecture 14: Graph Algorithms](https://reader033.vdocuments.us/reader033/viewer/2022051623/56815d48550346895dcb4ecf/html5/thumbnails/40.jpg)
40
Properties of BFS• Proposition: Let G be a graph with n vertices and m edges.
A BFS traversal of G takes time O(n + m). Also, there exist O(n + m) time algorithms based on BFS for the following problems:– Testing whether G is connected.– Computing a spanning tree of G– Computing the connected components of G– Computing, for every vertex v of G, the minimum
number of edges of any path between s and v.