0% found this document useful (0 votes)
41 views

IT202-DS-Unit 4 - Non-Linear-Data-Structures

The document discusses non-linear data structures, specifically trees and graphs. It provides definitions and terminology for binary trees, including root, leaf, internal and external nodes, height, depth, siblings, and tree traversal methods like preorder, inorder, and postorder. Binary tree representations as arrays and linked lists are also covered.

Uploaded by

Bhuvaneshwari B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

IT202-DS-Unit 4 - Non-Linear-Data-Structures

The document discusses non-linear data structures, specifically trees and graphs. It provides definitions and terminology for binary trees, including root, leaf, internal and external nodes, height, depth, siblings, and tree traversal methods like preorder, inorder, and postorder. Binary tree representations as arrays and linked lists are also covered.

Uploaded by

Bhuvaneshwari B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 110

IT202 – DS - Unit 4

Non-Linear Data Structures

1
Unit 4 - Syllabus
Non-Linear Data Structures
Binary tree – array and linked implementation of binary trees –
application of trees – tree traversals
Graphs – representation – breadth first search – depth first search –
spanning trees – application of graphs
Tree Structures

Trees are data structures that store elements hierarchically


Tree Terminology - root, branches, leaves, forest, non leaf, height, subtree, left child, right child, parent, ancestor,
descendent, path
Tree terminology
Root node: Topmost distinguished node in the
tree which does not have a parent

Sub-Trees: T1, T2 & T3 are called sub trees

Leaf node : A node that has no children is


called the leaf node or the terminal node.

Path: A sequence of consecutive edges is


called a path. For example, the path from the
root node A to node I is given as: A, D, and I.
Tree terminology
Ancestor node: An ancestor of a node is any
predecessor node on the path from root to
that node. Nodes A, C, and G are the
ancestors of node K.

Descendant node: A descendant node is


any successor node on any path from the
node to a leaf node. Leaf nodes do not have
any descendants. Nodes C, G, J, and K are
the descendants of node A.
Tree terminology
Level number: Every node in the tree is
assigned a level number in such a way that
• the root node is at level 0,
• children of the root node are at level
number 1.
Thus, every node is at one level higher than
its parent. So, all child nodes have a level
number given by parent’s level number + 1.

Degree: Degree of a node is equal to the


number of children that a node has. The
degree of a leaf node is zero.
I. General Trees
A node in a general tree (except the leaf nodes) may
have zero or more sub-trees.

The number of sub-trees for any node may vary.

For example, node D has 1 sub-tree, whereas Q has 3


sub-trees.

The unlimited number of sub trees for a node


becomes a problem for tree representation and
other operations – such as searching, traversing,
adding, and deleting
II. Forest

A forest is a disjoint union of trees. A


set of disjoint trees (or forests) is
obtained by deleting the root and the
edges connecting the root node to
nodes at level 1.
III. Binary Trees

• In a binary tree, the topmost element is called the


root node, and each node has 0, 1, or at the most 2
children.
• A node that has zero children is called a leaf node or a
terminal node.
• Every node contains a data element, a left pointer
which points to the left child, and a right pointer
which points to the right child.
III. Binary Trees

• In the tree, root node 1 has two successors: 2 and 3.


• Node 2 has two successor nodes: 4 and 5.
• Node 4 has two successors: 8 and 9.
• Node 5 has no successor.
• Node 3 has two successor nodes: 6 and 7.
• Node 6 has two successors: 10 and 11.
• Finally, node 7 has only one successor: 12.
III. Binary Trees - Terminology

Parent
• If N is any node in T that has left successor S1
and right successor S2, then N is called the
parent of S1 and S2.
• Correspondingly, S1 and S2 are called the left
child and the right child of N.
• Every node other than the root node has a
parent.
III. Binary Trees - Terminology
Level number

• Every node in the binary tree is assigned a level


number.

• The root node is defined to be at level 0.

• The left and the right child of the root node have a
level number 1.

• Similarly, every node is at one level higher than its


parents. So all child nodes are defined to have level
number as parent's level number + 1.
III. Binary Trees - Terminology
Degree of a node

• It is equal to the number of children that a node has.

• For example, in the tree, degree of node 4 is 2,


degree of node 5 is zero and degree of node 7 is 1.
III. Binary Trees - Terminology
Sibling
• All nodes that are at the same level and share the
same parent are called siblings (brothers).
• For example, nodes 2 and 3; nodes 4 and 5; nodes 6
and 7; nodes 8 and 9; and nodes 10 and 11 are
siblings.
III. Binary Trees - Terminology
Leaf and non Leaf nodes
• A node that has no children is called a leaf node or a
terminal node. The leaf nodes in the tree are: 8, 9, 5,
10, 11, and 12.
• A node that has at least one child is called a non-leaf
node or a non-terminal node. The non-leaf nodes in
the tree are: 1, 3, 4 and 6
III. Binary Trees - Terminology
Depth
• The depth of a node N is given as the length of the
path from the root R to the node N.
• The depth of the root node is zero.

Height of a tree
• It is the total number of nodes on the path from the
root node to the deepest node in the tree.
• A tree with only a root node has a height of 1.
III. Binary Trees - Terminology
21 – 1 = 1 • A binary tree of height h has at least h nodes
and at most 2h – 1 nodes.
• This is because every level will have at least one
Tree Height 22 – 1 = 3
node and can have at most 2 nodes.
log2n • So, if every level has two nodes then a tree with
3
2 –1=7 height h will have at the most 2h – 1 nodes as at
level 0, there is only one element called the
24 – 1 = 15 root.
• The height of a binary tree with n nodes is at
least log2(n+1) and at most n

Tree Height Derivation


Number of nodes in a binary tree = n
n = 2r
r = log2n
III. Binary Tree - Variants

Complete Binary Tree Left Skewed Binary Tree Right Skewed Binary Tree
Complete Binary Trees

A complete binary tree is a


binary tree with
◦ every level, except possibly the last, is
completely filled
◦ all nodes appear as far left as possible
Binary tree Vs extended binary tree
In an extended binary tree,
◦ nodes having children are called internal
nodes
◦ nodes having no children are called
external nodes.

Internal nodes are represented


using circles and the external
nodes are represented using
squares.
Binary Tree Representation - 1
Sequential representation using array
A one-dimensional array, is used to store
the elements of tree.
The root of the tree will be stored in the
first location
The children of a node stored in location
K will be stored in locations (2 × K) and (2
× K+1).
The maximum size of the array is given
as (2h–1), where h is the height of the
tree.
An empty tree or sub-tree is specified
using NULL. If array’s first element is
NULL, then the tree is empty.
Binary Tree Representation - 2

Linked representation
of binary trees
struct node {
struct node *left;
int data;
struct node *right;
};
TRAVERSING A BINARY TREE
◦ Traversing a binary tree is the process of visiting each node in the tree exactly once in a
systematic way.
◦ Unlike linear data structures in which the elements are traversed sequentially, tree is a
nonlinear data structure in which the elements can be traversed in many different ways.
◦ There are different algorithms for tree traversals.
◦ These algorithms differ in the order in which the nodes are visited.
◦ Pre-order Traversal –> D-L-R
◦ In-order Traversal -> L-D-R
◦ Post-order Traversal -> L-R-D
Pre-order Traversal
To traverse a non-empty binary tree in pre-order, the following
operations are performed recursively at each node.
◦ 1. Visiting the root node,
◦ 2. Traversing the left sub-tree
◦ 3. Traversing the right sub-tree

Preorder(TREE)
Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: Write TREE-> DATA
Step 3: PREORDER(TREE->LEFT)
Step 4: PREORDER(TREE-> RIGHT)
[END OF LOOP]
Step 5: END
Preorder traversal (D-L-R)

A, B, D, G, H, L, E, C, F, I, J, and K

A, B, D, C, E, F, G, H, and I
In-order Traversal (L-D-R)
◦ 1. Traversing the left sub-tree,
◦ 2. Visiting the root node, and finally
◦ 3. Traversing the right sub-tree.

Step 1: Repeat Steps 2 to 4 while TREE != NULL


Step 2: INORDER(TREE-> LEFT)
Step 3: Write TREE->DATA
Step 4: INORDER(TREE->RIGHT)
[END OF LOOP]
Step 5: END
Inorder traversal (L-D-R)

G, D, H, L, B, E, A, C, I, F, K, and J

B, D, A, E, H, G, I, F, and C
Post-order Traversal (L-R-D)
1. Traversing the left sub-tree,
2. Traversing the right sub-tree, and finally
3. Visiting the root node.

Step 1: Repeat Steps 2 to 4 while TREE != NULL


Step 2: POSTORDER(TREE LEFT)
Step 3: POSTORDER(TREE RIGHT)
Step 4: Write TREE DATA
[END OF LOOP]
Step 5: END
Post-order traversal (L-R-D)

G, L, H, D, E, B, I, K, J, F, C, and A

D, B, H, I, G, F, E, C, and A
Quiz
The inorder and preorder traversal of a binary tree are d b e a f c g
and a b d e c f g, respectively. The postorder traversal of the binary
tree is _________.
In order : d b e a f c g - LDR
Preorder: a b d e c f g - DLR
Quiz
The inorder and preorder traversal of a binary tree are d b e a f c g
and a b d e c f g, respectively. The postorder traversal of the binary
tree is _________.
In order : d b e a f c g - LDR
Preorder: a b d e c f g - DLR

a
Quiz
The inorder and preorder traversal of a binary tree are d b e a f c g
and a b d e c f g, respectively. The postorder traversal of the binary
tree is _________.
In order : d b e a f c g - LDR
Preorder: a b d e c f g - DLR

cfg
Quiz
The inorder and preorder traversal of a binary tree are d b e a f c g
and a b d e c f g, respectively. The postorder traversal of the binary
tree is _________.
In order : d b e a f c g - LDR
Preorder: a b d e c f g - DLR

a
In : d b e In : f c g
Pre: b d e Pre: c f g
Quiz
The inorder and preorder traversal of a binary tree are d b e a f c g
and a b d e c f g, respectively. The postorder traversal of the binary
tree is _________.
In order : d b e a f c g - LDR
Preorder: a b d e c f g - DLR

a
In : d b e In : f c g
Pre: b d e Pre: c f g
Quiz
The inorder and preorder traversal of a binary tree are d b e a f c g
and a b d e c f g, respectively. The postorder traversal of the binary
tree is _________.
In order : d b e a f c g - LDR
Preorder: a b d e c f g - DLR

a
b c
In : d b e In : f c g
Pre: b d e d e f g Pre: c f g
Quiz
The inorder and preorder traversal of a binary tree are d b e a f c g
and a b d e c f g, respectively. The postorder traversal of the binary
tree is _________.
In order : d b e a f c g - LDR
Preorder: a b d e c f g - DLR

a
b c

d e f g

(A) e d b g f c a
(B) d e f g b c a
(C) d e b f g c a
(D) d e b f g a c
Quiz
The inorder and preorder traversal of a binary tree are d b e a f c g
and a b d e c f g, respectively. The postorder traversal of the binary
tree is _________.
In order : d b e a f c g - LDR
Preorder: a b d e c f g - DLR

a
b c

d e f g

(A) e d b g f c a
(B) d e f g b c a
(C) d e b f g c a
(D) d e b f g a c
Applications of Trees
Trees are used to store simple as well as complex data
B-trees are prominently used to store data on external storage.
Trees are an important data structure used for compiler construction.
Trees are also used in database design.
Trees are used in file system directories.
Trees are also widely used for information storage and retrieval in
symbol tables
Nim Game tree
Optimal Decision Tree – 8 coins problem
Expression Trees
Binary trees are widely used to store algebraic expressions

Exp = (a – b) + (c * d) Exp = ((a + b) – (c * d)) % ((f ^g) / (h – i))


Expression Trees
• Given the expression, Exp = a + b / c * d – e, construct the corresponding binary tree.
• Use the operator precedence chart to find the sequence in which operations will be
performed.
• The given expression can be written as
Exp = ((a + ((b/c) * d)) – e)
Graph Data Structure
It is an abstract data structure that is used to implement the
mathematical concept of graphs.
It is basically a collection of vertices (also called nodes) and edges
that connect these vertices.
Graphs are widely used to model any situation where entities or
things are related to each other in pairs.
For example
Transportation networks in which nodes are airports, ports, stations
etc. The edges can be airline flights, one-way roads, shipping routes,
etc.
Graph - Definition
A graph G is defined as an ordered set (V, E), where V(G) represents
the set of vertices and E(G) represents the edges that connect these
vertices.
Figure shows a graph with V(G) = {A, B, C, D and E} and E(G) = {(A, B),
(B, C), (A, D), (B, D), (D, E), (C, E)}.
Note that there are five vertices or nodes and six edges in the graph.
Graph Terminology

A graph can be directed or undirected.


In an undirected graph, edges do not have any direction associated
with them. That is, if an edge is drawn between nodes A and B, then
the nodes can be traversed from A to B as well as from B to A..
In a directed graph, edges form an ordered pair. If there is an edge
from A to B, then there is a path from A to B but not from B to A.

Undirected Directed
graph graph
Graph Terminology
Adjacent nodes or neighbors
For every edge, e = (u,v) that connects nodes u and v, the nodes
u and v are the end-points and are said to be the adjacent nodes or
neighbours.

Degree of a node
Degree of a node u, deg(u), is the total number of edges containing
the node u. If deg(u) = 0, it means that u does not belong to any
edge and such a node is known as an isolated node.
Graph Terminology
Path
A path P written as P = {v0, v1, v2, ..., vn), of length n from a node u to v
is defined as a sequence of (n+1) nodes. Here, u = v0, v = vn and vi–1 is
adjacent to vi for i = 1, 2, 3, ..., n.

Closed path
A path P is known as a closed path if the edge has the same end-points.
That is, if
v0 = vn.
Graph Terminology
Simple path
A path P is known as a simple path if all the nodes in the path are distinct with an
exception that v0 may be equal to vn. If v0 = vn, then the path is called a closed
simple path.
Cycle
A path in which the first and the last vertices are same. A simple cycle has no
repeated edges or vertices (except the first and last vertices).
Graph Terminology
Connected graph
A graph is said to be connected if for any two vertices (u, v) in V, there is a path
from u to v. That is to say that there are no isolated nodes in a connected graph. A
connected graph that does not have any cycle is called a tree. Therefore, a tree is
treated as a special graph

(a) (b) (c)


Graph Terminology

Complete graph
A graph G is said to be complete, if all its nodes are fully connected. That is,
there is a path from one node to every other node in the graph. A complete graph
has n(n–1)/2 edges, where n is the number of nodes in G.
Graph Terminology
Labelled graph or weighted graph
A graph is said to be labelled if every edge in the graph is assigned some data. In a
weighted graph, the edges of the graph are assigned some weight or length. The
weight of an edge denoted by w(e) is a positive value which indicates the cost of
traversing the edge.
Graph Terminology
Multi-graph
A graph with multiple edges and/or loops is called a multi-graph.
Graph Terminology
Out-degree of a node
The out-degree of a node u, written as outdeg(u), is the number of edges that
originate at u.
In-degree of a node
The in-degree of a node u, written as indeg(u), is the number of edges that
terminate at u.
Degree of a node
The degree of a node, written as deg(u), is equal to the sum of in-degree and
out-degree of that node. Therefore, deg(u) = indeg(u) + outdeg(u).
Graph Terminology
Strongly connected directed graph
A digraph is said to be strongly connected if and only if there exists a path between
every pair of nodes in G. That is, if there is a path from node u to v, then there
must be a path from node v to u.
Graph Terminology
Transitive Closure of a Directed Graph
For a directed graph G = (V,E), where V is the set of vertices and E is
the set of edges, the transitive closure of G is a graph G* = (V,E*). In
G*, for every vertex pair v, w in V there is an edge (v, w) in E* if and
only if there is a valid path from v to w in G.
Graph Terminology
Transitive Closure of a Directed Graph - Applications

To find the reachability analysis of transition networks representing


distributed and parallel systems.
It is used in the construction of parsing automata in compiler construction.
To evaluate recursive database queries (because almost all practical
recursive queries are transitive in nature).
Graph Terminology
BI-CONNECTED components
A bi-connected graph is defined as a connected
graph that has no articulation vertices. That is, a
bi-connected graph is connected and non-
separable in the sense that even if we remove any
vertex from the graph, the resultant graph is still
connected.
Graph Terminology
BI-CONNECTED components
By definition, bi-connected undirected graph is a
connected graph that cannot be broken into
disconnected pieces by deleting any single vertex.
Articulation points and bridges
Articulation points and bridges
Articulation points and bridges
Bi-connected component
Graph Representation – using Adjacency Matrix
In an adjacency matrix, the rows and columns are labelled by graph
vertices.
An entry aij in the adjacency matrix will contain 1, if vertices vi and vj
are adjacent to each other. Otherwise, aij will be set to zero.
Graph Representation – using Adjacency Matrix
Graph Representation – using Adjacency List
Graph Traversal Methods
1. Breadth First Traversal
Start traversing from the source node and traverse the graph layer/level wise thus
exploring the neighbour nodes (nodes which are directly connected to source node).
We must move to the next layer/level only after traversing the current layer/level
completely.
A queue is used in the traversal process.
In short:
1. Move horizontally and visit all the nodes of the current layer/level.
2. Move to the next layer.
Graph Traversal Methods
2. Depth First Traversal
Depth first Traversal (DFT) algorithm starts with the source node of the graph G, and
then goes deeper and deeper until we find the node which has no children. The
algorithm, then backtracks from the dead end towards the most recent node that is
yet to be explored.
The data structure which is being used is stack.
Graph Traversal - Example

b c

d e f g

h i j k
DFT Vs BFT

DFT Sequence: a, b, d, h, e, i, j, c, f, k, g BFT Sequence: a, b, c, d, e, f, g, h, I, j, k


Graph Traversal Methods - Exercise
Find the Depth First and Breadth First Traversal
sequence of the following graphs.
Graph Traversal Methods - Exercise
Find the Depth First and Breadth First Traversal
sequence of the following graphs.

Depth First Traversal Sequence : A, B, E, C, G, H, I, D


Graph Traversal Methods - Exercise
Find the Depth First and Breadth First Traversal
sequence of the following graphs.

Breadth First Traversal Sequence : A, B, D, E, C, G, H, I


Dijkstra’s Algorithm – Single Source Shortest Path Distance
Algorithm
◦ Dijkstra’s algorithm, given by the Dutch scientist Edsger Dijkstra to find the length of an
optimal path distance between a source node to all other destination nodes (Optimal -
shortest, cheapest, or fastest) in a graph

◦ This algorithm is widely used in network routing protocols.

◦ Dijkstra’s algorithm is used when the graph has positive weights only. It is faster than the
other single source shortest path algorithms.
Dijkstra’s Algorithm – Single Source Shortest Path Distance
Algorithm
45 Input Parameters

10 i. Source vertex = 1 (vertex 1)


50
1 2 3 ii. Cost matrix
0 50 45 10 ∞
10 35 ∞ 0 10 15 ∞
20 15 20 30 Cost[5, 5]= ∞ ∞ 0 ∞ 30
20 ∞ ∞ 0 15
4 15
5 ∞ 20 35 15 0
iii. Number of vertices = 5
Algorithm steps
Output Parameter
1. Generate the shortest path to the nearest vertex and find distance
Distance array
2. Generate the shortest path to the 2nd nearest vertex and find distance
3. Generate the shortest path to the 3rd nearest vertex, etc
To do this, determine
a. The next vertex (to be visited) to generate shortest path
b. Find the shortest path distance to this vertex
Dijkstra’s Algorithm – Single Source Shortest Path Distance Algorithm
45
Algorithm ShortestPaths(v, cost, n, dist)
{
for i = 1 to n do 50 10
1 2 3
{ S[i] = false; dist[i] = cost[v,i]; }
S[v] = true; dist[v] = 0; 10 35
20 15 20
for num = 2 to n-1 do 30
{ Choose u among the vertices not true in S such that dist[u] is minimum
S[u] = true; 4 15 5
for(each w adjacent to u with S[w] = false) do
if( dist[w] > (dist[u] + cost[u,w]) ) then 0 50 45 10 ∞
dist[w] = dist[u] + cost[u,w] ∞ 0 10 15 ∞
} Cost[5, 5]= ∞ ∞ 0 ∞ 30
} 20 ∞ ∞ 0 15
∞ 20 35 15 0

1 2 3 4 5
𝑚𝑖𝑛
𝑑𝑖𝑠𝑡𝑘 𝑤 = 𝑢𝑛𝑣𝑖𝑠𝑖𝑡𝑒𝑑 𝑢 {𝑑𝑖𝑠𝑡
𝑘−1
𝑤 , 𝑑𝑖𝑠𝑡𝑘−1 𝑢 + 𝑐𝑜𝑠𝑡 𝑢, 𝑤 } S T F F F F
dist 0 50 45 10 ∞
Dijkstra’s Algorithm – Single Source Shortest Path Distance Algorithm
45
Algorithm ShortestPaths(v, cost, n, dist)
{
for i = 1 to n do 50 10
1 2 3
{ S[i] = false; dist[i] = cost[v,i]; }
S[v] = true; dist[v] = 0; 10 35
20 15 20
for num = 2 to n-1 do 30
{ Choose u among the vertices not true in S such that dist[u] is minimum
S[u] = true; 4 15 5
for(each w adjacent to u with S[w] = false) do
if(dist[w] > (dist[u] + cost[u,w])) then 0 50 45 10 ∞
dist[w] = dist[u] + cost[u,w] ∞ 0 10 15 ∞
} Cost[5, 5]= ∞ ∞ 0 ∞ 30
} 20 ∞ ∞ 0 15
∞ 20 35 15 0

num=2 1 2 3 4 5 num=2 1 2 3 4 5
S T F F T F S T F F F F
dist 0 50 45 10 25 dist 0 50 45 10 ∞
Dijkstra’s Algorithm – Single Source Shortest Path Distance Algorithm
45
Algorithm ShortestPaths(v, cost, n, dist)
{
for i = 1 to n do 50 10
1 2 3
{ S[i] = false; dist[i] = cost[v,i]; }
S[v] = true; dist[v] = 0; 10 35
20 15
for num = 2 to n-1 do 20 30
{ Choose u among the vertices not true in S such that dist[u] is minimum
S[u] = true; 4 15 5
for(each w adjacent to u with S[w] = false) do
if(dist[w] > (dist[u] + cost[u,w])) then 0 50 45 10 ∞
dist[w] = dist[u] + cost[u,w] ∞ 0 10 15 20
} Cost[5, 5]= ∞ ∞ 0 ∞ 30
} 20 ∞ ∞ 0 15
∞ 20 35 15 0

num=3 1 2 3 4 5 num=3 1 2 3 4 5
S T F F T T S T F F T F
dist 0 45 45 10 25 dist 0 50 45 10 25
Dijkstra’s Algorithm – Single Source Shortest Path Distance Algorithm
45
Algorithm ShortestPaths(v, cost, n, dist)
{
for i = 1 to n do 50 10
1 2 3
{ S[i] = false; dist[i] = cost[v,i]; }
S[v] = true; dist[v] = 0; 10 35
20 15
for num = 2 to n-1 do 20 30
{ Choose u among the vertices not true in S such that dist[u] is minimum
S[u] = true; 4 15 5
for(each w adjacent to u with S[w] = false) do
if(dist[w] > (dist[u] + cost[u,w])) then 0 50 45 10 ∞
dist[w] = dist[u] + cost[u,w] ∞ 0 10 15 20
} Cost[5, 5]= ∞ ∞ 0 ∞ 30
} 20 ∞ ∞ 0 15
∞ 20 35 15 0

num=4 1 2 3 4 5 num=4 1 2 3 4 5
1 2 3 4 5
S T T F T T S T F F T T
dist 0 45 45 10 25
dist 0 45 45 10 25 dist 0 45 45 10 25
Output
Applications of Graph
1. In circuit networks where points of connection are drawn as vertices and component wires
become the edges of the graph.
2. In transport networks where stations are drawn as vertices and routes become the edges of the
graph.
3. In maps that draw cities/states/regions as vertices and adjacency relations as edges.
4. In program flow analysis where procedures or modules are treated as vertices and calls to these
procedures are drawn as edges of the graph.
5. A graph of a particular concept can be used for finding shortest paths, project planning, etc.
6. In flowcharts or control-flow graphs, the statements and conditions in a program are represented
as nodes and the flow of control is represented by the edges.
7. In state transition diagrams, the nodes are used to represent states and the edges represent legal
moves from one state to the other.
8. Graphs are also used to draw activity network diagrams. These diagrams are extensively used as a
project management tool to represent the interdependent relationships between groups, steps,
and tasks that have a significant impact on the project.
Spanning Tree
A spanning tree is a sub-graph of an undirected connected graph, which
includes all the vertices of the graph with a minimum possible number of
edges. If a vertex is missed, then it is not a spanning tree.

Minimum spanning Tree


A minimum spanning tree 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.
Spanning Tree - Example
Finding Minimum Spanning Tree of a Graph – Kruskal’s Method
Kruskal’s Algorithm stepwise
10 1 28 1. Sort the edges in ascending order of
6 cost
14 2
25 24 7 16
2. Add an edge from the sorted list in
5
order to the resulting tree if it is not
18 3
forming a cycle (until the list is
22 4 12 exhausted)
3. The resulting tree is a minimum
spanning tree
4. Find the minimum total cost of the
tree
Kruskal’s Algorithm stepwise
10 1
Edges in sorted order
28
6 1. (1,6) - 10
14 2
25 2. (3,4) - 12
24 7 16
3. (2,7) - 14
5 18 3
4. (2,3) – 16
22 4 12
5. (7,4) – 18
6. (5,4) – 22
7. (5,7) – 24
8. (5,6) – 25
9. (1,2) - 28
Kruskal’s Algorithm stepwise
1
Edges in sorted order
6 1. (1,6) - 10
2
2. (3,4) - 12
7
3. (2,7) - 14
5 3
4. (2,3) – 16
4
5. (7,4) – 18
6. (5,4) – 22
7. (5,7) – 24
8. (5,6) – 25
9. (1,2) - 28
Kruskal’s Algorithm stepwise
10 1
Edge inclusion – in order without cycle
6 1. (1,6) - 10
2
2. (3,4) - 12
7
3. (2,7) - 14
5 3
4. (2,3) – 16
4
5. (7,4) – 18
6. (5,4) – 22
7. (5,7) – 24
8. (5,6) – 25
9. (1,2) - 28
Kruskal’s Algorithm stepwise
10 1
Edge inclusion – in order without cycle
6 1. (1,6) - 10
2
2. (3,4) - 12
7
3. (2,7) - 14
5 3
12 4. (2,3) – 16
4
5. (7,4) – 18
6. (5,4) – 22
7. (5,7) – 24
8. (5,6) – 25
9. (1,2) - 28
Kruskal’s Algorithm stepwise
10 1
Edge inclusion – in order without cycle
6 1. (1,6) - 10
14 2
2. (3,4) - 12
7
3. (2,7) - 14
5 3
12 4. (2,3) – 16
4
5. (7,4) – 18
6. (5,4) – 22
7. (5,7) – 24
8. (5,6) – 25
9. (1,2) - 28
Kruskal’s Algorithm stepwise
10 1
Edge inclusion – in order without cycle
6 1. (1,6) - 10
14 2
2. (3,4) - 12
7 16
3. (2,7) - 14
5 3
12 4. (2,3) – 16
4
5. (7,4) – 18
6. (5,4) – 22
7. (5,7) – 24
8. (5,6) – 25
9. (1,2) - 28
Kruskal’s Algorithm stepwise
10 1
Edge inclusion – in order without cycle
6 1. (1,6) - 10
14 2
2. (3,4) - 12
7 16
3. (2,7) - 14
5 3
12 4. (2,3) – 16
4
5. (7,4) – 18 – forming a cycle
6. (5,4) – 22
7. (5,7) – 24
8. (5,6) – 25
9. (1,2) - 28
Kruskal’s Algorithm stepwise
10 1
Edge inclusion – in order without cycle
6 1. (1,6) - 10
14 2
2. (3,4) - 12
7 16
3. (2,7) - 14
5 3
12 4. (2,3) – 16
22 4
5. (7,4) – 18 – forming a cycle
6. (5,4) – 22
7. (5,7) – 24
8. (5,6) – 25
9. (1,2) - 28
Kruskal’s Algorithm stepwise
10 1
Edge inclusion – in order without cycle
6 1. (1,6) - 10
14 2
2. (3,4) - 12
7 16
3. (2,7) - 14
5 3
12 4. (2,3) – 16
22 4
5. (7,4) – 18 – forming a cycle
6. (5,4) – 22
7. (5,7) – 24 – forming a cycle
8. (5,6) – 25
9. (1,2) - 28
Kruskal’s Algorithm stepwise
10 1
Edge inclusion – in order without cycle
6 1. (1,6) - 10
14 2
25
2. (3,4) - 12
7 16
3. (2,7) - 14
5 3
12 4. (2,3) – 16
22 4
5. (7,4) – 18 – forming a cycle
6. (5,4) – 22
7. (5,7) – 24 – forming a cycle
8. (5,6) – 25
9. (1,2) - 28
Kruskal’s Algorithm stepwise
10 1
Edge inclusion – in order without cycle
6 1. (1,6) - 10
14 2
25
2. (3,4) - 12
7 16
3. (2,7) - 14
5 3
12 4. (2,3) – 16
22 4
5. (7,4) – 18 – forming a cycle
6. (5,4) – 22
7. (5,7) – 24 – forming a cycle
8. (5,6) – 25
9. (1,2) – 28 – forming a cycle
Kruskal’s Algorithm stepwise
The minimum Cost Tree
10 1
Edge inclusion – in order without cycle
6 1. (1,6) - 10
14 2
25
2. (3,4) - 12
7 16
3. (2,7) - 14
5 3
12 4. (2,3) – 16
22 4
5. (7,4) – 18 – forming a cycle
Total cost of the tree= 10+ 12+14+16+22+25 =99
6. (5,4) – 22
7. (5,7) – 24 – forming a cycle
8. (5,6) – 25
9. (1,2) – 28 – forming a cycle
Finding Minimum Spanning Tree of a Graph - Prim’s Algorithm
Prim’s Algorithm stepwise
10 1 28 1. Initialize visited set as empty, unvisited set as set
6 of all vertices in the graph
14 2 2. Choose the shortest edge – include it in the result
25 24 7 16 3. Include these two vertices in visited set
5 18 3 4. Find the set of edges from visited set of vertices to
22
unvisited set of vertices
4 12
- Choose the edge with least cost
- Include it in the result
- Move this unvisited vertex to visited set
5. Repeat step 4 until unvisited set is empty
6. Find the minimum total cost of the tree
Prim’s Algorithm stepwise
10 1 Visited set = { }
28
Unvisited set = {1, 2, 3, 4, 5, 6, 7)
6
14 2
25 24 7 16
5 18 3
22 4 12
Prim’s Algorithm stepwise
10 1 Visited set = { 1,6} – 2 vertices of first least costed edge
28
Unvisited set = {2, 3, 4, 5, 7)
6
14 2
25 24 7 16
5 18 3
22 4 12
Prim’s Algorithm stepwise
10 1 Visited set = { 1,6,5}
28
Unvisited set = {2, 3, 4, 7)
6
14 2
25 24 7 16
5 18 3
22 4 12
Prim’s Algorithm stepwise
10 1 Visited set = { 1,6,5,4}
28
Unvisited set = {2, 3, 7)
6
14 2
25 24 7 16
5 18 3
22 4 12
Prim’s Algorithm stepwise
10 1 Visited set = { 1,6,5,4,3}
28
Unvisited set = {2, 7)
6
14 2
25 24 7 16
5 18 3
22 4 12
Prim’s Algorithm stepwise
10 1 Visited set = { 1,6,5,4,3,2}
28
Unvisited set = {7}
6
14 2
25 24 7 16
5 18 3
22 4 12
Prim’s Algorithm stepwise
10 1 Visited set = { 1,6,5,4,3,2,7}
Unvisited set = { }
6 All vertices are visited, hence iteration stops
14 2
Total cost of the tree = 10+25+22+12+16+14=99
25 No testing of cycle formation
7 16
5 3
22 4 12
Practicing Exercise
Given the memory representation of a tree that stores the names of
family members, construct the corresponding tree from the given
data
Practicing Exercise
Given the binary tree, write down the expression that it represents in
infix, postfix and prefix form (use inorder, postorder and preorder
traversal algorithms)
Practicing Exercise
Consider the binary search tree given below to do the following operations:
◦ Find the result of in-order, pre-order, and post-order traversals.
◦ Show the deletion of the root node
◦ Insert 11, 22, 33, 44, 55, 66, and 77 in the tree
Practicing Exercise
Create a binary search tree with the input given below:
98, 2, 48, 12, 56, 32, 4, 67, 23, 87, 23, 55, 46

◦ (a) Insert 21, 39, 45, 54, and 63 into the tree
◦ (b) Delete values 23, 56, 2, and 45 from the tree
Practicing Exercise
Consider five cities: (1) New Delhi, (2) Mumbai, (3) Chennai, (4)
Bangalore, and (5) Kolkata, and a list of flights that connect these
cities as shown in the following table. Use the given information to
construct a graph.
Practicing Exercise
Given the following adjacency matrix, draw the weighted graph.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy