Elementary Graph Algorithms: Manoj Agnihotri M.Tech I.T Dept of CSE ACET Amritsar
Elementary Graph Algorithms: Manoj Agnihotri M.Tech I.T Dept of CSE ACET Amritsar
Manoj Agnihotri
M.Tech I.T
Dept of CSE
ACET Amritsar
What is a Graph?
A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices in V
An edge e = (u,v) is a pair of vertices
Example:
a
b
c
d
e
V= {a,b,c,d,e}
E= {(a,b),(a,c),(a,d),
(b,e),(c,d),(c,e),
(d,e)}
Applications
electronic circuits
networks (roads, flights, communications)
CS16
LAX
JFK
LAX
DFW
STL
HNL
FTL
Terminology:
Adjacent and Incident
If (v0, v1) is an edge in an undirected graph,
v0 and v1 are adjacent
The edge (v0, v1) is incident on vertices v0 and v1
If <v0, v1> is an edge in a directed graph
v0 is adjacent to v1, and v1 is adjacent from v0
The edge <v0, v1> is incident on v0 and v1
The degree of a vertex is the number of edges
incident to that vertex
For directed graph,
the in-degree of a vertex v is the number of edges
that have v as the head
the out-degree of a vertex v is the number of edges
that have v as the tail
if di is the degree of a vertex i in a graph G with n vertices and e edges,
the number of edges is
e d
i
n
=
( ) /
0
1
2
Terminology:
Degree of a Vertex
Why? Since adjacent vertices each
count the adjoining edge, it will be
counted twice
0
1 2
3 4 5 6
G1
G2
3
2
3 3
1
1
1
1
directed graph
in-degree
out-degree
0
1
2
G3
in:1, out: 1
in: 1, out: 2
in: 1, out: 0
0
1 2
3
3
3
3
Examples
7
Terminology:
Path
path: sequence of
vertices v
1
,v
2
,. . .v
k
such
that consecutive vertices
v
i
and v
i+1
are adjacent.
3
3
3
3
2
a
b
c
d
e
a b
c
d
e
a b e d c b e d c
More Terminology
simple path: no repeated vertices
cycle: simple path, except that the last vertex is the same as the first
vertex
a b
c
d
e
b e c
a c d a
a b
c
d
e
Even More Terminology
subgraph: subset of vertices and edges forming a graph
connected component: maximal connected subgraph. E.g., the graph below
has 3 connected components.
connected not connected
connected graph: any two vertices are connected by some path
0 0
1 2 3
1 2 0
1 2
3
(i) (ii) (iii) (iv)
(a) Some of the subgraph of G
1
0
0
1
0
1
2
0
1
2
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G
3
0
1 2
3
G1
0
1
2
G3
Subgraphs Examples
More
tree - connected graph without cycles
forest - collection of trees
tree
forest
tree
tree
tree
Connectivity
Let n = #vertices, and m = #edges
A complete graph: one in which all pairs of vertices are
adjacent
How many total edges in a complete graph?
Each of the n vertices is incident to n-1 edges, however, we would
have counted each edge twice! Therefore, intuitively, m = n(n -1)/2.
Therefore, if a graph is not complete, m < n(n -1)/2
n = 5
m = (5
-
4)/2 = 10
More Connectivity
n = #vertices
m = #edges
For a tree m = n - 1
n = 5
m = 4
n = 5
m = 3
If m < n - 1, G is not
connected
Oriented (Directed) Graph
A graph where edges are directed
Directed vs. Undirected Graph
An undirected graph is one in which the pair
of vertices in a edge is unordered, (v0, v1) =
(v1,v0)
A directed graph is one in which each edge is a
directed pair of vertices, <v0, v1> != <v1,v0>
tail
head
Graph Representations
Adjacency Matrix
Adjacency Lists
Adjacency Matrix
Let G=(V,E) be a graph with n vertices.
The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0
The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph
need not be symmetric
Examples for Adjacency Matrix
0
1
1
1
1
0
1
1
1
1
0
1
1
1
1
0
(
(
(
(
0
1
0
1
0
0
0
1
0
(
(
(
0
1
1
0
0
0
0
0
1
0
0
1
0
0
0
0
1
0
0
1
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
(
(
(
(
(
(
(
(
(
(
(
G1
G2
G4
0
1 2
3
0
1
2
1
0
2
3
4
5
6
7
symmetric
undirected: n
2
/2
directed: n
2
Merits of Adjacency Matrix
From the adjacency matrix, to determine the
connection of vertices is easy
The degree of a vertex is
For a digraph (= directed graph), the row sum is
the out_degree, while the column sum is the
in_degree
adj mat i j
j
n
_ [ ][ ]
=
0
1
ind vi A j i
j
n
( ) [ , ] =
=
0
1
outd vi A i j
j
n
( ) [ , ] =
=
0
1
0
1
2
3
0
1
2
0
1
2
3
4
5
6
7
1 2 3
0 2 3
0 1 3
0
1 2
G1
1
0 2
G3
1 2
0 3
0 3
1 2
5
4
6
5 7
6
G4
0
1 2
3
0
1
2
1
0
2
3
4
5
6
7
An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
Some Operations
ndegree of a vertex in an undirected graph
# of nodes in adjacency list
n# of edges in a graph
determined in O(n+e)
nout-degree of a vertex in a directed graph
# of nodes in its adjacency list
nin-degree of a vertex in a directed graph
traverse the whole data structure
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).
Breadth-first Search
Input: Graph G = (V, E), either directed or undirected,
and source vertex s e V.
Output:
d[v] = distance (smallest # of edges, or shortest path) from s to
v, for all v e V. d[v] = if v is not reachable from s.
t[v] = u such that (u, v) is last edge on shortest path s v.
u is vs predecessor.
Builds breadth-first tree with root s that contains all reachable
vertices.
Definitions:
Path between vertices u and v: Sequence of vertices (v
1
, v
2
, , v
k
) such that
u=v
1
and v =v
k
, and (v
i
,v
i+1
) e E, for all 1s i s k-1.
Length of the path: Number of edges in the path.
Path is simple if no vertex is repeated.
Error!
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.
Comp 122, Fall 2004
BFS(G,s)
1. for each vertex u in V[G] {s}
2 do color[u] white
3 d[u]
4 t[u] nil
5 color[s] gray
6 d[s] 0
7 t[s] nil
8 Q u
9 enqueue(Q,s)
10 while Q = u
11 do u dequeue(Q)
12 for each v in Adj[u]
13 do if color[v] = white
14 then color[v] gray
15 d[v] d[u] + 1
16 t[v] u
17 enqueue(Q,v)
18 color[u] black
white: undiscovered
gray: discovered
black: finished
Q: a queue of discovered
vertices
color[v]: color of v
d[v]: distance from s to v
t[u]: predecessor of v
Example: animation.
Example (BFS)
0
r s t u
v w x y
Q: s
0
Example (BFS)
1 0
1
r s t u
v w x y
Q: w r
1 1
Example (BFS)
1 0
1
2
2
r s t u
v w x y
Q: r t x
1 2 2
Example (BFS)
1 0
1
2
2
2
r s t u
v w x y
Q: t x v
2 2 2
Example (BFS)
1 0
1
2
2
3
2
r s t u
v w x y
Q: x v u
2 2 3
Comp 122, Fall 2004
Example (BFS)
1 0
1
2 3
2
3
2
r s t u
v w x y
Q: v u y
2 3 3
Comp 122, Fall 2004
Example (BFS)
1 0
1
2 3
2
3
2
r s t u
v w x y
Q: u y
3 3
Comp 122, Fall 2004
Example (BFS)
1 0
1
2 3
2
3
2
r s t u
v w x y
Q: y
3
Comp 122, Fall 2004
Example (BFS)
1 0
1
2 3
2
3
2
r s t u
v w x y
Q: C
Comp 122, Fall 2004
Example (BFS)
1 0
1
2 3
2
3
2
r s t u
v w x y
BF Tree
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.
Comp 122, Fall 2004
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)
t[v] : predecessor of v = u, such that v was discovered during
the scan of us adjacency list.
Uses the same coloring scheme for vertices as BFS.
Comp 122, Fall 2004
Pseudo-code
DFS(G)
1. for each vertex u e V[G]
2. do color[u] white
3. t[u] NIL
4. time 0
5. for each vertex u e V[G]
6. do if color[u] = white
7. then DFS-Visit(u)
Uses a global timestamp time.
DFS-Visit(u)
1. color[u] GRAY V White vertex u
has been discovered
2. time time + 1
3. d[u] time
4. for each v e Adj[u]
5. do if color[v] = WHITE
6. then t[v] u
7. DFS-Visit(v)
8. color[u] BLACK V Blacken u;
it is finished.
9. f[u] time time + 1
Example: animation.
Comp 122, Fall 2004
Classification of Edges
Tree edge: in the depth-first forest. Found by exploring
(u, v).
Back edge: (u, v), where u is a descendant of v (in the
depth-first tree).
Forward edge: (u, v), where v is a descendant of u, but
not a tree edge.
Cross edge: any other edge. Can go between vertices in
same depth-first tree or in different depth-first trees.
Theorem:
In DFS of an undirected graph, we get only tree and back edges. No forward or
cross edges.
Comp 122, Fall 2004
Example (DFS)
1/
u v w
x y z
Comp 122, Fall 2004
Example (DFS)
1/
2/
u v w
x y z
Comp 122, Fall 2004
Example (DFS)
1/
3/
2/
u v w
x y z
Comp 122, Fall 2004
Example (DFS)
1/
4/ 3/
2/
u v w
x y z
Comp 122, Fall 2004
Example (DFS)
1/
4/ 3/
2/
u v w
x y z
B
Comp 122, Fall 2004
Example (DFS)
1/
4/5 3/
2/
u v w
x y z
B
Comp 122, Fall 2004
Example (DFS)
1/
4/5 3/6
2/
u v w
x y z
B
Comp 122, Fall 2004
Example (DFS)
1/
4/5 3/6
2/7
u v w
x y z
B
Comp 122, Fall 2004
Example (DFS)
1/
4/5 3/6
2/7
u v w
x y z
B
F
Comp 122, Fall 2004
Example (DFS)
1/8
4/5 3/6
2/7
u v w
x y z
B
F
Comp 122, Fall 2004
Example (DFS)
1/8
4/5 3/6
2/7 9/
u v w
x y z
B
F
Comp 122, Fall 2004
Example (DFS)
1/8
4/5 3/6
2/7 9/
u v w
x y z
B
F
C
Comp 122, Fall 2004
Example (DFS)
1/8
4/5 3/6 10/
2/7 9/
u v w
x y z
B
F
C
Comp 122, Fall 2004
Example (DFS)
1/8
4/5 3/6 10/
2/7 9/
u v w
x y z
B
F
C
B
Comp 122, Fall 2004
Example (DFS)
1/8
4/5 3/6
10/11
2/7 9/
u v w
x y z
B
F
C
B
Comp 122, Fall 2004
Example (DFS)
1/8
4/5 3/6
10/11
2/7 9/12
u v w
x y z
B
F
C
B
Comp 122, Fall 2004
Analysis of DFS
Loops on lines 1-2 & 5-7 take O(V) time, excluding time
to execute DFS-Visit.
DFS-Visit is called once for each white vertex veV when
its painted gray the first time. Lines 3-6 of DFS-Visit is
executed |Adj[v]| times. The total cost of executing
DFS-Visit is
veV
|Adj[v]| = O(E)
Total running time of DFS is O(V+E).
Comp 122, Fall 2004
Parenthesis Theorem
Theorem 22.7
For all u, v, exactly one of the following holds:
1. d[u] < f [u] < d[v] < f [v] or d[v] < f [v] < d[u] < f [u] and neither u
nor v is a descendant of the other.
2. d[u] < d[v] < f [v] < f [u] and v is a descendant of u.
3. d[v] < d[u] < f [u] < f [v] and u is a descendant of v.
So d[u] < d[v] < f [u] < f [v] cannot happen.
Like parentheses:
OK: ( ) [ ] ( [ ] ) [ ( ) ]
Not OK: ( [ ) ] [ ( ] )
Corollary
v is a proper descendant of u if and only if d[u] < d[v] < f [v] < f [u].
Comp 122, Fall 2004
Example (Parenthesis Theorem)
3/6
4/5 7/8
12/13
2/9 1/10
y z s
x w v
B F
14/15
11/16
u
t
C C C
C
B
(s (z (y (x x) y) (w w) z) s) (t (v v) (u u) t)