Graph Tree Final
Graph Tree Final
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
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)
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.
Three SCCs
are present
here
3 5
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
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.
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
D E
F G
D E F G
H I J K
D E F G
H I
B C Fully binary? No
D E F G
H I J
left right
12
23 34
X 45 X X 56 X X 67 X X 78 X
1 2 3
/ C postorder traversal
AB /C * D * E +
postfix expression
A B
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
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
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
- If ptr->right_child is null,
replace it with a pointer to the node that would
be visited after ptr in an inorder traversal
D E F G
inorder traversal:
H I H, D, I, B, E, A, F, C, G
dangling D E F G
inorder traversal:
H I H, D, I, B, E, A, F, C, G
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
– y in right subtree of x,
then key[y] key[x]. 12 24 27
56 Magic! It is sorted
26 200
18 28 190 213
12 24 27
Prepared by Partha Basuchowdhuri, Ph. no. 9163883328
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
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
6 6
2 8 2 8
1 4 1 4
Delete 3
3
6 6
6
2 8 2 8
2 8
1 4 1 4
Delete 4 1 3
3 3
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)
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……
2 6
BF = +1, BF = -1,
Left heavy Right heavy
1 8
+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
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
-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
-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
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]
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 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
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
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 . . . . .
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
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
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
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 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
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
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
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
F F 8 (F, B) 2 nd visit
(F, H) 2nd visit
F’s list is exhausted, delete F
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
Final MST