4. Graphs
• G = (V,E), an ordered pair
• V is the vertex set.
• Vertices are also called nodes and points.
• E is the edge set.
• Each edge connects two different vertices.
• Edges are also called arcs and lines.
• Directed edge has an orientation (u,v).
u v
5. Graphs
• Undirected edge has no orientation (u,v).
u v
• Undirected graph => no oriented edge.
• Directed graph => every edge has an
orientation.
15. Even More Terminology
• connected component: maximal connected subgraph. e.g., the graph below has
3 connected components.
16. 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
17. 17
Graph – An Example
1
4
2
5
3
A graph G (undirected)
The graph G= (V,E) has 5 vertices and 6 edges:
V = {1,2,3,4,5}
E = { (1,2),(1,3),(1,4),(2,5),(3,4),(4,5)}
• Adjacent:
1 and 2 are adjacent -- 1 is adjacent to 2 and 2 is adjacent to 1
18. 18
Directed Graph – An Example
1
4
2
5
3
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) }
• Adjacent:
1 is adjacent to 2, but 2 is NOT adjacent to 1
19. Set notation of a Graph
• 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)}
20. Applications
• Traffic Flow
• Communication System
• Supply chain
• Electrical Network
• Airlines
• Social Network (Graph Search)
22. Subgraphs
• A subgraph consists of a subset of a graph’s
vertices and a subset of its edges.
• Let G=(V,E) and )
,
( E
V
G
E
V
G
G
E
and
V
iff
23. 0 0
1 2 3
1 2 0
1 2
3
(i) (ii) (iii) (iv)
(a) Some of the subgraph of G1
0 0
1
0
1
2
0
1
2
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G3
0
1 2
3
G1
0
1
2
G3
Subgraphs Examples
24. Complete Graph
• 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
25. • Every complete graph is connected but
the converse need not be true.
1
4
2
5
3
Connected but not complete
27. 27
Terminology:
Path
• path: sequence of
vertices v1,v2,. . .vk such
that consecutive vertices vi
and vi+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
28. 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
29. 29
Directed Graph – An Example
1
4
2
5
3
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) }
•Path:
1,2,5 ( a simple directed path) but 1,4,3,1,2 is not a simple path.
• Cycle:
1,4,3,1 (a directed cycle),
31. 31
Spanning trees
• spanning tree of a graph is a connected subgraph
in which there are no cycles
A connected,
undirected graph
Four of the spanning trees of the graph
• A subgraph of G contains all the vertices of
G and is a tree is called spanning tree.
32. Bipartite graph: a graph whose vertices can be partitioned into two
disjoint sets V and U, not necessarily of the same size, so that every
edge connects a vertex in V to a vertex in U.
A graph is bipartite if and only if it does not have a cycle of an odd
length.
4 5
11
10
9
8
7
1 2 3
V
U
6
12
Bipartite trees
34. Degree of a vertex in an
undirected graph
• The degree (or valency) of a vertex of a
graph is the number of edges incident to the
vertex
2
3
1
4
5
d(1)=2
d(2)=2
d(3)=1
d(5)=3
d(4)=2
35. Handshaking lemma
• Let G(V,E) be an undirected graph. Let
and then
(a)
(b)
Proof:
(a) Every edge is incident on exactly two
vertices. Hence the sum of the vertices equals
two times the number of edges.
n
V
e
E
e
d
n
i
i 2
1
2
/
)
1
(
0
n
n
e
36. Degree of a vertex in a directed
graph
1 2
3 4
1
1
1
out
in
d
d
2
and
1 2
2
out
in
d
d
1
and
2 4
4
out
in
d
d
2
3
3
out
in
d
d
37. Handshaking lemma
• Let G(V,E) be an directed graph. Let
and then
(a)
(b)
Proof:
(a) Every edge is incident on exactly two
vertices. Hence the sum of the vertices equals
two times the number of edges.
n
V
e
E
e
d
d
n
i
out
i
n
i
in
i 2
1
1
)
1
(
0
n
n
e
38. Adjacency Matrix
• 0/1 n x n matrix, where n = # of vertices
• A(i,j) = 1 iff (i,j) is an edge
2
3
1
4
5
1 2 3 4 5
1
2
3
4
5
0 1 0 1 0
1 0 0 0 1
0 0 0 0 1
1 0 0 0 1
0 1 1 1 0
39. Adjacency Matrix Properties
2
3
1
4
5
1 2 3 4 5
1
2
3
4
5
0 1 0 1 0
1 0 0 0 1
0 0 0 0 1
1 0 0 0 1
0 1 1 1 0
•Diagonal entries are zero.
•Adjacency matrix of an undirected graph is
symmetric.
A(i,j) = A(j,i) for all i and j.
40. Adjacency Matrix (Digraph)
2
3
1
4
5
1 2 3 4 5
1
2
3
4
5
0 0 0 1 0
1 0 0 0 1
0 0 0 0 0
0 0 0 0 1
0 1 1 0 0
•Diagonal entries are zero.
•Adjacency matrix of a digraph need not be
symmetric.
41. Adjacency Matrix
• n2 bits of space
• For an undirected graph, may store only
lower or upper triangle (exclude diagonal).
(n-1)n/2 bits
• O(n) time to find vertex degree and/or
vertices adjacent to a given vertex.