M3 - Non Linear Data Structures
M3 - Non Linear Data Structures
Syllabus
Non Linear Structures - Graphs - Trees - Graph and Tree implementation using array and Linked List -Binary trees - Binary tree traversals - pre-order, in-order and postorder - Threaded binary trees Binary Search trees - AVL trees B trees and B+ trees - Graph traversals - DFS, BFS shortest path - Dijkstras algorithm, Minimum spanning tree - Kruskal Algorithm, Prims algorithm
10/25/2012
Trees
10/25/2012
Trees
A tree is a collection of nodes, one of which is designated as the root, along with a relation (parenthood). Represents hierarchical relationship. A node can be of any type.
10/25/2012
Trees
A tree can be recursively defined as : A single node by itself is a tree. This node is called the root of the tree. A tree is a finite set of one or more nodes such that there is a specially designated node called the root and the remaining nodes are partitioned into n>=0 disjoint sets T1, T2, ,Tn, where each of these sets is a subtree. Every node except the root has a parent node associated to it.
10/25/2012 CS 09 303 Data Structures - Module 3 5
TREE TERMINOLGIES
Root :Specially designed first node Degree of a node: Number of subtrees of a node Degree of a Tree :Maximum degree of any node in a given tree. Terminal node/Leaf node: Node with degree zero.
10/25/2012
degree is non-zero.
Siblings: Children nodes of same parent node. Level : If a node is at level n, then its children will be
that ni is the parent of ni+1 for 1<=i<k, then this sequence is called a path from node n1 to node nk.
10/25/2012 CS 09 303 Data Structures - Module 3 7
path.
Ancestor/descendant of a node: If there is a path from
descendant of node ,other than the node itself is called proper ancestor or proper descendant respectively.
10/25/2012
node to a leaf.
Height of a tree: Height of the root. Depth of a node : Length of the unique path from root
to that node. Depth of a tree :One more than the maximum level of any node in a given tree.
10/25/2012
Order of nodes
Children of a node are usually ordered from left-to-right.
If a and b are siblings, and a is to the left of b, then all the descendants of a are to the left of all descendants of b. A tree in which the order of nodes is ignored is referred to as unordered tree.
10/25/2012
11
Otherwise, let T be a tree with root n and subtrees T1, T2, , Tk.
T1
T2
Tk
The preorder listing of the nodes of T is the root n of T followed by the nodes of T1 in preorder, then the nodes of T2 in preorder, and so on, upto the nodes of Tk in preorder.
10/25/2012
13
The inorder listing of the nodes of T is the nodes of T1 in inorder, followed by the root n of T , followed by the nodes of T2, , Tk in inorder. The postorder listing of the nodes of T is the nodes of T1 in postorder, then the nodes of T2 in postorder, and so on, upto Tk, all followed by node n.
10/25/2012
14
Preorder Procedure
Procedure PREORDER (n : node) begin (1) list n; (2) for each child c of n, if any, in order from the left do PREORDER(c) end;
10/25/2012
15
a a b c
10/25/2012
16
a b a b c
10/25/2012
17
a b d a b c
10/25/2012
18
a b d e a b c
10/25/2012
19
a b d e c a b c
10/25/2012
20
a b d e c f a b c
10/25/2012
21
a b d e c f g a b c
10/25/2012
22
A, B, D, H, I, E, C, F, J, G, K
10/25/2012 CS 09 303 Data Structures - Module 3 23
Postorder Procedure
Procedure POSTORDER (n : node) begin (1) for each child c of n, if any, in order from the left do POSTORDER(c) (2) list n; end;
10/25/2012
24
d a b c
10/25/2012
25
d e a b c
10/25/2012
26
d e b a b c
10/25/2012
27
d e b f a b c
10/25/2012
28
d e b f g a b c
10/25/2012
29
d e b f g c a b c
10/25/2012
30
d e b f g c a a b c
10/25/2012
31
H, I, D, E, B, J, F, K, G, C, A
10/25/2012 CS 09 303 Data Structures - Module 3 32
Inorder Procedure
Procedure INORDER (n : node) begin if n is a leaf then list n; else begin INORDER (leftmost child of n); list n; for each child c of n, except for the leftmost, in order from the left do INORDER(c) end end;
10/25/2012 CS 09 303 Data Structures - Module 3 33
d a b c
10/25/2012
34
d b a b c
10/25/2012
35
d b e a b c
10/25/2012
36
d b e a a b c
10/25/2012
37
d b e a f a b c
10/25/2012
38
d b e a f c a b c
10/25/2012
39
d b e a f c g a b c
10/25/2012
40
H, D, I, B, E, A, J, F, C, K, G
10/25/2012 CS 09 303 Data Structures - Module 3 41
10/25/2012
42
n1 = (a + b) * (a + c)
*
n2 = (a + b)
+ a b a
n3 = (a + c)
n4 = (a)
n7 = (c)
n5 = (b) n6 = (a)
10/25/2012
43
The preorder listing of labels in an expression tree gives the prefix form of the expression, where the operator precedes its left and right operands. The prefix expression for (E1)#(E2), with # a binary operator, is #P1P2 , where P1 and P2 are the prefix expressions for E1 and E2 respectively.
10/25/2012
44
The postorder listing of labels in an expression tree gives the postfix form of the expression, where left and right operands precede the operator. The postfix expression for (E1)#(E2), with # a binary operator, is P1P2#, where P1 and P2 are the postfix expressions for E1 and E2 respectively. The inorder listing of labels in an expression tree gives the infix expression.
10/25/2012
45
RIGHT_SIBLING (n, T) : Returns the right sibling of node n in tree T, defined to be that node m with same parent p as n such that m lies immediately to the right of n in the ordering of children of p.
10/25/2012 CS 09 303 Data Structures - Module 3 46
LABEL (n, T) : Returns the label of node n in tree T. CREATEi (v, T1, T2, , Ti) : For each value of i = 0, 1, 2, CREATEi makes a new node r with label v and gives it i children, which are the roots of trees T1, T2, , Ti, in order from the left. The tree with root r is returned. ROOT (T) : Returns the node that is the root of tree T, or returns NULL T is null tree. MAKENULL (T) : makes T the null tree.
10/25/2012 CS 09 303 Data Structures - Module 3 47
IMPLEMENTATION OF TREE
Array Representation Linked List representation
10/25/2012
48
IMPLEMENTATION OF TREES
Array Representation (Parent Representation) Uses the property of trees that each node has a unique parent. Uses a linear array A where A[i] =j, if node j is parent of node i, and A[i]=0,if node i is the root. It supports LABEL operator, where L be an array with L[i], the Label of node i.
10/25/2012 CS 09 303 Data Structures - Module 3 49
10/25/2012
50
With this representation, the parent of a node can be found in constant time. Limitations: Lacks child-of information. Given a node n, it is expensive to determine the children of n, or the height of n. Parent pointer representation does not specify the order of the children of a node.
10/25/2012
51
10/25/2012
52
Right Sibling Operation function RIGHT_SIBLING (n:node; T:Tree): node; {returns right sibling of node n in tree T} Var i,parent:node ; begin parent:=T[n]; for i = n+1 to maxnodes do {search for node after n with same parent} if T[i] = parent then return(i); return(0);{null node will be returned if no right sibling is ever found} End;(RIGHT_SIBLING}
10/25/2012
53
10/25/2012
55
Definition Type node = integer; LIST = {appropriate definition for list of nodes}; position = {appropriate definition for positions in lists}; TREE = record header : array[1..maxnodes] of LIST; labels : array[1..maxnodes] of labeltype; root : node; End;
10/25/2012 CS 09 303 Data Structures - Module 3 56
LEFTMOST-CHILD Operation function LEFTMOST_CHILD (n:node ;T:Tree): node; {returns the leftmost child of node n of tree T} Var L : LIST {list of ns children} begin L:= T.header[n]; if EMPTY(L) then {n is a leaf} return(0) else return (RETRIEVE(FIRST(L),L); End;{LEFTMOST_CHILD}
10/25/2012
57
10/25/2012
58
PARENT Operation function PARENT(n:node ;T:Tree): node; {returns the parent of node n of tree T} Var p: node; {runs through possible parents of n} i: position; {runs down the list of ps children} begin for p:=1 to maxnodes do begin i := T.header[p]; while i <> 0 do { see if n is among children of p} if cellspace[i].node = n then return(p)
10/25/2012
59
else i:= cellspace[i].next; end; return(0); end;{PARENT} {returns null node if parent not found}
10/25/2012
60
Shortcoming Inability to create large trees from smaller trees using CREATEi operator.
10/25/2012
61
10/25/2012
62
Definition Of Cell Space Var cellspace :array[1..maxnodes] of record label:labeltype; leftmost_child :integer; right_sibling: integer; end;
10/25/2012
63
10/25/2012
64
10/25/2012
65
Binary Trees
Definition A binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left child" and "right child". A binary tree can be defined as : (1) either an empty tree, or (2) a tree in which every node has either no children, a left child, a right child, or both a left and a right child.
10/25/2012 CS 09 303 Data Structures - Module 3 66
10/25/2012
67
An Ordinary Tree
68
Left-Skewed: If a binary tree has only left subtree, it is called left-skewed. Right-Skewed: If a binary tree has only right subtree, it is called right-skewed.
10/25/2012
69
10/25/2012
70
In a binary tree a degree of every node is maximum two. Binary tree with n nodes has exactly (n-1) edges. A full binary tree is a tree in which every node other than the leaves has two children. A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
10/25/2012
71
10/25/2012
72
10/25/2012
73
10/25/2012
74
10/25/2012
75
10/25/2012
76
10/25/2012
77
INSERTION Algorithm Function insert(root: node,digit:number): node; Var root : node; begin If root = NULL then begin new(root); root .leftchild:=NULL; root .rightchild:=NULL; root .data := digit; count := count+1; end;{if}
10/25/2012 CS 09 303 Data Structures - Module 3 80
else if count %2 = 0 then begin root .leftchild :=insert(root .leftchild, digit); else root .rightchild := insert(root .rightchild, digit); end;{elseif} return(root); end;{insert}
10/25/2012
81
10/25/2012
82
Recursive TRAVERSAL Pre-order Algorithm Steps: (1) Visit the root node (2) Traverse the left subtree in pre-order (3) Traverse the right subtree in pre-order
10/25/2012
83
Recursive TRAVERSAL Pre-order Algorithm procedure preorder (root : node) begin if root <> NULL begin write (root .data); preorder(root .lchild); preorder(root .rchild); end{if} end;{preorder}
10/25/2012
84
Recursive TRAVERSAL In-order Algorithm Steps: (1) Traverse the left subtree in inorder (2) Visit the root node (3) Traverse the right subtree in inorder
10/25/2012
85
Recursive TRAVERSAL Inorder Algorithm procedure inorder(root : node) begin if root <> NULL begin inorder(root .lchild); write (root .data); inorder(root .rchild); end{if} end;{inorder}
10/25/2012
86
Recursive TRAVERSAL Postorder Algorithm Steps: (1) Traverse the left subtree in postorder (2) Traverse the right subtree in postorder (3) Visit the root node
10/25/2012
87
Recursive TRAVERSAL
Postorder Algorithm procedure postorder(root : node) begin if root <> NULL begin postorder(root .lchild); postorder(root .rchild); write (root .data); end{if} end;{preorder}
10/25/2012
88
10/25/2012
90
10/25/2012
91
10/25/2012
92
BST INSERTION
procedure INSERT (x: elementtype, var A: Set)); {add x to set A} Begin if A = NIL then begin new (A); A . element := x; A . leftchild := NIL; A . rightchild := NIL; end
10/25/2012 CS 09 303 Data Structures - Module 3 93
else if x < A . element then INSERT (x, A .leftchild); else if x > A . element then INSERT (x, A .rightchild); {if x = A . element then, do nothing; x is already in the set} End; {INSERT}
10/25/2012
94
10/25/2012
95
BST SEARCH
function MEMBER (x: elementtype, var A : SET) : boolean; {returns true if x is in A, false otherwise} Begin if A = NIL then return (false) else if x = A .element then return (true) else if x < A .element then return (MEMBER (x, A .leftchild)) else {x > A .element} return (MEMBER (x, A .rightchild)) End; {MEMBER}
10/25/2012 CS 09 303 Data Structures - Module 3 96
BST DELETION
(1) Deleting leaf node - Just delete the leaf node (2) Deleting a node with a single child (either a left child or a right child) - Replace the node with its left (or right) child. (3) Deleting a node with both left and right child - Replace the node to be deleted with its inorder successor (with smallest value in its right subtree) and then delete the node.
10/25/2012 CS 09 303 Data Structures - Module 3 97
BST DELETION
Procedure DELETE (x: elementtype, var A : SET); {remove x from set A} Begin if A <> NIL then if x < A .element then DELETE (x, A .leftchild) else if x > A .element then DELETE(x, A .rightchild)) {if we reach here, x is at the node pointed to by A}
10/25/2012 CS 09 303 Data Structures - Module 3 98
else if A .leftchild=NIL and A .rightchild=NIL then A := NIL; {delete the leaf holding x} else if A .leftchild=NIL then {A has only right child} A := A .rightchild; else if A .rightchild=NIL then {A has only left child} A := A .leftchild; else {both children are present} A .element := DELETEMIN (A .rightchild); End; {DELETE}
10/25/2012
99
Function DELETEMIN (var A : SET) : elementtype; {returns and removes the smallest element from set A} Begin if A .leftchild = NIL then begin {A points to the smallest element} DELETEMIN := A .element; A := A .rightchild); {replace the node pointed to by A by its right child} end else {the node pointed to by A has a left child} DELETEMIN := DELETEMIN (A .leftchild); End; {DELETEMIN}
10/25/2012 CS 09 303 Data Structures - Module 3 100
(1)
10/25/2012
101
(2)
Replace the node with its right child (with value 20).
10/25/2012
102
(3)
Replace the node with its inorder successor and delete the node.
10/25/2012
103
BST Traversal
10/25/2012
105
Node to be deleted : node with value 7. The triangles represent subtrees of arbitrary size. Rule 1: Find the largest node of left subtree.
10/25/2012
107
Application of BST
Sorting: We can sort the data by reading it, item by item, and constructing a BST as we go. The inorder traversal of a BST gives the elements in ascending order. A sorted array can be produced from a BST by traversing the tree in inorder and inserting each element sequentially into the array as it is visited. Time Complexity -?????
10/25/2012 CS 09 303 Data Structures - Module 3 108
Threaded Trees
Binary trees have a lot of wasted space: each of the leaf nodes has 2 null pointers. We can use these pointers to help us in inorder traversals. We have the pointers that reference the next node in an inorder traversal; called threads. To know whether a pointer is an actual link or a thread, a boolean variable can be maintained for each pointer.
10/25/2012 CS 09 303 Data Structures - Module 3 109
10/25/2012
110
10/25/2012
111
Types: Single Threaded: each node is threaded towards either the inorder predecessor or successor. Double threaded: each node is threaded towards both the inorder predecessor and successor.
10/25/2012
112
Threads are references to the predecessors and successors of the node according to an inorder traversal. Inorder of the threaded tree is ABCDEFGHI.
10/25/2012
113
10/25/2012
114
Inorder: DBAEC
10/25/2012
115
10/25/2012
116
Output 1
8 11 13
117
Output 1 3
8 11 13
118
8 11 13
Output 1 3 5
119
8 11 13
Output 1 3 5 6
120
8 11 13
Output 1 3 5 6 7
121
8 11 13
Output 1 3 5 6 7 8
122
8 11 13
Output 1 3 5 6 7 8 9
123
8 11 13
Output 1 3 5 6 7 8 9 11
124
8 11 13
Output 1 3 5 6 7 8 9 11 13
125
10/25/2012
126
Advantages
The traversal operation is faster than that of its unthreaded version, because with threaded binary tree non-recursive implementation is possible which can run faster. We can efficiently determine the predecessor and successor nodes starting from any node (no stack required). Any node is accessible from any other node. Threads are usually upward whereas links are downward. Thus, in a threaded tree, one can move in either direction and nodes are in fact circularly linked.
10/25/2012 CS 09 303 Data Structures - Module 3 127
Limitations of BSTs
BSTs can become highly unbalanced (In worst case, time complexity for BST operations become O(n)).
root A C F M Z
10/25/2012 CS 09 303 Data Structures - Module 3 128
AVL Trees
Named after Russian Mathematicians: G.M. AdelsonVelskii and E.M. Landis who discovered them in 1962. An AVL tree is a binary search tree which has the following properties: The sub-trees of every node differ in height by at most one. Every sub-tree is an AVL tree.
10/25/2012
130
10/25/2012
131
Implementations of AVL tree insertion rely on adding an extra attribute, the balance factor to each node. Balance factor (bf) = HL - HR An empty binary tree is an AVL tree. A non-empty binary tree T is an AVL tree iff : | HL - HR | <= 1 For an AVL tree, the balance factor, HL - HR , of a node can be either 0, 1 or -1.
10/25/2012 CS 09 303 Data Structures - Module 3 132
The balance factor indicates whether the tree is: left-heavy (the height of the left sub-tree is 1 greater than the right sub-tree), balanced (both sub-trees are of the same height) or right-heavy (the height of the right sub-tree is 1 greater than the left sub-tree).
10/25/2012
133
+1
0 0
+1
-1
0 AVL Tree 2
0 AVL Tree 0
-1
0 0
10/25/2012
134
10/25/2012
135
10/25/2012
136
10/25/2012
137
For each node checked, if the balance factor remains 1, 0, or +1 then no rotations are necessary. However, if balance factor becomes less than -1 or greater than +1, the subtree rooted at this node is unbalanced.
10/25/2012
138
Theorem: When an AVL tree becomes unbalanced after an insertion, exactly one single or double rotation is required to balance the tree. Let A be the root of the unbalanced subtree. There are four cases which need to be considered. Left-Left (LL) rotation Right-Right (RR) rotation Left-Right (LR) rotation Right-Left (RL) rotation
10/25/2012 CS 09 303 Data Structures - Module 3 139
LL Rotation: Inserted node is in the left subtree of left subtree of node A. RR Rotation: Inserted node is in the right subtree of right subtree of node A. LR Rotation: Inserted node is in the right subtree of left subtree of node A. RL Rotation: Inserted node is in the left subtree of right subtree of node A.
10/25/2012
140
LL Rotation: New element 2 is inserted in the left subtree of left subtree of A , whose bf becomes +2 after insertion.
+1 B 4 0 AR 0 BL BR BL 2 BR A 6 +1 4 AR B +2 A 6
To rebalance the tree, it is rotated so as to allow B to be the root with BL and A to be its left subtree and right child respectively, and BR and AR to be the left and right subtrees of A.
10/25/2012 CS 09 303 Data Structures - Module 3 141
B 0 4 0 BL 2 A 0 6
BR
AR
10/25/2012
142
RR Rotation: New element 10 is inserted in the right subtree of right subtree of A , whose bf becomes -2 after insertion.
-2 -1 A 6 B AL BL 0 8 BL BR BR 10 AL 8 A 6 B -1
To rebalance the tree, it is rotated so as to allow B to be the root with A as its left child and BR as its right subtree, and AL and BL as the left and right subtrees of A respectively.
10/25/2012 CS 09 303 Data Structures - Module 3 143
B 0 8 0 A 6 0 10
AL
BL
BR
10/25/2012
144
Left-Left case and Left-Right case: If the balance factor of P is 2, then the left subtree outweighs the right subtree of the given node, and the balance factor of the left child L must be checked. The right rotation with P as the root is necessary. If the balance factor of L is +1, a single right rotation (with P as the root) is needed (Left-Left case). If the balance factor of L is -1, two different rotations are needed. The first rotation is a left rotation with L as the root. The second is a right rotation with P as the root (Left-Right case).
10/25/2012 CS 09 303 Data Structures - Module 3 145
10/25/2012
146
10/25/2012
147
Right-Right case and Right-Left case: If the balance factor of P is -2 then the right subtree outweighs the left subtree of the given node, and the balance factor of the right child (R) must be checked. The left rotation with P as the root is necessary. If the balance factor of R is -1, a single left rotation (with P as the root) is needed (Right-Right case). If the balance factor of R is +1, two different rotations are needed. The first rotation is a right rotation with R as the root. The second is a left rotation with P as the root (Right-Left case).
10/25/2012 CS 09 303 Data Structures - Module 3 148
10/25/2012
149
10/25/2012
150
10/25/2012
151
The retracing can stop if the balance factor becomes 1 or +1 indicating that the height of that subtree has remained unchanged. If the balance factor becomes 0 then the height of the subtree has decreased by one and the retracing needs to continue. If the balance factor becomes 2 or +2 then the subtree is unbalanced and needs to be rotated to fix it.
10/25/2012
153
Reference
Sanjay Pahuja, A Practical Approach to Data Structures and Algorithms, First Ed. 2007. For Height Balanced Trees: AVL, B-Trees, refer: Pg. 292 296, 301 315
10/25/2012
154
B-Trees
B-tree is a tree data structure that is a generalization of a binary search tree.ie; A node can have more than two children. It is also called Balanced M-way tree or balanced sort tree. A node of the tree may contain many records or keys and pointers to children. Used in external sorting. It is not a binary tree.
10/25/2012 CS 09 303 Data Structures - Module 3 155
10/25/2012
156
(3) Keys are arranged in a defined order within the node. All keys in the subtree to the left of a key are predecessors of the key and those to the right are successors of the key. (4) All leaves are on the same level. ie. There is no empty subtree above the level of the leaves.
10/25/2012
157
B-Tree Insertion
(1) (2) (3) Search and find the position for insertion. Add the key to the node if the node can accommodate it. If not, ie. if a new key is to be inserted into a full node, the node is split into two and the key with median value is inserted in parent node. Continue splitting upward, if required, until the root is reached. If parent node is the root node, and has to be split, a new root is created and the tree grows taller by one level.
10/25/2012 CS 09 303 Data Structures - Module 3 158
B-Tree Deletion
(1) Search and find the key to be deleted. (2) If the key is in a terminal node, the key along with appropriate pointer is deleted. (3) If the key is not in a terminal node, it is replaced by a copy of its successor(key with next higher value).
10/25/2012
159
(4) If on deleting the key, the new node size is lower than the minimum, an underflow occurs. (a) If either of adjacent siblings contains more than minimum number of keys, the central key is chosen from the collection:
contents of node with less than minimum number of keys, more than minimum number of keys, and the separating key from parent node.
This key is written back to parent; the left and right halves are written back to siblings.
10/25/2012 CS 09 303 Data Structures - Module 3 160
(b) If none of the adjacent siblings contains more than minimum number of keys, concatenation is used. The node is merged with its adjacent sibling and the separating key from its parent.
10/25/2012
161
10/25/2012
162
10/25/2012
163
10/25/2012
164
10/25/2012
165
10/25/2012
166
10/25/2012
167
10/25/2012
168
10/25/2012
169
10/25/2012
170
10/25/2012
171
Delete H
Since H is in a leaf and the leaf has more than minimum number of keys, we just remove it.
10/25/2012
172
Delete T
Since T is not in a leaf, we find its successor (the next item in ascending order), which happens to be W. Move W up to replace the T. ie; What we really have to do is to delete W from the leaf .
10/25/2012
173
B+ Trees
Variant of the original B-tree in which all records are stored in the leaves and all leaves are linked sequentially. The B+ tree is used as an indexing method in relational database management systems. All keys are duplicated in the leaves. This has the advantage that all the leaves are linked together sequentially, and hence the entire tree may be scanned without visiting the higher nodes at all.
10/25/2012
174
10/25/2012
175
The B + Tree consists of two types of nodes: (1) internal nodes and (2) leaf nodes Internal nodes point to other nodes in the tree. Leaf nodes point to data in the database using data pointers. Leaf nodes also contain an additional pointer, called the sibling pointer, which is used to improve the efficiency of certain types of search.
10/25/2012
176
The B + -Tree is a balanced tree because every path from the root node to a leaf node is the same length. A balanced tree is one in which all searches for individual values require the same number of nodes to be read from the disc. Order of a B + Tree The order of a B + Tree is the number of keys and pointers that an internal node can contain. An order size of m means that an internal node can contain m-1 keys and m pointers.
10/25/2012 CS 09 303 Data Structures - Module 3 177
Insertion in B+ Tree
Insert sequence : 5, 8, 1, 7, 3, 12, 9, 6 Order: 3 Empty Tree The B+Tree starts as a single leaf node. A leaf node consists of one or more data pointers and a pointer to its right sibling. This leaf node is empty.
10/25/2012
178
Inserting Key Value 5 To insert a key, search for the location where the key has to be inserted. Here, the B+Tree consists of a single leaf node, L1, which is empty. Hence, the key value 5 must be placed in leaf node L1.
10/25/2012
179
Inserting Key Value 8 Again, search for the location where key value 8 is to be inserted. This is in leaf node L1. There is room in L1; so insert the new key.
10/25/2012
180
Inserting Key Value 1 Searching for where the key value 1 should appear also results in L1 but L1 is now full as it contains the maximum two records.
10/25/2012
181
L1 must be split into two nodes. The first node will contain the first half of the keys and the second node will contain the second half of the keys.
10/25/2012
182
We now require a new root node to point to each of these nodes. We create a new root node and promote the rightmost key from node L1.
10/25/2012
183
Insert Key Value 7 Search for the location where key 7 is to be inserted, that is, L2. Insert key 7 into L2.
10/25/2012
184
Insert Key Value 3 Search for the location where key 3 is to be inserted. That is L1. But, L1 is full and must be split.
10/25/2012
185
The rightmost key in L1, i.e. 3, must now be promoted up the tree.
L1 was pointing to key 5 in B1. Therefore, all the key values in B1 to the right of and including key 5 are moved one position to the right.
10/25/2012 CS 09 303 Data Structures - Module 3 186
Insert Key Value 12 Search for the location where key 12 is to be inserted, L2. Try to insert 12 into L2 but L2 is full and it must be split.
As before, we must promote the rightmost value of L2 but B1 is full and so it must be split.
10/25/2012 CS 09 303 Data Structures - Module 3 187
Now the tree requires a new root node, so we promote the rightmost value of B1 into a new node.
10/25/2012
188
10/25/2012
189
Insert Key Value 9 Search for the location where key value 9 is to be inserted, L4. Insert key 9 into L4.
10/25/2012
190
Insert Key Value 6 Key value 6 should be inserted into L2 but it is full. Therefore, split it and promote the appropriate key value.
10/25/2012
191
Deletion in B+ Tree
Deletion sequence: 9, 8, 12.
10/25/2012
192
Delete Key Value 9 First, search for the location of key value 9, L4. Delete 9 from L4. L4 is not less than half full and the tree is correct.
10/25/2012
193
Delete Key Value 8 Search for key value 8, L5. Deleting 8 from L5 causes L5 to underflow, that is, it becomes less than half full.
10/25/2012
194
Redistribute some of the values from L2. This is possible because L2 is full and half its contents can be placed in L5.
As some entries have been removed from L2, its parent B2 must be adjusted to reflect the change.
10/25/2012 CS 09 303 Data Structures - Module 3 195
Deleting Key Value 12 Deleting key value 12 from L4 causes L4 to underflow. However, because L5 is already half full we cannot redistribute keys between the nodes. L4 must be deleted from the index and B2 adjusted to reflect the change.
10/25/2012
196
B+ Trees
Reference: http://www.mec.ac.in/resources/notes/notes/ds/bplus .htm
10/25/2012
197
GRAPH
Graph G = (V,E) is a collection of vertices and edges. (1) Set of vertices - V (2) Set of Edges - E
10/25/2012
199
GRAPH TERMINOLOGIES
Directed Graph (Digraph) :A graph
called tail.
10/25/2012
200
vertex.
ADJACENT VERTICES :Two vertices are adjacent if
the vertex.
10/25/2012
201
PATH: A sequence of vertices. SIMPLE PATH : Path in which first and last vertices
are
distinct.
CYCLE : Length of path must be minimum 1 and begins and
path from any vertex to any other vertex. Otherwise, it is said to be disconnected. {Disconnected graph contains components.}
10/25/2012
204
REPRESENTATION OF GRAPH
1) Sequential Representation
10/25/2012
205
10/25/2012
207
10/25/2012
208
10/25/2012
211
Example
Suppose the adjacency matrix for a graph is:
1 1 1 0 0 2 1 0 1 1 3 1 0 0 1 4 1 0 1 0 1 2 3 4
The corresponding adjacency list representation is: 1 -> 1 -> 2 -> 3 -> 4 2 -> 1 3 -> 2 -> 4 4 -> 2 -> 3
10/25/2012 CS 09 303 Data Structures - Module 3 212
Operations on Graphs
FIRST (v) : returns the index for the first vertex adjacent to v. NEXT(v,i ) : returns the index after index i for the vertices adjacent to v. VERTEX ( v, i ) : returns the vertex with index i among the vertices adjacent to v.
10/25/2012
213
(1) FIRST (V) : Var A : array [1..n,1..n] of boolean Function FIRST (v: integer ):integer; Var i : integer ; Begin for i:= 1 to n do if A [v, i] then return (i); return (0); { if we reach here v has no adjacent vertex} End; {FIRST }
10/25/2012 CS 09 303 Data Structures - Module 3 214
(2) NEXT(v,i ) : Function NEXT (v: integer , i : integer ):integer; Var j : integer ; Begin for j:= i+1 to n do if A [v, j] then return (j); return (0); End; {NEXT } (3) VERTEX ( v, i ) : returns the vertex with index i
(4)TRAVERSAL OF ADJACENT VERTICES OF V i :=FIRST(v); while i <> NULL do begin w : = VERTEX (v,i); { some action on w } i:=NEXT(v,i); end; { while }
10/25/2012
216
(5)CREATING A GRAPH Steps (1) Input total number of vertices in a graph, say n. (2) Allocate memory dynamically for the vertices to store in list array. (3) Input the first vertex and vertices through which it has edge by linking node from list array through nodes. (4) Repeat the process incrementing the list array to add other vertices and edges. (5) Exit
10/25/2012 CS 09 303 Data Structures - Module 3 217
(6)SEARCHING & DELETING FROM A GRAPH Steps (1) Input an edge to be searched. (2) Search for an initial vertex of edge in list arrays by incrementing the array index. (3) Once it is found, search through linked list for the terminal vertex of the edge. (4) If found, display The edge is present in the graph. (5) Then delete the node where the terminal vertex is found and rearrange the linked list. (6) Exit.
10/25/2012 CS 09 303 Data Structures - Module 3 218
(7)TRAVERSING A GRAPH Breadth First Search (BFS ) (1) Input the vertices of the graph and its edges G=(V,E ) (2) Input the source vertex and mark it as visited. (3) Add the source vertex to queue . (4) Repeat step 5 and 6 until the queue is empty. ( i.e, front >rear ) (5) Pop the front element of queue and display it. (6) Add the vertices, which is neighbor to just popped element, if it is not in the queue . (7) Exit.
10/25/2012 CS 09 303 Data Structures - Module 3 219
Breadth First Search (BFS ) ALGORITHM Procedure bfs (V) {bfs visits all vertices adjacent to V using BFS} Var Q : QUEUE of vertex ; x,y : vertex; begin mark[v] := visited; ENQUEUE(V,Q);
10/25/2012
220
10/25/2012
while not EMPTY (Q) do begin x:= FRONT (Q); DEQUEUE(Q); for each vertex y adjacent to x do if mark[y] = unvisited then begin mark[y] := visited; ENQUEUE(y,Q); end {if} end;{while } End; {bfs}
CS 09 303 Data Structures - Module 3 221
Example
10/25/2012
222
Depth First Search (DFS ) ALGORITHM STEPS (1) Input the vertices and edges of graph G=(V,E). (2) Input the source vertex and assign it to the variable S. (3) Push the source vertex to the stack. (4) Repeat steps 5 and 6 until the stack is empty. (5) Pop the top element of stack & display it. (6) Push the vertices adjacent to just popped element if it is not in the stack & is displayed (i.e. not visited). (7) exit
10/25/2012
223
Depth First Search (DFS ) ALGORITHM ALGORITHM Procedure DFS(V: Vertex ); Var W : vertex; begin mark(V) := visited; for each vertex W on L(V) do if mark[w]=unvisited then DFS(W); end; {DFS} L(V) is the adjacency list.
CS 09 303 Data Structures - Module 3 224
10/25/2012
Tree Searches
A B C
A tree search starts at the root and explores nodes from there, looking for a goal node (a node that satisfies certain conditions, depending on the problem)
10/25/2012
225
A depth-first search (DFS) explores a path all the way to a leaf before backtracking and exploring another path. For example, after searching A, then B, then D, the search backtracks and tries another path from B. Node are explored in the order ABDEHLMNIOPCFG JKQ N will be found before J
226
10/25/2012
A breadth-first search (BFS) explores nodes nearest the root before exploring nodes further away. For example, after searching A, then B, then C, the search proceeds with D, E, F, G. Node are explored in the order ABCDEFGHIJKLMN OPQ J will be found before N
10/25/2012
227
How to do DFS?
Put the root node on a stack; while (stack is not empty) do begin remove a node from the stack; if (node is a goal node) return success; put all children of node onto the stack; end return failure; At each step, the stack contains a path of nodes from the root. The stack must be large enough to hold the longest possible path, that is, the maximum depth of search.
10/25/2012 CS 09 303 Data Structures - Module 3 228
How to do BFS?
Put the root node on a queue; while (queue is not empty) do begin remove a node from the queue; if (node is a goal node) return success; put all children of node onto the queue; end return failure; Just before starting to explore level n, the queue holds all the nodes at level n-1. In a typical tree, the number of nodes at each level increases exponentially with the depth.
10/25/2012 CS 09 303 Data Structures - Module 3 229
Greedy Algorithms
10/25/2012
230
The vertex set V1 is same as that of graph G. The edge set E1 is a subset of G. There is no cycle. A graph can have many different spanning trees.
10/25/2012
231
A weighted tree is one in which each edge is assigned a weight. Weight or cost of a spanning tree is the sum of weights of its edges. A Minimum Spanning Tree or Minimum-Weight Spanning Tree is a spanning tree with weight less than or equal to the weight of every other spanning tree.
10/25/2012
232
This figure shows there may be more than one minimum spanning tree in a graph. In the figure, the two trees below the graph are two possibilities of minimum spanning tree of the given graph.
10/25/2012 CS 09 303 Data Structures - Module 3 233
MST Algorithms
Kruskals Algorithm Finds an MST for a connected weighted undirected graph. If the graph is not connected, then it finds a minimum spanning forest (a minimum spanning tree for each connected component). Prims Algorithm Finds an MST for a connected weighted undirected graph. Prim's algorithm requires the graph to be connected.
10/25/2012
234
KRUSKALs ALGORITHM
Kruskals algorithm works by growing the minimum spanning tree one edge at a time, adding the lowest cost edge that does not create a cycle.
It starts with each vertex as a separate tree and merges these trees together by repeatedly adding the lowest cost edge that merges two distinct subtrees (i.e. does not create a cycle).
10/25/2012
235
KRUSKALs ALGORITHM
- It builds the MST in forest (A forest is a disjoint union of trees.). Initially, each vertex is in its own tree in forest. Then, the algorithm considers each edge in order by increasing weight. If an edge (u, v) connects two different trees, then (u, v) is added to the set of edges of the MST, and the two trees connected by the edge (u, v) are merged into a single tree. On the other hand, if the edge (u, v) connects two vertices in the same tree, then edge (u, v) is discarded.
10/25/2012 CS 09 303 Data Structures - Module 3 236
Kruskal(G) - Informal Algorithm Sort the edges in order of increasing weight count = 0 while (count < n-1) do get next edge (u,v) if (component (u) <> component(v)) add edge to T component(u) = component(v) end end end
10/25/2012 CS 09 303 Data Structures - Module 3 237
DELETEMIN : deletes edge of minimum cost from a PRIORITY QUEUE . MERGE (A,B,C ): merge components A and B in C and call the result A or B arbitrarily. FIND (v, C) : returns the name of the component of C of which vertex v is a member. This operation will be used to determine whether the two vertices of an edge are in the same or in different components. INITIAL (A, v, C ) : makes A the name of the component in C containing only one vertex, namely v.
CS 09 303 Data Structures - Module 3 238
10/25/2012
Var
ncomp : integer ; { current number of components } edges : PRIORITYQUEUE ;{ the set of edges } components :MFSET ; {the set V grouped into a MERGE- FIND set of components} u,v : vertex; e : edge ; nextcomp : integer ; {name for new component } ucomp, vcomp :integer ;{component names }
10/25/2012
239
Begin MAKENULL(T); MAKENULL(edges); nextcomp := 0; ncomp := number of members of V; for v in V do begin { initialize a component to contain one vertex of V } nextcomp:= nextcomp+1; INITIAL(nextcomp, v, components ); end; {for} for e in E do {initialize priority queue of edges } INSERT(e, edges);
10/25/2012 CS 09 303 Data Structures - Module 3 240
while ncomp > 1 do begin e:= DELETEMIN(edges); let e = (u,v); ucomp := FIND(u, components); vcomp:= FIND(v, components); if ucomp <> vcomp then begin {e connects two different components} MERGE (ucomp , vcomp, components ); ncomp := ncomp -1; INSERT (e ,T); end ;{if ] end; {while } End; {kuruskal }
10/25/2012 CS 09 303 Data Structures - Module 3 241
10/25/2012
242
10/25/2012
243
10/25/2012
244
10/25/2012
245
10/25/2012
246
10/25/2012
247
Algorithm (Informal)
Start with a tree which contains only one node. Identify a node (outside the tree) which is closest to the tree and add the minimum weight edge from that node to some node in the tree and incorporate the additional node as a part of the tree. If there are less than n 1 edges in the tree, go to 2.
10/25/2012
248
PRIMS ALGORITHM
Procedure Prim (G: graph; var T: set of edges); {Prim constructs a minimum-cost spanning tree T for G}. Var U: set of vertices; u, v : vertex; Begin T:= ; U:= {1};
10/25/2012
249
while U<>V do begin let (u,v) be a lowest cost edge such that u is in U and v is in V-U;
10/25/2012
250
10/25/2012
251
10/25/2012
252
10/25/2012
253
begin for i:= 2 to n do begin {initialize with only vertex 1 in the set U } LOWCOST[i] :=C [1,i] ; CLOSEST[i]:=1; end;{for }
10/25/2012
255
For i:= 2 to n do begin {find the CLOSEST vertex k outside of U to some vertex in U} Min:= LOWCOST[2]; k:= 2; For j:=3 to n do If LOWCOST[i] < min then begin Min := LOWCOST[j]; k := j; end; Writeln(k, CLOSEST[k]); {print edge } LOWCOST[k] := infinity; {k is added to U }
10/25/2012 CS 09 303 Data Structures - Module 3 256
For j:=2 to n do { adjust costs to U } If (C[k,j] < LOWCOST[j] ) and (LOWCOST[j] < infinity ) then begin LOWCOST[j] := C[k,j]; CLOSEST[j]:= k; End; {if } End {for } End ;{Prim} Where CLOSEST[i] Gives vertex in U that is currently closest to vertex i in V-U. LOWCOST[i] Cost of the edge (i,CLOSEST[i])
10/25/2012
257
10/25/2012
258
10/25/2012
259
v3 is closest to tree . . .
10/25/2012
260
10/25/2012
261
v6 is closest to tree . . .
10/25/2012
262
10/25/2012
263
10/25/2012
264
All-Pairs Shortest Paths Algorithm to find for each ordered pair of vertices (v,w), the shortest path from v to w.
Floyds Algorithm
10/25/2012
265
Dijkstras Algorithm
The algorithm works by maintaining a set S of vertices whose shortest distance from the source is already known. Initially, S contains only the source vertex. At each step, we add to S a remaining vertex v whose distance from the source is as short as possible. Assuming that all arcs have nonnegative costs, we can always find a shortest path from the source to v that passes only through vertices in S.
10/25/2012 CS 09 303 Data Structures - Module 3 266
At each step of the algorithm, we use an array D to record the length of the shortest path to each vertex. Once S includes all vertices, all paths are shortest paths, so D will hold the shortest distance from the source to each vertex.
10/25/2012
267
Procedure Dijikstra {Dijikstra computes the cost of shortest paths from vertex 1 to every vertex of a directed graph} Begin S := {1}; For i :=2 to n do D[i] :=C[1,i]; {Initialize D } For i:=1 to n-1 do begin Choose a vertex w in V-S such that D[w] is minimum ; add w to S; For each vertex v in V-S do D[v]:= min( D[v], D[w] + C[w,v]); end;{for} End;{Dijikstra}
10/25/2012 CS 09 303 Data Structures - Module 3 268
10/25/2012
269
10/25/2012
270
FLOYDS ALGORITHM
Procedure Floyd(Var A :array[1..n ,1..n] of real; C :array[1..n,1..n] of real ); {Floyd computes shortest path matrix A given arc cost matrix C} Var i, j, k : integer; Begin For i := 1 to n do For j := 1 to n do A[i, j] := C[i, j]; For i:=1 to n do A[i, i] := 0;
10/25/2012
271
For k := 1 to n do For i:=1 to n do For j:=1 to n do If A[i, k] + A[k, j] < A[i,j] then A[i,j] := A[i, k] + A[k, j]; End; {Floyd }
10/25/2012
272
10/25/2012
273
10/25/2012
274
Thank You
10/25/2012
275