Basic Graph Algorithms CS 583 Analysis of Algorithms 7/1/2016

advertisement
Basic Graph Algorithms
CS 583
Analysis of Algorithms
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
1
Outline
• Graphs
– Definitions
– Representation
• Breadth-First Search
– Algorithm
– Shortest paths
– Correctness
• Self-Test
– 22.1-2, 22.2-1, 22.2-2
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
2
Graphs
• Graphs are a popular data structure in computer
science.
– Algorithms for working with them are fundamental in the
field.
– There are multiple computational problems (applications)
defined in terms of graphs.
• A graph G=(V,E) is defined as a set of vertices (V)
and edges (E).
– Each edge connects two vertices.
– The running time of graph algorithms is defined on the
number of vertices |V| and the number of edges |E| of the
graph.
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
3
Adjacency-List Representation
• The adjacency-list representation:
– Preferred as a compact way to represent sparse graphs.
• |E| << |V|2
– Uses array Adj of size |V|.
– For each u  V, the adjacency list Adj[u] contains all
vertices v such that there is an edge (u,v)  E.
– The vertices in each adjacency list are sorted in an
arbitrary order.
– In a directed graph the sum of the lengths of all adjacency
lists is |E|.
• Each edge is represented by a single element in a list.
– In an undirected graph the sum is 2 |E|.
• Each edge (u,v) is represented by 2 elements, <u,v> and <v,u>.
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
4
Adjacency-Matrix Representation
• The adjacency-matrix representation:
– May be preferred for dense graphs.
• |E| is close to |V|2
– Assume all vertices are numbered 1,2, ... , |V|
– The adjacency |V|  |V| matrix A = (aij) such that
• aij = 1 if (i,j)  E, 0 otherwise
– The adjacency matrix requires (V2) memory
independent of the number of edges.
– Define the transpose of a matrix A to be AT = (aijT), where
aijT = aji.
• In an undirected graph A = AT since both (u,v) and (v,u) represent
the same edge.
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
5
Breadth-First Search
• Given a graph G=(V,E) and a source vertex s, the
algorithm:
– Explores all edges of G to “discover” every vertex that is
reachable from s.
– Computes the distance (smallest number of edges) from s
to each reachable vertex.
– Produces a “breadth-first tree” with root s that contains all
reachable vertices.
• For any vertex v reachable from s the path in the tree corresponds
to a “shortest path” from s to v in G.
• The shortest path is a path containing the smallest number of
edges.
– The algorithm works on both directed and undirected
graphs.
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
6
Breadth-First Search: Algorithm
• Features:
– Discovers all vertices at distance k before discovering any
vertices at distance (k+1), -- “breadth-first”.
– In the process it colors each vertex white, gray, or black:
• All vertices start white.
• The vertex is black if all its adjacent vertices are discovered.
• The vertex is gray if it has any white adjacent vertices
– It constructs a breadth-first tree starting with s as a root.
• When a white vertex v is discovered when scanning an adjacency
list of a discovered vertex u, the edge (u,v) is added to the tree.
• Vertex u is a predecessor, or parent of the vertex v.
• Ancestors and descendants are defined relative to root:
– If u lies on a path from s to v, then u is an ancestor of v, and v is a
descendant of u.
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
7
Breadth-First Search: Pseudocode
The procedure below assumes that the graph is represented using adjacency
lists. It maintains additional fields for each node: color[], predecessor [], and
distance d[].
BFS(G,s)
// Set up the structure for all vertices <> s
1 for each vertex u  V[G] – {s}
2
color[u] = WHITE
3
d[u] = 
4
[u] = NIL
// Set up the structure for vertex s
5 color[s] = GRAY
6 d[s] = 0
7 [s] = NIL
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
8
Breadth-First Search: Pseudocode (cont.)
8
9
10
11
12
13
14
15
16
17
18
// Use a queue to manage gray vertices
Q = 
ENQUEUE(Q,s)
while Q  
u = DEQUEUE(Q)
for each v  Adj[u]
if color[v] = WHITE
color[v] = GRAY
d[v] = d[u] + 1
[v] = u
ENQUEUE(Q,v)
color[u] = BLACK
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
9
BFS: Invariant
• The while loop 10-18 iterates as long as there are
gray vertices.
– These are discovered vertices that have not yet had their
adjacency lists fully explored.
• The loop maintains the invariant:
– At the test in line 10, the queue Q consists of the set of
gray vertices.
– This invariant is not used to prove correctness, but it’s
easy to see that it holds.
• The breadth-first tree may vary depending on the
order the vertices are visited.
– The distances d[] computed by the algorithm will not
vary.
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
10
BFS: Performance
• Running time on an input graph G=(V,E):
– The test on line 13 ensures that each vertex is enqueued
and dequeued at most once in (1) time.
• The total time spent on all vertices is O(V).
– Each adjacency list is scanned at most once.
• Operation on each vertex takes (1).
• The total sum of the lengths of all adjacency lists is (E).
• The total time spent scanning all adjacency lists is O(E).
– The initialization at lines 1-8 is O(V).
– The total running time is:
• O(V) + O(V) + O(E) = O(V+E)
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
11
Shorted Paths
• Define the shortest-path distance (s,v) from s to v
as the minimum number of edges in any path from
vertex s to vertex v.
– If there’s no path from s to v, then (s,v) = 
• A path of length (s,v) from s to v is called a
shortest path from s to v.
• We will show that BFS computes shortest path
distances.
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
12
Shortest-Path Distance Property
Lemma 22.1
Let G=(V,E) be a directed or undirected graph, and let s  V be an arbitrary
vertex. Then, for any edge (u,v)  E,
(s,v) (s,u) + 1
Proof.
If u is reachable from s, then so is v. The shortest path to v cannot be longer
then the shortest path to u followed by the edge (u,v), that is inequality holds.
If u is not reachable from s, then (s,u)= , and the inequality holds.
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
13
BFS Distance Computing
Lemma 22.2
Let G=(V,E) be a directed or undirected graph, and BFS runs from a vertex s.
Then upon termination:
d[v]  (s,v)for each v  V
Proof.
We use induction on the number (n) of ENQUEUE operations.
For the basis of the induction (n=1), note that after s in enqueued in line 9, the
hypothesis holds as
d[s,s] = 0 = (s,s)
d[v] =   (s,v) for all v  V-{s}
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
14
BFS Distance Computing (cont.)
For the inductive step, consider a white vertex v that is about to be enqueued in
step 17 after being discovered in a search from the vertex u.
d[u]  (s,u) (induction assumption)
d[v] = d[u] + 1
 (s,u) + 1
 (s,v) (from Lemma 22.1)
Vertex v is then enqueued and is never enqueued again, hence its distance
never changes. Thus the inductive hypothesis is maintained. 
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
15
BFS Queue Maintenance
Lemma 22.3
Suppose that during the execution of BFS the queue Q contains the vertices
(v1,v2,...,vr) in that order. Then,
d[vr]  d[v1] + 1
d[vi]  d[vi+1], i=1,2,...,r-1
Proof.
We use induction on the number (n) of queue operations.
When n=1 and the queue contains only s, the induction hypothesis is trivial
and holds.
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
16
BFS Queue Maintenance (cont.)
For the inductive step we must prove that the lemma holds after both
dequeuing and enqueuing a vertex.
When a vertex is dequeued, the list becomes (v2,...,vr):
d[vi]  d[vi+1], i=2,...,r-1 (induction assumption)
d[vr]  d[v1] + 1
 d[v2] + 1 (as d[v1]  d[v2] by assumption)
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
17
BFS Queue Maintenance (cont.)
When a vertex vr+1 is enqueued in line 17, the list becomes (v1,...,vr,vr+1). Note
that, by that time the vertex u whose adjacency list is being scanned is removed
from the queue. Hence,
d[vr+1] = d[u] + 1
 d[v1] + 1 (as d[u]  d[v1] by assumption)
d[vr]  d[u] + 1 (by inductive assumption)
= d[vr+1]
The rest of inequalities are unaffected and thus the Lemma follows. 
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
18
BFS Correctness
Corollary 22.4
Suppose that vertices vi and vj are enqueued during the execution of BFS and
that vi is enqueued before vj. Then at the time vj is enqueued:
d[vi]  d[vj]
Proof.
Immediately follows from Lemma 22.3 and the flow of the algorithm’s
execution. (The vertex receives a value d at most once.) 
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
19
BFS Correctness (cont.)
Theorem 22.5
During the execution of BFS for each discovered vertex v, d[v] = (s,v).
Moreover, for any vs that is reachable from s, one of the shortest paths from s
to v is a shortest path from s to (v), followed by the edge ((v),v).
Proof.
By contradiction assume d[v]  (s,v) for some v. By Lemma 22.2
d[v]  (s,v) => d[v] > (s,v)
Note that v is reachable from s. (Otherwise, (s,v) =  and the above inequality
is not correct.).
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
20
BFS Correctness (cont.)
Let u be a vertex immediately preceding v on a shortest path from s to v.
(s,v) = (s,u) + 1 =>
d[v] > (s,v) = (s,u) + 1
= d[u] + 1 (d[u] = (s,u) by the way u and v
are chosen)
When u is dequeued in line 11, vertex v can be white, gray, or black.
When v is white, d[v] = d[u] + 1 in line 15 => contradiction.
When v is black, it’s already removed from the queue. By Corollary 22.4:
d[v] <= d[u] => contradiction.
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
21
BFS Correctness (cont.)
When v is gray, it was painted gray as it was discovered from an adjacency list
of some vertex w that was removed from a queue earlier than u.
d[v] = d[w] + 1
 d[u] + 1 (d[w] <= d[u] by Corollary 22.4)
=> contradiction.
Hence d[v] = (s,v) for all v. To conclude the proof, note that if (v) = u, then
d[v] = d[u] + 1. Thus, we can obtain a shortest path from s to v by taking a
shortest path from s to (v), and then from (v) to v. 
7/1/2016
CS583 Fall'06: Basic Graph Algorithms
22
Download