08 Graph Algorithms Part1
08 Graph Algorithms Part1
Graph Algorithms-Part1
Computer Science Dept.
Instructor: Dr. Ameera Jaradat
Outline
Concepts & Terminology
Representation
Traversal
Breadth-first Search (BFS).
Depth-first Search (DFS).
2
?What is a Graph
A graph G = (V, E) consists of:
V: set of vertices
E: set of edges connecting the vertices in V
An edge e = (v, w) is a pair of vertices, where v, w V. Sometimes each
edge is associated with a weight or a cost.
If the pair is ordered, then the graph is directed. Directed graphs are
sometimes referred to as digraphs.
Examples
a b a b
c c
d e d e
V = {a, b, c, d, e} V = {a, b, c, d, e}
,E = {(a, b), (a, c), (a, d) ,E = {(a, b), (a, c), (a, d)
,)c, e( ,)c, d( ,)b, e( ,)c, e( ,)c, d( ,)b, e(
})d, e( )pairs are ordered( })d, e(
Problems that can be represented by a graph
computer networks
airline flights
road map
course prerequisite structure
many more
Any individuals interaction can be represented as a graph were the
individuals are the nodes and the interactions are the links.
Graph Terminology
Path
A path in a graph is a sequence of vertices w1, w2, w3, …, wN such that
wi+1 is adjacent to wi (i.e, (wi, wi+1) E) for 1 <= i < N.
a b a b
c c
d e d e
The length of a path, w1, w2, w3, …, wN, is the number of edges on the
path, which is equal to N-1.
Graph Terminology
Loops
If the graph contains an edge (v, v) from a vertex to itself, then the path
v, v is sometimes referred to as a loop.
a b
d e
Simple paths
A simple path is a path such that all vertices are distinct, except that the
first and last could be the same.
a b
abedc is a simple path.
c cdec is a simple path.
abedce is NOT a simple path.
d e
Graph Terminology
Cycles
A cycle in a directed graph is a path of length at least 1 such that the first
vertex on the path is the same as the last one; if the path is simple, then
the cycle is a simple cycle.
a b
.abeda is a simple cycle
c .abeceda is a cycle, but is NOT a simple cycle
.abedc is NOT a cycle
d e
a b
a b
c
c
d e
d e
c c
d e d e
Connectivity in Directed Graphs
Weakly connected : can get from a to b in underlying undirected graph
weak strong
Degree of a Vertex
a b
d c
Complete Graph
Let n = #vertices, and m = #edges
A complete graph: 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, 0 < m < n(n -1)/2
For a connected graph of n vertices:
n ( n 1)
n 1 m
2
n 5
tree complete m (5
Weighted vs unweighted
Sparse vs Dense
A graph is sparse if | E | | V |
A graph is dense if | E | | V |2.
Simple vs not simple graph
4 4 3
3
Representation of Graphs
Adjacency matrix representation
Adjacency list representation
Adjacency Matrix
|V| |V| matrix A.
Number vertices from 1 to |V| in some arbitrary manner.
A is then given by: A[i, j ] a 1 if (i, j ) E
ij
0 otherwise
1 2
a b 4 3 2 1
1 1 1 0 1
0 1 0 0 2
c d4 1 0 0 0 3
3 0 0 0 0 4
1 2 4 3 2 1 .A = AT for undirected graphs
a b
1 1 1 0 1
0 1 0 1 2
c d 1 0 1 1 3
3 4 0 1 0 1 4
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.
a b a b d c
b c
c d c d
If weighted, store weights also in
d adjacency lists.
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)
List vs. Matrix
Adjacency list representation
Space-efficient, when a graph is sparse.
Determining if an edge (u,v) G is not efficient.
Search in u’s adjacency list. (degree(u)) time.
(V) in the worst case.
Adjacency matrix representation
Space: (V2).
An appropriate representation for a dense graph.
list all vertices adjacent to u: (V).
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).
Breadth-first Search
r s t u
0
v w x y
Q: s
0
r s t u
1 0
1
v w x y
Q: w r
1 1
Example (BFS)
r s t u
1 0 2
1 2
v w x y
Q: r t x
2 2 1
Example (BFS)
r s t u
1 0 2
2 1 2
v w x y
Q: t x v
2 2 2
Example (BFS)
r s t u
1 0 2 3
2 1 2
v w x y
Q: x v u
3 2 2
Example (BFS)
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: v u y
3 3 2
Example (BFS)
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: u y
3 3
Example (BFS)
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: y
3
Example (BFS)
r s t u
1 0 2 3
2 1 2 3
v w x y
Q:
Example (BFS)
r s t u
1 0 2 3
2 1 2 3
v w x y
BF Tree
Analysis of BFS
Initialization takes O(V).
Traversal Loop
After initialization, each vertex is enqueued and dequeued at most
once, and each operation takes O(1). So, total time for queuing is
O(V).
The adjacency list of each vertex is scanned at most once. The
sum of lengths of all adjacency lists is (E).
u v w
/1
x y z
Example (DFS)
u v w
/1 /2
x y z
Example (DFS)
u v w
/1 /2
/3
x y z
Example (DFS)
u v w
/1 /2
/4 /3
x y z
Example (DFS)
u v w
/1 /2
/4 /3
x y z
Example (DFS)
u v w
/1 /2
4/5 /3
x y z
Example (DFS)
u v w
/1 /2
4/5 3/6
x y z
Example (DFS)
u v w
/1 2/7
4/5 3/6
x y z
Example (DFS)
u v w
/1 2/7
F B
4/5 3/6
x y z
Example (DFS)
u v w
1/8 2/7
F B
4/5 3/6
x y z
Example (DFS)
u v w
1/8 2/7 /9
F B
4/5 3/6
x y z
Example (DFS)
u v w
1/8 2/7 /9
F B C
4/5 3/6
x y z
Example (DFS)
u v w
1/8 2/7 /9
F B C
u v w
1/8 2/7 /9
F B C
u v w
1/8 2/7 /9
F B C
u v w
1/8 2/7 9/12
F B C
Tree edges
Forward edges
Back edges a
Cross edges
b c
c
Applications of DFS(1)
Cycle detection:
Does a given graph G contain a cycle?
60
DFS And Cycles
A digraph is acyclic if and only if any DFS forest of yields
no back edges.
What will be the running time? O(V+E)
We can actually determine if cycles exist in O(V) time:
In an undirected acyclic forest, |E| |V| - 1
So count the edges: if ever see |V| distinct edges, must have
seen a back edge along the way
Applications of DFS(2)
Connected components of an undirected graph.
Each call to DFS_VISIT (from DFS) explores an entire connected
component (see DFS(G) line 4).
modify DFS to count the number of times it calls
DFS_VISIT
4. for each vertex u G->V
5. if (u->color == WHITE)
6. cc_counter ←cc_counter + 1
7. DFS_Visit(u);
62
Applications of DFS(3)
Topological sort
if G = (V, E) is a DAG then a topological sorting of G
is a linear ordering of G such that for each edge (u, v)
in the DAG, u appears before v in the linear ordering.
TOPOLOGICAL-SORT(G):
1) call DFS(G) to compute finishing times f[v] for each vertex
v
2) as each vertex is finished, insert it onto the front of a linked
list
3) return the linked list of vertices
Topological sort
∞=d ∞ d
d=1
∞=f b c ∞=f
∞=d ∞=d
∞=f d e ∞=f
∞=d f
∞=f
Topological sort
∞=d
∞=f a
∞=d d=1
∞=f b c ∞=f
∞
d=2
d ∞=d
∞=f d e ∞=f
∞=d f
∞=f
Topological sort
∞=d
∞=f a
∞=d d=1
∞=f b c ∞=f
d=2 ∞=d
∞=f d e ∞=f
d=3 f
∞
f ==4f
f
Topological sort
∞=d
∞=f a
∞=d d=1
∞=f b c ∞=f
d=2 ∞=d
f=5 d e ∞=f
d=3 f
f=4
d f
Topological sort
∞=d
∞=f a
∞=d d=1
∞=f b c ∞=f
d=2 ∞=d
f=5 d e ∞=f
d=3 f
f=4
d f
Topological sort
∞=d
∞=f a
∞=d d=1
∞=f b c ∞=f
d=2 d=6
f=5 d e ∞=f
d=3 f
f=4
e d f
Topological sort
∞=d
∞=f a
∞=d d=1
∞=f b c ∞=f
d=2 d=6
f=5 d e f=7
d=3 f
f=4
c e d f
Topological sort
∞ d
d=9
∞=f a
∞=d d=1
∞=f b c f=8
d=2 d=6
f=5 d e f=7
d=3 f
f=4
c e d f
Topological sort
d=9
∞=f a
d = 10 d=1
11f b
f∞= = c f=8
d=2 d=6
f=5 d e f=7
d=3 f
f=4
b c e d f
Topological sort
d=9
∞=f a
d = 10 d=1
f = 11 b c f=8
d=2 d=6
f=5 d e f=7
d=3 f
f=4
b c e d f
Topological sort
d=9
f = 12 a
d = 10 d=1
f = 11 b c f=8
d=2 d=6
f=5 d e f=7
d=3 f
f=4
a b c e d f
Topological sort
d=9
f = 12 a
d = 10 d=1
f = 11 b c f=8
d=2 d=6
d e
f=5 f=7
d=3 f
f=4
a b c e d f
Time complexity of TS(G)
Running time of topological sort:
Θ(n + m)
where n=|V| and m=|E|
Why? Depth first search takes Θ(n + m) time in the worst
case, and inserting into the front of a linked list takes Θ(1)
time