Professional Documents
Culture Documents
2 Connectivity 12-22
3 Algorithms 23-35
6 Matchings 58-62
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
Fundamental
Concepts 1
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.
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
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
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
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.
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
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
Solution:
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
Exercises
Q1. Draw the following graph
a) G(6,3) b) G(7,3)
a) b)
a)
b)
Fundamental
10 Concepts
NOTES
b)
*****
Fundamental
Concepts 11
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
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.
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?
Connectivity 15
16 Connectivity
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
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:
Similarly three edges labeled (4) can be drawn between vertices {W, B},
{G, G}, {R, B}. These edges come from the fourth cube.
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
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
NOTES
Q2. State whether the following graph has Euler path and or Euler Circuit.
*****
22 Connectivity
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.
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
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
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
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
Algorithms 27
NOTES
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
Step 2: Select next vertex which is at the left depth of vertex 1.Here it is
vertex 2.
1→2
Algorithms 29
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
Solution :
Step 1 :
Algorithms 31
NOTES
Step3
Step 4 :
32 Algorithms
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
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
*****
Algorithms 35
UNIT - IV
NOTES
COLORING OF GRAPHS
AND PLANARITY
Vertex Coloring
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.
Now we apply graph coloring and then we get the following color graph,
Coloring of Graphs
and planarity 37
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
Solution : This graph is planar graph because we can draw the same graph
in the following way in which no edges are crossing.
Solution : It is not a planar graph as we can not draw this graph without
crossing one pair of edges.
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.
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
Exercise Questions
Q 1. State whether the following graphs are planar .
*****
Coloring of Graphs
and planarity 41
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
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
NOTES
We can represent the same by using decision tree in the following manner.
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.
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
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/+
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.
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
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.
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 :
NOTES
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
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:
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
Exercise NOTES
Q 1. Use Prim’s algorithm to find spanning tree in the following graph.
*****
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
NOTES
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
Output
Maximum number of applicants matching for job: 5
60 Matchings
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.
Matchings 61
NOTES
Problem Exercise
Q 1. Find maximum flow from source 0 to sink 7 in the following network.
*****
62 Matchings