Title: Basic Graph Algorithms
1Basic Graph Algorithms
- CS 583
- Analysis of Algorithms
2Outline
- Graphs
- Definitions
- Representation
- Breadth-First Search
- Algorithm
- Shortest paths
- Correctness
- Self-Test
- 22.1-2, 22.2-1, 22.2-2
3Graphs
- 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.
4Adjacency-List Representation
- The adjacency-list representation
- Preferred as a compact way to represent sparse
graphs. - E ltlt V2
- Uses array Adj of size V.
- For each u ? V, the adjacency list Adju
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,
ltu,vgt and ltv,ugt.
5Adjacency-Matrix Representation
- The adjacency-matrix representation
- May be preferred for dense graphs.
- E is close to V2
- 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.
6Breadth-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.
7Breadth-First Search Algorithm
- Features
- Discovers all vertices at distance k before
discovering any vertices at distance (k1), --
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.
8Breadth-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 ltgt s 1
for each vertex u ? VG s 2 coloru
WHITE 3 du ? 4 ?u NIL // Set up
the structure for vertex s 5 colors GRAY 6
ds 0 7 ?s NIL
9Breadth-First Search Pseudocode (cont.)
// Use a queue to manage gray vertices 8 Q
? 9 ENQUEUE(Q,s) 10 while Q ? ? 11 u
DEQUEUE(Q) 12 for each v ? Adju 13 if
colorv WHITE 14 colorv GRAY 15
dv du 1 16 ?v u 17
ENQUEUE(Q,v) 18 coloru BLACK
10BFS 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 its 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.
11BFS 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(VE)
12Shorted 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 theres 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.
13Shortest-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.
14BFS 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 dv ? ?(s,v)for each v ?
V Proof. We use induction on the number (n) of
ENQUEUE operations. For the basis of the
induction (n1), note that after s in enqueued in
line 9, the hypothesis holds as ds,s 0
?(s,s) dv ? ? ?(s,v) for all v ? V-s
15BFS 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.
du ? ?(s,u) (induction assumption) dv
du 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. ?
16BFS 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, dvr ?
dv1 1 dvi ? dvi1, i1,2,...,r-1
Proof. We use induction on the number (n) of
queue operations. When n1 and the queue
contains only s, the induction hypothesis is
trivial and holds.
17BFS 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) dvi ? dvi1,
i2,...,r-1 (induction assumption) dvr ? dv1
1 ? dv2 1 (as dv1 ? dv2 by
assumption)
18BFS Queue Maintenance (cont.)
When a vertex vr1 is enqueued in line 17, the
list becomes (v1,...,vr,vr1). Note that, by that
time the vertex u whose adjacency list is being
scanned is removed from the queue.
Hence, dvr1 du 1 ? dv1 1 (as
du ? dv1 by assumption) dvr ? du 1 (by
inductive assumption) dvr1 The rest
of inequalities are unaffected and thus the Lemma
follows. ?
19BFS 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 dvi ? dvj Proof. Immediately
follows from Lemma 22.3 and the flow of the
algorithms execution. (The vertex receives a
value d at most once.) ?
20BFS Correctness (cont.)
Theorem 22.5 During the execution of BFS for each
discovered vertex v, dv ?(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 dv ?
?(s,v) for some v. By Lemma 22.2 dv ? ?(s,v)
gt dv gt ?(s,v) Note that v is reachable from
s. (Otherwise, ?(s,v) ? and the above
inequality is not correct.).
21BFS Correctness (cont.)
Let u be a vertex immediately preceding v on a
shortest path from s to v. ?(s,v) ?(s,u) 1
gt dv gt ?(s,v) ?(s,u) 1 du 1
(du ?(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, dv du 1 in line 15 gt
contradiction. When v is black, its already
removed from the queue. By Corollary 22.4 dv
lt du gt contradiction.
22BFS 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. dv dw 1 ? du 1 (dw lt du
by Corollary 22.4) gt contradiction. Hence
dv ?(s,v) for all v. To conclude the proof,
note that if ?(v) u, then dv du 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. ?