Elementary Graph Algorithm - PowerPoint PPT Presentation

1 / 58
About This Presentation
Title:

Elementary Graph Algorithm

Description:

A graph-searching algorithm can discover much about the structure of a graph ... As a collection of adjacency list: preferred when graph is sparse ... – PowerPoint PPT presentation

Number of Views:467
Avg rating:3.0/5.0
Slides: 59
Provided by: cosmosSo
Category:

less

Transcript and Presenter's Notes

Title: Elementary Graph Algorithm


1
Elementary Graph Algorithm
  • 10144017 ???
  • 10144019 ???
  • 10144044 ???

2
Contents
  • Representations of graphs
  • Breadth-first search
  • Depth-first search
  • Topological sort
  • Strongly connected components

3
Elementary Graph Algorithms
  • Searching a graph means systematically following
    the edges of the graph so as to visit the
    vertices of the graph
  • A graph-searching algorithm can discover much
    about the structure of a graph
  • Other graph algorithms are organized as simple
    elaborations of basic graph-searching algorithms

4
Representations of graphs
  • Two standard ways to represent a graph G (V, E)
  • As a collection of adjacency list preferred when
    graph is sparse
  • As a collection of adjacency matrix preferred
    when graph is dense
  • Either way is applicable to both directed and
    undirected graph

5
Adjacency-list representation
6
Adjacency-list representation
  • Adjacency-list representation of a graph G (V,
    E) consists of an array Adj of V lists, one for
    each vertex in V.
  • Sum of the lengths of all the adjacency lists
  • Directed graph E
  • Undirected graph 2E
  • Memory requirement
  • both directed and undirected T(V E)
  • Adjacency lists can readily be adopted to
    represent weighted graph simply stores weight
    with vertex

7
Adjacency-matrix representation
8
Adjacency-matrix representation
  • Adjacency-matrix representation of a graph G
    consists of a V x V matrix such A aij that
  • Aij 1 if(I, j) ? E, 0 otherwise
  • Transpose of a matrix A (aij) to be AT aTij
    (aji)
  • Since in an undirected graph A AT, memory
    needed to store the graph almost in half
  • Simplicity of an adjacency matrix may make it
    preferable when when graph are resonably small
  • When a graph is undirected, adjacncy matrix uses
    only one bit per entry

9
Breadth-first search
  • Simplest algorithms for searching a graph
  • Breadth-first search systematically explores the
    edges of graph to discover every vertex that
    reachable from source vertex
  • Discovers all vertices at distance k from s
    before discovering any vertices at distance k1
  • u is the predecessor or parent of v Vertex v
    discovered in the course of scanning
    adjacency-list of already discovered vertex u

10
  • BFS(G, s)
  • 1 for each vertex u ? VG s
  • 2 do coloru lt- WHITE
  • 3 du lt- 8
  • 4 pu lt- NIL
  • 5 Colors lt- GRAY
  • 6 ds lt- 0
  • 7 ps lt- NIL
  • 8 Q lt- Æ
  • 9 ENQUEUE(Q, s)
  • 10 while Q ¹ Æ
  • 11 do u lt- DEQUEUE(Q)
  • 12 for each v ÃŽ Adju
  • 13 do if colorv WHITE
  • 14 then colorv lt- GRAY
  • 15 dv lt- du 1
  • 16 pv lt- u
  • 17 ENQUEUE(Q, v)
  • 18 Coloru lt- BLACK

11
Figure 22.3
12
Figure 22.3
13
Figure 22.3
14
Figure 22.3
15
Figure 22.3
16
BFS Analysis
  • Operations of enqueueing and dequeueing take O(1)
    time, so the total time devoted to queue
    operation is O(V)
  • Total time spent in scanning adjacency lists is
    O(E)
  • Total running time of breadth-first search is O(V
    E)

17
Shortest Paths
  • Shortest path distance d(s, v) from s to v as
    the minimum number of edges in any path from
    vertex s to vertex v
  • If there is no path from s to v, then d(s, v)
    8
  • Shortest path a path length d(s, v) from s to v
    is said to be shortest path
  • Breadth first search actually computes shortest
    path distances

18
Lemma 22.1
  • Let G (V, E) be a directed or undirected graph,
    and let s?V be an arbitary vertex
  • Then, for any edge (u,v) ? E, d(s,v) ?
    d(s,u)1
  • Proof If u is reachable from s, then so is v. In
    this case, the shortest path from s to v cannot
    be longer than the shortest path from s to u
    followed by the edge(u,v), and thus the
    inequality holds. If u is not reachable from s,
    then d(s,u) ?, and the inequality holds

19
Lemma 22.2
  • Let G(V,E) be a directed or undirected graph,
    and suppose that BFS is run on G from a given
    source vertex s ? V
  • Then upon termination, for each vertex v ? V, the
    value dv computed by BFS satisfies dv ?
    d(s,v)

20
Lemma 22.3
  • Suppose that during the execution of BFS on a
    graph G(V,E), the queue Q contains the vertices
    ltv1, v2, , vrgt, where v1 is the head of Q and
    vr is the tail.
  • Then, dvr ? dv11 and dvi ? dvi12 for i
    1, 2, . , r-1

21
Corollary 22.4
  • Suppose that vertices vi and vj are enqueued
    during the execution of BFS, and that vi is
    enqueued before vj
  • Then dvi ? dvj at the time that vj is
    enqueued
  • Proof immediate from Lemma 22.3 and the property
    that each vertex receives a finite d value at
    most once during the course of BFS

22
Theorem 22.5(Correctness of breadth-first search)
  • Let G(V,E) be a directed or undirected graph,
    and suppose that BFS is run on G from a given
    source vertex s ? V
  • Then, during its execution, BFS discovers every
    vertex v ? V that is reachable from the source,
    and upon termination, dv d(s,v) for all v ? V

23
Breadth-first trees
  • Predecessor subgraph of G as Gp(Vp,Ep), where
  • Vp v ? V pv? NIL ? s
  • Ep (pv,v) v ? Vp- s
  • The predecessor subgraph Gp is a beadth-first
    tree if Vp consists of the vertices reachable
    from s and, for all v ? Vp, there is a unique
    simple path from s to v in Gp that is also a
    shortest path from s to v in G

24
Lemma 22.6
  • When applied to a directed or undirected graph G
    (V, E), procedure BFS constructs p so that the
    predecessor subgraph Gp (Vp,Ep) is a
    breadth-first tree

25
Depth-first search
  • In depth-first search, edges are explored out of
    the most recently discovered vertex v that still
    has unexplored edges leaving it
  • Predecessor subgraph of a depth-first search
    forms a depth-first forest composed of several
    depth-first tree
  • Gp(V, Ep) where Ep (pv,v) v?V and
    pv?NIL

26
Depth-first search(cont)
  • Besides creating a depth-first forest,
    depth-first search also timestamps each vertex
  • Each vertex has two timestaps
  • dv timestamp records when v is first
    discovered
  • fv timestamp records when the search finishes
    examining vs adjacency list
  • Timestamps are integers between 1 and 2V
  • For every vertex v, dv lt fv
  • The running time of DFS_VISIT is Q(E)
  • the running time of DFS Q(VE)

27
DFS(G) for each vertex u ? VG do coloru lt-
WHITE pu lt- NIL time lt- 0 for each vertex u ?
VG do if coloru WHITE then
DFS-VISIT(u) DFS-VISIT(u) coloru lt-
GRAY ?White vertex u has just been
discovered time lt- time 1 du lt- time for each
v ? Adju ? Explore edge (u, v) do if colorv
WHITE then pv lt- u DFS-VISIT(v) coloru
lt- BLACK ? Blacken u it is finished fu lt-
time lt- time 1
28
Figure 22.4
29
Figure 22.4
30
Figure 22.4
31
Figure 22.4
32
Figure 22.4
33
Figure 22.4
34
Figure 22.4
35
Figure 22.4
36
Properties of depth-first search
  • The predecessor subgraph Gp does indeed from a
    forest of trees, since the structure of the depth
    first-tree exactly mirrors the structure of
    recursive calls of DFS-VISIT
  • Discovery and finishing times have parenthesis
    structure (figrure 22.5)

37
Threorem 22.7 (Parenthesis theorem)
  • In any depth-first search of a (directed of
    undirected) graph G (V, E), for any two
    vertices u and v, exactly one of the following
    three conditions hold
  • The intervals du,fu and dv,fv are
    entirely disjoint, and neither u nor v is a
    descendant of the other in the depth-first forest
  • ZThe intervals du,fu is contained entirely
    within the interval dv,fv, and u is a
    descendant of v in a depth-first tree,or
  • The intervals dv,fv is contained entirely
    within the interval du,fu, and u is a
    descendant of u in a depth-first tree

38
Figure 22.5
s
t
z
y
1/10
11/16
3/6
2/9
F
B
B
C
4/5
7/8
14/15
12/13
C
C
C
w
v
x
u
(a)
39
Figure 22.5
s
t
z
v
u
y
w
x
1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16 (s (z (y (x x) y) (w w) z)
s) (t (v v) (u u) t)
(b)
40
Figure 22.5
s
t
B
C
C
F
z
v
u
B
C
y
w
C
x
(c)
41
Corollary 22.8 (Nesting of descendants
intervals)
  • Vertex is a proper descendant of vertex u in the
    depth-first forest for a (directed or undirected)
    graph G if and only if du lt dv lt fv lt fu
  • Immediate from Theorem 22.7

42
Threorem 22.9 (White-path theorem)
  • In a depth-first forest of a (directed or
    undirected) graph G (V, E), vertex v is a
    descendant of vertex u if any only if at the time
    du that the search discovers u, vertex v can be
    reached from u along a path consisting entirely
    of white vertices

43
Classification of edges
  • Tree edges edges in the depth-first forest Gp.
    Edge(u,v) is a tree edge if v was first
    discovered by exploring edge(u, v)
  • Back edges edges (u, v) connecting a vertex u to
    an ancestor v in a depth-first tree(include
    self-loop)
  • Forward edges nontree edges (u, v) connecting a
    vertex u to a descendant v in a depth-first tree
  • Cross edges all other edges

44
Theorem 22.10
  • In a depth-first search of an undirected graph G,
    every edge of G is either a tree edge or a back
    edge.

45
Topological sort
  • Topological sort of a dag G (V, E)
  • Linear ordering of all its vertices such that if
    G contains an edge(u, v), then u appears before v
    in the ordering
  • Can be viewed as an ordering of its vertices
    along a horizontal line so thatall directed edges
    go from left to right
  • Different usual kind of sorting(ex. Quicksort)
  • TOPOLOGICAL-SORT(G)
  • call DFS(G) to compute finishing times fv for
    each vectex v
  • as each vertex is finished, insert it onto the
    front of a linked list
  • return the linked list of vertices

46
Figure 22.7
undershorts
socks
11/16
17/18
watch
9/10
pants
shoes
12/15
13/14
shirt
1/8
belt
6/7
tie
2/5
jacket
3/4
(a)
47
Figure 22.7
undershorts
pants
belt
shirt
tie
jacket
shoes
socks
watch
11/16
12/15
6/7
1/8
2/5
3/4
17/18
13/14
9/10
(b)
48
Lemma 22.11
  • A directed graph G is acyclic if and only if a
    depth-first search of G yields no back edges.

49
Figure 22.8
n
o
p
m
s
r
q
t
u
v
w
x
y
z
50
Theorem 22.12
  • TOPOLOGICAL-SORT(G) produces a topological sort
    of a directed acyclic graph G.

51
Strongly connected components
  • Many algorithm that work with directed graphs
    begin with decomposing a directed graph into its
    strongly connected components
  • STRONGLY-CONNECTED-COMPONENTS(G)
  • call DFS(G) to compute finishing times fu for
    each vertex u
  • compute Gt
  • call DFS(Gt), but in the main loop of DFS,
    consider the vertices in order of decreasing
    fu(as computed in line 1)
  • Output the vertices of each tree in the
    depth-first forest formed in line 3 as a separate
    strongly connected component

52
Figure 22.9
c
d
b
a
1/10
8/9
13/14
11/16
12/15
3/4
5/6
2/7
f
g
e
h
(a)
53
Figure 22.9
c
d
b
a
f
g
e
h
(b)
54
Figure 22.9
cd
abe
h
fg
(c)
55
Lemma 22.13
  • Let C and C be distinct strongly connected
    components in directed graph G (V,E), let
    u,v?C, let u, v ?C, and suppose that there is
    a path ugtu in G. Then there cannot also be a
    path v gtv in G.

56
Lemma 22.14
  • Let C and C be distinct strongly connected
    components in directed graph G(V,E). Suppose
    that there is an edge (u,v)?ET. where u?C
    andv?C. Then f(C)ltf(C).
  • Proof 2 cases depending on which strongly
    connected component, C or C, had the first
    discovered vertex during the DFS.

57
Corollary 22.15
  • Let C and C be distinct strongly connected
    components in directed graph G(V,E). Suppose
    that there is an edge (u,v)?ET. where u?C
    andv?C. Then f(C)ltf(C).
  • Proof Since(u,v)?ET, we have (v,u)?E. Since the
    strongly connected components of G and GT are the
    same, Lemma 22.14 implies that f(C)ltf(C).

58
Theorem 22.16
  • STRONGLY-CONNECTED-COMPONENTS(G) correctly
    computes the strongly connected components of a
    directed graph G.
Write a Comment
User Comments (0)
About PowerShow.com