Graph BasicsHandouts
Graph BasicsHandouts
Graph Algorithms
Graphs
A graph G = (V, E) consists of a set of vertices V, and a set
of edges E, such that each edge in E is a connection
between a pair of vertices in V.
● A graph G = (V, E)
■ V = set of vertices
■ E = set of edges = subset of V V (unordered pair of vertices)
■ Thus |E| = O(|V|2)
1
9/3/2013
Graphs
a b c d
e f g h
i j k l
V = { a, b, c, d, e, f, g, h, i, j, k, l }
E = { (a, b), (a, e), (b, e), (b, f), (c, j), (c, g), (c, h), (d, h), (e, j),
(g, k), (g, l), (g, h), (i, j) } 3
A Directed Graph
In a directed graph (digraph), edges are ordered pairs.
TW 45
NW 35
BOS
ORD
SFO DL 247
JFK AA 903
DL 335
AA 1387
UA 877
MIA
AA 49 AA 523
LAX DFW
AA 411
4
2
9/3/2013
Undirected Graph
■ In an undirected graph:
○ Edge (u,v) = edge (v,u)
○ No self-loops
●An undirected graph is connected if there is at least one path
from any vertex to any other.
3
9/3/2013
Connected Graph
b c
f
e
If G is not connected, the maximal connected subgraphs are the
connected components of G.
C3
C2
a
C1 d e f g
b c
7
Connected Graph
■ A connected graph has a path from every vertex to
every other
Complete Graph
■ A graph G is said to be complete if every node u
in G is adjacent to every other node v in G.
■ A complete graph with n nodes have n(n-1)/2
edges
a
d f g
b c
8
4
9/3/2013
Property of Connectivity
If G is connected, then | E | ≥ | V | – 1.
If G is a tree, then | E | = | V | – 1.
If G is a forest, then | E | ≤ | V | – 1.
An acyclical
undirected graph is
forest
9
a c
b d f
1 2 3
4 5 6
10
5
9/3/2013
Simple Graph
11
source c
b
a e
destination
d
12
6
9/3/2013
Handshaking Theorem
∑ deg(v) = 2 | E | | E | ≤ | V | · (| V | – 1) / 2
vV
4
K4 has ( 2) = 6 edges
If G is directed:
∑ indeg(v) = ∑ outdeg(v) = | E |
vV vV
| E | ≤ | V | · (| V | – 1)
13
Path
A path of length k is a sequence v 0 , v 1 , …, v k of vertices such
that (vi , vi+1 ) for i = 0, 1, …, k – 1 is an edge of G.
b, c, d not a path
a b c d
Simple path:
a, e, k, p, l, q
m, h, d, c, g
e f g h
(no repeated Non-simple path:
vertices) a, b, e, f, g, b, g, l
j k l m
n o p q
14
7
9/3/2013
Cycle
A cycle is a path that starts and ends at the same vertex.
a b c d
e f g h
k, j, n, k, p, o,k j k l m
is not simple.
n o p q
15
8
9/3/2013
Hamiltonian Cycle
hamiltonian cycle:
a,b,c,d,f,e,(then back to a)
17
Eulerian Tour
f
e
18
9
9/3/2013
a c
b
d e
f g
19
Subgraph
A subgraph H of G
is a graph;
its edges and vertices are subsets of those of G.
a b c d
e f g h
j k l m
n o p q
V(H) = {b, d, e, f, g, h, l, p, q} E(H) = {(b,
20 e), (b, g), (e, f), (d, h), (l, p), (l, q)}
10
9/3/2013
Tree Graph/Tree
21
11
9/3/2013
Graphs
23
Representing Graphs
24
12
9/3/2013
● Example:
A 1 2 3 4
1
a 1
2 d
4 2
3
b c
??
3 4
25
● Example:
A 1 2 3 4
1
a 1 0 1 1 0
2 d
4 2 0 0 1 0
b c 3 0 0 0 0
3 4 0 0 1 0
26
13
9/3/2013
2 A = (a ij )
1 if (i, j) E(G)
1 3 a ij =
0 otherwise
5 4 1 2 3 4 5
1 0 1 1 0 1
2 1 0 1 0 0
3 1 1 0 1 1
4 0 0 1 0 1
5 1 0 1 1 0
Space: (|V| 2 ).
Preferred when the graph is small or dense.
27
28
14
9/3/2013
29
15
9/3/2013
32
16
9/3/2013
33
17
9/3/2013
Operation Time
35
18
9/3/2013
Two methods
1- Breath-first Search
- Use a queue as an auxiliary structure to hold
nodes for future processing
2- Depth-first Search
- Use stack to handle traversing
37
Breadth-First Search
“Explore” a graph, turning it into a tree
■One vertex at a time
■Pick a source vertex to be the root
■Find (“discover”) its children, then their children, etc.
Shortest Path
■It computes the distance (smallest number of edges) from s
(source vertex) to each reachable vertices.
■For every vertex v reachable from s the path in the breath-first
tree from s to v corresponds to a “shortest path” from s to v in
G, that is the path containing the smallest number of edges.
Discovery (discovers all vertices at distance k from s before
discovering any vertices at distance k +1)
38
19
9/3/2013
Breadth-First Search
r s t u
v w x y
40
20
9/3/2013
r s t u
0
v w x y
Q: s
41
r s t u
1 0
1
v w x y
Q: w r
42
21
9/3/2013
r s t u
1 0 2
1 2
v w x y
Q: r t x
43
r s t u
1 0 2
2 1 2
v w x y
Q: t x v
44
22
9/3/2013
r s t u
1 0 2 3
2 1 2
v w x y
Q: x v u
45
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: v u y
46
23
9/3/2013
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: u y
47
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: y
48
24
9/3/2013
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: Ø
49
50
25
9/3/2013
Breadth-First Search
(slightly different form book)
BFS(G, s) {
initialize vertices;
Q = {s}; // Q is a queue (duh); initialize to s
while (Q not empty) {
u = RemoveTop(Q);
for each v u->adj {
if (v->color == WHITE)
v->color = GREY;
v->d = u->d + 1; What does v->d represent?
v->p = u; What does v->p represent?
Enqueue(Q, v);
}
u->color = BLACK;
}
}
51
52
26
9/3/2013
Analysis BFS
● The operation of enqueuing and dequeuing take O(1)
time. (i.e each vertex is enqueued at most once and thus
dequeued at most once) so the total time devoted to queue
operation is O(V).
● Adjacency list of each vertex is scanned only when the
vertex is dequeued, each adjacency list is scanned at
most once.
● Sum of length of the lengths of all the adjacency list is
O(E), the total time spent in scanning adjacency lists is
O(E).
● The overhead for initialization is O(V)
● So the total running time of BFS is O(V+E)
53
27
9/3/2013
55
28
9/3/2013
Depth-First Search
Depth-First Search
● If any undiscovered vertices remain, then one
of them is selected as a new source and the
search is repeated from the source.
● The entire process is repeated until all the
vertices are discovered.
● Vertices initially colored white
● Then colored gray when discovered
● Then black when finished
58
29
9/3/2013
DFS Example
source
vertex
59
DFS Example
source
vertex
d f
1 | | |
| |
| | |
60
30
9/3/2013
DFS Example
source
vertex
d f
1 | | |
2 | |
| | |
61
DFS Example
source
vertex
d f
1 | | |
2 | |
3 | | |
62
31
9/3/2013
DFS Example
source
vertex
d f
1 | | |
2 | |
3 | 4 | |
63
DFS Example
source
vertex
d f
1 | | |
2 | |
3 | 4 5 | |
64
32
9/3/2013
DFS Example
source
vertex
d f
1 | | |
2 | |
3 | 4 5 | 6 |
65
DFS Example
source
vertex
d f
1 | 8 | |
2 | 7 |
3 | 4 5 | 6 |
66
33
9/3/2013
DFS Example
source
vertex
d f
1 | 8 | |
2 | 7 |
3 | 4 5 | 6 |
67
DFS Example
source
vertex
d f
1 | 8 | |
2 | 7 9 |
3 | 4 5 | 6 |
34
9/3/2013
DFS Example
source
vertex
d f
1 | 8 | |
2 | 7 9 |10
3 | 4 5 | 6 |
69
DFS Example
source
vertex
d f
1 | 8 |11 |
2 | 7 9 |10
3 | 4 5 | 6 |
70
35
9/3/2013
DFS Example
source
vertex
d f
1 |12 8 |11 |
2 | 7 9 |10
3 | 4 5 | 6 |
71
DFS Example
source
vertex
d f
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 |
72
36
9/3/2013
DFS Example
source
vertex
d f
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 14|
73
DFS Example
source
vertex
d f
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 14|15
74
37
9/3/2013
DFS Example
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
76
38
9/3/2013
77
78
39
9/3/2013
● The loops on line 1-3 and lines 5-7 of DFS take time
O(V), exclusive DFS call.
79
80
40
9/3/2013
81
82
41
9/3/2013
DFS Example
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
Tree edges
83
84
42
9/3/2013
DFS Example
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
85
86
43
9/3/2013
DFS Example
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
87
88
44
9/3/2013
DFS Example
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
89
90
45
9/3/2013
Properties of DFS
91
92
46
9/3/2013
93
47
9/3/2013
48
9/3/2013
98
49