Basic Graph
Basic Graph
Basic Graph
1 2 3
V = {1, 2, 3, 4, 5, 6}
1 2 3
V = {1, 2, 3, 4, 5, 6}
4 5 6
1
• A directed graph is strongly connected if every pair of vertices are reachable from each other
– The strongly connected components are the equivalence classes of the vertices under the
“mutual reachability” relation.
• Graphs appear all over the place in all kinds of applications, e.g:
– Trees (|E| = |V | − 1)
– Connectivity/dependencies (house building plans, WWW-page connections = internet
graph)
• Often the edges (u, v) in a graph have weights w(u, v), e.g.
1.1 Representation
• Adjacency-list representation:
Examples:
1 2
1 2 3
2 2 4 5
3
4 5 1
5 4
4 5 6
6 3
1 2 5
1 2 3
2 1 5
3 6
4
5 2 1
4 5 6
6 3
• Adjacency-matrix representation:
2
– |V | × |V | matrix A where (
1 if (i, j) ∈ E
aij =
0 otherwise
Examples:
j
1 2 3 4 5 6
1 2 3
1 0 1 0 0 0 0
2 0 1 0 1 1 0
3 0 0 0 0 0 0
i
4 1 0 0 0 1 0
5 0 0 0 1 0 0
6 0 0 1 0 0 0
4 5 6
j
1 2 3 4 5 6
1 2 3
1 0 1 0 0 1 0
2 1 0 0 0 1 0
i 3 0 0 0 0 0 1
4 0 0 0 0 0 0
5 1 1 0 0 0 0
6 0 0 1 0 0 0
4 5 6
– Note: For undirected graphs, the adjacency matrix is symmetric along the main diagonal
(AT = A).
– If graph is weighted, weights are stored instead of one’s.
• Comparison of matrix and list representation:
2 Graph traversal
• There are two standard (and simple) ways of traversing all vertices/edges in a graph in a
systematic way
– Breadth-first
– Depth-first
• We can use them in many fundamental algorithms, e.g finding cycles, connected components,
...
3
2.1 Breadth-first search (BFS)
• Main idea:
• BFS corresponds to computing shortest path distance (number of edges) from s to all other
vertices.
• To control progress of our BFS algorithm, we think about coloring each vertex
• We use a queue Q to hold all gray vertices—vertices we have seen but are still not done with.
• We remember from which vertex a given vertex v is colored gray – i.e. the node that discovered
v first; this is called parent[v].
• Algorithm:
BFS(s)
color[s] = gray
d[s] = 0
ENQUEUE(Q, s)
WHILE Q not empty DO
DEQUEUE(Q, u)
FOR (u, v) ∈ E DO
IF color[v] = white THEN
color[v] = gray
d[v] = d[u] + 1
parent[v] = u
ENQUEUE(Q, v)
FI
color[u] = black
OD
4
• Algorithm runs in O(|V | + |E|) time
• Example (for directed graph):
a) b)
a s d f 1
11
00
00
11 11
00
00
11
00
11 00
11
Q: Q : c, a
111
000
000
111
000
111
b c e g 1
c)
1 2 d) 1 2
11
00
00
11 111
000
000
111 111
000
000
111 11
00
00
11
00
11 000
111 000
111 00
11
Q : a, d, e Q : d, e, b
111
000
000
111 11
00
00
11 11
00
00
11
000
111
000
111 00
11
00
11 00
11
00
11
1 2 2 1 2
e)
1 2 3 f) 1 2 3
11
00
00
11 11
00
00
11 111
000
000
111 11
00
00
11
00
11 00
11 000
111 00
11
Q : e, b, f Q : b, f
11
00
00
11 111
000
000
111 11
00
00
11
00
11 000
111 00
11
2 1 2 2 1 2
g) h)
1 11
00
00
11
2 00
11
3
11
00
1 111
000
000
111
2 3
00
11
00
11 00
11
00
11 000
111
000
111
Q:f Q:g
11
00
00
11 11
00
00
11
00
11 00
11
2 1 2 2 1 2 4
i)
1 2 3
11
00 11
00
00
11
00
11 00
11
00
11
Q:
2 1 2 4
• Note:
– parent[v] forms a tree; BFS-tree.
– d[v] contains length of shortest path from s to v. (Prove by induction)
– We can use parent[v] to find the shortest path from s to a given vertex.
• If graph is not connected we have to try to start the traversal at all nodes.
5
– Note: We can use algorithm to compute connected components in O(|V | + |E|) time.
• We can write DFS iteratively using the same algorithm as for BFS but with a STACK instead
of a QUEUE, or, we can write a recursive DFS procedure
– We will color a vertex gray when we first meet it and black when we finish processing
all adjacent vertices.
• Algorithm:
DFS(u)
color[u] = gray
d[u] = time
time = time + 1
FOR (u, v) ∈ E DO
IF color[v] = white THEN
parent[v] = u
DFS(v)
FI
OD
color[u] = black
f [u] = time
time = time + 1
– As before we can extend algorithm to unconnected graphs and we can use it to detect
cycles in O(|V | + |E|) time.
6
• Example:
idth=12cmdfs1.pdf
g) h)
1/ 3/ 4/ 1/ 3/ 4/
111
000
000
111 11
00
00
11 11
00
00
11 11
00
00
11 11
00
00
11 000
111
00 111
000
000
111 00
11 00
11 00
11 11 000
111
111
000 11
00 11
00
000
111 00
11 00
11
000
111 00
11 00
11
2/ 6/7 5/ 2/ 6/7 5/8
i) j)
1/ 3/ 4/9 1/ 3/10 4/9
111
000 11
00 11
00
000
111
000
111 00
11
00
11 00
11
00
11
111
000 11
00
000
111 00
11
000
111 00
11
2/ 6/7 5/8 2/ 6/7 5/8
k) l)
1/ 3/10 4/9 1/ 3/10 4/9
111
000 11
00 11
00 111
000
000
111 00
11 00
11 000
111
000
111 00
11 00
11 000
111
111
000 11
00
000
111 00
11
000
111 00
11
000
111 00
11
2/11 6/7 5/8 2/11 6/7 5/8
m) n)
12/ 1/ 3/10 4/9 12/ 1/ 3/10 4/9
11 111
00 000 11
00 11
00 11
00 111
000
00
11
00 111
11 000
000
111 00
11
00
11 00
11
00
11 00
11
00
11 000
111
000
111
11
00 11
00 11
00
00
11
00
11 00
11
00
11 00
11
00
11
2/11 6/7 5/8 13/ 2/11 6/7 5/8
o) p)
12/ 1/ 3/10 4/9 12/15 1/16 3/10 4/9
11
00
00
11 000
111 11
00 11
00 111
000
00 111
000 00
11 00
11 000
111
11 000
111 00
11 00
11 000
111
11
00 11
00
00
11 00
11
00
11 00
11
13/14 2/11 6/7 5/8 13/14 2/11 6/7 5/8
– Note: If u is descendent of v in DFS-tree then d[v] < d[u] < f [u] < f [v]
3 Topological sorting
• Definition: Topological sorting of directed acyclic graph G = (V, E) is a linear ordering of
vertices V such that (u, v) ∈ E ⇒ u appear before v in ordering.
7
• Topological ordering can be used in scheduling:
Socks
Watch
Underwear Shoes
Shirt
Pants
Tie
Belt
Jacket
1 2 3 4 5 6 7 8 9
• Algorithm: Topological order just reverse DFS finish time (⇒ O(|V | + |E|) running time).
– Proof: When (u, v) is explored by DFS algorithm, v must be white or black (gray ⇒
cycle).
∗ v white: v visited and finished before u is finished ⇒ f (v) < f (u)
∗ v black: v already finished ⇒ f (v) < f (u)
• Alternative algorithm: Count in-degree of each vertex and repeatedly number and remove
in-degree 0 vertex and its outgoing edges: Homework.