You are on page 1of 55

B.TECH.

SECOND YEAR
(III SEM. CSE/IT/CCE)
ACADEMIC YEAR: 2022-2023

COURSE NAME: ENGINEERING MATHEMATICS III


COURSE CODE : MA 2101
LECTURE SERIES NO : UNIT-III
CREDITS : 3
Trees
Definition: A tree is a connected undirected graph with no cycles. In other words a tree
T is a simple graph such that for every pair of vertices v and w there is a unique path
from v to w.

When talking about graphs, we say a tree is a graph that is:


• undirected
• acyclic
• Connected.
A Tree?
9 10 11 12

5 6 7 8

2 3 4

1
A Tree?
9 10 11 12

5 6 7 8

2 3 4

1
Facts about Trees
• All trees are graphs, but NOT all graphs are trees.
• An undirected graph is a tree if and only if there is a unique simple path between any
two of its vertices.
• Trees are structures used to represent hierarchical relationship.
• Each tree consists of nodes and edges.
• Each node represents an object.
• Each edge represents the relationship between two nodes.

node
edge
Some Applications of Trees

Organization Chart Expression Tree

President
+
VP VP
Personnel Marketing * 5

3 2
Director Director Applications of trees:
Customer Sales
Relation •Represent organization
•Represent computer file systems
•Networks to find best path in the Internet
•Chemical formulas representation etc.
Certain Properties of Trees
Theorem 1: A graph G is a tree if and only if there is a unique simple path between any two
vertices of G.

Theorem 2: Every tree, T = (V, E) with |V|≥2, has at least two vertices that have degree = 1.

Theorem 3: Every tree with n vertices has exactly n−1 edges.

(Questions based in above theorems)


Rooted Trees
Definition: A rooted tree is a graph G such that:
• G is connected
• G has no cycles
• G has exactly one vertex called the root of the tree.
i.e. Rooted tree is a tree in which one vertex is distinguished and called a root

A
• Level of a vertex is the number of edges between the
vertex and the root
B C
• The height of a rooted tree is the maximum level of
D E F any vertex
• Children, siblings and parent vertices in a rooted tree
G H • Ancestor, descendant relationship between vertices.
Basic Terminology I
• For any two nodes u and v, if there is an edge pointing from u to v, u is called the
parent of v while v is called the child of u. Such edge is denoted as (u, v).

• In a tree, there is exactly one node without parent, which is called the root. The nodes
without children are called leaves.

root

u
u: parent of v
v: child of u
v
leaves
Basic Terminology II

• In a tree, the nodes without children are called leaves. Otherwise, they are called internal
nodes.

internal nodes

leaves
Basic Terminology III
• If two nodes have the same parent, they are siblings.

• A node u is an ancestor of v if u is parent of v or parent of parent of v or …

• A node v is a descendant of u if v is child of v or child of child of v or …

u
v and w are siblings
u and v are ancestors of x v w
v and x are descendants of u
x
Basic Terminology IV
• A subtree is any node together with all its descendants.

T
A subtree of T
v v
A Rooted Tree
Level of a vertex and tree height
Let T be a rooted tree:
• The level l(v) of a vertex v is the length of the simple path from v to the root of
the tree
• The height h of a rooted tree T is the maximum of all level numbers of its
vertices:
h = max { l(v) }
v  V(T)

Example:
the tree on the right has height 3
Tree Anatomy
Tree Anatomy
Problems based on Rooted Tree
• Which are the parent nodes?
• Which are the child nodes?
• Which are the leaves?
• What is the height and depth of the tree?
• What is the height and depth of node E? Node F?
m-ary Tree
A rooted tree is called an m-ary tree if every internal vertex has at most 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.

•A full m-ary tree with i internal vertices contains n = mi+1 vertices.


Properties of m-ary Tree

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 and i = (l-1)/(m-1) internal vertices.
Binary Tree
• Binary Tree: Tree in which every internal node has at most 2 children
• Left child of u: the child on the left of u
• Right child of u: the child on the right of u

x: left child of u
u v y: right child of u
w: right child of v
z: left child of w
x y w
z
Full Binary Tree
Definition: A Binary tree T is full if each node is either a leaf or possesses exactly two
child nodes
• If T is empty, T is a full binary tree of height 0.
• If T is not empty and of height h >0, T is a full binary tree if both subtrees of the root
of T are full binary trees of height h-1.

Full binary tree


Property of Binary Tree (I)
• A full binary tree of height h has 2h-1 nodes
No. of nodes = 20 + 21 + … + 2(h-1)
= 2h – 1

Level 1: 20 nodes

Level 2: 21 nodes

Level 3: 22 nodes
Property of Binary Tree (II)

• Consider a binary tree T of height h. The number of nodes of T  2h-1.


• The minimum height of a binary tree with n nodes is log2 (n+1).

By property : n  2h-1
Thus, 2h  n+1
That is, h  log2 (n+1)
Distance & Centre

Distance between Two Vertices:

It is number of edges in a shortest path between Vertex U and Vertex V. If there are
multiple paths connecting two vertices, then the shortest path is considered as the
distance between the two vertices.

Notation: d(U,V)
Distance & Centre
Example
Take a look at the following graph −

Here, the distance from vertex ‘d’ to vertex ‘e’ or simply ‘de’ is 1 as there is one edge
between them. There are many paths from vertex ‘d’ to vertex ‘e’ −
•da, ab, be
•df, fg, ge
•de (It is considered for distance between the vertices)
•df, fc, ca, ab, be
•da, ac, cf, fg, ge
Eccentricity of a Vertex
The maximum distance between a vertex to all other vertices is considered as the
eccentricity of vertex.

Notation : e(V)

The distance from a particular vertex to all other vertices in the graph is taken and
among those distances, the eccentricity is the highest of distances.
Eccentricity of a Vertex
Example
In the above graph, the eccentricity of ‘a’ is 3.
The distance from ‘a’ to ‘b’ is 1 (‘ab’),
from ‘a’ to ‘c’ is 1 (‘ac’),
from ‘a’ to ‘d’ is 1 (‘ad’),
from ‘a’ to ‘e’ is 2 (‘ab’-‘be’) or (‘ad’-‘de’),
from ‘a’ to ‘f’ is 2 (‘ac’-‘cf’) or (‘ad’-‘df’),
from ‘a’ to ‘g’ is 3 (‘ac’-‘cf’-‘fg’) or (‘ad’-‘df’-‘fg’).
So the eccentricity is 3, which is a maximum from vertex ‘a’ from the distance between
‘ag’ which is maximum.
In other words,
e(b) = 3, e(e) = 3
e(c) = 3, e(d) = 2
e(f) = 3, e(g) = 3.
Radius of a Connected Graph
The minimum eccentricity from all the vertices is considered as the radius of the
Graph G. The minimum among all the maximum distances between a vertex to all
other vertices is considered as the radius of the Graph G.

Notation: r(G)

From all the eccentricities of the vertices in a graph, the radius of the connected
graph is the minimum of all those eccentricities.

Example

In the above graph r(G) = 2, which is the minimum eccentricity for ‘d’.
Diameter of a Graph
The maximum eccentricity from all the vertices is considered as the diameter of the
Graph G. The maximum among all the distances between a vertex to all other vertices
is considered as the diameter of the Graph G.

Notation: d(G) − From all the eccentricities of the vertices in a graph, the diameter of
the connected graph is the maximum of all those eccentricities.

Example

In the above graph, d(G) = 3; which is the maximum eccentricity.


Centre
Central Point
If the eccentricity of a graph is equal to its radius, then it is known as the central point of the
graph. If
e(V) = r(V),
then ‘V’ is the central point of the Graph ’G’.
Example
In the example graph, ‘d’ is the central point of the graph.
e(d) = r(d) = 2
Centre
The set of all central points of ‘G’ is called the centre of the Graph.
Example
In the example graph, {‘d’} is the centre of the Graph.
Summary
• The eccentricity ecc(v) of v in G is the greatest distance from v to any other
node.
• The radius rad(G) of G is the value of the smallest eccentricity.
• The diameter diam(G) of G is the value of the greatest eccentricity.
• The center of G is the set of nodes v such that ecc(v)=rad(G).
Matrix Representation of a Graph
A pictorial representation of a graph is very convenient for a visual study. A matrix is a
convenient and useful way of representing a graph to a computer. In many applications of
graph theory, such as in electrical network analysis and operations research, matrices also turn
out to e the natural way of expressing the problem.

• Adjacency matrix

• Incidence matrix

• Undirected graphs and symmetric matrices

• Number of walks of a particular length between two vertices


Representing Graphs
Definition: Let G = (V, E) be a simple graph with |V| = n (no two edges connect
the same pair of vertices and each edge connects two different vertices).
Suppose that the vertices of G are listed in arbitrary order as v1, v2, …, vn.

The adjacency matrix A (or AG) of G, with respect to this listing of the vertices,
is the nn zero-one matrix with 1 as its (i, j) entry when vi and vj are adjacent,
and 0 otherwise.

In other words, for an adjacency matrix A = [aij],

aij = 1 if {vi, vj} is an edge of G,


aij = 0 otherwise.
Representing Undirected Graphs

Example: What is the adjacency matrix AG for the following graph G based on the
order of vertices a, b, c, d ?
a
Solution: b
d
0 1 1 1 c
1 0 0 1
AG = 
1 0 0 1
 
1 1 1 0

Note: Adjacency matrices of undirected graphs are always symmetric.


Representing Directed Graphs

Example: What is the adjacency matrix AG for the following directed graph G based
on the order of vertices a, b, c, d ?
a
d
b
Solution:
0 0 1 0
1 0 0 0 c
AG =  
0 0 0 0
 
1 1 1 0
Representing Pseudo Graphs

Example : What is the adjacency matrix AG for the following pseudo graph G based
on the order of vertices a, b, c, d ?

Solution:
Representing Graphs
Definition: Let G = (V, E) be an undirected graph with |V| = n. Suppose that the
vertices and edges of G are listed in arbitrary order as v1, v2, …, vn and e1, e2, …, em,
respectively.

The Incidence matrix of G with respect to this listing of the vertices and edges is
the nm zero-one matrix with 1 as its (i, j) entry when edge ej is incident with vi, and
0 otherwise.

In other words, for an incidence matrix M = [mij],

mij = 1 if edge ej is incident with vi


mij = 0 otherwise.
Representing Graphs
Example: What is the incidence matrix M for the following graph G based on the
order of vertices a, b, c, d and edges 1, 2, 3, 4, 5, 6?
a 1
Solution: 2
b
d 3
1 1 0 0 1 0 5
1 0 1 0 0 0 4 c
M = 
0 0 0 1 1 1 6
 
0 1 1 1 0 0

Note: Incidence matrices of undirected graphs contain two 1s per column for edges
connecting two vertices and one 1 per column for loops.
Shortest Path
In an edge-weighted graph, the weight of an edge measures the cost of traveling that
edge.
For example, in a graph representing a network of airports, the weights could
represent: distance, cost or time.
Such a graph could be used to answer any of the following:
What is the fastest way to get from A to B?
Which route from A to B is the least expensive?
What is the shortest possible distance from A to B?
Each of these questions is an instance of the same problem:
The shortest path problem!
Shortest Path by Dijkstra's Algorithm
• Dijkstra's algorithm solves the single-source shortest path problem for a
non-negative weights graph.

• It finds the shortest path from an initial vertex(say s) to all the other
vertices.

• Works on both directed and undirected graphs. However, all edges must
have nonnegative weights.
Dijkstra's Algorithm
Single-Source Shortest Path Problem - The problem of finding shortest paths from a
source vertex v to all other vertices in the graph.
Applications of Dijkstra's Algorithm

- Maps (Map Quest, Google Maps)


- Routing Systems
Dijkstra's Algorithm
Dijkstra's algorithm - is a solution to the single-source shortest path problem
in graph theory.

Works on both directed and undirected graphs. However, all edges must have
nonnegative weights.

Input: Weighted graph G={E,V} and source vertex v∈V, such that all edge
weights are nonnegative

Output: Lengths of shortest paths (or the shortest paths themselves) from a
given source vertex v∈V to all other vertices.
Approach of Dijkstra's Algorithm
• The algorithm computes for each vertex u the distance to u from the start
vertex v, that is, the weight of a shortest path between v and u.

• The algorithm keeps track of the set of vertices for which the distance has
been computed, called the cloud C.

• Every vertex has a label D associated with it. For any vertex u, D[u] stores
an approximation of the distance between v and u. The algorithm will
update a D[u] value when it finds a shorter path from v to u.

• When a vertex u is added to the cloud, its label D[u] is equal to the actual
(final) distance between the starting vertex v and vertex u.
Dijkstra's Algorithm Pseudocode
Dijkstra(v1, v2):
for each vertex v: // Initialization
v's distance := infinity.
v's previous := none.
v1's distance := 0.
List := {all vertices}.

while List is not empty:


v := remove List vertex with minimum distance.
mark v as known.
for each unknown neighbor n of v:
dist := v's distance + edge (v, n)'s weight.

if dist is smaller than n's distance:


n's distance := dist.
n's previous := v.

reconstruct path from v2 back to v1,


following previous pointers.
Example: Dijkstra's Algorithm
• Initialization

Distance(source) = 0 0 ∞ Distance (all vertices but


A
2
B source) = ∞

4 1 3 10

2 2 ∞
∞ C D E

5 8 ∞ 4 6

1
F G

∞ ∞

Pick vertex in List with minimum distance


Example: Dijkstra's Algorithm
• Update neighbours' distance

0 2
2
A B

4 1 3 10

2 2 ∞
∞ C D E

5 8 1 4 6

1
F G

∞ ∞
Example: Dijkstra's Algorithm
• Remove vertex with minimum distance

0 2
2
A B

4 1 3 10

2 2 ∞
∞ C D E

5 8 1 4 6

1
F G

∞ ∞

Pick vertex in List with minimum distance i.e. D


Example: Dijkstra's Algorithm
• Update neighbors

0 2
2
A B

4 1 3 10

2 2
3 C D E 3

5 8 1 4 6

Distance(C) = 1 + 2 = 3 1
F G
Distance(E) = 1 + 2 = 3
Distance(F) = 1 + 8 = 9 9 5
Distance(G) = 1 + 4 = 5
Example: Dijkstra's Algorithm
• Pick vertex in List with minimum distance (B) and update neighbors

0 2
2
A B

4 1 3 10

2 2
3 C D E 3

5 8 1 4 6
Note : distance(D) not updated since D is
1 already known and distance(E) not
F G
updated since it is larger than previously
9 5 computed
Example: Dijkstra's Algorithm
Pick vertex List with minimum distance (E) and update neighbors

0 2
2
A B

4 1 3 10

2 2
3 C D E 3

5 8 1 4 6

1
F G

9 5
Example: Dijkstra's Algorithm
Pick vertex List with minimum distance (C) and update neighbors

0 2
2
A B

4 1 3 10

2 2
3 C D E 3

5 8 1 4 6

1
F G

8 5
Example: Dijkstra's Algorithm
Pick vertex List with minimum distance (G) and update neighbors

0 2
2
A B

4 1 3 10

2 2
3 C D E 3

5 8 1 4 6

1
F G
Previous distance
6 5
Distance(F) = min (8, 5+1) = 6
Example: Dijkstra's Algorithm
• Example (end)

0 2
2
A B

4 1 3 10

2 2
3 C D E 3

5 8 1 4 6

1
F G

6 5

Pick vertex not in S with lowest cost (F) and update neighbors
THANK YOU

You might also like