07 CS316 - Algorithms - Graph 1 - Search
07 CS316 - Algorithms - Graph 1 - Search
01/20/2024
NON-LINEAR DATA STRUCTURES
Tree Graph
LINEAR DATA STRUCTURES
Stack Queue
GRAPH – DEFINITIONS
4
APPLICATIONS
Applications that involve not only a set of items, but also the
connections between them
Hypertext Circuits
GRAPH – DEFINITIONS
Two vertices of a graph are adjacent if they are joined by an edge.
Ex:
With Edge (B,D) Ex:
. B is adjacent to D • 3 is adjacent to 1 (1,3)
. D is adjacent to B. • 1 is adjacent to 3 (3,1)
With Edge (B,C) • 1 is adjacent to 11(11,1)
. B is adjacent to C
. C is adjacent to B. 6
GRAPH – DEFINITIONS
A (directed) path between two vertices is a sequence of edges that begins at one vertex
and ends at another vertex.
7
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.
10
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
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
12
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 so 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.
REPRESENTATION OF GRAPHS
Two standard ways.
a b a b d c
• Adjacency Lists. a c
b
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 list. (degree(u))
time.
• (V) in the worst case.
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
0 otherwise
1 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 and 0
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).
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
r s t u
π:
0 s nil
r
t
u
v
v w x y w
x
Q: s y
d: 0
EXAMPLE (BFS)
π:
r s t u
s nil
1 0
r s
t
u
1 v
v w x y w s
x
Q: w r y
d: 1 1
EXAMPLE (BFS)
π:
r s t u
s nil
1 0 2
r s
t
w
1 2 u
v w x y v
w s
Q: r t x x
d: 1 2 2 w
y
EXAMPLE (BFS)
π:
r s t u
s nil
1 0 2
r s
t
w
2 1 2 u
v w x y v r
w s
Q: t x v x
d: 2 2 2 w
y
EXAMPLE (BFS)
π:
r s t u
s nil
1 0 2 3
r s
t
w
2 1 2 u t
v w x y v r
w s
Q: x v u x
d: 2 2 3 w
y
EXAMPLE (BFS)
π:
r s t u
s nil
1 0 2 3
r s
t
w
2 1 2 3 u t
v w x y v r
w s
Q: v u y x
d: 2 3 3 w
y
EXAMPLE (BFS)
π:
r s t u
s nil
1 0 2 3
r s
t
w
2 1 2 3 u t
v w x y v r
w s
Q: EMPTY x
w
y x
BREADTH-FIRST TREE
π:
r s t u
s nil
1 0 2 3
r s
t
w
2 1 2 3 u t
v w x y v r
w s
x
Breadth First Tree
w
y x
BFS(G,s)
BFS(G,s) ANALYSIS OF BFS
1.
1. for
foreach
eachvertex
vertexuuininV[G]
V[G]–– {s}
{s}
2.
2. do color[u]
docolor[u] white
white
3.
3. d[u]
d[u] O(V)
4.
4. [u]
[u]nilnil
5. color[s]
5. color[s] gray
gray
6. d[s]
6. d[s] 00
7. [s]
7. [s]nil
nil
8. QQ
8.
9.
9. enqueue(Q,s)
enqueue(Q,s) Each vertex is enqueued and
10. whileQQ
10. while dequeued at most once.
So, total time for queuing is O(V).
11. douu
11. do dequeue(Q)
dequeue(Q)
12.
12. for
foreach
eachvvin
inAdj[u]
Adj[u]
13.
13. do
doififcolor[v] white Adjacency
color[v]==white vertex
list of each
is scanned at most
14.
14. then color[v]
thencolor[v] gray
once.
grayThe sum of lengths
15.
15. d[v]
d[v] d[u]
d[u]of +
+11adjacency lists is
all
16.
16. [v]
[v]uu (E).
17.
17. enqueue(Q,v)
enqueue(Q,v)
18.
18. color[u]
color[u] black
black
ANALYSIS OF BFS
u v w π:
u
nil
v
u
w
x y z
nil
x y
y v
z
w
ANALYSIS OF DFS
DFS(G)
DFS(G) DFS-Visit(u)
DFS-Visit(u)
1.1. for
foreach vertexuuV[G]
eachvertex V[G] (V) 1.
1. color[u]
color[u] GRAY
GRAY White White
2.2. do color[u]
docolor[u] white
white vertex
vertexuuhashasbeenbeendiscovered
discovered
3.3. [u]
[u]NIL
NIL 2.
2. time
time time
time++11
time
4.4. time 00 3.
3. d[u]
d[u] time
time
5.5. for
foreach vertexuuV[G]
eachvertex V[G] (V) 4.
4. for
foreach
eachvvAdj[u] Adj[u]
6.6. do
doififcolor[u]
color[u]==white
white 5.
5. do
doififcolor[v]
color[v]==WHITE WHITE (E)
7.7. then
thenDFS-Visit(u)
DFS-Visit(u) 6.
6. then
then[v] [v]uu
7.
7. DFS-Visit(v)
DFS-Visit(v)
8.
8. color[u]
color[u] BLACK
BLACK
Blacken
Blackenu; u; ititisisfinished.
finished.
9.
9. f[u]
f[u]time
time timetime++11
ANALYSIS OF DFS
Loops on lines 1-2 & 5-7 take (V) time, excluding time to
execute DFS-Visit.