DM - Unit 5 (Trees) - Notes_passworded
DM - Unit 5 (Trees) - Notes_passworded
INTRODUCTION
A tree is a connected undirected graph with no simple circuits.
Because a tree cannot have a simple circuit, a tree cannot contain multiple edges or
loops.Therefore any tree must be a simple graph.
Rooted Trees
A rooted tree is a tree in which one vertex has been designated as the root and every
edge is directed away from the root.
Terminology for rooted trees
If v is a vertex in T other than the root, the parent of v is the unique vertex u such that
there is a directed edge from u to v .
If u is the parent of v , v is called a child of u.
Two Vertices with the same parent are called siblings.
A vertex of a tree is called a leaf if it has no children. A terminating vertex (or a
leaf) in a tree is a vertex of degree 1.
Vertices that have children are called internal vertices. An internal vertex (or a
branch vertex) in a tree is a vertex of degree greater than 1.
Vertices are sometimes referred to as nodes, particularly when dealing with graph
trees.
The ancestors of a vertex other than the root are the vertices in the path from the root
to this vertex, excluding the vertex itself and including the root (i.e., its parent, its
parent’s parent, and so on, until the root is reached).
The descendants of a vertex v are those vertices that have v as an ancestor.
If a is a vertex in a tree, the subtree with a as its root is the subgraph of the tree
consisting of a and its descendants and all edges incident to these descendants.
Figure 5: Tree and Rooted Trees Formed by Designating Two Different Roots.
Example 3: In the rooted tree T(with root a) shown in Figure 6, find the parent of c,
the children of g, the siblings of h, all ancestors of e, all descendants of b, all internal
vertices, and all leaves. What is the subtree rooted at g?
Example 4: Answer these questions about the rooted tree illustrated in Figure 8.
a) Which vertex is the root? b) Which vertices are internal?
c) Which vertices are leaves? d) Which vertices are children of j?
e) Which vertex is the parent of h? f) Which vertices are siblings of o?
g) Which vertices are ancestors of m? h) Which vertices are descendants of b?
M-ary Tree
A rooted tree is called an m-ary tree if every internal vertex has no more than m
children. The tree is called a full m-ary tree if every internal vertex has exactly m
children. An m-ary tree with m = 2 is called a binary tree.
Example 6: Are the rooted trees in Figure 9 full m-ary trees for positive integer m?
Figure 10: A Binary Tree T and Left and Right Subtrees of the Vertex c.
Solution:The left child of d is fand the right child is g. We show the left and right subtrees
of c in Figures 10(b) and 10(c), respectively.
Properties of Trees
Every tree with n vertices has exactly n - 1 edges.
A full m-ary tree with i internal vertices contains n = mi + 1 vertices.
Proof: Every vertex, except the root, is the child of an internal vertex. Because each of
the i internal vertices has m children, there are mi vertices in the tree other than the
root. Therefore, the tree contains n = mi + 1 vertices.
A full m-ary tree with
(i) n vertices has i = (n −1)/m internal vertices and l = [(m −1) n + 1] / m leaves,
(ii) i internal vertices has n = mi + 1 vertices and l = (m − 1)i + 1 leaves,
(iii) l leaves has n = (ml −1)/(m −1) vertices & i = (l − 1)/(m − 1) internal vertices.
Prefix Code
Consider the problem of using bit strings to encode the letters of the English alphabet
(where no distinction is made between lowercase and uppercase letters). We can
represent each letter with a bit string of length five, because there are only 26 letters
and there are 32 bit strings of length five. The total number of bits used to encode data
is five times the number of characters in the text when each character is encoded with
five bits.
Consider using bit strings of different lengths to encode letters. Letters that occur
more frequently should be encoded using short bit strings, and longer bit strings
should be used to encode rarely occurring letters. When letters are encoded using
varying numbers of bits, some method must be used to determine where the bits for
each character start and end. For instance, if e were encoded with 0,a with 1, and t
with 01, then the bit string 0101 could correspond to eat, tea, eaea,or tt.
One way to ensure that no bit string corresponds to more than one sequence of letters
is to encode letters so that the bit string for a letter never occurs as the first part of the
bit string for another letter.
Codes with this property are called prefix codes. For instance, the encoding of e as 0,
a as 10, and t as 11 is a prefix code.
A set of sequence is said to be prefix code if no sequence in the set is prefix of
another. OR
A code is called a prefix (free) code if no codeword is a prefix of another one.
A word can be recovered from the unique bit string that encodes its letters. For
example, the string 10110 is the encoding of ate.
To see this, note that the initial 1 does not represent a character, but 10 does represent
a (and could not be the first part of the bit string of another letter). Then, the next 1
does not represent a character, but 11 does represent t. The final bit, 0, represents e.
For Example: The set {01,10,11,000} is a prefix code. The set {1,00,01,000,0001} is
not prefix code because the sequence 00 is prefix of the sequence 000 and 0001.
A prefix code can be represented using a binary tree, where the characters are the
labels of the leaves in the tree. The edges of the tree are labeled so that an edge
leading to a left child is assigned a 0 and an edge leading to a right child is assigned a
1. The bit string used to encode a character is the sequence of labels of the edges in
the unique path from the root to the leaf that has this character as its label.
(a) (b)
Figure 11: A Binary Tree with a Prefix Code
For Figure 11(a) Represents the encoding of e by 0, a by 10, t by 110, n by 1110, and
s by 1111.
Bit string 11111011100 using the code in Figure 11(a) encode the message the “sane”
1111 10 1110 0
s a n e
Optimal Tree
Let T be any full binary tree and w1,w2,w3,….wt be the weights of the terminal
vertices.Then the weight W of a binary tree is given by:
W(T) =
Where li is the length of the leaf i from the root of the tree.The fully binary tree is
called optimal if its weight is minimum.
Optimal tree are used to construct variable length codes,where the letters of alphabets
are represented by binary digit.
Example 11: Figure 12 show two fully binary tree T1 and T2, with the weights of the
leaves as 3,4 and 5. Show which tree is optimal tree.
Solution:
W(T1) = 3*2 + 4*2 + 5*1 = 6 + 8 + 5 = 19
W(T2) = 3*1 + 4*2 + 5*2 = 2 + 8 + 10 = 21
Here W(T1) < W(T2), Thus T1 is optimal tree for weights 3,4, and 5.
Example 13: Construct Huffman code for the data: A: 0.08, B:0.10, C: 0.12, D: 0.15,
E: 0.20, F: 0.35. What is the average number of bits used to encode a character?
Solution: (Draw tree)
The average number of bits used to encode a symbol using this encoding is
3*0.08 + 3*0.10 + 3*0.12 + 3*0.15 + 2*0.20 + 2*0.35 =2.45
Example 14: Use Huffman coding to encode following symbols with given
frequencies and find the average number of bits required to encode a character.
i. a: 0.20, b: 0.10, c: 0.15, d: 0.25, e: 0.30.
ii. A: 0.10, B: 0.25, C: 0.05, D: 0.15, E: 0.30, F: 0.07, G: 0.08.
Example 15: For the following set of weights, construct optimal binary prefix code.
For each weight in the set, give the corresponding code words.
i) 10,30,05,15, 20, 15, 05. ii) 8, 9, 10, 11, 13, 15, 22.
Example 16: A secondary storage media contains information in files with different
formats. The frequency of different types of files is as follows. Exe (20), bin(75),
bat(20), jpeg(85), dat(51), doc(32), sys(26), c(19), cpp(25), bmp(30), avi(24), prj
(29), 1st(35), zip(37).Construct the Huffman code of this.
Spanning Tree
Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree
containing every vertex of G.
In general, a graph may have several spanning trees, but a graph that is not connected
will not contain a spanning tree
A simple graph with a spanning tree must be connected, because there is a path in the
spanning tree between any two vertices.
The converse is also true; that is, every connected simple graph has a spanning tree.
Theorm: A simple graph is connected if and only if it has a spanning tree.
Example 17: Find a spanning tree of the simple graph G shown in Figure 13.
Example 18: How many edges must be removed from a connected graph with v
vertices and e edges to produce a spanning tree?
Solution:
The graph has e edges. The spanning tree has v - 1 edges.
Therefore we need to remove : e - (v - 1) edges.
Hence Graph has : e – v + 1 edges.
Example 19: Find a spanning tree for each simple graph G shown in Figure below:
Prim’s Algorithm
Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a
connected weighted undirected graph.
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.
This algorithm is directly based on the MST( minimum spanning tree) property.
The algorithm may informally be described as performing the following steps:
1. Initialize a tree with a single vertex, chosen arbitrarily from the graph.
2. Grow the tree by one edge: of the edges that connect the tree to vertices not yet in
the tree, find the minimum-weight edge, and transfer it to the tree.
3. Repeat step 2 (until all vertices are in the tree).
Following are details step of implementation of Prim’s algorithm:
Let G(V,E) be connnected weighted graph
Step 1: Select any vertex V0 in graph. Set T={ V0, ∅}
Step 2: Find edge ei= (V0, Vi) in E such that its one vertex is V0 ∈ T and its
weight is minimum. Therefore New Set T={{ V0, V1},{ei}}
Step 3: Choose next edge ek = (Vk, Vj) in such a way that it’s one vertex Vk ∈ T
and other vertex Vj ∉ T and weight of ek is as small as possible. Again join vetex
Vj and edge ek to T.
Step 4: Repeat step 3 until T contain all vertices of graph G.The result wil be
MST of graph G.
Example 21: Using Prim’s Algorithm Find MST for given graph.
Solution: Procedure for finding Minimum Spanning Tree using Prism’s Algorithm
Step 1: Select Vertex 0 as a starting vertex.
Therefore T = {{0},∅}
Step 2: Vertex 0 is adjacent to vertex 1, 2 and 3.
Edge (0, 1) = 3, edge (0, 2) = 1 and edge (0, 3) = 6.
Select edge with minimum weight
i.e. say edge e1 = (0,2) = 1
Therefore T = {{0, 2}, e1}.
Example 22: Using Prim’s Algorithm Find MST for given graph.
Solution:
Step 1: Select vertex S as initial vetex and Step 2: Edge AC is minimum so select AC
then edge SA is minimum so select SA.
Example 23: Using Prim’s Algorithm Find MST for given graph.
Example 24: Using Prim’s Algorithm Find MST for given graph.
Selecting a as starting vertex also yeild same result and gives same edges.
Step 1: Select e as Starting vertex. Step 2: Select edge ef = 1.
Minimum Cost = 1 + 3 + 3 + 4 + 3 + 2 + 2 + 4 = 22
Example 25: Obtain the minimum spanning tree using Prim’s algorithm for the
following graph. Obtain the total cost of minimum spanning tree.
Step 1: Select A as Starting vertex. And Step 2: Select edge DF = 6.
AD as first edges
Minimum Cost = 5 + 6 + 7 + 7 + 5 + 9 = 39
Example 26: Obtain the minimum spanning tree using Prim’s algorithm for the
following graph. Obtain the total cost of minimum spanning tree.
Krushal's 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.
Following are details step of implementation of Krushal’s algorithm.
Let G(V,E) be weighted connected graph.
Step 1: Pick up the ei of the Graph G such that its weights W(e,i) is minimum.(If
there are more edges of the minimum weight then select all those edges which do
not form circuit)
Step 2: If edges e1,e2,e3,…..en have been chosen then pick an edge en+1 such that
i. en+1 ≠ei for i=1,2,3,…,n
ii. The edges e1,e2,e3,…..en en+1 do not form circuit.
iii. W(en+1) is as small as possible subject to condition (ii).
Step 3: Stop procedure when step two cannot be implemented further.
Example 27: Using Krushal algorithm Find MST for given graph.
Solution:
1. AD and CE are the shortest edges, with
length 5, and AD has been arbitrarily
chosen, so it is highlighted.
Example 28: Using Krushal’s Algorithm Find MST for given graph.
Next cost in the table is 4, and we observe that adding it will create a circuit in the graph.
We ignore it. In the process we shall ignore/avoid all edges that create a circuit. We observe
that edges with cost 5 and 6 also create circuits. We ignore them and move on.
Now we are left with only one node to be
added. Between the two least cost edges
available 7 and 8, we shall add the edge
with cost 7.
By adding edge S, A we have included
all the nodes of the graph and we now
have minimum cost spanning tree.
Minimum Cost Spanning Tree = 2 + 2 + 3 + 3 + 7 = 17.
Example 29: Using Krushal’s Algorithm Find MST for given graph.
Example 30: Using Prism’s and Krushal’s algorithm find MST for following graph
MST = 4 + 8 + 1 + 2 + 4 + 2 + 7 + 9 = 37
Example 31: Find the minimum spanning tree of the given graph using Kruskal’s
algorithm
Weight = 16 Weight = 40
Fundamental Circuit
For a Graph G and its associated spanning tree T, the fundamental circuit of a chord
‘e’ of T is given by the circuit formed due to addition of ‘e’ in T.
In other words Let G be connected graph and Let T be spanning tree of G. Consider a
chord of T i.e. an edge of G which is not in T.Then ‘T + e’ contain a unique circuit
called as fundamental circuit.
For different spanning tree of graph G, the fundamental circuit will be different.
The number of fundamental circuit in any spanning tree is equal to the numbers of
chords of that spanning tree.
Hence if the connected graph G has v number of vertices and e number of edges then
spanning tree T has (v-1) branches and ( e - v + 1 ) numbers of chords.
Thus there will be ( e - v + 1 ) number of fundamental circuit in G with respect to the
spanning tree T.
Example 32: Find the fundamental circuit for the graph show in figure below w.r.t
the spanning tree T.
Solution:
Chord Fundamental Circuit
e1 { e1, e4, e6, e8 }
e2 { e2, e3, e6, e8 }
e5 { e5, e4, e6 }
e7 {e7, e3, e6 }
Example 33: Find the fundamental circuit for the graph show in figure below w.r.t
the spanning tree T.
Solution:
Fundamental circuit for above graph G and spanning tree T are
Fundamental Cut-sets
Let T be the spanning tree of the connected graph G.
The removal of any branch from a spanning tree,breaks the spanning tree into two
trees i.e there is division of the vertices in the graph into two subsets corresponding to
the vertices in the two trees.
It follows that for every branch in a spanning tree there is a corresponding cut set
called fundamental cut set.
The fundamental cut set of the graph G with respect to the spanning tree T is called
fundamental system of cut set.
The number of fundamental cut sets is equal to the number of branches in the
spanning tree.
If the connected graph G has v number of vertices and e number of edges,then
spanning tree has (v-1) branches which is the same as the number of fundamental
circuit.
Example 34: Find the fundamental circuit for the graph show in figure below w.r.t
the spanning tree T.
Solution:
Branch Fundamental cutset
e4 { e1, e5, e4 }
e3 { e2, e7, e3 }
e6 { e1, e5, e6, e7, e2 }
e8 {e1, e8, e2 }
Example 35: Find the fundamental system of cutset for the graph show in figure
below w.r.t the spanning tree T.
Solution: The spanning tree T has 7 branches {e2,e4,e5,e7,e10,e12,e13}. Therefore its
has 7 fundamental cutset.
Prof. S. B. Shinde [MES College of Engineering, Pune] Page 26
Discrete Mathematics: Unit 5
Example 36: Find the fundamental system of cutset for the graph show in figure
below w.r.t the spanning tree T.
Example 37: Find the fundamental circuit and fundamental system of cutset for the
graph show in figure below w.r.t the spanning tree T.
Network Flow
In graph theory, a flow network (also known as a transportation network) is
a directed graph where each edge has a capacity and each edge receives a flow.
The amount of flow on an edge cannot exceed the capacity of the edge.
A directed graph is called a network, the vertices are called nodes and the edges are
called arcs.
A flow must satisfy the restriction that the amount of flow into a node equals the
amount of flow out of it.
It is a source, which has only outgoing flow, and sink which has only incoming flow.
A network can be used to model traffic in a road system, circulation with demands,
fluids in pipes, currents in an electrical circuit, or anything similar in which something
travels through a network of nodes.
Two Methods for finding Max Flow in a transport network
Ford Fulkerson Algorithm
Labeling Procedure Algorithm
Step of Labeling Procedure Algorithm
Suppose a transport network with the capacity of each edge is given.
1. Initially, assign the zero flow to each edge of the given network.
Also assign the label (-, ∞) to the sourec s.
2. Scan all those vertices which are adjacent to the source s.
Suppose the vertex b is adjacent to s and C(s,b) > F(s,b) then label the vertex b as (a+,
Δb) where Δb = C(s,b) – F(s,b).
The vertex b is not labeled if C(s,b) = F(s,b).
3. Scan all those vertices which are adjacent to labeled vertices. Suppose the vertex q is
adjacent to the labeled vertex b. The vetex q is labeled as (b+, Δq).
where Δq = min(Δb,[C(b,q) – F(b,q)] ) if [C(b,q) > F(b,q)].
The vertex q is not labeled if C(b,q) = F(b,q).
Also the vertex q is labeled (b-, Δq), where Δq = min(Δb,F(b,q)] ) if F(b,q) > 0.
4. Repeat step 3 till we reach sink z vertex.
5. If the sink z is labeled with the label (y+,Δz) where y is some labeled vertex, then
increase flow of the edge (y,z) from F(y,z) to F(y,z) = Δz.
Note that the vertex y must be labeled either (q+, Δy) or (q-,Δy) with Δy.
If y is labeled (q+,Δy) then increase the flow in the edge(q,y) form F(q,y) to F(q,y)+Δy.
On the other hand if y is labeled (q-,Δy) then decrease the flow in the edge(y,q) form
F(y,q) to F(y,q)-Δy.
This process is continued back to source s and the value of the flow in the transport
network will be increased by amount Δz.Again start labelling procedure to further
increase the value of the flow in the netwrk i.e. step 3.
6. If the sink is not labeled, then denote all the labeled vertices by P and unlabeled vertices
by P-.Determine the capacity of the cut(P,P-) which is the value of the maximal flow.