Chapter 5 Graphs
Chapter 5 Graphs
Chapter5
Graph Theory
Applications of Graphs
Potentially anything (graphs can represent
relations, relations can describe the extension
of any predicate)
Applications in networking, scheduling, flow
optimization, circuit design, path planning.
Geneology analysis, computer game playing,
program compilation, object-oriented design,
….
Varying Applications (examples)
Computer networks
Distinguish between two chemical
compounds with the same molecular
formula but different structures
Solve shortest path problems between
cities
Scheduling exams and assign channels
to television stations
Topics Covered
Definitions
Types
Terminology
Representation
Sub-graphs
Connectivity
Hamilton and Euler definitions
Shortest Path
Planar Graphs
Graph Coloring
Definitions - Graph
What is a graph G?
It is a pair G = (V, E),
where
V = V(G) = set of vertices
E = E(G) = set of edges
Example:
V = {s, u, v, w, x, y, z}
E = {(x,s), (x,v)1, (x,v)2, (x,u),
(v,w), (s,v), (s,u), (s,w), (s,y),
(w,y), (u,y), (u,z),(y,z)}
Edges
An edge may be labeled by a pair of
vertices, for instance e = (v,w).
e is said to be incident on v and w.
Isolated vertex = a vertex without
incident edges.
Definitions – Edge Type
Directed: Ordered pair of vertices. Represented as (u,
v) directed from vertex u to v.
u v
u v
Definitions – Edge Type
Loop: A loop is an edge whose endpoints are
equal i.e., an edge joining a vertex to it self is
called a loop. Represented as {u, u} = {u}
u v
w
Definitions – Graph Type
Multigraph: G(V,E), consists of set of vertices V, set of
Edges E and a function f from E to {{u, v}| u, v V, u ≠
v}. The edges e1 and e2 are called multiple or parallel
edges if f (e1) = f (e2).
Representation Example: V = {u, v, w}, E = {e1, e2, e3}
u
e1 e2 w
e3
v
u
e1 w e4
e2
v e3
Definitions – Graph Type
Directed Graph: G(V, E), set of vertices V, and set of Edges
E, that are ordered pair of elements of V (directed edges)
Representation Example: G(V, E), V = {u, v, w}, E = {(u, v),
(v, w), (w, u)}
u v
u
w e4
e1 e2
v e3
Definitions – Graph Type
w
Terminology – Directed
graphs
For the edge (u, v), u is adjacent to v OR v is adjacent
from u, u – Initial vertex, v – Terminal vertex
In-degree (deg - (u)): number of edges for which u is
terminal vertex
Out-degree (deg + (u)): number of edges for which u is
initial vertex
Note: A loop contributes 1 to both in-degree and out-degree
(why?)
Representation Example: For V = {u, v, w} , E = { (u, w),
( v, w), (u, v) }, deg- (u) = 0, deg+ (u) = 2, deg- (v) = 1,
deg+ (v) = 1, and deg- (w) = 2, deg+ (u) = 0
u v
w
Theorems: Undirected Graphs
Theorem 1
The Handshaking theorem:
2e v
vV
(why?)
Every edge connects 2 vertices
Theorems: Undirected
Graphs
Theorem 2:
An undirected graph has even number of vertices with
odd degree
Pr oof V 1 is the set of even degree vertices and V2 refers to odd degree vertices
2e deg(v) deg(u) deg(v)
vV u V1 v V2
Theorem 3: deg +
(u) = deg - (u) = |E|
Simple graphs – special
cases
Complete graph: Kn, is the simple graph that contains
exactly one edge between each pair of distinct vertices.
K1 K2 K3
K4
Simple graphs – special
cases
Cycle: Cn, n ≥ 3 consists of n vertices v1, v2, v3 … vn and
edges {v1, v2}, {v2, v3}, {v3, v4} … {vn-1, vn}, {vn, v1}
Representation Example: C3, C4
C3 C4
W3 W4
0 1
00 01
Q1 Q2
What is Q3?
Bipartite graphs
In a simple graph G, if V can be partitioned into two disjoint sets V1 and V2 such
that every edge in the graph connects a vertex in V1 and a vertex V2 (so that no
edge in G connects either two vertices in V1 or two vertices in V2)
Application example: Representing Relations
Representation example: V1 = {v1, v2, v3} and V2 = {v4, v5, v6},
v4
v1
v5
V(G) = V(V1) V(V2)
v2
|V(V1)| = m, |V(V2)| = n
v6
v3
V(V1) V(V2) =
V V
2
1
Complete Bipartite graphs
Km,n is the graph that has its vertex set portioned into two
subsets of m and n vertices, respectively There is an edge
between two vertices if and only if one vertex is in the
first subset and the other vertex is in the second subset.
Representation example: K2,3, K3,3
V1
|V(V1)| = m
|V(V2)| = n
V2
K2,3
K3,3
Discrete Mathematics
Graph Theory
cont.
Subgraphs
A subgraph of a graph G = (V, E) is a graph H =(V’, E’)
where V’ is a subset of V and E’ is a subset of E
Application example: solving sub-problems within a graph
Representation example: V = {u, v, w}, E = ({u, v}, {v,
w}, {w, u}}, H1 , H2
u u u
v w v w v
G H1 H2
Subgraphs
G = G1 U G2 wherein E = E1 U E2 and V = V1 U V2, G, G1
and G2 are simple graphs of G
Representation example:
V1 = {u, w}, E1 = {{u, w}}, V2 = {w, v}, E1 = {{w, v}},
V = {u, v ,w}, E = { { {u, w}, {w, v} } }
u
u
w v
w w v
G1 G2 G
Representation
Incidence (Matrix): Most useful when
information about edges is more desirable than
information about vertices.
w 1 0 1 0 1
x 0 0 0 1 1
y 0 1 1 1 0
Representation- Adjacency
Matrix
There is an N x N matrix, where |V| = N , the Adjacenct
Matrix (NxN) A = [aij]
For undirected graph
1 if {vi, vj} is an edge of G
a ij
0 otherwise
For directed graph
v u w
u
v 0 1 1
u 1 0 1
v w
w 1 1 0
Representation- Adjacency
Matrix
Example: directed Graph G (V, E)
v u w
u
v 0 1 0
u 0 0 1
v w
w 1 0 0
Linked Representation-
Adjacency List
Each node (vertex) has a list of which nodes (vertex) it is adjacent
u
node Adjacency List
u v,w
v w, u
v w
w u,v
G= [u: v, w; v: w, u; w: u, v]
Graph - Isomorphism
u1 u2 v1 v2
u3 u4 v4
v3
Connectivity
Basic Idea: In a Graph Reachability among
vertices by traversing the edges
Application Example:
- In a city to city road-network, if one city can
be reached from another city.
- Problems if determining whether a message
can be sent between two computer using
intermediate links
- Efficiently planning routes for data delivery in
the Internet
Connectivity – Path
A Path is a sequence of edges that begins at a
vertex of a graph and travels along edges of the
graph, always connecting pairs of adjacent
vertices.
2
1 v
3
u
4 5
Connectivity – Path
Definition for Directed Graphs
A Path of length n (> 0) from u to v in G is a sequence of n
edges e1, e2 , e3, …, en of G such that f (e1) = (xo, x1), f (e2)
= (x1, x2), …, f (en) = (xn-1, xn), where x0 = u and xn = v. A
path is said to pass through x0, x1, …, xn or traverse e1, e2 ,
e3, …, en
v2 v5
Connectivity –
Connectedness
Undirected Graph
v3
v5
v1
v2
v4
Connectivity –
Connectedness
Directed Graph
A directed graph is strongly connected if there is a path from a to b
a b
A directed graph is weakly connected if there is a (undirected) path
between every two vertices in the underlying undirected path
c d
c d
Unilaterally connected:
A directed graph is unilaterally connected or
unilateral if for any pair of vertices a and b,
there is a path from a to b to b to a, i.e. one of
them is reachable from the other.
Strongly connected=> Unilaterally=>Weakly
connected
Digraph G Digraph H
rify that G is strongly connected. H is not strongly connected
H unilateral ?
Connectivity –
Connectedness
Directed Graph
Representation example: G1 (Strong component), G2 (Weak
Component), G3 is undirected graph representation of G2 or G1
G1 G2 G3
Connectivity –
Connectedness
Directed Graph
Strongly connected Components: subgraphs
of a Graph G that are strongly connected
Representation example: G1 is the strongly
connected component in G
G G1
Counting Paths
Theorem: Let G be a graph with adjacency matrix A
with respect to the ordering v1, v2, …, Vn (with directed
on undirected edges, with multiple edges and loops
allowed). The number of different paths of length r from
Vi to Vj, where r is a positive integer, equals the (i, j)th
entry of (adjacency matrix) Ar.
A=0110 A4 = 8 0 0 8
1001 0880
1001 0880
0110 8008
0 otherwise
ij
0 0 0 1 1 0 1 0 1 0 0 2
1 0 1 1 2 0 1 2
3 0 2 3
A A
2
A
3
1 0 0 1 1 0 1 1 2 0 1 2
1 0 1 0 1 0 0 2
2 0 2 1
2 0 2 1
Thus B4=A+A2+A3+A4
5 0 3 5
A
4
4 0 3 4
3 0 2 3
11 0 7 11
3 0 1 4 7 0 4 7
7
0 4 7
Continued:
1 0 1 1
By the above
theorem, the path 1 0 1 1
P
matrix P will be
1 0 1 1
given by
1 0 1 1
The whole zero column represents that v2 is not
Reachable from any other vertices. Since the matrix
P has zero entries therefore the digraph is not
trongly connected.
Discrete Mathematics
Graph Theory R.K.Bajaj
Cont:
(Euler and
Hamilton)
The Seven Bridges of Königsberg,
Germany
The residents of Königsberg wondered if it was
possible to take a walking tour of the town that
crossed each of the seven bridges over the
Presel river exactly once. Is it possible to start at
some node and take a walk that uses each edge
exactly once, and ends at the starting node?
The Seven Bridges of Königsberg
You can redraw the original picture as long as for every edge
between nodes i and j in the original you put an edge between
nodes i and j in the redrawn version (and you put no other
edges in the redrawn version).
Original:
2 3
Redrawn: 2
4 1
3
The Seven Bridges of Königsberg
Euler:
a b
c d e
The problem in our language:
(vi) = 2m
i=1
S= (xp) (even)
T= (yj) thus T is even, therefore k is even
S+T = (Vi)= 2m (even)
Euler - theorems
1. A connected graph G is Eulerian if and only if G is
connected and has no vertices of odd degree
a b
f c d
c d
Delete the simple path:
{a,b}, {b,c}, {c,f}, {f,a}
c d
a b
f c d
c d
Delete the simple path:
{a,b}, {b,c}, {c,f}, {f,a}
c d
a b
f c d
“Splice” the circuits in the 2 graphs:
{a,b}, {b,c}, {c,f}, {f,a}
“+”
e {c,d}, {d,e}, {e,c}
“=“
{a,b}, {b,c}, {c,d}, {d,e}, {e,c}, {c,f}
{f,a}
Representation- Incidence
Matrix
e1 e2 e3 e4 e5 e6 e7
e1
a b a 1 0 0 0 0 0 1
e2 b 1 1 0 0 0 0 0
e7
e3 c 0 1 1 0 1 1 0
f c d
d 0 0 1 1 0 0 0
e6
e5 e 0 0 0 1 1 0 0
e e4
f 0 0 0 0 0 1 1
Theorem?
Is it Hamiltonian?
Yes
.
Hamiltonian Graph
The Hamiltonian
cycle (000, 001,
011, 010, 110, 111,
101, 100, 000)
joins vertices that
differ by one bit.
Planar Graphs:
A graph is called planar if it can be drawn in
the plane such that its edges do not cross.
Such a drawing is called a planar
representation of the graph.
Is K4 planar?
Other examples:
Hence G is nonplanar
Coloring of a graph:
Definition 1: A coloring of a simple graph is
the assignment of a color to each vertex of
the graph so that no two adjacent vertices
are assigned the same color.
Definition 2: The chromatic number of a
graph is the least number of colors needed
(G )it is denoted
for a coloring of the graph and
by
Example1:
Trees
Application Examples
Useful for locating items in a list
Used in Huffman coding algorithm
Study games like checkers and chess
to determine winning strategies
Weighted trees used to model
various categories of problems
Trees
Definition: A connected
undirected graph with no simple
circuits
Characteristics
- No multiple edges
- No loops
Example
UoK
CS EE ME Math.
Hierarchical definition
Which of the following graphs are
trees?
e a e
a b a
b
b c c
d a a
c f
e d d b
e f d c
A B C D
A & B : Trees
C: Not a Tree (Cycle abdcfa)
D: Not a Tree (Not connected). However it is a forest i.e.
unconnected graphs
Sub-trees
A subtree of a tree T is a tree T' such that
V(T') V(T) and
L
E(T') E(T)
H M
E K S
J T
Tree Theorem -Theorem
1
An undirected Graph is a tree if and only if there
is a unique simple path between any two of its
vertices
Proof (=>) The graph is a Tree and it does not have
cycles. If there were cycles then there will be more than
one simple path between two vertices. Hence, there is a
unique simple path between every pair of vertices
Internal Nodes = A, B, C,
B E and F
C
D F
E
G H I J
K
H M
E K S
J T
Definition - Height
H M
E K S
J T
Ordered Rooted Trees:
An ordered rooted tree is a rooted tree
where the children of each internal vertex
are shown in order from left to right.
Contd:
Observe that number of decimal points in any label
is one less than the level of the vertex. We refere
this system of labeling as Universal address system
for an ordered rooted tree.
The addresses in the previous tree are linearly
ordered as follows:
0 , 1, 1.1, 1.2, 1.2.1, 1.2.2, 1.2.2.1, 2, 2.1, 3, 3.1,
3.1.1, 3.2, 3.2.1, 3.2.1.1, 3.2.2
This is also called Dictionary order or Lexicographical
order.
Definition: m-ary trees
Rooted tree where every vertex has no more
than ‘m’ children
Full m-ary if every internal vertex has exactly
‘m’ children (i.e., except leaf/external vertices).
m=2 gives a binary tree
A complete m - ary tree is a full m-ary tree
where every leaf is at the same level.
Exercise: Construct a complete binary tree of
height 4 and a complete 3-ary tree of height 3.
Example: 3-ary tree
Definition: Binary Tree
Every internal vertex has a maximum of
2 children
H M
E K S
J T
Definition: Balanced
H M
E K S
J T
Theorem ????
Let T be a graph with n vertices.
The following are equivalent
T is a tree
T is connected and acyclic
T is a connected and has n-1 edges
T is acyclic and has n-1 edges
Tree Traversal
Information is stored in Ordered Rooted tree
Methods are needed for visiting each vertex
of the tree in order to access data
Three most commonly used traversal
algorithms are:
1. Preorder Traversal
2. Inorder Traversal
3. Postorder Traversal
Preorder Traversal
Let T be an ordered rooted tree with
root r only r, then r is the preorder traversal.
If T has
Otherwise, suppose T1, T2 are the left and right
subtrees at r. The preorder traversal begins by
visiting r. Then traverses T1 in preorder, then
traverses T2 in preorder.
Preorder Traversal Example: J E A H T M Y
‘J’
‘E’ ‘T’
‘J’
‘E’ ‘T’
‘J’
‘E’ ‘T’
+ - +
/
x y x 4
- 3
/ + 2
2 - 3 x y x 4
+
y x 4
x
Thus in this way, for such a given
expression we can draw an ordered
rooted tree in which the operations are
the internal nodes and variables are
leaves.
# We obtain the infix form of an expression when
we traverse its rooted tree in inorder. In inorder
traversal it is necessary to include parentheses
whenever we encounter an operation because the
infix form of the expressions (x+y)/(x+3) ,
(x+(y/x))+3, and x+(y/(x+3)) will be x+y/x+3.
Prim’s Algorithm
Kruskal’s Algorithm
Prims Algorithm
Procedure Prims (G: weighted connected undirected graph
with n vertices)
T := a minimum-weight edge
For i := 1 to n-2
Begin
e:= an edge of minimum weight incident to a vertex in
T and not forming a simple circuit in T if added to T
T := T with e added
End {T is a minimum spanning tree of G}
Prim’s Example
Initially
3
2 b d
8 T = {}
a 5
3
7
4 c e
4
Prim’s Example
Initial step, chose min weight edge
3
2 b d
8 T = {{a, b}}
a 5
3
7
4 c e
4
Prim’s Example
Iteration 1
3
2 b d
8 T = {{a, b}, {b, c}}
a 5
3
7
4 c e
4
Prim’s Example
Iteration 2
3
2 b d
8 T = {{a, b}, {b, c}, {b,
a 5
3 d}}
7
4 c e
4
Prim’s Example
Iteration 3
3
2 b d
8 T = {{a, b}, {b, c}, {b,
a 5 d} , {c, e}}
3
7
4 c e
4
Kruskal’s algorithm
Procedure Kruskal (G: weighted connected undirected
graph with n vertices)
T := empty graph
For i := 1 to n-1
Begin
e := any edge in G with smallest weight that does
not form simple circuit when added to T
T := T with e added
End { T is the minimum spanning tree of G}
Kruskal’s Example
Initially
3
2 b d
8 T = {}
a 5
3
7
4 c e
4
Kruskal’s Example
Iteration 1
3
2 b d
8 T = {{a, b}}
a 5
3
7
4 c e
4
Kruskal’s Example
Iteration 2
3
2 b d
8 T = {{a, b}, {b,
a 5
3 c}}
7
4 c e
4
Kruskal’s Example
Iteration 3
3
2 b d
8 T = {{a, b}, {b, c}, {b,
a 5
3 d}}
7
4 c e
4
Kruskal’s Example
Iteration 4
3
2 b d
8 T = {{a, b}, {b, c}, {b,
a 5 d} , {c, e}}
3
7
4 c e
4
Prim’s and Kruskal’s
Prim’s
Chosen edges: minimum weight &
incident to a vertex already in the
tree.
Kruskal’s
Chosen edge: minimum weight & not
necessarily
Understand theincident
difference:to a vertex in the
tree.
http://students.ceid.upatras.gr/~papagel/project/prim.htm
http://students.ceid.upatras.gr/~papagel/project/kruskal.htm