Dms Mod5
Dms Mod5
The previous part brought forth the different tools for reasoning, proofing and problem
solving. In this part, we will study the discrete structures that form the basis of
formulating many a real-life problem.
The two discrete structures that we will cover are graphs and trees. A graph is a set of
points, called nodes or vertices, which are interconnected by a set of lines called edges.
The study of graphs, or graph theory is an important part of a number of disciplines in
the fields of mathematics, engineering and computer science.
What is a Graph?
Definition: A graph (denoted as G = (V, E)) consists of a non-empty set of vertices or
nodes V and a set of edges E.
a c
d
b
Even and Odd Vertex: If the degree of a vertex is even, the vertex is called an even
vertex and if the degree of a vertex is odd, the vertex is called an odd vertex.
Degree of a Vertex: The degree of a vertex V of a graph G (denoted by deg (V)) is the
number of edges incident with the vertex V.
a 2 even
b 2 even
c 3 odd
d 1 odd
Degree of a Graph: The degree of a graph is the largest vertex degree of that graph.
For the above graph the degree of the graph is 3.
The Handshaking Lemma: In a graph, the sum of all the degrees of vertices is equal
to twice the number of edges.
56
Discrete Mathematics
Types of Graphs
There are different types of graphs, which we will learn in the following section.
Null Graph
a c
b
Null graph of 3 vertices
Simple Graph
A graph is called simple graph/strict graph if the graph is undirected and does not
contain any loops or multiple edges.
a c
b
Simple graph
Multi-Graph
If in a graph multiple edges between the same set of vertices are allowed, it is called
Multi-graph.
a c
b
Multi-graph
57
Discrete Mathematics
a c
b
Undirected graph
a c
b
Directed graph
a c
d
b
Connected graph
a c
d
b
Unconnected graph
58
Discrete Mathematics
Regular Graph
A graph is regular if all the vertices of the graph have the same degree. In a regular
graph G of degree r, the degree of each vertex of G is r.
a c
Complete Graph
A graph is called complete graph if every two vertices pair are joined by exactly
one edge. The complete graph with n vertices is denoted by Kn
a c
b
Complete graph K3
Cycle Graph
If a graph consists of a single cycle, it is called cycle graph. The cycle graph with n
vertices is denoted by Cn
a c
b
Cyclic graph C3
59
Discrete Mathematics
Bipartite Graph
If the vertex-set of a graph G can be split into two sets in such a way that each edge of
the graph joins a vertex in first set to a vertex in second set, then the graph G is called a
bipartite graph. A graph G is bipartite if and only if all closed walks in G are of even
length or all cycles in G are of even length.
a c
b d
Bipartite graph
a c
b
d
Complete bipartite graph K2,2
Representation of Graphs
There are mainly two ways to represent a graph:
Adjacency Matrix
Adjacency List
Adjacency Matrix
An Adjacency Matrix A[V][V] is a 2D array of size V×V where V is the number of vertices in a
undirected graph. If there is an edge between Vx to Vy then the value of A[Vx][ Vy]=1 and
A[Vy][ Vx]=1, otherwise the value will be zero. And for a directed graph, if there is an edge
between Vx to Vy, then the value of A[Vx][ Vy]=1, otherwise the value will be zero.
60
Discrete Mathematics
a c
b d
An undirected graph
a b c d
a 0 1 1 0
b 1 0 1 0
c 1 1 0 1
d 0 0 1 0
a c
d
b
A directed graph
a b c d
a 0 1 1 0
b 0 0 1 0
c 0 0 0 1
d 0 0 0 0
61
Discrete Mathematics
Adjacency List
In adjacency list, an array (A[V]) of linked lists is used to represent the graph G with V
number of vertices. An entry A[Vx] represents the linked list of vertices adjacent to the
Vx-th vertex. The adjacency list of the graph is as shown in the figure below:
a b c
b a c
c a b d
d c
a c
b d
Planar graph
a c
b d
Non-planar graph
62
Discrete Mathematics
Isomorphism
If two graphs G and H contain the same number of vertices connected in the same way, they are called isomorphic graphs (denoted by G≅H).
Example
The following graphs are isomorphic:
a
c
a c
b d b d a d c b
Homomorphism
A homomorphism from a graph G to a graph H is a mapping (May not be a bijective mapping) h: G H such that: (x, y) ∈ E(G) (h(x), h(y)) ∈ E(H)
. It maps adjacent vertices of graph G to the adjacent vertices of the graph H.
Euler Graphs
A connected graph G is called an Euler graph, if there is a closed trail which includes
every edge of the graph G. An Euler path is a path that uses every edge of a graph
exactly once. An Euler path starts and ends at different vertices.
An Euler circuit is a circuit that uses every edge of a graph exactly once. An Euler circuit
always starts and ends at the same vertex. A connected graph G is an Euler graph if and
only if all vertices of G are of even degree, and a connected graph G is Eulerian if and
only if its edge set can be decomposed into cycles.
63
Discrete Mathematics
a b d
1
3
2 4
7
6 c 5 e
f
Euler graph
4
5 2
3 c
d
Non-Euler graph
Hamiltonian Graphs
A connected graph G is called Hamiltonian graph if there is a cycle which includes every
vertex of G and the cycle is called Hamiltonian cycle. Hamiltonian walk in graph G is a
walk that passes through each vertex exactly once.
If G is a simple graph with n vertices, where n ≥ 3 If deg(v) ≥ 1/2 n for each vertex v,
then the graph G is Hamiltonian graph. This is called Dirac's Theorem.
If G is a simple graph with n vertices, where n ≥ 2 if deg(x) + deg(y) ≥ n for each pair
of non-adjacent vertices x and y, then the graph G is Hamiltonian graph. This is called
Ore's theorem.
a b
1
5
6
8 2
e 4
7
3 c
d
Hamiltonian graph
64
Discrete Mathematics
a b
1
5
f
6
8 2
e 4
9
7
3 c
d
Non-Hamiltonian graph
65
15. TREES Discrete Mathematics
TRODUCTION TO TR
Tree is a discrete structure that represents hierarchical relationships between individual
elements or nodes. A tree in which a parent has no more than two children is called a
binary tree.
A tree
Step 2: Repeat step 1 until either a single vertex or two vertices joined by an edge is
left. If a single vertex is left then it is the center of the tree and if two vertices joined by
an edge is left then it is the bi-center of the tree.
71
Discrete Mathematics
Problem 1
Find out the center/bi-center of the following tree:
a b c d e
Tree T1
Solution
At first, we will remove all vertices of degree 1 and also remove their incident edges and
get the following tree:
b c d
Again, we will remove all vertices of degree 1 and also remove their incident edges and
get the following tree:
Finally we got a single vertex ‘c’ and we stop the algorithm. As there is single vertex,
this tree has one center ‘c’ and the tree is a central tree.
72
Discrete Mathematics
Problem 2
Find out the center/bi-center of the following tree:
e f
a b c d
A tree T2
Solution
At first, we will remove all vertices of degree 1 and also remove their incident edges and
get the following tree:
b c d e
Again, we will remove all vertices of degree 1 and also remove their incident edges and
get the following tree:
c d
Finally, we got two vertices ‘c’ and ‘d’ left, hence we stop the algorithm. As two vertices
joined by an edge is left, this tree has bi-center ‘cd’ and the tree is bi-central.
73
Discrete Mathematics
Labeled Trees
Definition: A labeled tree is a tree the vertices of which are assigned unique numbers
from 1 to n. We can count such trees for small values of n by hand so as to conjecture a
n-2
general formula. The number of labeled trees of n number of vertices is n . Two
labelled trees are isomorphic if their graphs are isomorphic and the corresponding points
of the two trees have the same labels.
Example
1 2
1 2 3 1 3 2
2 1 3
Unlabeled trees
Definition: An unlabeled tree is a tree the vertices of which are not assigned any
numbers. The number of labeled trees of n number of vertices is (2n)! / (n+1)!n!
Example
74
Discrete Mathematics
Rooted Tree
A rooted tree G is a connected acyclic graph with a special node that is called the root of
the tree and every edge directly or indirectly originates from the root. An ordered rooted
tree is a rooted tree where the children of each internal vertex are ordered. If every
internal vertex of a rooted tree has not more than m children, it is called an m-ary tree.
If every internal vertex of a rooted tree has exactly m children, it is called a full m-ary
tree. If m = 2, the rooted tree is called a binary tree.
75
Discrete Mathematics
Root Node
Internal Internal
Node Internal Node
Node
A Rooted Tree
So, the value of all the vertices of the left sub-tree of an internal node V are less than or
equal to V and the value of all the vertices of the right sub-tree of the internal node V
are greater than or equal to V. The number of links from the root node to the deepest
node is the height of the Binary Search Tree.
Example
50
40 70
30 45 60
76
Discrete Mathematics
77
17. SPANNING TREES Discrete Mathematics
A spanning tree of a connected undirected graph G is a tree that minimally includes all of
the vertices of G. A graph may have many spanning trees.
Example
b c d
A Graph G
b c d
78
Discrete Mathematics
Example
f
f
1 7
2 1
2
5 3
b c d 3
b c d
1 14
9
4
e e
Kruskal's Algorithm
Kruskal's algorithm is a greedy algorithm that finds a minimum spanning tree for a
connected weighted graph. It finds a tree of that graph which includes every vertex and
the total weight of all the edges in the tree is less than or equal to every possible
spanning tree.
Algorithm
Step 1: Arrange all the edges of the given graph G (V,E) in non-decreasing order as per
their edge weight.
Step 2: Choose the smallest weighted edge from the graph and check if it forms a cycle
with the spanning tree formed so far.
Step 3: If there is no cycle, include this edge to the spanning tree else discard it.
Step 4: Repeat Step 2 and Step 3 until (V-1) number of edges are left in the spanning
tree.
79
Discrete Mathematics
Problem
Suppose we want to find minimum spanning tree for the following graph G using
Kruskal’s algorithm.
20 13
9
1 2
b e d
4 3
5
14
f
Weighted Graph G
Solution
From the above graph we construct the following table:
80
Discrete Mathematics
Now we will rearrange the table in ascending order with respect to Edge weight:
a
a
c
c
1
b e d
b e d
f f
81
Discrete Mathematics
a a
c c
1 1
2 2
3
b e d b e d
f f
a a
c c
1 1
2 2
3 3
b e d b e d
5 5
f f
Since we got all the 5 edges in the last figure, we stop the algorithm and this is the
minimal spanning tree and its total weight is (1+2+3+5+9) = 20.
Prim's Algorithm
Prim's algorithm, discovered in 1930 by mathematicians, Vojtech Jarnik and Robert C.
Prim, is a greedy algorithm that finds a minimum spanning tree for a connected
weighted graph. It finds a tree of that graph which includes every vertex and the total
weight of all the edges in the tree is less than or equal to every possible spanning tree.
Prim’s algorithm is faster on dense graphs.
82
Discrete Mathematics
Algorithm
1. Create a vertex set V that keeps track of vertices already included in MST.
2. Assign a key value to all vertices in the graph. Initialize all key values as infinite.
Assign key value as 0 for the first vertex so that it is picked first.
3. Pick a vertex ‘x’ that has minimum key value and is not in V.
6. Repeat step 3 to step 5 until the vertex set V includes all the vertices of the graph.
Problem
Suppose we want to find minimum spanning tree for the following graph G using Prim’s
algorithm.
20 13
9
1 2
b e d
4 3
5
14
f
Weighted Graph G
83
Discrete Mathematics
Solution
Here we start with the vertex ‘a’ and proceed.
a a
c c
b e d b e d
f f
a
a
9
9
c
c
1
b e d
b e d
f
f
84
Discrete Mathematics
a a
9 9
c c
1 1
2
2
b e D b e d
3
f f
c
1 2
b e d
3
This is the minimal spanning tree and its total weight is (1+2+3+5+9) = 20.
85