Professional Documents
Culture Documents
SECOND YEAR
(III SEM. CSE/IT/CCE)
ACADEMIC YEAR: 2022-2023
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
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.
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.
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.
(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.
Level 1: 20 nodes
Level 2: 21 nodes
Level 3: 22 nodes
Property of Binary Tree (II)
By property : n 2h-1
Thus, 2h n+1
That is, h log2 (n+1)
Distance & Centre
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
• Adjacency matrix
• Incidence matrix
The adjacency matrix A (or AG) of G, with respect to this listing of the vertices,
is the nn zero-one matrix with 1 as its (i, j) entry when vi and vj are adjacent,
and 0 otherwise.
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
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 nm zero-one matrix with 1 as its (i, j) entry when edge ej is incident with vi, and
0 otherwise.
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
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}.
4 1 3 10
2 2 ∞
∞ C D E
5 8 ∞ 4 6
1
F G
∞ ∞
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
∞ ∞
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