Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

07 CS316 - Algorithms - Graph 1 - Search

CS 316 covers algorithms for graph representation and search. Specifically, it discusses linear and non-linear data structures, including trees and graphs. It defines graphs mathematically and covers applications of graphs. The document also defines key graph terms such as vertices, edges, paths, cycles, and graph types. Finally, it introduces two common graph search algorithms - breadth-first search and depth-first search.

Uploaded by

Essam
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

07 CS316 - Algorithms - Graph 1 - Search

CS 316 covers algorithms for graph representation and search. Specifically, it discusses linear and non-linear data structures, including trees and graphs. It defines graphs mathematically and covers applications of graphs. The document also defines key graph terms such as vertices, edges, paths, cycles, and graph types. Finally, it introduces two common graph search algorithms - breadth-first search and depth-first search.

Uploaded by

Essam
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 59

CS 316: ALGORITHMS

GRAPH REPRESENTATION AND SEARCH


Assoc. Prof. Ensaf Hussein
Department Of Computer Science

01/20/2024
NON-LINEAR DATA STRUCTURES

Tree Graph
LINEAR DATA STRUCTURES

Array Linked List

Stack Queue
GRAPH – DEFINITIONS

 Graph – mathematical object consisting of a set of:


 Denoted by G = (V, E)
 V = nodes (vertices, points).
 E = edges (links, arcs) between pairs of nodes, where
each edge is a pair (v,w) s,t. v,w  V

4
APPLICATIONS
Applications that involve not only a set of items, but also the
connections between them

Maps Schedules Computer networks

Hypertext Circuits
GRAPH – DEFINITIONS
Two vertices of a graph are adjacent if they are joined by an edge.

Vertex w is adjacent to v if edge (v,w)  E.

Undirected graph Directed 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  (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.

w1, w2, …, wN is a path if (wi, wi+1)  E for 1  i . N-1


A simple path passes through a vertex only once.
A cycle is a path that begins and ends at the same vertex.
• For undirected graph it is a path of length at least 1 such that w1 = wN, the edges
must be distinct No Self loops
• In a directed graph is a path of length at least 1 such that w1 =
wN.  Self loops
• A directed acyclic graph (DAG) is a type of directed graph having
no cycles.
A simple cycle is a cycle that does not pass through other vertices more than once.

7
DIRECTED GRAPH – AN EXAMPLE
1 2

3 4 5

The graph G= (V,E) has 5 vertices and 6 edges:


V = {1,2,3,4,5}
E = { (1,2),(1,4),(2,5),(4,5),(3,1),(4,3),(5,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.

connected disconnected complete

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

Note: degree(v) in graph = in-degree + out-degree in digraph = Number of adjacent


nodes to node v
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

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|
vV
No. of edges leaving v
• Total storage: (V+E)
For undirected graphs:
• Sum of lengths of all adj. lists is
degree(v) = 2|E|
vV 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

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 the last edge on the 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
Foreach
eachvertex
vertexuuininV[G]
V[G]–– {s}
{s}
2.
2. do color[u]
docolor[u] white
white
3.
3. d[u]
d[u] 
4. [u]
4. [u] nil
nil white: undiscovered
5. color[s]
5. color[s] gray
gray gray: discovered
black: finished
6. d[s]
6. d[s] 00
7. [s]
7. [s]nil
nil Q: a queue of
8. QQ
8.   discovered vertices
9.
9. enqueue(Q,s)
enqueue(Q,s) color[v]: color of v
d[v]: distance from s to
10. whileQQ
10. while  v
11. douu
11. do dequeue(Q)
dequeue(Q) [u]: predecessor of v
12.
12. for
foreach
eachvvin
inAdj[u]
Adj[u]
13.
13. do
doififcolor[v]
color[v]==white
white
14.
14. then color[v]
thencolor[v] gray
gray
15.
15. d[v]
d[v] d[u]
d[u]++11
16.
16. [v]
[v]uu
17.
17. enqueue(Q,v)
enqueue(Q,v)
18.
18. color[u]
color[u] black
black
EXAMPLE (BFS)

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

Total running time of BFS is O(V+E),


linear in the size of the adjacency list representation of graph.
DEPTH-FIRST SEARCH (DFS)
Explore edges out of the most recently discovered vertex v.
When all edges of v have been explored, backtrack to explore other
edges leaving the vertex from which v was discovered (its
predecessor).
“Search as deep as possible first.”
Continue until all vertices reachable from the original source are
discovered.
If any undiscovered vertices remain, then one of them is chosen as a
new source and search is repeated from that source.
DEPTH-FIRST SEARCH
Input: G = (V, E), directed or undirected. No source vertex given!
Output:
• 2 timestamps on each vertex. Integers between 1 and 2|V|.
• d [v] = discovery time (v turns from white to gray)
• f [v] = finishing time (v turns from gray to black)
• [v] : predecessor of v = u, such that v was discovered during the
scan of u’s adjacency list.
Uses the same coloring scheme for vertices as BFS.
PSEUDO-CODE
DFS(G)
DFS(G) DFS-Visit(u)
DFS-Visit(u)
1.1. for
foreach vertexuuV[G]
eachvertex V[G] 1.
1. color[u]
color[u] GRAY
GRAY //White
//White
2.2. do color[u]
docolor[u] white
white vertex
vertexuuhashasbeen
beendiscovered
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 vertexuuV[G]
eachvertex V[G] 4.
4. for
foreach
eachvvAdj[u]
Adj[u]
6.6. do
doififcolor[u]
color[u]==white
white 5.
5. do
doififcolor[v]
color[v]==WHITEWHITE
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
Uses a global timestamp time. //
//Blacken
Blackenu;u; ititisisfinished.
finished.
9.
9. f[u]
f[u]time
time timetime++11
EXAMPLE (DFS)
u v w π:
1/ u
nil
v
w
x z x
U y
y
z
EXAMPLE (DFS)
u v w
π:
1/ 2/ u
nil
v
v u
x y z w
u x
y
z
EXAMPLE (DFS)
u v w
π:
1/ 2/ u
nil
y v
v 3/ u
x y z w
u x
y
v
z
EXAMPLE (DFS)
u v w π:
1/ 2/ u
X nil
y v
v 4/ 3/
u
w
u x y z
x
y
y v
z
EXAMPLE (DFS)
u v w π:
1/ 2/ u
X B
nil
y v
v 4/ 3/
u
w
u x y z
x y
y v
z
EXAMPLE (DFS)
u v w π:
1/ 2/ u
nil
y B
v
v u
4/5 3/
u x y z w
x y
y v
z
EXAMPLE (DFS)
u v w π:
1/ 2/ u
B
nil
v
v u
4/5 3/6
u x y z w
x y
y v
z
EXAMPLE (DFS)
u v w π:
1/ 2/7 u
B
nil
v
4/5 3/6
u
u x y z w
x y
y v
z
EXAMPLE (DFS)
u v w π:
1/ 2/7 u
B
nil
F
v
4/5 3/6
u
u x y z w
x y
y v
z
EXAMPLE (DFS)
u v w π:
1/8 2/7 u
B
nil
F
v
4/5 3/6
u
x y z w
x y
y v
z
EXAMPLE (DFS)
u v w π:
1/8 2/7 9/ u
B
nil
F
v
3/6
u
4/5
w x y z w
nil
x y
y v
z
EXAMPLE (DFS)
u v w π:
1/8 2/7 9/ u
B C
nil
F
v
4/5 3/6
u
w x y z w
nil
x y
y v
z
EXAMPLE (DFS)
u v w π:
1/8 2/7 9/ u
B C
nil
F
v
z u
4/5 3/6 10/
w x y z w
nil
x y
y v
z
w
EXAMPLE (DFS)
u v w π:
1/8 2/7 9/ u
B C
nil
F
v
z u
4/5 3/6 10/ B
w x y z w
nil
x y
y v
z
w
EXAMPLE (DFS)
u v w π:
1/8 2/7 9/ u
B C
nil
F
v
4/5 3/6 10/11 B
u
w x y z w
nil
x y
y v
z
w
EXAMPLE (DFS)
u v w π:
1/8 2/7 9/12 u
B C
nil
F
v
4/5 3/6 10/11 B
u
x y z w
nil
x y
y v
z
w
DEPTH FIRST FOREST

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 vertexuuV[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 vertexuuV[G]
eachvertex V[G] (V) 4.
4. for
foreach
eachvvAdj[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.

DFS-Visit is called once for each white vertex vV when


it’s painted gray the first time. Lines 3-6 of DFS-Visit is
executed |Adj[v]| times. The total cost of executing DFS-
Visit is vV|Adj[v]| = (E)

Total running time of DFS is (V+E).


Total running time of DFS is (V+E).
DFS: KINDS OF EDGES
DFS introduces an important distinction among edges in the
original graph:
• Tree edge: encounter new (white) vertex
• Back edge >> from descendent to ancestor
• Forward edge: from ancestor to descendent, but not a tree
edge.
• Cross edge: between a tree or subtrees
Note: tree & back edges are important; most algorithms don’t
distinguish forward & cross
Theorem:
Theorem:
In
InDFS
DFSofofan
anundirected
undirectedgraph,
graph,we
weget
getonly
onlytree
treeand
andback
backedges.
edges.No
Noforward
forwardor
orcross
cross
edges.
edges.
REFERENCES

Introduction to Algorithms, Second Edition by Thomas H.


Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford
Stein, The MIT Press © 2001
Chapter 22.1, 22.2, 22.3

You might also like