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

Graph Tree Final

Graphs and trees are examples of non-linear data structures. A graph G consists of two sets: a set of vertices V and a set of edges E that connect the vertices. An undirected graph has unordered edge pairs, while a directed graph has ordered edge pairs with a head and tail vertex. Finding the shortest path between vertices in a graph is a non-trivial problem that can be solved using algorithms like Dijkstra's or Floyd-Warshall's.
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)
132 views

Graph Tree Final

Graphs and trees are examples of non-linear data structures. A graph G consists of two sets: a set of vertices V and a set of edges E that connect the vertices. An undirected graph has unordered edge pairs, while a directed graph has ordered edge pairs with a head and tail vertex. Finding the shortest path between vertices in a graph is a non-trivial problem that can be solved using algorithms like Dijkstra's or Floyd-Warshall's.
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/ 124

Graph and Tree

Non-linear data structures


• In linear data structure, manipulation of data
(insertion, deletion, traversal) is possible in
linear time.
• In non-linear data structure manipulation of
data may not be possible in linear run-time.
• Examples-
- Graph
- Tree
- Lattice
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph
G := (V, E)
)
A V := {v1, v2, ….., vn}
C Order = |V|=
|V|=nn
e1
E := {e1, e2, ….., em}
E
B Size = |E|= m

Edges are denoted


D F by a pair of vertices
e1 = (A, B) = (B, A)
H - unordered pair
G
- undirected edges

This type of graph is called undirected graph


Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph
V = {A, B, C, D, E, F, G, H}
A
C
|V| = n = 8
e1
E
E = {(A,B), (B,C), (B,D),
B
(C,D), (C,E), (C,G), (D,E),
(D,G), (D,H), (E,F)}
D F |E| = m = 10

H
Some representation shows both
G ways of an edge, hence the count
becomes redundant and actual m
value is half the number of edges
listed
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph
Edges may also be
A
denoted by a pair of
C
e1 e2 vertices
e1 = (B, A) ≠ (A, B)
E
B e2 = (C, B) ≠ (B, C)
e3 e3 = (D, B) = (B, D)
D
- ordered pair
F
- directed edges
H
G
This type of graph is
called directed graph
or digraph
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph
V = {A, B, C, D, E, F, G, H}
A
|V| = n = 8
C
e1 e2
E = {(B,A), (B,D), (C,B),
E
B (D,B), (D,C), (D,H), (E,C),
e3 (E,D), (F,E), (G,C), (G,D)}
D F |E| = m = 11

H
G

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Graph - Definitions
 A graph, G, consists of two sets, V and E. V is a finite,
nonempty set of vertices. E is a set of pairs of vertices;
these pairs are called edges.

 V(G) and E(G) represent the sets of vertices and edges,


respectively, of graph G. We write G=(V,E) to represent a
graph.

 In an undirected graph the pair of vertices representing


any edge is unordered. In a directed graph each edge is
represented by a directed/ ordered pair (u, v); u is the tail
and v the head of the edge.
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph - Definitions
 Self-loops or edges- If there exists an edge where,
(u, u) E, i.e, the edge starts from and ends at the same
node, it is called a self-loop/edge. This is not permitted/
legal in regular graphs.

 Multiplex/ Multi-graph: A graph consisting of multiple


edges between same pair of nodes is called a multi-graph.
2 multi-graph 2
1 1
3 3
4 simple graph 4

 Simple graph- Does not have multiple edges between


pair of nodes. Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph - Definitions
 Weight of edges- In some of the graphs, edges have
certain values w(u, v) assigned to them (u, v) E. This
weight is sometimes defined to be the cost of traversing an
edge. Graphs having weighted edges are called weighted
graphs.
Multi-graphs can be converted to simple graphs by
assigning uniform weights to all edges.
2 multi-graph 2 2
1 1 1
3 3
4
simple graph 4 3
 Graph having no weighted edge in it, is called un-
weighted graph Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph - Definitions
 The maximum # of distinct unordered pairs (u, v) with
u≠v, in a graph with n, vertices is n(n-1)/2. An n-vertex,
undirected graph with exactly n(n-1)/2 edges is said to be a
complete graph (Kn).
 In a directed graph on n vertices, the maximum # of edge
is n(n-1).
2
1 n= 4, m= 4.(4-1)/2 = 6
3 Hence this is a complete graph
4

 If (u, v) is an edge in E(G), then we say the vertices u and


v are adjacent and the edge (u, v) is incident on vertices u
and v. If (u, v) is a directed edge, then vertex u is adjacent to
v, and v is adjacent from u. u is the source and v is the sink.
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph - Definitions
 A subgraph of a graph G, is defined as a graph G’, such
that, V (G ) V (G) and E(G ) E(G)

 A complete subgraph is called a clique (k-clique). Clique


is the densest form of graph topological structure. Chain or
stars are sparsest form of graphs.

 Problem of finding subgraph of same topological


structure within a graph is called subgraph isomorphism.
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph - Definitions
• A path from vertex u to vertex v in graph G is a sequence
of vertices u, i1, i2,…, ik, v such that (u, i1), (i1, i2), …, (ik,
v) are edges in E(G). A
C

B E
-paths (A,F) are,
A-B-C-E-F, A-B-C-D-E-F, A-B-D-E-F D F
H
G
• The length of a path is the # of edges on it.
• A simple path is a path in which all vertices except
possibly the first and last are distinct. Ex. A-B-C-E-F
• A cycle is a simple path in which the first and last
vertices are the same. Ex: A-B-D-G-C-D-E-F, this is a path
with cycle and D-G-C-D is the cycle
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph - Definitions
• Shortest path of all the possible paths from vertex u to
vertex v in graph G is called the shortest path between (u,v)
A
- acyclic paths between (A,F) are, C
A-B-C-E-F (4), A-B-C-D-E-F (5), B E
A-B-D-E-F (4), A-B-D-G-C-E-F (6),
D F
A-B-D-C-E-F(5)
H
G
Here shortest paths are, A-B-C-E-F (4) and A-B-D-E-F (4)

• Finding shortest path is a non-trivial problem and can be


approached in (loosely) two ways,
- Single source shortest path problem
- All pair shortest path problem
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph - Definitions
• Finding shortest path is a non-trivial problem and can be
approached in (loosely) two ways,
- Single source shortest path problem A
C
Finding shortest path from one source node
B E
Algorithms-
 Dijkstra’s O( | E | + | V | log | V | ) D F
 Bellman-Ford O( | V | | E | ) H
G
- All pair shortest path problem
Finding shortest path between all pair of nodes in G
Algorithms-
 Floyd-Warshall’s O(|V|3)
 Johnson’s O(|V|3)
 Brandes O( | V | | E | ) Issue of scalability
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Prepared by Partha Basuchowdhuri, Ph. no.
9163883328
The eccentricity ε(v) of a graph vertex v in a connected graph G is
the maximum graph distance between v and any other vertex u of
G. For a disconnected graph, all vertices are defined to have
infinite eccentricity

Diameter of a graph:
Maximum eccentricity/ biggest of all pair shortest paths
MSN 200million user,
Radius of a graph: Minimum eccentricity. diameter = 29
Avg. path length = 6.6
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph - Definitions
 An undirected graph is said to be connected iff for
every pair of distinct vertices u and v in V(G) there is a
path from u and v in G.

 A connected component of an undirected graph is a


maximal connected subgraph (weakly connected
component – WCC)

 A tree is a connected acyclic (no cycles) graph

 A directed graph G is said to be strongly connected iff


for every pair of distinct vertices u and v in V(G), there
is a directed path from u and v and also from v to u
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph - Definitions
 A strongly connected component (SCC) is a maximal
subgraph that is strongly connected.

Three SCCs
are present
here

 The largest connected subgraph is called the largest


connected component (largest connected component –
LCC)
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph - Definitions
 The degree of a vertex is the # of edges incident to
that vertex.
If G is a directed graph, we define the in-degree of a
vertex v to be the # of edges for which v is the head. The
out-degree is defined to be the # of edges for which v is
the tail.

3 5

If dvi is the degree of vertex vi in a graph G with n


vertices and m edges, then the # of edges is,
1 n 1
m dvi
2i 0
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph - Definitions
 Cut-vertex/ Articulation point:
A
C
Removal of a node
B E results in disconnected/
D F
disjoint components it is
H
called a cut-vertex
G

 Bipartite graph:
- Vertex set divided into two
mutually exclusive subsets (U, V).
- Every edge have one of the
incident nodes from U and the other
from V.
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph - Representations
 Three most common representations-
- Adjacency matrix (adjacent nodes/ neighbors marked)
- Adjacency list
- Adjacency multi-list

 Adjacency matrix representation- O|V2|


n

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Graph - Representations
 Adjacency list representation-
node(degree): neighbors
1(3): 1 2 5
2(3): 1 3 5
3(2): 2 4
4(3): 3 5 6
5(3): 1 2 4
6(1): 4 Nodes with degree 1 are
called pendant node
Any redundancy?

Time complexity to build adjacency list? O|E|< O|V2|


Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph - Representations

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Graph - Representations
 Adjacency multi-list representation-

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Tree - Definitions
A tree is an undirected simple connected graph G (with
n vertices) that has no simple cycles and has n − 1 edges.

Other features of a tree:


- G has no cycles, and a simple cycle is formed if any
edge is added to G.
- G is connected, and it is not connected anymore if any
edge is removed from G.
- Any two vertices in G can be connected by a unique
simple path.

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Tree - Definitions
• Tree
– Nodes
– Each node can have 0 or more children
– A node can have at most one parent
• Binary tree
– Tree with 0–2 children per node

Tree Binary Tree


Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Tree - Definitions
A rooted tree is a finite set of one or more
nodes such that:

- There is a specially designated node called


the root.

- The remaining nodes are partitioned into


n>=0 disjoint sets T1, ..., Tn, where each of
these sets is a tree.

- We call T1, ..., Tn the sub-trees of the root.

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Tree - Definitions
• Terminologies:
– Root no parent
– Leaf no child
– Interior non-leaf
– Height (of a tree) distance from root to furthest leaf
– Depth (of a node) distance from root to a node
Level
Root node 0

Height
Interior nodes 1 =2

Leaf nodes 2
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Tree - Definitions
The degree of a node is the number of sub-trees
of the node
The node with degree 0 is a leaf or terminal node.

A node that has sub-trees is the parent of the roots of


the sub-trees.
The roots of these sub-trees are the children of the
node.
Children of the same parent are siblings.

The ancestors of a node are all the nodes along the


path from the root to the node. Similarly, predecessor.
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Binary Tree
A special class of trees: max degree for each node is 2
Recursive definition: A binary tree is a finite set of
nodes that is either empty or consists of a root and
two disjoint binary trees called the left sub-tree and
the right sub-tree.
A
B
E C

K F G D

L H

M I

J
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Types of Binary Trees
• Degenerate – only one child
• Fully /strictly binary tree or 2-tree – always two children
• Balanced – “mostly” two children, sub-trees are “equal” height
• Complete - every level, except possibly the last, is completely
filled, and all nodes are as far left as possible

Degenerate Balanced Full and complete


binary tree binary tree binary tree
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Binary Trees Properties
• Degenerate • Balanced
– Height = O(n) for n – Height = O( log(n) )
nodes for n nodes
– Similar to linked list – Useful for searches

Degenerate Complete Balanced


binary tree binary tree binary tree
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Binary Trees Properties
Property Value
A
Number of nodes
C
Height
B
Root Node
Leaves
D E F Interior nodes
Number of levels
Ancestors of H
G Descendants of B
Siblings of E
H I Right sub-tree
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Binary Trees Properties
Complete? No
A

B C Fully binary? Yes

D E

F G

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Binary Trees Properties
A Complete? No

B C Fully binary? Yes

D E F G

H I J K

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Binary Trees Properties
A Complete? Yes

B C Fully binary? Yes

D E F G

H I

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Binary Trees Properties
A Complete? Yes

B C Fully binary? No

D E F G

H I J

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Binary Trees Properties
The maximum number of nodes on level i of a binary
tree is 2i-1, i>=1.
The maximum number of nodes in a binary tree
of depth k is 2k-1, k>=1.
In a complete binary tree with height h>=0, minimum
number of nodes are 2h and maximum number of nodes
are 2h+1-1.
k
2i 1
20 21 ..... 2 k 1
2k 1
i 1

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Binary Trees Properties
For any nonempty binary tree, T, if n0 is the number of
leaf nodes and n2 the number of nodes of degree 2, then
n0=n2+1
Proof: Let n and B denote the total number of nodes &
branches in T. Let n0, n1, n2 represent the nodes with no
child, single child, and two children respectively.
We know, n= n0+n1+n2 -----(1)
B+1=n ------------(2)
B= n1+2n2---------(3)
From 2 & 3, n= n1+2n2+1---(4)
From 1 & 4, n0+n1+n2 = n1+2n2+1 or, n0=n2+1
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Binary Trees Properties
For any 2-tree, if Q is the number of non-leaf nodes, E
be the external path length and I be the internal path
length, then E=I+2Q
Proof: Let us consider the paths for a non-leaf node v, k
being the number of branches on the path from root to v.
By deleting 2 children of v, # non-leaf nodes become
Q-1, revised value of external path length becomes,
E – 2(k+1) +k = E – k – 2
E – k – 2 = (I – k) + 2(Q – 1)
or, E – k – 2 = (I – k) + 2Q – 2
or, E = I + 2Q
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Binary Tree Representations
Array representation of a binary tree:
- First node is root, it has no parent. Located at 0-th
index.
- For a node at i-th index,
Parent will be located at (i-1)/2, if i is odd
Parent will be located at (i-2)/2, if i is even
Left child of the node will be located at index 2i+1
Right child of the node will be located at index 2i+2

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Sequential Representation
A [0] A A
A
[1] B B
[2] - C
B
[3] C D
B C
[4] - E
C [5] - F
[6] - G F G
D E
D [7] D H
[8] - I
. . H I
E
[15] E

(1) waste space


(2) insertion/deletion problem
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Linked Representation
struct tnode
{
int data;
struct tnode *left, *right;
};
- root instead of head
- allocate every node using dynamic memory allocation
- leaf nodes will have NULL left and right pointers

left data right data

left right

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Linked Representation

12

23 34

X 45 X X 56 X X 67 X X 78 X

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Binary Tree Traversals

Let l, r, and R stand for moving left, moving right and


visiting the root

There are six possible combinations of traversal


lRr, lrR, Rlr, Rrl, rRl, rlR

Adopt convention that we traverse left before


right, only 3 traversals remain
lRr, lrR, Rlr
inorder, postorder, preorder

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Binary Tree Traversals

Algorithm for pre-order traversal is as follows:


1. Process root
2. Traverse left-subtree (recursive)
3. Traverse right-subtree (recursive)

1 2 3

Pre- root left right


In- left root right
Post- left right root
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Arithmetic Expression Using BT
inorder traversal
+ A/B * C * D +E
infix expression
* E
preorder traversal
+* * /AB C D E
D
* prefix expression

/ C postorder traversal
AB /C * D * E +
postfix expression
A B

level order traversal


+* E *D /CAB
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Inorder Traversal (recursive version)

void inorder(struct tnode *ptr)


/* inorder tree traversal */
{
if (ptr) {
inorder(ptr->left); A/B * C * D + E
printf(“%d”, ptr->data);
indorder(ptr->right);
}
}

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Preorder Traversal (recursive version)

void preorder(struct tnode *ptr)


/* preorder tree traversal */
{
if (ptr) {
printf(“%d”, ptr->data); +**/AB CDE
preorder(ptr->left);
predorder(ptr->right);
}
}

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Postorder Traversal (recursive version)

void postorder(struct tnode *ptr)


/* postorder tree traversal */
{
if (ptr) {
postorder(ptr->left); AB /C * D * E +
postdorder(ptr->right);
printf(“%d”, ptr->data);
}
}

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Binary Tree Traversal - Example

Pre-order: F B A D C E G I H
B G
In-order: A B C D E F G H I

A D I Post-order: A C E D B H I G F

Level-order: F B G A D I C E H
C E H

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Binary Tree Traversal - Example
Stack Preorder string
F Push F
F F
empty Pop F
B G GB Push G, B
G Pop B FB
A D I
GDA Push D, A
C E H G Pop A, D FBAD
GEC Push E, C
G Pop C, E FBADCE
Pre-order: empty Pop G FBADCEG
FBADCEGIH I Push I
empty Pop I FBADCEGI
H Push H
empty Pop H FBADCEGIH
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Binary Tree Traversal - Example

A Pre-order:
AB D E F C G H J LK
B C
In-order:
DBF EAGCLJ HK
D E G H
Post-order:
DFEBGLJKHCA
F J K

L
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Binary Tree Traversal - Example

A Pre-order:
AB D G H K C E F
B C
In-order:
GDHKBAECF
D E F
Post-order:
G KH D B EF CA
H
G

K
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Binary Tree Traversal - Example
A

Pre-order:
B C AB D G K H LM C E

D E In-order:
KGDLHMBAEC

H
G Post-order:
KGLMHDBECA
K L M

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Binary Tree Traversal - Example
A

Pre-order:
B
AB C E I F J D G H K L

C D In-order:
E I C F J B G D K H LA

F G H
E Post-order:
IE JF CG K LH D BA
I J K L

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Threaded Binary Trees
• Too many null pointers in current
representation of binary trees:
n: number of nodes
number of non-null links: n-1 (edges)
total links: 2n
null links: 2n-(n-1)=n+1

• Replace these null pointers with some useful


“threads”.

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Threaded Trees
• Inorder traversal is a common operation
• Recursive methods have disadvantages:
– Require extra memory to store a stack
– Can’t start from an arbitrary node … need to start
at the root
• Want to make it easier to find inorder
successor of a node
– Inorder predecessor would be nice too!

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Threaded Binary Trees
- If ptr->left_child is null,
replace it with a pointer to the node that would
be visited before ptr in an inorder traversal

- If ptr->right_child is null,
replace it with a pointer to the node that would
be visited after ptr in an inorder traversal

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Types of Threaded Binary Trees
• It can be based on any of the traversal orders (pre-
order, in-order, post-order)
• Threading can be done based on how many pointers
to account for-
- One way threading (accounts for only one of the
left/right NULL pointers)
- Two way threading (accounts for both the NULL
pointers)
- Two way threading with root (accounts for dangling
pointers)
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Threaded Binary Tree
1. One way in-order right threaded binary tree:
root A
dangling
B C

D E F G

inorder traversal:
H I H, D, I, B, E, A, F, C, G

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Threaded Binary Tree
2. Two way in-order threaded binary tree:
root A
dangling
B C

dangling D E F G

inorder traversal:
H I H, D, I, B, E, A, F, C, G

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Memory Representation of A Threaded BT
3. Two way in-order threaded binary tree with root:
root

f A f

f B f f C f

f D f t E t t F t t G t

t H t t I t

Dangling pointers are accounted for


Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Binary Search Trees
• View today as data structures that can support
dynamic set operations.
– Search, Minimum, Maximum, Predecessor,
Successor, Insert, and Delete.
• Can be used to build
– Dictionaries.
– Priority Queues.
• Basic operations take time proportional to the
height of the tree – O(h).
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Binary Search Trees
• Stores keys in the nodes in a way so that searching,
insertion and deletion can be done efficiently.
For every node X,
– all the keys in its left sub-tree are smaller than the key
value in X,
– and all the keys in its right sub-tree are larger than the
key value in X

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Binary Search Trees
• How to create, search…….?
• How does search becomes easier?
Input: 56, 26, 28, 200, 27, 18, 213, 24, 190, 12
• Stored keys must satisfy 56

the binary search tree


26 200
property.
– y in left subtree of x,
then key[y] key[x]. 18 28 190 213

– y in right subtree of x,
then key[y] key[x]. 12 24 27

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Binary Search Trees
• Searching takes O(log2n) ----- average case, insertion
in random order
Inorder: 12 18 24 26 27 28 56 190 200 213

56 Magic! It is sorted

26 200

18 28 190 213

12 24 27
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Binary Search Trees

• Gives the worst case – 1


2
run-time O(n)
3
4
• Can happen, if inserted
in sorted order 5
6
7

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Are these Binary Search Trees?

21 21 21 45

54
22 28 20 28 22 28
59

27 39 27 39 17 67 27 39 65

No Yes No Yes

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Binary Search Tree - Search
1. begin
2. if (tree->data == key OR tree == NULL) then
3. return tree;
4. else
5. if (key < tree->data) then
6. return binTreeSearch(tree->left, key);
7. else
8. return binTreeSearch(tree->right, key);
9. end if
10. end if
11. end
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Binary Search Tree - Search

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


BST – Insertion Part 1
struct node *insertBST(struct node *tree, int key)
{
struct node *ptr, *nodeptr, *parentptr;
ptr = (struct node *)malloc(sizeof(struct node));
ptr->data = key;
ptr->left = NULL;
ptr->right = NULL;
if(tree==NULL)
{
tree=ptr;
tree->left = NULL;
tree->right = NULL;
}
else
{
parentptr = NULL;
nodeptr = tree;

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


BST – Insertion Part 2 (contd.)
….continued from previous page

while(nodeptr != NULL)
{
parentptr = nodeptr;
if(key < nodeptr->data) O(log2n)
nodeptr = nodeptr->left;
else
nodeptr = nodeptr->right;
}
if(key < parentptr->data)
parentptr->left = ptr;
else
parentptr->right = ptr;
}
return tree;
}
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Binary Search Tree - findMax
1. begin
2. if (tree->right == NULL OR tree == NULL) then
3. return tree;
4. else
5. return findMax(tree->right);
6. end if
7. end

Max element of the left sub-tree of a node is the


inorder predecessor of the element
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Binary Search Tree - findMin
1. begin
2. if (tree->left == NULL OR tree == NULL) then
3. return tree;
4. else
5. return findMin(tree->left);
6. end if
7. end

Min element of the right sub-tree of a node is the


inorder successor of the element
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Binary Search Tree - Delete
Three cases for node deletion:
(1) The node is a leaf
– Delete it immediately, no pointer updating

6 6

2 8 2 8

1 4 1 4
Delete 3
3

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Binary Search Tree - Delete
(2) The node has one child
– Adjust a pointer from the parent to bypass
that node

6 6
6
2 8 2 8
2 8

1 4 1 4
Delete 4 1 3

3 3

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Binary Search Tree - Delete
(3) The node has two children
– replace the key of that node with the minimum element at the
right sub-tree (here 3)
– delete the minimum element
• Has either no child or only right child because if it has a
left child, that left child would be smaller and would have
been chosen. So invoke case 1 or 2.
6 6
6
2 8 3 8
3 8

1 4 1 4
Delete 2 Delete 3 1 4

3 3
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
BST – Deletion Part 1
struct node *deleteBST(struct node *tree, int key)
{
struct node *ptr;
if(tree==NULL)
printf(“\n %d is not present”, key);
else if(key < tree->data)
deleteBST(tree->left, key);
else if(key > tree->data)
deleteBST(tree->right, key);
else
{
if(tree->left && tree->right)
{
ptr = findMax(tree->left);
tree->data = ptr->data;
deleteBST(tree->left, ptr->data);
}
...........(continued)

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


BST – Deletion Part 2
...........(continued)

else
{
ptr = tree;
if(tree->left==NULL && tree->right==NULL)
tree = NULL;
else if(tree->left!=NULL)
tree = tree->left;
else
tree = tree->right;
}
}
return tree;
}

Code is incomplete……

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Balanced BST – AVL Tree
Self-balancing binary search tree.
Adelson-Velskii, Landis (AVL)
For binary search tree, we want to exploit the fact that
average case height can be O(log2n), but if tree is
unbalanced, it is not possible.

If T is a non-empty BST, then T is said to be height


balanced if,
1. TL and TR are height balanced
2. |hTL – hTR| {0, 1}, where h is the height of
tree/sub-tree
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Balanced BST – AVL Tree
Balance factor:
Balance factor (BF) of a node, in T is |hTL – hTR|,
where h is the height of sub-trees.
If BF = -1  right-heavy
= +1  left-heavy
= 0  perfectly balanced

2 6
BF = +1, BF = -1,
Left heavy Right heavy
1 8

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Balanced BST – AVL Tree
Balancing is done by keeping in-order same.

How to find whether a tree is balanced or not-


Evaluate balance factor of each node
+2
6 0
-1 0 4
2 8 0 -1
2 6
0 +1
1 4 0 +1 0
3 8
1
0
3 But how?
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Balanced BST – AVL Tree
Types of rotation:
1. Left-left (LL) rotation:

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Balanced BST – AVL Tree
Types of rotation:
2. Right-right (RR) rotation:

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Balanced BST – AVL Tree
Types of rotation:
3. Left-right (LR) rotation:

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Balanced BST – AVL Tree
Types of rotation:
4. Right-left (RL) rotation:

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Balanced BST – AVL Tree
Build an AVL tree with – 8, 9, 10, 2, 1, 5, 3, 6, 4,-2 7, 11, 12
-2
-1 0
8
0 8 9
-1
8 0
9 RR 0 0
9 10
0 8
Step 1 Step 2 Step 3 10
+2
9
+1
+2 0 +1
9
8 10 9
+1 0
0 0
8 10 +1
2 10
2 LL
0 0 0
0
2 Step 4 Step 5 1 8
8
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Balanced BST – AVL Tree
Build an AVL tree with – 8, 9, 10, 2, 1, 5, 3, 6, 4, 7, 11, 12

+2 +2
9 9
0
-1 0 +2 0
10 10 8
2 8
0 +1
0 +1 0
LR RR 2 9
1 8 2
0 0 0
0 0 0
1 5 10
5 1 5

Step 6

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Balanced BST – AVL Tree
Build an AVL tree with – 8, 9, 10, 2, 1, 5, 3, 6, 4, 7, 11, 12
After steps 7, 8 tree was balanced
+2 +2
+1
8 8
8
-2 -1 -2 -1 0 -1
RL LL
2 9 2 9 3 9
0 +1 0 0 -2 0 +1 0 0

1 5 10 1 3 10 2 5 10
0 0 0
-1 0 0
1 4 6
3 6 5

0 0 0
4 4 6
Step 9
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Balanced BST – AVL Tree
Build an AVL tree with – 8, 9, 10, 2, 1, 5, 3, 6, 4, 7, 11, 12

+2
8 0
-1 -1 5
9 0
3
+1 8
+1 0
-1
10 3 -1 -1
2 5
LR +1 0 6 9
0 0 -1
2 4 0 0
1 4 6
0 0 7 10
7 1

Step 10

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Balanced BST – AVL Tree
Build an AVL tree with – 8, 9, 10, 2, 1, 5, 3, 6, 4, 7, 11, 12

-1 0
5 5
-1 0
+1 8 +1 8
3 -1 -2 3 0
-1
+1 0 6 9 RR +1 6 10
0
2 4 0 -1 2 4 0
0 0
0 7 10 0 7 11
9
1 0 1
11
Step 11

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Balanced BST – AVL Tree
Build an AVL tree with – 8, 9, 10, 2, 1, 5, 3, 6, 4, 7, 11, 12

-1
5
-1
+1 8
3 -1 -1 Final tree
+1 0 6 10
2 4 0 0 -1
0 7 9 11
1 0
12

Step 12

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Balanced BST – AVL Tree
Smallest AVL tree of height h:
f(h) = f(h-1) + f(h-2) + 1
f(0) = 0, f(1) = 1, f(2) = 0+1+1 = 2
f(3) = 2+1+1 = 4
5
6

3 6
2 8

4 8
2
1

f(3) = 4 1
f(4) = 7
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Balanced BST – AVL Tree
f(8) = ?
f(8) = f(7) + f(6) + 1
= f(6) + f(5) + 1 + f(5) + f(4) + 1 + 1
= 3.f(5) + 2.f(4) + 3 + 1
= 54 9

5 12

3 6 11 13

f(5) = 12 2 4 8 10

1
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Balanced BST – AVL Tree
Deletion follows BST rules + self-balancing (if needed)

Exercise:
1. Build an AVL tree with – 8, 12, 9, 11, 7, 6
2. Build an AVL tree with – 25, 45, 50, 55, 60, 65, 75, 85
3. Build an AVL tree with – 17, 25, 19, 23, 75
[WBUT08]

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Graph Traversals
Mainly of two types:
1. Depth first traversal – also known as depth first search (DFS)
2. Breadth first traversal – also known as breadth first search
(BFS)

“Search” signifies search for a node, but popular usage of these


traversals are made other issues such as –
1. Finding the connected components of a graph. If the graph is
disconnected creates a forest.
2. Finding acyclicity of a graph.
3. DFS can be used for finding articulation points.
4. BFS can be used for finding minimum edge path from one
node to another.
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Graph Traversals
Before we start let’s have a look at all types of edges –
(in case of undirected graphs)
1. Tree-edge (for both DFS, BFS)
2. Back-edge (for DFS)
3. Cross-edge (for BFS)
4. Forward-edge (digraph only)

In case of directed graphs


both DFS and BFS can
have all of these edges.

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Depth First Search
Stack is used for DFS, along with adjacency list/
adjacency matrix.
Algorithm:
Step 1: Select a root node (any node), push it in stack.
Step 2: Repeat steps 3, 4 until adjacency list of root is exhausted.
Step 3: If there is an unvisited/unlabeled child node, push it in
stack and add label.
Step 4: If node does not have any more unvisited child node pop
from stack
Using adjacency matrix takes O(|V|2)
Using adjacency list takes O(|V|+|E|)

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


DF traversal- Part 1
G F A(4): B C G H E(2): B D

B(5): A C D E F F(2): B H
A B E
C(2): A B G(1): A

D C H D(2): B E H(2): A F

Stack Current node DFS label link processed link type

A A 1 (A, B) tree
Push B . . . . .
A,B B 2 (B, A) 2 nd visit
(B, C) tree
Push C . . . . .
A , B, C C 3 (C, A) back-edge
**1st cycle**
(C, B) 2 nd visit
C’s list is exhausted, back-track to parent(C) = B , Pop C
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
DF traversal- Part 2
G F A(4): B C G H E(2): B D

B(5): A C D E F F(2): B H
A B E
C(2): A B G(1): A

D C H D(2): B E H(2): A F

Stack Current node DFS label link processed link type

A, B B (B, D) tree
Push D . . . . .
A , B, D D 4 (D, B) 2 nd visit
(D, E) tree
Push E . . . . .
A , B, D, E E 5 (E, B) back-edge
**2nd cycle**
(E, D) 2 nd visit
E’s list is exhausted, back-track to parent(E) = D , Pop E
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
DF traversal- Part 3
G F A(4): B C G H E(2): B D

B(5): A C D E F F(2): B H
A B E
C(2): A B G(1): A

D C H D(2): B E H(2): A F

Stack Current node DFS label link processed link type

A, B, D D
D’s list exhausted, back-track to parent(D) = B , Pop D
A,B B (B, E) 2 nd visit
(B, F) tree
Push F . . . . .
A , B, F F 6 (F, B) 2 nd visit
(F, H) tree
Push H . . . . .

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


DF traversal- Part 4
G F A(4): B C G H E(2): B D

B(5): A C D E F F(2): B H
A B E
C(2): A B G(1): A

D C H D(2): B E H(2): A F

Stack Current node DFS label link processed link type


A , B, F, H H 7 (H, A) back-edge
**3rd cycle**
(H, F) 2 nd visit
H’s list is exhausted, back-track to parent(H) = F , Pop H
A , B, F F
F’s list is exhausted, back-track to parent(F) = B , Pop F
A,B B
B’s list is exhausted, back-track to parent(B) = A , Pop B

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


DF traversal- Part 5
G F A(4): B C G H E(2): B D

B(5): A C D E F F(2): B H
A B E
C(2): A B G(1): A

D C H D(2): B E H(2): A F

Stack Current node DFS label link processed link type


A A (A, C) 2 nd visit
(A, G) tree
Push G
A,G G 8 (G, A) 2 nd visit
G’s list is exhausted, back-track to parent(G) = A , Pop G
A A (A, H) 2nd visit
A’s list is exhausted, back-track to parent(A) = NULL
……….algorithm stops here
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
DF traversal- Part 6 (Final)
G F A(4): B C G H E(2): B D

B(5): A C D E F F(2): B H
A B E
C(2): A B G(1): A

D C H D(2): B E H(2): A F

A Also write the nodes in label-wise


sequence for WBUT exam.
B G
A B C D E F H G
F
C D Difference in DFS with directed graph
H would be construction of adjacency list
E and presence of all 4 types of edges.
Depth First Tree
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
DF traversal- Part 6 (Final)
A
No. of cycles ≥ No. of back-edges

B G Every cycle must have a back-edge


F (for DFS in undirected graph)
C D
H
Does a cycle need to have at least a
E
tree edge?
No
Depth First Tree
A X B Y C

If all nodes are covered during DFS, then the graph is


connected. Otherwise, randomly pick any other node and
start DFS within the rest. This way we’ll get a DF forest
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Breadth First Search
Data structures used for BFS are – queue, adjacency
matrix/ adjacency list
Algorithm:
Step 1: Select a root node (any node), insert it in queue.
Step 2: Repeat steps 3, 4 until queue is empty.
Step 3: Insert all the unvisited/ unlabeled nodes in the adjacency
list of the node at front (being processed) and add label.
Step 4: If all unvisited neighbors have been inserted, delete the
node at front.

adjacency matrix - O(|V|2), adjacency list - O(|V|+|E|)

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


BF traversal- Part 1
G F A(4): B C G H E(2): B D

B(5): A C D E F F(2): B H
A B E
C(2): A B G(1): A

D C H D(2): B E H(2): A F

Queue Current node BFS label link processed link type

A A 1 (A, B) tree
Insert B, BFS label = 2 . . . . .
A,B A (A, C) tree
Insert C, BFS label = 3 . . . . .
A , B, C A (A, G) tree
Insert G, BFS label = 4 . . . . .
A , B, C, G A (A, H) tree
Insert H, BFS label = 5 . . . . .
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
BF traversal- Part 2
G F A(4): B C G H E(2): B D

B(5): A C D E F F(2): B H
A B E
C(2): A B G(1): A

D C H D(2): B E H(2): A F

Queue Current node BFS label link processed link type

A, B, C, G, H
A’s adjacency list exhausted, delete A . . . . .
B, C, G, H B 2 (B, A) 2 nd visit
(B, C) cross-edge
**1st cycle**
(B, D) tree
Insert D, BFS label = 6 . . . . .
B, C, G, H, D B (B, E) tree
Insert E, BFS label = 7 . . . . .
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
BF traversal- Part 3
G F A(4): B C G H E(2): B D

B(5): A C D E F F(2): B H
A B E
C(2): A B G(1): A

D C H D(2): B E H(2): A F

Queue Current node BFS label link processed link type

B, C, G, H, D, E B 2 (B, F) tree
Insert F, BFS label = 8
B, C, G, H, D, E, F
B’s list exhausted, delete B
C, G, H, D, E, F C 3 (C, A) 2 nd visit
(C, B) 2nd visit
C’s list exhausted, delete C
G, H, D, E, F G 4 (G, A) 2 nd visit
G’s list exhausted, delete G
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
BF traversal- Part 4
G F A(4): B C G H E(2): B D

B(5): A C D E F F(2): B H
A B E
C(2): A B G(1): A

D C H D(2): B E H(2): A F

Queue Current node BFS label link processed link type


H, D, E, F H 5 (H, A) 2nd visit
(H, F) cross-edge
** 2nd cycle**
H’s list is exhausted, delete H
D, E, F D 6 (D, B) 2 nd visit
(D, E) cross-edge
**3rd cycle**
D’s list is exhausted, delete D

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


BF traversal- Part 5
G F A(4): B C G H E(2): B D

B(5): A C D E F F(2): B H
A B E
C(2): A B G(1): A

D C H D(2): B E H(2): A F

Queue Current node BFS label link processed link type


E, F E 7 (E, B) 2nd visit
(E, D) 2nd visit
E’s list is exhausted, delete E

F F 8 (F, B) 2 nd visit
(F, H) 2nd visit
F’s list is exhausted, delete F

Queue is empty . . . . . . . . . . algorithm stops here


Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
BF traversal- Part 6 (Final)
A

BFS in undirected graphs


B C G H have tree-edges and cross-
edges.
D E F No back-edge is produced.
Breadth First Tree

-Also, BFS can be used for getting shortest path from


one source, i.e, the root. Say, we can find shortest path
from A to F is 2 hops (can be seen from the tree).
- Can we find all-pair shortest paths from the BF-tree ?

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Graph traversal- Exercise
Find DFS and BFS for the following graph-
A D
B E
C F
A
G
Answer:
A B
E
C
B C D
D

E F
F

G
Breadth First Tree Depth First Tree G
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Minimum Cost Spanning Tree
Minimum spanning tree of a graph G(V,E) is defined as a
tree that can be extracted from the graph where, the number
of vertex set in the tree is same as in the graph, but the edge
set is a subset of E forming a tree, such that the aggregated
weight of all the edges is less than equal to all other
possible subsets of edges that could generate trees using the
vertex set V.

1 1 1 1
A B A B A B A B
2 2 2
5 5 5
3 3 3
C D C D C D C D

W(T) = 6 W(T) = 9 W(T) = 8


Minimum cost spanning tree (MST)
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Prim’s algorithm
Objective: To find minimum cost spanning tree for a weighted
graph (negative weights are allowed)
Algorithm:
Input: non-empty connected weighted graph G(V,E)
Output: GT(V,ET), where ET E, |V |= |ET|+1

Initialize: Vnew = {x}, where x is an arbitrary node (starting point)


from V, ET = {}
repeat:
Choose an e(u, v) with minimal weight such that
u is in Vnew and v is not
/* if there are multiple edges with the same weight, pick any one*/
Add v to Vnew, and e(u, v) to ET
until Vnew = V
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Greedy method - Prim’s algorithm
Greedy method/algorithm:
To avoid evaluation of all possible solution for a
problem, we want to (greedily) take the best possible
solution for solving the problem at every step.

Examples of greedy methods?


Prim’s algorithm
adjacency matrix - O(|V|2)
binary heap, adjacency list - O((|V|+|E|) log2 |V|)
fibonacci heap, adjacency list - O(|E|+|V|log2 |V|)

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Prim’s algorithm
Find minimum cost spanning tree:
Tree vertices Possible paths Search space

{D} A(5) – min {A, B, C,


B(9) E, F, G}
E(15)
F(6)

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Prim’s algorithm
Find minimum cost spanning tree:
Tree vertices Possible paths Search space
{D, D- E(15) {B, C,
A} A- B(7) E, F, G}
D- F(6) (min)
D- B(9)

{D, D- E(15) {B, C,


F, A- B(7) (min) E, G}
A} F- E(8)
D- B(9)
F- G(11)
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Prim’s algorithm
Find minimum cost spanning tree:
Tree vertices Possible paths Search space
{D, D- E(15) {C, E, G}
F, B- C(8)
B, F- G(11)
A} B- E(7) (min)
F- E(8)
*D- B not considered for acyclicity
{D, E- C(5) (min) {C, G}
F, B- C(8)
B, F- G(11)
E, A} E- G(9)

*D- B, D- E, E- F excluded for acyclicity


Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
Prim’s algorithm
Find minimum cost spanning tree:
Tree vertices Possible paths Search space
{D, F E- G(9) (min) {G}
B, A, F- G(11)
E, C}
So, EG included.
V = {A, B, C, D, E, F, G}
ET = {DA, DF, AB, BE, EC, EG}

Final MST

Prepared by Partha Basuchowdhuri, Ph. no. 9163883328


Prim’s algorithm
Is minimum cost spanning tree / MST of a graph unique?
Under which special circumstance, can it be unique?
What is a maximum spanning tree of a graph?
- Correctness proof of Prim’s left out for CS503 (not in
WBUT CS302 syllabus)
- Find minimum cost spanning tree:
1
A D
3 6
4 4
C B F
5 5
2
6 8
E
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328

You might also like