You are on page 1of 66

CONTENTS

Unit Contents Page No.

1 Fundamental Concepts 1-11

2 Connectivity 12-22

3 Algorithms 23-35

4 Coloring of Graphs and planarity 36-41

5 Trees and Distance 42-57

6 Matchings 58-62

“Only for Private Circulation”


“Only for Private Circulation”
Graph Theory

UNIT - I
FUNDAMENTAL CONCEPTS
NOTES

Definition of Graph
A graph is an ordered pair G = (V, E) comprising
• V a set of vertices (also called nodes or points);
• E ⊆ {{x, y} | (x, y) ∈ V2 ∧ x ≠ y} a set of edges (also called links or
lines), which are unordered pairs of vertices (i.e., an edge is associated
with two distinct vertices).
V and E are usually taken to be finite, and many of the well-known results
are not true (or are rather different) for infinite graphs because many of the
arguments fail in the infinite case. Moreover, V is often assumed to be non-empty,
but E is allowed to be the empty set.
The order of a graph is |V|, its number of vertices.
The size of a graph is |E|, its number of edges. The degree or valency of a
vertex is the number of edges that are incident to it, where a loop is counted
twice.
Example 1: The graph G=(3,2) can be drawn as

Example 2: The graph G=(5,5) is

Fundamental
Concepts 1

“Only for Private Circulation”


Graph Theory Graph Models
Graphs are used in a wide variety of models. There are various graph models
which can be used in various fields. Some of them are listed below.
NOTES
1. Niche Overlap Graphs in Ecology
Such graphs are used in many models involving the interaction of
different species of animals. The competition between species in an
ecosystem is modeled by using Niche Overlap graph. Each species is
represented by a vertex. An undirected edge is used between vertices
if the species compete for the same food. It is shown in the following
graph.

2. Influence Graph
When we want to show the influence of one person on another person,
we make use of influence graph. There is a directed edge from vertex
'a 'to vertex 'b' when the person represented by vertex 'a' influences the
person represented by vertex' b'. It is shown in the following graph.
Here Amit influences Aman and Sumit .Sumit influences Sajid. Sajid
influences Sumit and so on.

3. Round Robin Tournament graph


This model is useful in fixing the matches amongst the teams in such
Fundamental a way that each team plays exactly one match with the other team. An
2 Concepts

“Only for Private Circulation”


edge between vertices represent match and vertices represents the Graph Theory
team. It is shown in the following graph.
Here team A will play with teams B,C,D,E,F . Team B will play with team
C,D,E,F. Team C will play with team D,E,F. Team D will play with team E and NOTES
F. Team E will play with team F.

Sub graphs
A subgraph S of a graph G is a graph whose set of vertices and set of edges
are all subsets of G.
A Subgraph S of a graph G is a graph whose vertex set V(S) is a subset of
the vertex set V(G), that is V(S)⊆V(G), and whose edge set E(S) is a subset of
the edge set E(G), that is E(S)⊆E(G).
Example: The following graph S is a subgraph of G:

Decomposition
Graph decomposition problems rank among the most prominent areas of
research in graph theory and combinatorics and further it has numerous
applications in various fields such as networking, block designs, and
bioinformatics.
A decomposition of a graph G is a collection of edge-disjoint subgraphs
of such that every edge of G belongs to exactly one . Various types of path
decompositions and corresponding parameters have been studied by several
authors by imposing conditions on the paths in the decomposition. It is obvious
that every graph admits a decomposition in which each subgraph is either a path
Fundamental
or a cycle.
Concepts 3

“Only for Private Circulation”


Graph Theory A good number of research articles have been published in which Gallai's
is the focus of study and still this conjecture remains unsettled for more than 30
years. Towards a proof of the conjecture, Lovasz made the first significant
NOTES contribution by proving the following theorem.

Special Graphs
There are some special graphs which are listed below.
1. Complete graph
The complete graph, represented as Kn is the simple graph that
contains exactly one edge between each pair of distinct vertices n. For
example

2. Cycles
The cycle ,represented as Cn with n vertices 1,2,….,n and edges {1,2}
,{2,3} ………... {n-1,n} and {n+1} is a complete graph. For example

3. Wheels
We obtain the wheel Wn when we add an additional vertex to the cycle
Cn , for n≥3 and connect this new vertex to each of the n vertices in
Cn , by new edges.For example

Fundamental
4 Concepts

“Only for Private Circulation”


Connection in Graphs Graph Theory
An undirected graph is connected when it has at least one vertex and there
is a path between every pair of vertices. Equivalently, a graph is connected when
it has exactly one connected component. In a connected graph, there are no NOTES
unreachable vertices. An undirected graph that is not connected is called
disconnected.
An undirected graph G is therefore disconnected if there exist two vertices
in G such that no path in G has these vertices as endpoints.
A graph with just one vertex is connected. An edgeless graph with two or
more vertices is disconnected.
Example : Following graph is called as connected graph.

If an edge ef is removed from the above graph then it is called as


disconnected graph.

Bipartite Graph
A simple graph G is called bipartite if its vertex set 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 in V2 so that no edge in G connects either two vertices
in V1 or two vertices in V2 .When this condition holds , we call the pair (V1,
V2) a bipartition of the vertex set V of G.
Example : The following graph is bipartite as its 6 vertices can be
partitioned into two disjoint sets V1 ={a,c,e} and set V2 ={b,d,f} in such a way
that every edge of the given graph connects a vertex in V1 and a vertex V2.

Degree of a vertex
The degree of a vertex is a numerical value which indicates the number of
edges associated with that vertex. It is represented as deg(vi).

Fundamental
Concepts 5

“Only for Private Circulation”


Graph Theory Example : In the following graph , the degree of each vertex is given.

NOTES
deg(a) =3, deg(b)=2, deg(d) =2, deg(c)= 1
In case of directed graph , we have two types of degree of the vertex. They
are :
a) In-degree
The number of edges which are entering into the vertex is called as in-
degree of a vertex .It is denoted by deg-(v).

b) Out-degree
The number of edges which are emerging out from the vertex is called
as out-degree of a vertex .It is denoted by deg+(v).
Example :Find in degree and out degree of each of the following diagraph.

deg-(a) = 0, deg-(b) = 1, deg-(c) = 2, deg-(d) = 1


deg+(a) =3, deg+(b) =1, deg+(c) =0, deg+(d) =0

Directed Graph
A directed graph is graph, i.e., a set of objects (called vertices or nodes) that
are connected together, where all the edges are directed from one vertex to
another. A directed graph is sometimes called a digraph or a directed network.
Example : Following is a directed graph.

Undirected Graph : A graph in which the edges are not having direction .
Fundamental
6 Concepts

“Only for Private Circulation”


Example: The following graph is undirected. Graph Theory

NOTES

Weighted graph
A weighted graph or a network is a graph in which a non negative number
(the weight) is assigned to each edge. Such weights might represent for example
costs, lengths or capacities, depending on the problem at hand. Such graphs arise
in many contexts, for example in shortest path problems such as the traveling
salesman problem.
Example :

Here , a ,b,c,d,e and f are various place and the distance between them, is a
numerical value called as weights.Vertices and edges can be defined according
to application.

Regular Graph
In graph theory, a regular graph is a graph where each vertex has the same
number of neighbors; i.e. every vertex has the same degree or valency. A regular
directed graph must also satisfy the stronger condition that the in degree and out
degree of each vertex are equal to each other.
Example :The following graph is a regular graph as each vertex is having
same degree i.e. 2

Fundamental
Concepts 7

“Only for Private Circulation”


Graph Theory Dual graph
The dual graph of a plane graph G is a graph that has a vertex for each face
of G. The dual graph has an edge whenever two faces of G are separated from
NOTES each other by an edge, and a self-loop when the same face appears on both sides
of an edge.
When graph is used to represent the map, we make use of dual graph. Each
map in the plane can be represented by a graph. To set up this correspondence,
each region of the map is represented by a vertex .Edges connect two vertices if
the regions represented by these vertices have a common border. Two regions
that touch at only one point are not considered adjacent. The resulting graph is
called the dual graph of the map.
Example : Suppose we have given a map like

Its dual graph is

Representing Graph in computer memory


Graphs can be represented in computers by using following methods.

Incidence Matrix Method

Let G=(V,E) be an undirected graph.Suppose that 1,2,3,…..n are the vertices


and e1 , e2 ,….em are edges of G.Then the incidence matrix with respect to this
Fundamental ordering of V and E is the n x m matrix =[mij] where
8 Concepts

“Only for Private Circulation”


Incidence matrices can also used to represent multiple edges and loops. Graph Theory
Multiple edges are represented in the incidence matrix using columns with
identical entries, because these edges are incident with the same pair of vertices.
Loops are represented using a column with exactly one entry equal to 1, NOTES
corresponding to the vertex that is incident with this loop.
Example: Represent the following graph by using incidence matrix.

Solution:

b. Adjacency Matrix Method


Suppose G is a graph with m vertices and suppose the vertices have been
ordered, say v1,v2,…vm. Then adjacency matrix A =[aij] of the graph G is the
m x m matrix, defined by

Example 1: Represent the following graph by using adjacency matrix

Solution : Here there are four vertices. Hence the matrix is of 4 x 4 order.

Fundamental
Concepts 9
“Only for Private Circula-
Graph Theory Example 2: Represent following graph by using adjacency matrix.

NOTES

Solution :There are four vertices.The matrix is of the size 4x4

Exercises
Q1. Draw the following graph
a) G(6,3) b) G(7,3)

Q2. Represnt the following graph in matrix form

a) b)

Q3. Represent the following matrix in the form of graph.

a)

b)

Fundamental
10 Concepts

“Only for Private Circulation”


Q4. Find in degree and out degree of each vertices of the following graph. Graph Theory
a)

NOTES

b)

*****

Fundamental
Concepts 11

“Only for Private Circulation”


Graph Theory

UNIT - II
CONNECTIVITY
NOTES

Walk
A walk is a sequence of vertices and edges of a graph i.e. if we traverse a
graph then we get a walk. Vertex can be repeated. Edges can be repeated.

Paths
A path is a trail in which all vertices (except possibly the first and last) are
distinct. A trail is a walk in which all edges are distinct. A walk of length in a
graph is an alternating sequence of vertices and edges, , which begins and ends
with vertices.

Trail
A walk in which no edge is repeated then we get a trail Vertex can be
repeated .Edges not repeated

Circuits
A circuit is a path which ends at the vertex it begins (so a loop is an circuit
of length one) complete graph. A complete graph with n vertices (denoted Kn) is
a graph with n vertices in which each vertex is connected to each of the others
(with one edge between each pair of vertices).

Connected Graph
A graph is said to be connected if there is a path between every pair of
vertex. From every vertex to any other vertex, there should be some path to
traverse. That is called the connectivity of a graph. .
Example 1
In the following graph, it is possible to travel from one vertex to any other
vertex. For example, one can traverse from vertex ‘a’ to vertex ‘e’ using the path
‘a-b-e’.

12 Connectivity
“Only for Private Circula-
Example 2 Graph Theory
In the following example, traversing from vertex ‘a’ to vertex ‘f’ is not
possible because there is no path between them directly or indirectly. Hence it is
a disconnected graph. NOTES

Bridge
An edge in an undirected connected graph is a bridge iff removing it
disconnects the graph. For a disconnected undirected graph, definition is similar,
a bridge is an edge removing which increases number of disconnected
components.
Like Articulation Points, bridges represent vulnerabilities in a connected
network and are useful for designing reliable networks. For example, in a wired
computer network, an articulation point indicates the critical computers and a
bridge indicates the critical wires or connections.
Example : Following is an example of a graph with bridge.

Here an edge (b,d) and edge(b,c) are bridges because when we remove them
then the graph gets disconnected.

Isomorphic Graphs
Two graphs which contain the same number of graph vertices connected in
the same way are said to be isomorphic. Formally, two graphs and with graph
Connectivity 13

“Only for Private Circulation”


Graph Theory vertices are said to be isomorphic if there is a permutation of such that is in the
set of graph edges iff is in the set of graph edges ,
In words, isomorphic graphs must have the same number of vertices and
NOTES edges. It is important to note that just having v1 = v2 and e1 = e2 is NOT a
guarantee thattwo graphs will be isomorphic. The degree of a vertex is the
number of edges attached to that vertex.
An isomorphism of graphs G and H is a bijection between the vertex sets
of G and H such that any two vertices u and v of G are adjacent in G if and only
if f(u) and f(v) are adjacent in H. This kind of bijection is commonly described
as "edge-preserving bijection", in accordance with the general notion of
isomorphism being a structure-preserving bijection. In the case when the bijection
is a mapping of a graph onto itself, i.e., when G and H are one and the same
graph, the bijection is called an automorphism of G.
Graph isomorphism is an equivalence relation on graphs and as such it
partitions the class of all graphs into equivalence classes. A set of graphs
isomorphic to each other is called an isomorphism class of graphs.
Example : State whether the following graphs are isomorphic ?

Solution :
1. Both the graphs have each 5 vertices.
2. The degree of each vertex of graph G is 2 and the degree of each vertex
of graph H is also
3. The degree of adjacent vertices of each vertex of graph G is same as
that of degree of adjacent vertices of graph H.
Hence both graphs are isomorphic

Eulerian Graph
An Eulerian trail (or Eulerian path) is a trail in a finite graph which visits
every edge exactly once. Similarly, an Eulerian circuit or Eulerian cycle is an
Eulerian trail which starts and ends on the same vertex. They were first discussed
by Leonhard Euler while solving the famous Seven Bridges of Königsberg
problem in 1736.
Euler proved that a necessary condition for the existence of Eulerian circuits
is that all vertices in the graph have an even degree, and stated without proof that
14 Connectivity connected graphs with all vertices of even degree have an Eulerian circuit.

“Only for Private Circulation”


The term Eulerian graph has two common meanings in graph theory. One Graph Theory
meaning is a graph with an Eulerian circuit, and the other is a graph with every
vertex of even degree. These definitions coincide for connected graphs.
NOTES
Definition
An Euler circuit in a graph G is a simple circuit containing every edge of .
An Euler path in G is a simple path containing every edge of G
Example : State whether following graph has Euler path ?

Solution: It has Euler path c-a-b-d-c-b.

Hamilton Graph
A Hamiltonian path (or traceable path) is a path in an undirected or directed
graph that visits each vertex exactly once. A Hamiltonian cycle (or Hamiltonian
circuit) is a Hamiltonian path that is a cycle.
Hamiltonian paths and cycles are named after William Rowan Hamilton
who invented the icosian game, now also known as Hamilton's puzzle, which
involves finding a Hamiltonian cycle in the edge graph of the dodecahedron.

Definition
A simple path in a graph G that passes through every vertex exactly once is
called a Hamilton path.
A simple circuit in a graph G that passes through every vertex exactly once
is called a Hamilton circuit.
Example : State whether the following graph has Hamilton path?

Solution : It has Hamilton path. The path is a,b,c,d,e

Connectivity 15

“Only for Private Circulation”


Graph Theory Bridges of Konigsberg problem
The Seven Bridges of Konigsberg is a historically notable problem in
mathematics. Its negative resolution by Leonhard Eulerin 1736 laid the
NOTES foundations of graph theory and prefigured the idea of topology.
The city of Konigsberg in Prussia (now Kaliningrad, Russia) was set on
both sides of the Pregel River, and included two large islands - Kneiphof and
Lomse - which were connected to each other, or to the two mainland portions of
the city, by seven bridges. The problem was to devise a walk through the city
that would cross each of those bridges once and only once.
By way of specifying the logical task unambiguously, solutions involving
either reaching an island or mainland bank other than via one of the bridges, or
accessing any bridge without crossing to its other end are explicitly unacceptable

Statement of Kongsberg Problem


There are four regions A,B,C and D which are covered by rivers. People
can move from one region to another region by using bridges 1,2,3,4,5,6 and 7.
The problem is to visit to each of the region by crossing each bridge only once.
Solution : Euler proved that the problem has no solution. The difficulty he
faced was the development of a suitable technique of analysis, and of subsequent
tests that established this assertion with mathematical rigor.
Euler pointed out that the choice of route inside each land mass is irrelevant.
The only important feature of a route is the sequence of bridges crossed. This
allowed him to reformulate the problem in abstract terms, eliminating all features
except the list of land masses and the bridges connecting them. In modern terms,
one replaces each land mass with an abstract "vertex" or node, and each bridge
with an abstract connection, an "edge", which only serves to record which pair
of vertices is connected by that bridge. The resulting mathematical structure is
called a graph.

16 Connectivity

“Only for Private Circulation”


Since only the connection information is relevant, the shape of pictorial Graph Theory
representations of a graph may be distorted in any way, without changing the
graph itself. Only the existence (or absence) of an edge between each pair of
nodes is significant. For example, it does not matter whether the edges drawn NOTES
are straight or curved, or whether one node is to the left or right of another.
Next, Euler observed that (except at the endpoints of the walk), whenever
one enters a vertex by a bridge, one leaves the vertex by a bridge. In other words,
during any walk in the graph, the number of times one enters a non-terminal
vertex equals the number of times one leaves it. Now, if every bridge has been
traversed exactly once, it follows that, for each land mass (except for the ones
chosen for the start and finish), the number of bridges touching that land mass
must be even (half of them, in the particular traversal, will be traversed "toward"
the landmass; the other half, "away" from it). However, all four of the land masses
in the original problem are touched by an odd number of bridges (one is touched
by 5 bridges, and each of the other three is touched by 3). Since, at most, two
land masses can serve as the endpoints of a walk, the proposition of a walk
traversing each bridge once leads to a contradiction.
Euler shows that the possibility of a walk through a graph, traversing each
edge exactly once, depends on the degrees of the nodes. The degree of a node is
the number of edges touching it. Euler's argument shows that a necessary
condition for the walk of the desired form is that the graph be connected and
have exactly zero or two nodes of odd degree. This condition turns out also to be
sufficient—a result stated by Euler and later proved by Carl Hierholzer. Such a
walk is now called an Eulerian path or Euler walk in his honor. Further, if there
are nodes of odd degree, then any Eulerian path will start at one of them and end
at the other. Since the graph corresponding to historical Königsberg has four
nodes of odd degree, it cannot have an Eulerian path.
An alternative form of the problem asks for a path that traverses all bridges
and also has the same starting and ending point. Such a walk is called an Eulerian
circuit or an Euler tour. Such a circuit exists if, and only if, the graph is connected,
and there are no nodes of odd degree at all. All Eulerian circuits are also Eulerian
paths, but not all Eulerian paths are Eulerian circuits.

Shortest path problems

City route
This problem requires the shortest possible route such that a person visits
each city exactly once and then returns to the origin point. It is one of the most
studied problems in optimization even though it is computationally quite large.

Puzzle problem
There are four cubes such that the six faces of each cube is variously colored
with either of the four colors (BLUE, GREEN, RED and WHITE). The
distribution of colors on each cube is unique.
Connectivity 17

“Only for Private Circulation”


Graph Theory

NOTES

The objective of the puzzle is to stack these cubes in a column so that each
side (front, back, left, and right) of the stack shows each of the four colors.

Solution
An exhaustive search for a possible solution would be almost impossible.
If we still try to systematically test all possible arrangements, we will end up
having 3 * 24 * 24 * 24 = 41472 unique cases to be tested.
Hence, we need to find a better approach to this and almost all such puzzles
can be solved using some knowledge from the graph theory. Considering the
above cubes we have to understand the following:
If the cubes are stacked one above the other, no two faces on one side must
have the same color. And there are four such sides to it. Let us name the sides as
LEFT, RIGHT, FRONT and BACK.
Here is the solution to the cubes showed above

Stepwise solution
Let us draw a graph with four vertices, each representing one of the colors.
Let there be an edge between two vertices (v1 and v2) if the opposite
faces of the cube have colors represented by v1 and v2.
Here is the image of the four cubes just for convenience:

We will name the vertices as R, G, W and B. The edges will be named as 1,


2, 3 and 4 depending on which cube they come from.
Keeping these two points in mind, we will have the following
Three edges labeled (1) can be drawn between vertices {B,W}, {R, R}, {G,
18 Connectivity R}. These edges come from the first cube.

“Only for Private Circulation”


Similarly three edges labeled (2) can be drawn between vertices {W, G}, Graph Theory
{G, R}, {W, B}. These edges come from the second cube.
Similarly three edges labeled (3) can be drawn between vertices {R, W},
{B, R}, {W, G}. These edges come from the third cube. NOTES

Similarly three edges labeled (4) can be drawn between vertices {W, B},
{G, G}, {R, B}. These edges come from the fourth cube.

The corresponding graph is shown below

This was the toughest part of the solution. Now it is just simple extractions.
Let us break down the problem and solve it in pieces. As per the expected
solution, we need 16 faces or two sets of eight opposite faces (front-back) and
(left-right) of the four cubes.
Also, consider one set (left-right) i.e. eight opposite faces at once. Which
means, we can probably think of it as one sub graph of this graph. Similarly the
(front-back) can be represented by another sub graph.
What should be the restrictions on these sub graph?
Both these sub graph cannot have the same edge. This is because an edge
represent the opposite faces of a cube in left-right or front-back arrangement.
And, hence the same pair cannot be present in both the arrangements.
These sub graphs must have only four edges. This is because we are only
concerned one pair of face from each cube.
Each vertex of the sub graph must have degree 2. This is because, a degree
two means that a vertex or color can be used at max in two cubes (one at the
front face and other at the back) If it has a degree more than two, then there is a
possibility of a particular color being repeated on either of the sides.
With these restrictions, it is very clear that we cannot have the self loops in
any of the sub graphs because the moment we have one self loop it will force
one color to be repeated more than once on one of the sides.

Connectivity 19

“Only for Private Circulation”


Graph Theory After removal of the self loops, if there are only two edges incident on any
vertices, those two edges will be retained in either of the sub graphs. And rest all
can be done through eliminations:
NOTES
Seating arrangement problem
Statement : “Twenty members of a club meet each evening for dinner at a
round table. They plan to sit such that every member has different neighbors
every evening. Find out the number of days for which this arrangement can last.”
Many a times we have been given problems like above and it is not really
easy to solve them if we are not equipped with the Graph Theory.

Consider the seating arrangement case


Without loss of generality, let us assume that the dining table is round and
the total number of members is 7 (numbered from 1 to 7)

Let us analyze the above images one by one.


Day 1 They can choose to sit in any possible arrangement.
Day 2 has to be an edge disjoint arrangement w.r.t Day 1.
Day 3 has to be an edge disjoint arrangement w.r.t Day 1 and Day 2.
In each subsequent arrangement we are increasing the length of the edge
by 1. Now if you observe it closely, you can very well understand that we can
increase it to a certain limit (N-1)/2 .
Basically, after (N-1)/2 the edge lengths start decreasing.
In Day 1, the length of each edge (number of vertices between the
endpoints) is 0. In Day 2, the length of each edge is 1.In Day 3 the length of each
edge is 2 If we try and draw Day 4, the first edge would be between vertices 1
and 6 and the length would be 2 which is different from what we expect i.e. 3.
Hence, we cannot draw the fourth Day arrangement.

Travelling salesman problem


The Traveling Salesman Problem is one of the most intensively studied
problems in computational mathematics. These pages are devoted to the history,
applications, and current research of this challenge of finding the shortest route
visiting each member of a collection of locations and returning to your starting
20 Connectivity point.

“Only for Private Circulation”


Traveling salesman problem, an optimization problem in graph theory in Graph Theory
which the nodes (cities) of a graph are connected by directed edges (routes),
where the weight of an edge indicates the distance between two cities.
The problem was first formulated in 1930 and is one of the most intensively NOTES
studied problems in optimization. It is used as a benchmark for many
optimization methods. Even though the problem is computationally difficult,
many heuristics and exact algorithms are known, so that some instances with
tens of thousands of cities can be solved completely and even problems with
millions of cities can be approximated within a small fraction of 1%.
The TSP has several applications even in its purest formulation, such as
planning, logistics, and the manufacture of microchips. Slightly modified, it
appears as a sub-problem in many areas, such as DNA sequencing. In these
applications, the concept city represents, for example, customers, soldering
points, or DNA fragments, and the concept distance represents travelling times
or cost, or a similarity measure between DNA fragments. The TSP also appears
in astronomy, as astronomers observing many sources will want to minimize the
time spent moving the telescope between the sources. In many applications,
additional constraints such as limited resources or time windows may be
imposed.
Example: Suppose a salesman is at place ‘a’ and he wishes to visit all other
places b,c,d,e and then finally returns to place original place ‘a’.The
distances(weights) amongst all these places are given in the form of weights as
shown in the following graph.

He has total 4! ways to visit each of the places ,starting from ‘a’ and again
reaching back to ‘a’.
The ways are :abcdea,abceda,abdcea, …..and so on.
We get 24 total ways.
Find the total distance of each of the 24 ways and then select the path with
minimum total weight.

Connectivity 21

“Only for Private Circulation”


Graph Theory Exercise
Q1. State whether each of the following graphs has Hamilton path

NOTES

Q2. State whether the following graph has Euler path and or Euler Circuit.

*****

22 Connectivity

“Only for Private Circulation”


Graph Theory

UNIT - III
ALGORITHMS
NOTES

Fleury’s algorithm
Fleury’s Algorithm is used to display the Euler path or Euler circuit from a
given graph. In this algorithm, starting from one edge, it tries to move other
adjacent vertices by removing the previous vertices. Using this trick, the graph
becomes simpler in each step to find the Euler path or circuit.
We have to check some rules to get the path or circuit −
• The graph must be a Euler Graph.
• When there are two edges, one is bridge, another one is non-bridge, we
have to choose non-bridge at first.
Choosing of starting vertex is also tricky, we cannot use any vertex as
starting vertex, if the graph has no odd degree vertices, we can choose any vertex
as start point, otherwise when one vertex has odd degree, we have to choose that
one first.

Shortest Path Algorithm


The shortest path problem is the problem of finding a path between two
vertices (or nodes) in a graph such that the sum of the weights of its constituent
edges is minimized.
The problem of finding the shortest path between two intersections on a
road map may be modeled as a special case of the shortest path problem in
graphs, where the vertices correspond to intersections and the edges correspond
to road segments, each weighted by the length of the segment.

1. Warshall’s Algorithm
Floyd–Warshall algorithm is an algorithm for finding shortest paths in a
weighted graph with positive or negative edge weights . Floyd–Warshall's
Algorithm is used to find the shortest paths between all pairs of vertices in a
graph, where each edge in the graph has a weight which is positive or negative.
The biggest advantage of using this algorithm is that all the shortest distances
between any 2 vertices could be calculated in O(V3), where V is the number of
vertices in a graph.

Algorithms 23

“Only for Private Circulation”


Graph Theory Algorithm
For a graph with N vertices
• Initialize the shortest paths between any 2 vertices with Infinity.
NOTES
• Find all pair shortest paths that use 0 intermediate vertices, then find the
shortest paths that use 1intermediate vertex and so on.. until using all N
vertices as intermediate nodes.
• Minimize the shortest paths between any 2 pairs in the previous
operation.
• For any 2 vertices (i,j) , one should actually minimize the distances
between this pair using the first Knodes, so the shortest path will be:
min(dist[i][k]+dist[k][j],dist[i][j]).
dist[i][k] represents the shortest path that only uses the first K vertices,
dist[k][j] represents the shortest path between the pair k,j. As the shortest path
will be a concatenation of the shortest path from i to k, then from k to j.
Example: Find the shortest path from each vertex to each of other vertex if
path exists by using Warshall’s algorithm in the following graph.

Solution: Warshall’s algorithm is applied in the following steps.


Step 1: Write the matrix W0 which is the initial matrix. In this matrix, we
write path length from each vertex to each of the vertex directly.

Step 2: Write matrix W1 by finding the path length from each vertex to
each of the other vertex through or via first vertex R.

24 Algorithms

“Only for Private Circulation”


Step 3: Graph Theory
Write matrix W2by finding the path length from each vertex to each of the
other vertex via vertex R or vertex S or both vertex R and S.
NOTES

Step 4: Write matrix W3 by finding the path length from each vertex to
each of the other vertex via vertex R or Vertex S or vertex T or vertices R and S
or vertices R and T or Vertices S and T or via vertices R,S and T.

Step 5: Write matrix W4 by finding the path length from each vertex to
each of the other vertex via vertex R or Vertex S or vertex T or vertex U or
vertices R and S or vertices R and T or Vertices S and T or via vertices R,S and
T so on.

This is the shortest path matrix by using Warshall’s algorithm. For example
, the shortest path from T to U is TSU and its path length is 5.The shortest path
from S to S is SURS and its length is 11.

Dijkstra’s Algorithm
Dijkstra's algorithm has many variants but the most common one is to find
the shortest paths from the source vertex to all other vertices in the graph.

Algorithm Steps
Set all vertices distances = infinity except for the source vertex, set the
source distance = 0.
Push the source vertex in a min-priority queue in the form (distance ,
vertex), as the comparison in the min-priority queue will be according to vertices
distances.
Pop the vertex with the minimum distance from the priority queue (at first
the popped vertex = source). Algorithms 25

“Only for Private Circulation”


Graph Theory Update the distances of the connected vertices to the popped vertex in case
of "current vertex distance + edge weight < next vertex distance", then push the
vertex with the new distance to the priority queue.
NOTES If the popped vertex is visited before, just continue without using it.
Apply the same algorithm again until the priority queue is empty.
Example: Find shortest path from vertex a to vertex z by using Dijkstra's
algorithm from the following graph.

Solution
Step 1 : Select starting vertex a with 0 length of shortest path from vertex
a and assign ∞ to all other vertex .

Step 2: Add vertex a to the list to the shortest path and find the length of
the path of adjacent vertices to a from a. Assign ∞ to all other vertices. Make a
circle around the selected vertex.

26 Algorithms

“Only for Private Circulation”


Step 3 : Add next vertex to the list ,which is closer to vertex a. Here it is Graph Theory
vertex c. Vertex b is 8 units away.
Find the path length of other vertices which are adjacent to c.Here the
vertices are b,d and e which are adjacent to c. NOTES

Step 4 :Add next vertex b to the list as it is closest to c as compared d and


e. Calcualte the distance of the adjacent vertex to b . Here d is closer via added
vertex.

Step 5 :Add vertex d to the list as it is closest to added vertices.

Algorithms 27

“Only for Private Circulation”


Graph Theory Step 6: Add vertex e to the added vertices as it is closest as compared to
vertex z.

NOTES

Step 7: Add final vertex z which is closer from e as compared to vertex b.

Therefore the shortest path is a-c-b-d-e-z (shown with dark lines) and its
length is 26.

Depth-First Search
The DFS algorithm is a recursive algorithm that uses the idea of
backtracking. It involves exhaustive searches of all the nodes by going ahead, if
possible, else by backtracking.
Here, the word backtrack means that when you are moving forward and
there are no more nodes along the current path, you move backwards on the same
path to find nodes to traverse. All the nodes will be visited on the current path
till all the unvisited nodes have been traversed after which the next path will be
selected.
This recursive nature of DFS can be implemented using stacks. The basic
idea is as follows.
1. Pick a starting node and push all its adjacent nodes into a stack.
2. Pop a node from stack to select the next node to visit and push all its
adjacent nodes into a stack.
3. Repeat this process until the stack is empty. However, ensure that the
nodes that are visited are marked. This will prevent you from visiting
28 Algorithms

“Only for Private Circulation”


the same node more than once. If you do not mark the nodes that are Graph Theory
visited and you visit the same node more than once, you may end up
in an infinite loop.
NOTES
Consider the following tree

The following steps show how DFS works.


Step 1: Select root node. Here it is 1.

Step 2: Select next vertex which is at the left depth of vertex 1.Here it is
vertex 2.

1→2

Algorithms 29

“Only for Private Circulation”


Graph Theory Step 3: Select next vertex which is at the left depth of vertex 2. Here it is
vertex 4.

NOTES

1→2→4
Step 4 : Select next vertex which is at the left depth of vertex 2. Here it is
not available.Then select the vertex at right depth which is here vertex 5.

1→2→4→5
Step 5: Select next vertex which is at the left depth of vertex 5. Here it is
not available. Then select the vertex at right depth of its previous vertex which
is 2 .Here the vertices are already added. Therefore select previous to vertex 2.
Here it is vertex 1. On left depth, we have vertex 3.Selct it.

1→2→4→5→3

30 Algorithms

“Only for Private Circulation”


Breadth First search in Directed Graph Graph Theory
BFS is the most commonly used approach.
BFS is a traversing algorithm where you should start traversing from a
NOTES
selected node (source or starting node) and traverse the graph layer wise thus
exploring the neighbour nodes (nodes which are directly connected to source
node). You must then move towards the next-level neighbour nodes.
As the name BFS suggests, you are required to traverse the graph breadth
wise as follows:
First move horizontally and visit all the nodes of the current layer
Move to the next layer
Example :Consider the following diagram.

Solution :
Step 1 :

Algorithms 31

“Only for Private Circulation”


Graph Theory Step 2 :

NOTES

Step3

Step 4 :

32 Algorithms

“Only for Private Circulation”


The distance between the nodes in layer 1 is comparatively lesser than the Graph Theory
distance between the nodes in layer 2. Therefore, in BFS, you must traverse all
the nodes in layer 1 before you move to the nodes in layer 2.
NOTES
Traversing child nodes
A graph can contain cycles, which may bring you to the same node again
while traversing the graph. To avoid processing of same node again, use a boolean
array which marks the node after it is processed. While visiting the nodes in the
layer of a graph, store them in a manner such that you can traverse the
corresponding child nodes in a similar order.
In the earlier diagram, start traversing from 0 and visit its child nodes 1, 2,
and 3. Store them in the order in which they are visited. This will allow you to
visit the child nodes of 1 first (i.e. 4 and 5), then of 2 (i.e. 6 and 7), and then of
3 (i.e. 7) etc.
To make this process easy, use a queue to store the node and mark it as
'visited' until all its neighbours (vertices that are directly connected to it) are
marked. The queue follows the First In First Out (FIFO) queuing method, and
therefore, the neigbors of the node will be visited in the order in which they were
inserted in the node i.e. the node that was inserted first will be visited first, and
so on.
The traversing will start from the source node and push s in queue. s will
be marked as 'visited'.

First iteration
• s will be popped from the queue
• Neighbors of s i.e. 1 and 2 will be traversed
• 1 and 2, which have not been traversed earlier, are traversed. They will
be:
o Pushed in the queue
o 1 and 2 will be marked as visited

Second iteration
• 1 is popped from the queue
• Neighbors of 1 i.e. s and 3 are traversed
• s is ignored because it is marked as 'visited'
• 3, which has not been traversed earlier, is traversed. It is:
o Pushed in the queue
o Marked as visited

Algorithms 33

“Only for Private Circulation”


Graph Theory Third iteration
• 2 is popped from the queue
• Neighbors of 2 i.e. s, 3, and 4 are traversed
NOTES
• 3 and s are ignored because they are marked as 'visited'
• 4, which has not been traversed earlier, is traversed. It is:
o Pushed in the queue
o Marked as visited

Fourth iteration
• 3 is popped from the queue
• Neighbors of 3 i.e. 1, 2, and 5 are traversed
• 1 and 2 are ignored because they are marked as 'visited'
• 5, which has not been traversed earlier, is traversed. It is:
o Pushed in the queue
o Marked as visited

Fifth iteration
• 4 will be popped from the queue
• Neighbors of 4 i.e. 2 is traversed
• 2 is ignored because it is already marked as 'visited'

Sixth iteration
• 5 is popped from the queue
• Neighbors of 5 i.e. 3 is traversed
• 3 is ignored because it is already marked as 'visited'
The queue is empty and it comes out of the loop. All the nodes have been
traversed by using BFS.
If all the edges in a graph are of the same weight, then BFS can also be used
to find the minimum distance between the nodes in a graph.

34 Algorithms

“Only for Private Circulation”


Exercises Graph Theory
Q 1. Apply Warshall’s algorithm to find shortest distance of each vertex
from each of the other vertex from the following graphs.
NOTES

Q 2. Apply Dijikstra’s algorithm to find the shortest path from vertex a to


vertex z in each the following graphs.

*****

Algorithms 35

“Only for Private Circulation”


Graph Theory

UNIT - IV
NOTES
COLORING OF GRAPHS
AND PLANARITY

Vertex Coloring

Vertex coloring is an assignment of colors to the vertices of a graph 'G' such


that no two adjacent vertices have the same color. Simply put, no two vertices of
an edge should be of the same color.
Consider the following graph with 5 vertices.

In order to color the vertex, we randomly assign ,say color Red to vertex a.
Now vertex b and c are adjacent to vertex a and hence we can not assign same
color red to these two vertices.Supose we assign color Blue to vertex b and color
Yellow to vertex c.Now we can assign color Red to vertex d and e.

Chromatic number of a graph


A k-coloring of G is an assignment of k colors to the vertices of G in such
a way that adjacent vertices are assigned different colors. If G has a k-coloring,
then G is said to be k-coloring, then G is said to be k-colorable. The chromatic
number of G, denoted by ℵ(G), is the smallest number k for which is k-colorable.
In above example, minimum three colors are required to color all vertices
.Hence its chromatic number ℵ(G) is 3.

Graph with Large Chromatic Number


Graphs with large cliques have a high chromatic number, but the opposite
is not true.
From Brooks’s theorem, graphs with high chromatic number must have high
maximum degree. Another local property that leads to high chromatic number is
the presence of a large clique. But colorability is not an entirely local
phenomenon: A graph with high girth looks locally like a tree, because all cycles
Coloring of Graphs
are long, but its chromatic number need not be 2
36 and planarity

“Only for Private Circulation”


4 color theorem Graph Theory
Statement : The chromatic number of a planer graph is no greater than four
In graph-theoretic terms, the theorem states that for loopless planar , the
NOTES
chromatic number of its dual graph ℵ(G)≤ 4.
The intuitive statement of the four color theorem, i.e. "given any separation
of a plane into contiguous regions, the regions can be colored using at most four
colors so that no two adjacent regions have the same color", needs to be
interpreted appropriately to be correct.
First, regions are adjacent if they share a boundary segment; two regions
that share only isolated boundary points are not considered adjacent. Second,
bizarre regions, such as those with finite area but infinitely long perimeter, are
not allowed; maps with such regions can require more than four colors.

Applications of graph coloring


Graph coloring is useful in many aspects.Some of them are listed below.
1.Scheduling Final Exams:
The scheduling problem can be solved using graph model, with vertices
representing courses and with an edge between two vertices if there is a common
student in the courses they represent. Each time slot for final exam is represented
by a different color.A scheduling of the exams corresponds to a coloring of the
associated graph.
Assume that there are seven finals to be scheduled. Suppose the courses are
numbered 1 to 7. Assume that there the following courses for which we have
common students:
(1,2),(1,3),(1,4),(1,7),(2,3),(2,4),(2,5),(2,7),(3,4),(3,7),(4,5),(4,6),(5,6),(5,7),
(6,7)
We can draw the following graph from the given information.

Now we apply graph coloring and then we get the following color graph,

Coloring of Graphs
and planarity 37

“Only for Private Circulation”


Graph Theory

NOTES

From the graph, we can schedule the exam in four different time slots I to
IV as shown in the following table.

2. Frequency Assignment
Graph coloring can be also used for assigning frequencies to different
stations.
If television channels 2 through 13 are assigned to stations in certain place
so that no two stations within 150 miles can operate on the same channel ,can be
answered by applying graph coloring.Here we have to construct a graph by
assigning a vertex to each station in such a way that two vertices are connected
by an edge if they are located within 150 miles of each other .An assignment of
channels corresponds a coloring of the graph where each color represents a
different channel.

3. Index Registers
In efficient compilers ,the execution of loops is speeded up when frequently
used variables are stored temporarily in index registers in the central processing
unit, instead of in regular memory. In order to find the number of registers
required ,we can make use of graph coloring model.
To set up the model , let each vertex of a graph represent a variable in the
loop.There is an edge between two vertices if the variables they represent must
be stored in index registers at the same time during the execution of the
loop.Thus, the chromatic number of the graph gives the number of index registers
needed, because different registers must be assigned to variables when the
vertices representing these variables are adjacent in the graph.

Coloring of Graphs
38 and planarity

“Only for Private Circulation”


Planar Graph Graph Theory
A planar graph is a graph that can be embedded in the plane, i.e., it can be
drawn on the plane in such a way that its edges intersect only at their endpoints.
In other words, it can be drawn in such a way that no edges cross each other. NOTES
Example 1: State whether the following graph is a planar graph.

Solution : This graph is planar graph because we can draw the same graph
in the following way in which no edges are crossing.

Example 2: State whether the following graph is planar graph.

Solution : It is not a planar graph as we can not draw this graph without
crossing one pair of edges.

Here one edge ag can be drawn


Coloring of Graphs
without crossing any other edge
and planarity 39

“Only for Private Circulation”


Graph Theory Euler’s Formula
Euler's formula states that if a finite, connected, planar graph is drawn in
the plane without any edge intersections, and v is the number of vertices, e is the
NOTES number of edges and r is the number of faces (regions bounded by edges,
including the outer, infinitely large region), then
r=e-v+2
Example :In the following graph , we have v = 5, e = 6 and r = 3.

In general, if the property holds for all planar graphs of r faces, any change
to the graph that creates an additional face while keeping the graph planar would
keep v − e + r an invariant. Since the property holds for all graphs with r = 2, by
mathematical induction it holds for all cases.

Proof of Euler's formula


If the graph isn't a tree, then remove an edge which completes a cycle. This
lowers both e and f by one, leaving v − e + r constant. Repeat until the remaining
graph is a tree; trees have v = e + 1 and r = 1, yielding v − e + r = 2, i. e., the
Euler characteristic is 2.
In a finite, connected, simple, planar graph, any face (except possibly the
outer one) is bounded by at least three edges and every edge touches at most two
faces; using Euler's formula, one can then show that these graphs are sparse in
the sense that if v ≥ 3:
e≤ 3v-6

Homomorphism
A graph homomorphism is a mapping between two graphs that respects
their structure. More concretely, it is a function between the vertex sets of two
graphs that maps adjacent vertices to adjacent vertices.
Homomorphisms generalize various notions of graph colorings and allow
the expression of an important class of constraint satisfaction problems, such as
certain scheduling or frequency assignment problems. The fact that
homomorphisms can be composed leads to rich algebraic structures: a preorder
on graphs, a distributive lattice, and a category (one for undirected graphs and
one for directed graphsThe computational complexity of finding a
homomorphism between given graphs is prohibitive in general, but a lot is known
about special cases that are solvable in polynomial time. Boundaries between
Coloring of Graphs tractable and intractable cases have been an active area of research.
40 and planarity

“Only for Private Circulation”


A graph homomorphism from a graph G = (V(G), E(G)) to a graph H = Graph Theory
(V(H), E(H)), written f : G → H , is a function from V(G) to V(H) that maps
endpoints of each edge in G to endpoints of an edge in H. Formally, {u,v} ∈ E(G)
implies {f(u),f(v)} ∈ E(H), for all pairs of vertices u, v in V(G). NOTES
Example : Following graphs G and H are homomorphic as they can be
obtained from graph I by adding appropriate vertices.

Exercise Questions
Q 1. State whether the following graphs are planar .

Q 2. Suppose that a connected planar simple graph has 20 vertices, each of


degree 3.Into how many regions does a representation of this planar
graph split the plane?(Hint :Apply Euler’s formula)
Q 3. State whether G1 and G2 graphs are homomorphic.

*****

Coloring of Graphs
and planarity 41

“Only for Private Circulation”


Graph Theory

UNIT - V
TREES AND DISTANCE
NOTES

Concept of Tree
A tree is an undirected graph in which any two vertices are connected by
exactly one path, or equivalently a connected acyclic undirected graph.
Mathematically speaking trees are a special class of a graph. The
relationship of a trees to a graph is very important in solving many problems in
Maths and Computer Science .However, in computer science terms it is
sometimes convenient to think of certain trees as separate data structures. They
have they own variations of data structure They have many specialised
algorithms to traverse, search etc. The various kinds of data structures referred
to as trees in computer science have underlying graphs that are trees in graph
theory, although such data structures are generally rooted trees. A rooted tree may
be directed, called a directed rooted tree, either making all its edges point away
from the root in which case it is called an arborescence or out-tree or making all
its edges point towards the root in which case it is called an anti-arborescence or
in-tree.
A rooted tree itself has been defined by some authors as a directed graph.

Definition of tree
A tree is an undirected graph G that satisfies any of the following equivalent
conditions:
1. G is connected and acyclic (contains no cycles).
2. G is acyclic, and a simple cycle is formed if any edge is added to G.
3. G is connected, but would become disconnected if any single edge is
removed from G.
4. G is connected and the 3-vertex complete graph K3 is not a minor
of G.
5. Any two vertices in G can be connected by a unique simple path.

Properties of Trees
1. Every tree is a bipartite graph and a median graph. Every tree with
only countably many vertices is a planar graph.
2. Every connected graph G admits a spanning tree, which is a tree that
contains every vertex of G and whose edges are edges of G.
42 Trees and Distance

“Only for Private Circulation”


3. A graph is bipartite if and only if it contains no cycles of odd length. Graph Theory
Since a tree contains no cycles at all, it is bipartite.
4. Every connected graph with only countably many vertices admits a
normal spanning tree There exist connected graphs with uncountably NOTES
many vertices which do not admit a normal spanning tree
5. Every finite tree with n vertices, with n > 1, has at least two terminal
vertices (leaves). This minimal number of leaves is characteristic of
path graphs; the maximal number, n − 1, is attained only by star graphs.
The number of leaves is at least the maximal vertex degree.
6. For any three vertices in a tree, the three paths between them have
exactly one vertex in common.
7. Every tree has a center consisting of one vertex or two adjacent
vertices. The center is the middle vertex or middle two vertices in
every longest path. Similarly, every n-vertex tree has a centroid
consisting of one vertex or two adjacent vertices. In the first case
removal of the vertex splits the tree into subtrees of fewer than n/2
vertices. In the second case, removal of the edge between the two
centroidal vertices splits the tree into two subtrees of exactly n/2
vertices.

Application of Trees
There are varies areas in which trees can be used to solve some specific
problems.Following are some of the application areas of trees.
1. Trees in binary search
Searching for items in the list is one of the most important tasks that
arises in computer science.We can make use of trees in achieving the
primary goal of finding ordered items effectively.Binary search tree is
one of the best solution for this wherein each child of vertex is
designed as a right or left child.No vertex has more than one left or
right child and each vertex is labeled with a key.Also vertices are
assigned keys so that the key of a vertex is both larger than the keys
of all verticesin its left subtree and smaller than the keys of all vertices
in its right subtree.

2. Trees as Models
Rooted trees can be used to model problems in which a series of decision
leads to a solution. Decision tree is one of the example where in we can
model the given problem of decion making with multiple solutions.
Decision tree is a rooted tree in which each internal vertex corresponds
to a decision, with a subtree at these vertices for each possible outcome
of decision.The possible solutions of the problem correspond to the
paths to the leaves of this rooted tree.
Trees and Distance 43

“Only for Private Circulation”


Graph Theory Example: Assume that we want to buy a mobile hand set by selecting one
of its manufacture along with its features .The following details are available.

NOTES

We can represent the same by using decision tree in the following manner.

The strengths of decision tree methods are:


• Decision trees are able to generate understandable rules.
• Decision trees perform classification without requiring much
computation.
• Decision trees are able to handle both continuous and categorical
variables.
• Decision trees provide a clear indication of which fields are most
important for prediction or classification.

The weaknesses of decision tree methods


• Decision trees are less appropriate for estimation tasks where the goal is
to predict the value of a continuous attribute.
• Decision trees are prone to errors in classification problems with many
class and relatively small number of training examples.
• Decision tree can be computationally expensive to train. The process of
growing a decision tree is computationally expensive. At each node, each
candidate splitting field must be sorted before its best split can be found.
In some algorithms, combinations of fields are used and a search must
be made for optimal combining weights. Pruning algorithms can also be
expensive since many candidate sub-trees must be formed and compared.

Game Trees
A game tree is a directed graph whose nodes are positions in a game and
whose edges are moves.
The complete game tree for a game is the game tree starting at the initial
position and containing all possible moves from each position; the complete tree
44 Trees and Distance is the same tree as that obtained from the extensive-form game representation.

“Only for Private Circulation”


The following tree shows the first two levels, or plies, in the game tree for Graph Theory
tic-tac-toe. The rotations and reflections of positions are equivalent, so the first
player has three choices of move: in the center, at the edge, or in the corner. The
second player has two choices for the reply if the first player played in the center, NOTES
otherwise five choices. And so on.

The number of leaf nodes in the complete game tree is the number of
possible different ways the game can be played. For example, the game tree for
tic-tac-toe has 255,168 leaf nodes.
Game trees are important in artificial intelligence because one way to pick
the best move in a game is to search the game tree using any of numerous tree
search algorithms, combined with minimax like rules to prune the tree. The game
tree for tic-tac-toe is easily searchable, but the complete game trees for larger
games like chess are much too large to search. Instead, a chess-playing program
searches a partial game tree: typically as many plies from the current position as
it can search in the time available. Except for the case of "pathological" game
trees which seem to be quite rare in practice, increasing the search depth generally
improves the chance of picking the best move.
Two-person games can also be represented as and-or trees. For the first
player to win a game, there must exist a winning move for all moves of the
second player. This is represented in the and-or tree by using disjunction to
represent the first player's alternative moves and using conjunction to represent
all of the second player's moves.

Tree Traversal
Tree traversal , known as tree search, is a form of graph traversal and refers
to the process of visiting each node in a tree data structure, exactly once. Such
traversals are classified by the order in which the nodes are visited. The following
algorithms are described for a binary tree, but they may be generalized to other
trees as well.
Trees and Distance 45

“Only for Private Circulation”


Graph Theory Traversing a tree involves iterating over all nodes in some manner. Because
from a given node there is more than one possible next node , then, assuming
sequential computation , some nodes must be deferred and stored in some way
NOTES for later visiting. This is often done via a stack (LIFO) or queue (FIFO). As a
tree is a self-referential data structure, traversal can be defined by recursion or,
more suitably, co recursion, in a very natural and clear fashion; in these cases
the deferred nodes are stored implicitly in the call stack.

Infix notation of arithmetic expression


It is of the form X + Y
Operators are written in-between their operands. This is the usual way we
write expressions. An expression such as A * ( B + C ) / D is usually taken to
mean something like: "First add B and C together, then multiply the result by A,
then divide by D to give the final answer."
Infix notation needs extra information to make the order of evaluation of
the operators clear: rules built into the language about operator precedence and
associativity, and brackets ( ) to allow users to override these rules. For example,
the usual rules for associativity say that we perform operations from left to right,
so the multiplication by A is assumed to come before the division by D. Similarly,
the usual rules for precedence say that we perform multiplication and division
before we perform addition and subtraction.
Hence infix form is A *B + C/D
Example 1: Write the following expression in infix form
(x+y)/(x+3)
Solution: By removing parenthesis , we get the infix form which is like
x+y/x +3
Example 2:Write the following expression in infix form
(x+(y/x))+3
Solution: By removing parenthesis , we get the infix form which is like
x+y/x +3

Postfix notation of arithmetic expression


It is of the form X Y +.
Operators are written after their operands.Hence the infix expression A*B
+C/D can be written as ABC+*D/
The order of evaluation of operators is always left-to-right, and brackets
cannot be used to change this order. Because the "+" is to the left of the "*" in
the example above, the addition must be performed before the multiplication.

46 Trees and Distance

“Only for Private Circulation”


Operators act on values immediately to the left of them. For example, the Graph Theory
"+" above uses the "B" and "C". We can add (totally unnecessary) brackets to
make this explicit:
( (A (B C +) *) D /) NOTES

Thus, the "*" uses the two values immediately preceding: "A", and the result
of the addition. Similarly, the "/" uses the result of the multiplication and the "D".
Example :What is the post fix of the expression ((x+y)^2) +((x-4)/3) ?
Solution: The post fix form of the given expression is
xy+2^x4-3/+

Binary Trees and its Properties


A binary tree is a tree data structure in which each node has at most two
children, which are referred to as the left child and the right child.
A binary tree may thus be also called a bifurcating arborescence , a term
which appears in some very old programming books, before the modern
computer science terminology prevailed. It is also possible to interpret a binary
tree as an undirected, rather than a directed graph, in which case a binary tree is
an ordered, rooted tree. Some authors use rooted binary tree instead of binary
tree to emphasize the fact that the tree is rooted, but as defined above, a binary
tree is always rooted. A binary tree is a special case of an ordered K-ary tree,
where k is 2.
Example : A binary tree is represented in the following diagram

Here root node is vertex a and height of binary tree is 2


In computing, binary trees are used in two very different ways:
First, as a means of accessing nodes based on some value or label associated
with each node. Binary trees labelled this way are used to implement binary
search trees and binary heaps, and are used for efficient searching and sorting.
The designation of non-root nodes as left or right child even when there is only
one child present matters in some of these applications, in particular it is
significant in binary search trees. However, the arrangement of particular nodes
into the tree is not part of the conceptual information. For example, in a normal
binary search tree the placement of nodes depends almost entirely on the order
in which they were added, and can be re-arranged without changing the meaning.

Trees and Distance 47

“Only for Private Circulation”


Graph Theory Second, as a representation of data with a relevant bifurcating structure. In
such cases the particular arrangement of nodes under and/or to the left or right
of other nodes is part of the information. Common examples occur with Huffman
NOTES coding and cladograms. The everyday division of documents into chapters,
sections, paragraphs, and so on is an analogous example with n-ary rather than
binary trees.

Properties of Binary trees


1. The number of nodes ‘n’ in a full binary tree, is at least n=2h + 1 and
maximum n=2h+1-1 where h is height of the tree. A tree consisting of
only a root node has a height ‘0’.
2. The number of leaf nodes L in a perfect binary tree of n nodes is
L=(n+1)/2.It means that a full binary tree with L leaves has n= 2L- 1.
3. In a balanced full binary tree has height h= ceiling functional value
of log2(n+1)
4. In a perfect full binary tree, L= 2h.
5. The number of null nodes in a complete binary tree of n nodes is n+1.
6. The number of internal nodes in a complete binary tree of nodes n is
integer value of n/2.

Binary Search Trees


A binary search tree is a rooted binary tree, whose internal nodes each store
a key (and optionally, an associated value), and each has two distinguished sub-
trees, commonly denoted left and right. The tree additionally satisfies the binary
search property, which states that the key in each node must be greater than or
equal to any key stored in the left sub-tree, and less than or equal to any key
stored in the right sub-tree.The leaves (final nodes) of the tree contain no key
and have no structure to distinguish them from one another.
Frequently, the information represented by each node is a record rather than
a single data element. However, for sequencing purposes, nodes are compared
according to their keys rather than any part of their associated records. The major
advantage of binary search trees over other data structures is that the related
sorting algorithms and search algorithms such as in-order traversal can be very
efficient; they are also easy to code.
Binary search trees are a fundamental data structure used to construct more
abstract data structures such as sets, multisets, and associative arrays.
When inserting or searching for an element in a binary search tree, the key
of each visited node has to be compared with the key of the element to be inserted
or found.
The shape of the binary search tree depends entirely on the order of
insertions and deletions and can become degenerate.
48 Trees and Distance

“Only for Private Circulation”


After a long intermixed sequence of random insertion and deletion, the Graph Theory
expected height of the tree approaches square root of the number of keys, √n,
which grows much faster than log n.
There has been a lot of research to prevent degeneration of the tree resulting NOTES
in worst case time complexity of O(n) .

Spanning Tree
A tree is a connected undirected graph with no cycles. It is a spanning tree
of a graph G if it spans G (that is, it includes every vertex of G) and is a subgraph
of G (every edge in the tree belongs to G). A spanning tree of a connected graph
G can also be defined as a maximal set of edges of G that contains no cycle, or
as a minimal set of edges that connect all vertices.

Minimum spanning Tree


A minimum spanning tree (MST) or minimum weight spanning tree is a
subset of the edges of a connected, edge-weighted undirected graph that connects
all the vertices together, without any cycles and with the minimum possible total
edge weight. That is, it is a spanning tree whose sum of edge weights is as small
as possible. More generally, any edge-weighted undirected graph (not necessarily
connected) has a minimum spanning forest, which is a union of the minimum
spanning trees for its connected components.
There are quite a few use cases for minimum spanning trees. One example
would be a telecommunications company trying to lay cable in a new
neighborhood. If it is constrained to bury the cable only along certain paths (e.g.
roads), then there would be a graph containing the points (e.g. houses) connected
by those paths. Some of the paths might be more expensive, because they are
longer, or require the cable to be buried deeper; these paths would be represented
by edges with larger weights. Currency is an acceptable unit for edge weight –
there is no requirement for edge lengths to obey normal rules of geometry such
as the triangle inequality. A spanning tree for that graph would be a subset of
those paths that has no cycles but still connects every house; there might be
several spanning trees possible. A minimum spanning tree would be one with the
lowest total cost, representing the least expensive path for laying the cable.

Back tracking applications


Backtracking is a general algorithm for finding all (or some) solutions to
some computational problems, notably constraint satisfaction problems, that
incrementally builds candidates to the solutions, and abandons a candidate
("backtracks") as soon as it determines that the candidate cannot possibly be
completed to a valid solution.
The classic textbook example of the use of backtracking is the eight queens
puzzle, that asks for all arrangements of eight chess queens on a standard
chessboard so that no queen attacks any other. In the common backtracking
approach, the partial candidates are arrangements of k queens in the first k rows Trees and Distance 49

“Only for Private Circulation”


Graph Theory of the board, all in different rows and columns. Any partial solution that contains
two mutually attacking queens can be abandoned.
Backtracking can be applied only for problems which admit the concept of
NOTES a "partial candidate solution" and a relatively quick test of whether it can possibly
be completed to a valid solution. It is useless, for example, for locating a given
value in an unordered table. When it is applicable, however, backtracking is often
much faster than brute force enumeration of all complete candidates, since it can
eliminate many candidates with a single test.
Backtracking is an important tool for solving constraint satisfaction
problems, such as crosswords, verbal arithmetic, Sudoku, and many other
puzzles. It is often the most convenient technique for parsing, for the knapsack
problem and other combinatorial optimization problems. It is also the basis of
the so-called logic programming languages such as Icon, Planner and Prolog.
Backtracking depends on user-given "black box procedures" that define the
problem to be solved, the nature of the partial candidates, and how they are
extended into complete candidates. It is therefore a metaheuristic rather than a
specific algorithm – although, unlike many other meta-heuristics, it is guaranteed
to find all solutions to a finite problem in a bounded amount of time.

Kruskal algorithm
Kruskal's algorithm is a minimum-spanning-tree algorithm which finds an
edge of the least possible weight that connects any two trees in the forest. It is a
greedy algorithm in graph theory as it finds a minimum spanning tree for a
connected weighted graph adding increasing cost arcs at each step. This means
it finds a subset of the edges that forms a tree that includes every vertex, where
the total weight of all the edges in the tree is minimized. If the graph is not
connected, then it finds a minimum spanning forest (a minimum spanning tree
for each connected component).

Algorithm
1. Create a forest F (a set of trees), where each vertex in the graph is a
separate tree
2. Create a set S containing all the edges in the graph
3. While S is nonempty and F is not yet spanning
a. remove an edge with minimum weight from S
b. if the removed edge connects two different trees then add it to
the forest F, combining two trees into a single tree
4. At the termination of the algorithm, the forest forms a minimum
spanning forest of the graph. If the graph is connected, the forest has
a single component and forms a minimum spanning tree

50 Trees and Distance

“Only for Private Circulation”


Example : Graph Theory
Find minimum spanning tree by using Kruskal’s algorithm for the
following graph.
NOTES

Solution :Stepwise solution is given the following table

The resultant minimum spanning tree is as follows:

Total weight =24 Trees and Distance 51

“Only for Private Circulation”


Graph Theory Prims Algorithm
Prim’s algorithm is also a Greedy algorithm. It starts with an empty
spanning tree. The idea is to maintain two sets of vertices. The first set contains
NOTES the vertices already included in the MST(minimum spanning tree) , the other set
contains the vertices not yet included. At every step, it considers all the edges
that connect the two sets, and picks the minimum weight edge from these edges.
After picking the edge, it moves the other endpoint of the edge to the set
containing .
A group of edges that connects two set of vertices in a graph is called cut in
graph theory. So, at every step of Prim’s algorithm, we find a cut (of two sets,
one contains the vertices already included in MST and other contains rest of the
vertices), pick the minimum weight edge from the cut and include this vertex to
MST Set

Working of Prim’s Algorithm


The idea behind Prim’s algorithm is simple, a spanning tree means all
vertices must be connected. So the two disjoint subsets of vertices must be
connected to make a Spanning Tree. And they must be connected with the
minimum weight edge to make it a Minimum Spanning Tree.

Algorithm
1) Create a set mstSet that keeps track of vertices already included in
MST.
2) Assign a key value to all vertices in the input graph. Initialize all key
values as INFINITE.
Assign key value as 0 for the first vertex so that it is picked first.
3) While mstSet doesn’t include all vertices
a) Pick a vertex u which is not there in mstSet and has minimum
key value.
b) Include u to mstSet.
c) Update key value of all adjacent vertices of u. To update the key
values, iterate through all adjacent vertices. For every adjacent
vertex v, if weight of edge u-v is less than the previous key value
of v, update the key value as weight of u-v
The idea of using key values is to pick the minimum weight edge from cut.
The key values are used only for vertices which are not yet included in MST, the
key value for these vertices indicate the minimum weight edges connecting them
to the set of vertices included in MST.

Example
Find minimum spanning tree by using Prim's algorithm for the following
52 Trees and Distance graph.

“Only for Private Circulation”


Graph Theory

NOTES

Solution :
Let us select node 1 as the starting node. Following table shows the edge
of minimum weight selected and set of vertices A.

Initial Step :

Trees and Distance 53

“Only for Private Circulation”


Graph Theory

NOTES

As we have added all vertices, the resultant tree is called as minimum


spanning tree.

Huffman’s algorithm
Huffman code is a particular type of optimal prefix code that is commonly
used for lossless data compression. The process of finding and/or using such a
code proceeds by means of Huffman coding, an algorithm developed by David
A. Huffman
The output from Huffman's algorithm can be viewed as a variable-length
code table for encoding a source symbol (such as a character in a file). The
algorithm derives this table from the estimated probability or frequency of
occurrence (weight) for each possible value of the source symbol. As in other
entropy encoding methods, more common symbols are generally represented
using fewer bits than less common symbols. Huffman's method can be efficiently
implemented, finding a code in time linear to the number of input weights if these
weights are sorted. However, although optimal among methods encoding
54 Trees and Distance

“Only for Private Circulation”


symbols separately, Huffman coding is not always optimal among all Graph Theory
compression methods.

Algorithm NOTES
Procedure Huffman( C :symbols ai with frequencies wi, i=1,2…..n)
F :=forest of n rooted trees, each consisting of the single vertex ai and
assigned weight wi
While F is not a tree
Begin
Replace the rooted trees T and T ' of least weighs from F with w(T) ≥ W(T')
with a tree
Having a new root that has T as its left subtree and T' as its right
subtree.Label the new edge to T with 0 and the new edge to T' with 1.
Assign w(T) + w(T') as the weight of the new tree
end
Example:
Use Huffman coding to encode the following symbols with the frequencies
listed :
A: 0.08, B: 0.10, C: 0.12 , D:0.15, F:0.20 , F:0.35. What is the average
number of bits used to encode a character?
Solution :
Initial Step:

Step 1 : Add frequencies of A and B . It comes to 0.18

Step 2: Add frequencies of C and D. It comes to 0.27.

Trees and Distance 55

“Only for Private Circulation”


Graph Theory Step 3: Add frequencies of first two nodes which comes to be 0.38. Place
this node after node F

NOTES

Step 4: Add frequencies of first two nodes which comes to 0.62. Place this
node after node with frequency 0.38

Step 5: Add last remaining two frequencies and we get the final tree as
follows.Write 0 on let branch and 1 on right branch

Now write the codes for each vertex, by reading the digits from that vertex
to the root node.
Here the codes are :
A: 111
B:011
C:110
D:010
E:01
F:00.
Also the average number of bits used to encode symbol is found out by
taking the sum of the product of braches of a vertex from the root node and the
frequencies.
56 Trees and Distance

“Only for Private Circulation”


Here, the average number of bits used to encode a symbol using this code Graph Theory
is : 3 x 0.08 + 3 x 0.10 + 3 x 0.12 + 3 x 0.15 + 2 x 0.20 + 2 x 0.035 =2.45

Exercise NOTES
Q 1. Use Prim’s algorithm to find spanning tree in the following graph.

Q 2. Use Kruskal’s algorithm to find minimum spanning tree of the


following graph.

Q 3. Use Huffman’s coding to encode following symbols with the given


frequencies.
a:0.20 , b:0.10 ,c:0.15 , d:0.225 , e:0.30.
Also find the average number of bits required to encode a character.

*****

Trees and Distance 57

“Only for Private Circulation”


Graph Theory

UNIT - VI
MATCHINGS
NOTES

Matching
Given a graph G = (V,E), a matching M in G is a set of pairwise non-
adjacent edges, none of which are loops; that is, no two edges share a common
vertex.
A vertex is matched (or saturated) if it is an endpoint of one of the edges in
the matching. Otherwise the vertex is unmatched.

Hall’s Condition
Given set A, let N(A) be the set of neighbors of A.Then the Bipartite graph
G with bipartitions X and Y has a perfect matching if and only if |N(A)|≥|A| for
all subsets A of X.

MinMax Theorem
In bipartite matching, we have K¨onig’s theorem as the min-max theorem,
which says that the maximum size of a matching is equal to the minimum size
of a vertex cover in a bipartite graph. However, this is not true for general graphs.
Consider a complete graph of n vertices. The maximum matching is of size n/2
but a minimum vertex cover needs n − 1 vertices. There is a min-max theorem
for general matching.
Tutte in 1947 gave a necessary and sufficient condition characterizing
graphs that have a perfect matching. Berge in 1958 observed that it implies a
min-max formula for the maximum size of a matching in a graph, now known as
the Tutte-Berge formula, which is a generalization of Tutte’s theorem.

Covers
A covering graph is a subgraph which contains either all the vertices or all
the edges corresponding to some other graph. A subgraph which contains all the
vertices is called a line/edge covering. A subgraph which contains all the edges
is called a vertex covering.

Line Covering
Let G = (V, E) be a graph. A subset C(E) is called a line covering of G if
every vertex of G is incident with at least one edge in C, i.e.,
deg(V) ≥ 1 ∀ V ∈ G because each vertex is connected with another vertex
by an edge. Hence it has a minimum degree of 1.
58 Matchings

“Only for Private Circulation”


Example: Graph Theory
Consider the following graph –

NOTES

Its subgraphs having line covering are as follows –


C1 ={(a,b),(c,d)}
C2={(a,b),(b,c)}
C3={(a,b),(b,c),(b,d)}
C4={(a,b),(b,c),(c,d)}
Line covering of ‘G’ does not exist if and only if ‘G’ has an isolated vertex.
Line covering of a graph with ‘n’ vertices has at least n/2 edges.

Maximum Bipartite Matching


The bipartite matching is a set of edges in a graph is chosen in such a way,
that no two edges in that set will share an endpoint. The maximum matching is
matching the maximum number of edges.
When the maximum match is found, we cannot add another edge. If one
edge is added to the maximum matched graph, it is no longer a matching. For a
bipartite graph, there can be more than one maximum matching is possible.

Algorithm
bipartiteMatch(u, visited, assign)
Input: Starting node, visited list to keep track, assign the list to assign node
with another node.
Output: Returns true when a matching for vertex u is possible.
Begin
for all vertex v, which are adjacent with u, do
if v is not visited, then
mark v as visited
if v is not assigned, or bipartiteMatch(assign[v], visited, assign) is true, then
assign[v] := u
return true
done
return false
End Matchings 59

“Only for Private Circulation”


Graph Theory Example : Find the maximum bipartite matching for the following graph in
the form of matrix:
011000
NOTES
100100
001000
001100
000000
000001

Output
Maximum number of applicants matching for job: 5

Weighted Bipartite Matching


In computer science, the maximum weight matching problem is the problem
of finding, in a weighted graph, a matching in which the sum of weights is
maximized.
A special case of it is the assignment problem, in which the input is
restricted to be a bipartite graph. Another special case is the problem of finding
a maximum cardinality matching on an unweighted graph: this corresponds to
the case where all edge weights are the same.

Maximum Networks Flow


Maximum flow problems involve finding a feasible flow through a single-
source, single-sink flow network that is maximum.
In graph theory , a flow network is defined as a directed graph involving a
source(S) and a sink(T) and several other nodes connected with edges.Each edge
has an individual capacity which is the maximum limit of flow that edge could
allow.
Flow in the network should follow the following conditions:
• For any non-source and non-sink node, the input flow is equal to output
flow.
• For any edge(Ei) in the network, 0≤flow(Ei)≤Capacity(Ei).
• Total flow out of the source node is equal total to flow in to the sink node.
• Net flow in the edges follows skew symmetry i.e. F(u,v)=−F(v,u) where
F(u,v) is flow from node u to node v. This leads to a conclusion where
you have to sum up all the flows between two nodes(either directions) to
find net flow between the nodes initially.

60 Matchings

“Only for Private Circulation”


Definition of Maximum Flow Graph Theory
It is defined as the maximum amount of flow that the network would allow
to flow from source to sink. Multiple algorithms exist in solving the maximum
flow problem. Two major algorithms to solve these kind of problems are Ford- NOTES
Fulkerson algorithm and Dinic's Algorithm. They are explained below.

Ford-Fulkerson Algorithm
It was developed by L. R. Ford, Jr. and D. R. Fulkerson in 1956. A
pseudocode for this algorithm is given below .Inputs required are network graph
G, source node S and sink node T.
function: FordFulkerson(Graph G,Node S,Node T):
Initialise flow in all edges to 0
while (there exists an augmenting path(P) between S and T in residual
network graph):
Augment flow between S to T along the path P
Update residual network graph
return
An augmenting path is a simple path from source to sink which do not
include any cycles and that pass only through positive weighted edges. A residual
network graph indicates how much more flow is allowed in each edge in the
network graph. If there are no augmenting paths possible from S to T, then the
flow is maximum. The result i.e. the maximum flow will be the total flow out of
source node which is also equal to total flow in to the sink node.
Example
Let’s take an image to explain how the above definition wants to say.

Here source is 0 and sink is 5


Each edge is labeled with capacity, the maximum amount of stuff that it can
carry. The goal is to figure out how much stuff can be pushed from the vertex
s(source) to the vertex t(sink).

Matchings 61

“Only for Private Circulation”


Graph Theory Maximum flow possible is : 23

NOTES

Problem Exercise
Q 1. Find maximum flow from source 0 to sink 7 in the following network.

*****

62 Matchings

“Only for Private Circulation”

You might also like