07 CS316 Graph 1 Search
07 CS316 Graph 1 Search
Stack Queue
NON-LINEAR DATA STRUCTURES
Tree Graph
GRAPH – DEFINITIONS
4
APPLICATIONS
Applications that involve not only a set of items, but
also the connections between them
Hypertext Circuits
DEFINITIONS
GRAPHS - DEFINITIONS
If the edge pair is ordered then the graph is called a directed
graph (also called digraphs) .
Undirected graph, which is not a directed graph, also called a
normal graph or just a graph.
Undirected Directed
graph graph
Directed edge
V = { A,B,C,D }
E = {(A,B), (B,A), (A,D), V = { 1, 3,5,7,9,11}
(D,A) (B,D), (D,B), (B,C), E = {(1,3) (3,1) (11,1) (9,11) (9,9) (5,9)
GRAPH – DEFINITIONS
Two vertices of a graph are adjacent if they are joined by an edge.
Undirected Directed
graph graph
- An edge is enough - if there is a direct edge from v
to w
w is successor of v
v is predecessor of w
Ex:
With Edge (B,D) Ex:
. B is adjacent to D • 3 is adjacent to 1
. D is adjacent to B. (1,3)
With Edge (B,C) • 1 is adjacent to 3
. B is adjacent to C (3,1)
. C is adjacent to B. • 8
GRAPH – DEFINITIONS
1 2
A graph G (undirected)
3 4 5
10
GRAPH – AN EXAMPLE
1 2
A graph G (undirected)
3 4 5
• Adjacent:
1 and 2 are adjacent -- 1 is adjacent to 2 and 2 is adjacent to 1
• Cycle:
2,5,4,1,2 (a simple cycle), 1,3,4,1,4,1 (a cycle, but not simple
cycle)
DIRECTED GRAPH – AN EXAMPLE
1 2
3 4 5
• Adjacent:
2 is adjacent to 1, but 1 is NOT adjacent to 2
• Path:
1,2,5 ( a directed path),
• Cycle:
1,4,3,1 (a directed cycle), 5,5 (a Self loop)
GRAPH -- DEFINITIONS
A connected graph has a path between each pair of distinct vertices
A complete graph has an edge between each pair of distinct vertices
• A complete graph is also a connected graph. But a connected
graph may not be a complete graph.
13
GRAPH – DEFINITIONS
In graph, degree is the number of edges incident on a
node
In digraph, Degree = in-degree + out-degree
•In-degree: Number of edges entering
•Out-degree: Number of edges leaving
outdeg(1)=2
indeg(1)=0
outdeg(2)=2
deg(1) = 2
indeg(2)=2
deg(2) = 3
deg(5) = 3
outdeg(3)=1
indeg(3)=4
WEIGHTED GRAPH
We can label the edges of a graph with numeric values, the
graph is called a weighted graph.
8
1 2 Weighted (Undirected) Graph
10 3 6
5 7
3 4 5
8
1 2 Weighted Directed Graph
10 3 6
5 7
3 4 5
15
TYPES OF GRAPHS
• Undirected: edge (u, v) = (v, u); for all v, (v, v) E (No self
loops.)
• Directed: (u, v) is edge from u to v, denoted as u v. Self
loops are allowed.
• Weighted: each edge has an associated weight, given by a
weight function w : E R.
• Dense: |E| |V|2.
• Sparse: |E| << |V|2.
Adjacency relationship is:
• Symmetric if G is undirected.
• Not necessarily symmetric if G is directed.
If G is connected:
• There is a path between every pair of vertices.
• |E| |V| – 1.
• Furthermore, if |E| = |V| – 1, then G is a tree.
GRAPH REPRESENTATIONS
HOW TO REPRESENT A GRAPH, IN MEMORY?
REPRESENTATION OF GRAPHS
Two standard ways.
• Adjacency Lists. a b a b d c
b a c
c d c d a b
d a c
• Adjacency Matrix.
1 2 1 2 3 4
a b
1 0 1 1 1
2 1 0 1 0
c d 3 1 1 0 1
3 4 4 1 0 1 0
ADJACENCY LISTS
Consists of an array Adj of |V| lists. One list per
vertex.
For u V, Adj[u] consists of all vertices adjacent to u.
Adj
a b a b d c
b c
If weighted, store weights also in
c d c d adjacency lists.
d
a b a b d c
b a c
c d c d a b
d a c
STORAGE REQUIREMENT
For directed graphs:
• Sum of lengths of all adj. lists is
out-degree(v) = |E|
vV
No. of edges leaving v
• Total storage: (V+E)
For undirected graphs:
• Sum of lengths of all adj. lists is
degree(v) = 2|E|
vV
No. of edges incident on v. Edge (u,v)
is incident on vertices u and v.
• Total storage: (V+E)
PROS AND CONS: ADJ LIST
Pros
• Space-efficient, when a graph is sparse.
• Can be modified to support many graph
variants.
Cons
• Determining if an edge (u,v) G is not
efficient.
• Have to search in u’s adjacency
a b list.
d c
(degree(u)) time. a c
b
• (V) in the worst case. d a b
c
d a c
ADJACENCY MATRIX
|V| |V| matrix A.
Number vertices from 1 to |V| in some arbitrary
manner.
A is then given by: 1 if (i, j ) E
A[i, j ] aij
1
0 otherwise
2 1 2 3 4
a b
1 0 1 1 1
2 0 0 1 0
c d4 3 0 0 0 1
3 4 0 0 0 0
1 2 1 2 3 4
a b
1 0 1 1 1
A = AT for undirected graphs.
2 1 0 1 0
c d 3 1 1 0 1
3 4 4 1 0 1 0
For weighted graph:
Weights are stored instead of 1
SPACE AND TIME
Space: (V2).
• Not memory efficient for large graphs.
Time: to list all vertices adjacent to u: (V).
Time: to determine if (u, v) E: (1).
1 2 3 4
1 0 1 1 1
2 0 0 1 0
3 0 0 0 1
4 0 0 0 0
SEARCHING A GRAPH
GRAPH-SEARCHING ALGORITHMS
Searching a graph:
• Systematically follow the edges of a graph
to visit the vertices of the graph.
Used to discover the structure of a graph.
Standard graph-searching algorithms.
• Breadth-first Search (BFS).
• Depth-first Search (DFS).
GRAPH-SEARCHING ALGORITHMS
BREADTH-FIRST SEARCH
Input: Graph G = (V, E), either directed or undirected,
and source vertex s V.
Output:
• d [v] = distance (smallest # of edges, or shortest path)
from s to v, for all v V. d [v] = if v is not reachable from
s.
• [v] = u such that (u, v) is last edge on shortest path s
v.
• u is v’s predecessor.
• Builds breadth-first tree with root s that contains all
reachable vertices.
BREADTH-FIRST SEARCH
Expands the frontier between discovered and
undiscovered vertices uniformly across the breadth of
the frontier.
• A vertex is “discovered” the first time it is
encountered during the search.
• A vertex is “finished” if all vertices adjacent to it
have been discovered.
Colors the vertices to keep track of progress.
• White – Undiscovered.
• Gray – Discovered but not finished.
• Black – Finished.
• Colors are required only to reason about the
algorithm. Can be implemented without colors.
BFS(G,s)
BFS(G,s)
1.
1. for
for each
each vertex
vertex uu inin V[G]
V[G] –– {s}
{s}
2.
2. dodo color[u]
color[u] white
white
3.
3. d[u]
d[u] white:
4.
4. [u][u] nil
nil undiscovered
5.
5. color[s]
color[s] gray
gray gray: discovered
black: finished
6.
6. d[s]
d[s] 00 Q: a queue of
7.
7. [s] nil
[s] nil
discovered
vertices
8.
8. Q Q color[v]: color of
9.
9. enqueue(Q,s)
enqueue(Q,s) v
d[v]: distance
10.
10.while
while Q Q from s to v
11.
11. dodo uu
dequeue(Q)
dequeue(Q) [u]: predecessor
12.
12. for
for each
each vv inin Adj[u]
Adj[u]
of v
13.
13. do
do if
if color[v]
color[v] == white
white
14.
14. then
then color[v]
color[v]
gray
gray
15.
15. d[v]
d[v] d[u]
d[u] +
+
11
16.
16. [v] uu
[v]
17.
17. enqueue(Q,v)
enqueue(Q,v)
BFS(G,s)
BFS(G,s)
1.
1. for
foreach
eachvertex
vertexuuin
inV[G]
V[G]–– {s}
{s} π:
2.
2. do
docolor[u]
color[u]white
white s
3.
3. d[u]
d[u]
4. nil
4. [u]nil
[u] nil
5.
5. color[s]
color[s]gray
gray r
6.
6. d[s]
d[s]00 nil
7. [s]nil
[s]
7.
8. QQ
nil
t nil
8.
9.
9. enqueue(Q,s)
enqueue(Q,s) u nil
v
r s t u nil
Example:
0 w
Q: nil
s
d: x0
nil
y
v w x y
nil
BFS(G,s)
BFS(G,s)
1.
1. for
for each
each vertex
vertex uu inin V[G]
V[G] –– {s}
{s}
2.
2. dodo color[u]
color[u] white
white
3.
3. d[u]
d[u] white:
4.
4. [u][u] nil
nil undiscovered
5.
5. color[s]
color[s] gray
gray gray: discovered
black: finished
6.
6. d[s]
d[s] 00 Q: a queue of
7.
7. [s] nil
[s] nil
discovered
vertices
8.
8. Q Q color[v]: color of
9.
9. enqueue(Q,s)
enqueue(Q,s) v
d[v]: distance
10.
10.while
while Q Q from s to v
11.
11. dodo uu
dequeue(Q)
dequeue(Q) [u]: predecessor
12.
12. for
for each
each vv inin Adj[u]
Adj[u]
of v
13.
13. do
do if
if color[v]
color[v] == white
white
14.
14. then
then color[v]
color[v]
gray
gray
15.
15. d[v]
d[v] d[u]
d[u] +
+
11
16.
16. [v] uu
[v]
17.
17. enqueue(Q,v)
enqueue(Q,v)
BFS(G,s)
BFS(G,s)
10.
10. while
whileQQ π:
11.
11. do
douudequeue(Q)
dequeue(Q) s
12.
12. for
foreach
eachvvin
inAdj[u]
Adj[u]
13. do nil
13. doififcolor[v]
color[v]==white
white
14.
14. then
thencolor[v]
color[v]gray
gray r
15.
15. d[v]
d[v]d[u]
d[u]++11 nil
16. [v]uu
[v]
16.
17. enqueue(Q,v)
t nil
17. enqueue(Q,v)
18.
18. color[u]
color[u]black
black u nil
v
r s t u nil
Example:
0 w
Q: nil
s
d: x0
nil
y
v w x y
nil
BFS(G,s)
BFS(G,s)
10.
10. while
whileQQ π:
11.
11. do
douudequeue(Q)
dequeue(Q) s nil
12.
12. for
foreach
eachvvin
inAdj[u]
Adj[u]
13. do
r
13. doififcolor[v]
color[v]==white
white
14.
14. then
thencolor[v]
color[v]gray
gray s
15.
15. d[v]
d[v]d[u]
d[u]++11 t
16. [v]uu
[v]
16.
17. enqueue(Q,v)
u
17. enqueue(Q,v)
18.
18. color[u]
color[u]black
black
v
w
r s t u s
Example:
1 0 x
Q: yw r
d: 1 1
1
v w x y
EXAMPLE (BFS)
π:
r s t u
s
1 0 2
nil
r
s
1 2 t
v w x y w
u
Q: r t x v
d: 1 2 2 w
s
EXAMPLE (BFS)
π:
r s t u
s
1 0 2
nil
r
s
2 1 2 t
v w x y w
u
Q: t x v v r
d: 2 2 2 w
s
EXAMPLE (BFS)
π:
r s t u
s nil
1 0 2 3
r
s
t
2 1 2 w
v w x y u t
v r
Q: x v u w
d: 2 2 3 s
x
EXAMPLE (BFS)
π:
r s t u
s
1 0 2 3
nil
r
s
2 1 2 3 t
v w x y w
u t
Q: v u y v r
d: 2 3 3 w
s
EXAMPLE (BFS)
π:
r s t u
s
1 0 2 3
nil
r
s
2 1 2 3 t
v w x y w
u t
Q: EMPTY v r
w
s
BREADTH-FIRST TREE
π:
r s t u
s
1 0 2 3
nil
r
s
2 1 2 3 t
v w x y w
u t
v r
Breadth First Tree
w
s
BREADTH-FIRST TREE
For a graph G = (V, E) with source s, the
predecessor subgraph of G is G = (V , E) where
• V ={vV : [v] NIL}{s}
• E ={([v],v)E : v V - {s}}
The predecessor subgraph G is a breadth-first tree
if:
• V consists of the vertices reachable from s and
• for all vV , there is a unique simple path from s
to v in G that is also a shortest path from s to v in
G.
The edges in E are called tree edges.
|E | = |V | - 1.
BFS(G,s)
BFS(G,s) ANALYSIS OF BFS
1.
1. for
for each
each vertex
vertex uu inin V[G]
V[G] –– {s}
{s}
2.
2. dodo color[u]
color[u] white
white
3.
3. d[u]
d[u] O(V)
4.
4. [u] nil
[u] nil
5.
5. color[s]
color[s] gray
gray
6.
6. d[s]
d[s] 00
7.
7. [s]
[s] nil
nil
8.
8. Q Q
9.
9. enqueue(Q,s)
enqueue(Q,s) Each vertex is enqueued and
10.
10.while
while Q Q dequeued at most once.
11.
11. dodo uu dequeue(Q)
dequeue(Q) So, total time for queuing is
12.
12. for
for each
each vv in Adj[u] O(V).
in Adj[u]
13.
13. do
do if
if color[v]
color[v] == white
whiteAdjacency list of
14.
14. then
then color[v]
color[v]each
vertex is
gray
gray scanned at most
15. d[v] once.+ The sum of
d[u]
15. d[v] d[u] + of all
lengths
11
adjacency lists is
16.
16. [v] u
[v] u(E).
17.
17. enqueue(Q,v)
enqueue(Q,v)
ANALYSIS OF BFS
u v w π:
u
nil
v
u
w
x y z
nil
x
y
y
v
ANALYSIS OF DFS
DFS(G)
DFS(G) DFS-Visit(u)
DFS-Visit(u)
1.
1. for
foreach vertexuu (V) 1.
eachvertex 1. color[u]
color[u] GRAY
GRAY White
White
V[G] vertex
V[G] vertex uu has
has been
been
2.
2. do
docolor[u] white
color[u] white discovered
discovered
3.
3. [u]
[u]NIL
NIL 2.
2. time
time time
time + + 11
4.
4. time
time 00 (V)
3.
3. d[u] time
d[u] time
5.
5. for
foreach vertexuu
eachvertex 4.
4. for
for each
each vv Adj[u]
Adj[u] (E)
V[G]
V[G] 5.
5. do
do ifif color[v]
color[v] = =
6.
6. do
doififcolor[u]
color[u]=
= WHITE
white
WHITE
white 6. then
6. then [v] uu
[v]
7.
7. then
thenDFS-
DFS-
Visit(u) 7.
7. DFS-
DFS-
Visit(u)
Visit(v)
Visit(v)
8.
8. color[u]
color[u] BLACK
BLACK
Blacken
Blacken u; u; itit is
is finished.
finished.
ANALYSIS OF DFS