Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Ullman
Syllabus
Module I (11 hours) Review of Data Types - Scalar Types - Primitive types - Enumerated types - Subranges - Arrays- sparse matrices - representation - Records - Complexity of Algorithms - Time & Space Complexity of Algorithms Recursion: Recursive algorithms - Analysis of Recursive algorithms Module II (18 hours) Linear Data Structures - Stacks Queues -Lists - Dequeus - Linked List - singly, doubly linked and circular lists - Application of linked lists - Polynomial Manipulation - Stack & Queue implementation using Array & Linked List - Typical problems - Conversion of infix to postfix - Evaluation of postfix expression priority queues Module III (18 hours) 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 Module IV (18 hours) Searching - Sequential Search - Searching Arrays and Linked Lists - Binary Searching - Searching arrays and Binary Search Trees - Hashing - Open & Closed Hashing - Hash functions - Resolution of Collision Sorting- n2 Sorts - Bubble Sort - Insertion Sort - Selection Sort - n log n Sorts - Quick Sort - Heap Sort Merge Sort - External Sort - Merge Files
CS09 303 Data Structures - Module 1
References
Text Books Aho A.V, Hopcroft J.E. & Ullman J.D, Data Structures and Algorithms, AddisonWesley Reference Books 1. Sahni S, Data Structures, Algorithms and Applications in C++, McGrawHill 2. Wirth N, Algorithms + Data Structures = Programs, Prentice Hall. 3. Cormen T.H, Leiserson C.E & Rivest R.L, Introduction to Algorithms in C++, Thomson Books. 4. Fundamentals of Computer Algorithms, Ellis Horowitz, S. Sahni 5. Deshpande P.S, Kakde O.G, C and Data Structures, Dream- tech India Pvt. Ltd.
CS09 303 Data Structures - Module 1
Module 1
Introduction Review of Data Types Scalar types Primitive types Enumerated types Subrange types Arrays - representation sparse matrices Records - representation Sets - representation
Complexity of Algorithms Time complexity Space Complexity Recursion: Recursive algorithms Analysis of Recursive algorithms
Problems to Programs
Problem formulation and specification Design of the solution Implementation Testing and documentation Evaluation of the solution
CS09 303 Data Structures - Module 1
Problems to Programs
Identify the problem Formal model (mathematical model informal algorithm) Check for existing programs for the given problem Algorithm pseudo-language and stepwise refinement Implementation
CS09 303 Data Structures - Module 1
Algorithm
Finite sequence of instructions Clear meaning Finite amount of effort Finite length of time (never enters an infinite loop on any input.)
Algorithm Specification
Pascal Language Pseudo Language Constructs of a programming language + Informal English statements
Mathematical model (together with various operations defined on the model). Designing Phase Data Structure: Logical Model (to represent the mathematical model underlying an ADT) Implementation Phase Both are models with collection of operations
CS09 303 Data Structures - Module 1
ADT
The model defines an abstract view to the problem. This implies that the model focuses only on problem and tries to define properties of the problem. These properties include: the data which are affected and the operations which are identified by the problem.
As an example, consider the administration of employees in an institution. What employee information administration? What tasks should be allowed? is needed by the
Employees are real persons who can be characterized with many properties; a few are: name, size, date of birth, social number, room number, hobbies.
CS09 303 Data Structures - Module 1
Only some of them are problem specific. Consequently, you create a model of an employee for the problem. This model only implies properties which are needed to fulfill the requirements of the administration, for instance, name, date of birth and social number. These properties are called the data of the (employee) model. Now you have described real persons with help of an abstract employee.
CS09 303 Data Structures - Module 1
There must be some operations defined with which the administration is able to handle the abstract employees. For example, there must be an operation which allows you to create a new employee once a new person enters the institution. Also, you have to identify the operations which should be able to be performed on an abstract employee. You also decide to allow access to the employees' data only with associated operations.
CS09 303 Data Structures - Module 1
Data Abstraction
Abstraction is the structuring of a nebulous(not properly defined) problem into well-defined entities by defining their data and operations. These entities combine data and operations.
An entity with the properties just described is called an abstract data type (ADT). ADT consists of an abstract data structure and operations. Only the operations are viewable from the outside.
Once a new employee is ``created', the data structure is filled with actual values; You then have an instance of an abstract employee. As many instances of an abstract employee as needed can be created to describe every real employed person.
An abstract data type (ADT) is characterized by the following properties: 1. It exports a type.
2. It exports a set of operations. This set is called interface. 3. Operations of the interface are the one and only access mechanism to the type's data structure. 4. Axioms and preconditions define the application domain of the type.
CS09 303 Data Structures - Module 1
To define an ADT for complex numbers. Complex numbers consists of two parts: real part and imaginary part. Both parts are represented by real numbers. Complex numbers define several operations: addition, subtraction, multiplication, division etc.
To represent a complex number, it is necessary to define the data structure to be used by its ADT. Two possibilities: Both parts are stored in a two-valued array where the first value indicates the real part and the second value the imaginary part of the complex number. If x denotes the real part and y the imaginary part, you could think of accessing them via array subscription: x=c[0] and y=c[1].
CS09 303 Data Structures - Module 1
Both parts are stored in a two-valued record. If the element name of the real part is r and that of the imaginary part is i, x and y can be obtained with: x=c.r and y=c.i. ADT definition also says that for each access to the data structure, there must be an operation defined. The addition of two complex numbers requires you to perform an addition for each part. Consequently, you must access the value of each part which is different for each version.
CS09 303 Data Structures - Module 1
By providing an operation ``add'' you can encapsulate these details from its actual use. In an application context you simply ``add two complex numbers'' regardless of how this functionality is actually achieved. Once you have created an ADT for complex numbers, say Complex, it can be used in the same way as well-known data types such as integers.
CS09 303 Data Structures - Module 1
Abstract Data Type (ADT) Mathematical model with collection of operations defined on that model Generalization of primitive data types Eg; Sets of integers together with operations of union, intersection and set difference. Eg; LIST (of integers)
Make list empty Get first member of list, return null if empty Get next member of list, return null if empty Insert integer into list
CS09 303 Data Structures - Module 1
Implementation of ADT is a translation into statements of a programming language of the declaration that defines a variable to be that ADT, plus a procedure in that language for each operation of that ADT. Implementation chooses a data structure to represent the ADT. Ie; Data structure is used to represent the mathematical model underlying an ADT.
BOOLEAN Values: FALSE , TRUE Operations: NOT, AND, OR, assignment Predefined functions: PRED, SUCC, ORD Predefined procedures: WRITE, WRITELN CHAR Values: space < '!' < ... < '0' < '1' < ... < '9' < ':' < ';' < '<' < '=' < '>' < '?' < '@'
< 'A' < 'B' < ... < 'Z' < ... < 'a' < 'b' < ... < 'z
Operations: assignment, comparison with relational characters Predefined functions: PRED, SUCC, ORD Predefined procedures: READ, READLN, WRITE, WRITELN
INTEGER Values: -32768, ..., -1, 0, 1, 2, 3, ... 32767 (= MAXINT) Operations: +, -, *, DIV, MOD, assignment, comparison with relationals Predefined functions: PRED, SUCC, ORD, ABS, SQR, SQRT, ODD, CHR Predefined procedures: READ, READLN, WRITE, WRITELN REAL Values: ratios of integers Operations: +, -, *, /, assignment, comparison Predefined functions: ABS, SQR, SQRT, ROUND, TRUNC Predefined procedures: READ, READLN, WRITE, WRITELN
CHR The chr or character position function returns the character associated with the ASCII value being asked. eg; chr( 65 ) will return the character A. ORD The ord or ordinal function returns the ASCII value of a requested character. In essence, it works backwards to the chr function. Ordinal data types are those which have a predefined, known set of values. Each value which follows in the set is one greater than the previous. Characters and integers are thus ordinal data types. ord( 'C' ) will return the value 67.
SUCC The successor function determines the next value or symbol in the set, thus succ( 'd' ) will return e. PRED The predecessor function determines the previous value or symbol in the set, thus pred( 'd' ) will return c. ord(false) ord(true) ord(21) ord('A') returns returns returns returns 0 1 21 65
var School_days: Work_days; Free_days: Week_end; Worst_days: Monday..Thursday; { user defined subrange type }
CS09 303 Data Structures - Module 1
type beverage = ( coffee, tea, cola, soda, milk, water ); color = ( green, red, yellow, blue, black, white ); var drink : beverage; chair : color; drink := coffee; chair := green; if chair = yellow then drink := tea;
CS09 303 Data Structures - Module 1
SUBRANGE TYPES
Identifiers can be defined so that they have a restricted range of values of a given ordinal type. The syntax is: Identifier: First value..Last value; These constructs are called subrange types. Examples: var num: -10..19; { Num has integer values -10 to 19 inclusive } alphabet : 'A'..'Z; { Alphabet is char with values 'A' to 'Z' }
CS09 303 Data Structures - Module 1
Subranges
type name = <constant1> .. <constant2> eg: type range = low..high type digit = 0..99
Cell
Cell Basic building block of data structures Capable of holding a value
Data Structure
Created by giving names to aggregates of cells.
Arrays
Homogeneous Random access structure Index array[0..99] of type var A :array[0..99] of char name:array[index type] of celltype Array is a sequence of cells of a given type often referred to as celltype. Indextype can be an enumerated data type or subrange.
Records
Collection of cells called fields May be of dissimilar types Records can be grouped into arrays.
record <name1> : <type1> <name2> :<type2> . . <namek> :<typek> end
Using var & array combination var reclist: array[1..4]of record data:real; next:integer; end;
type cardsuit = (clubs, diamonds, hearts, spades); card = record suit: cardsuit; value: 1 .. 13; end; var hand: array [ 1 .. 13 ] of card; trump: cardsuit;
Array Vs Record
Array Homogeneous Run Time Record Heterogeneous Compile time
SETS
Ordered collection of elements + No repetition Set brackets - individually or subranges eg:[a..z,A..Z],[0..9],[mon..sun] Operators defined on all set types - [+,-,*, in ]
type T = set of T0 Eg; type set1 = set of [1..3] var A: set1; or var A: set of [1..3];
CS09 303 Data Structures - Module 1
Set Operations =>Membership operator (in) =>Bitwise operator (Union, difference, intersection) =>Relational (<=,=,>=)
FILES
Sequence of values of some particular type No index type, accessed in order Number of elements in a file can be time-varying and unlimited.
type T = file of T0 Eg; type file1 = file of char var A: file1; Or var A: file of char;
POINTERS
Cell whose value indicates another cell.
var ptr : cell type;
type link = cell; cell = record info: integer; next: link end;
CURSOR
Integer valued cell, used as array(used in Fortran)
header
pointer to an
.
1.2 3.4 5.6 7.8
data reclist
1 2
3 0 2 1
next
3 4
Complexity of algorithm
Time
Space
Understandability
Big-Oh(Worst case)
The time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the size of the input to the problem. The time complexity of an algorithm is commonly expressed using Big - Oh notation. Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm Big - Oh notation usually only provides an upper bound on the growth rate of the function.
Asymptotic efficiency- how running time of an algorithm increases with the size of input.
CS09 303 Data Structures - Module 1
Definition of Big-Oh
T(n) is O (n) Means that there exist +ve constants c and n0 such that for all n n0 , we have, T(n) c n O(g(n)) = f(n) : if there exist +ve constants c and n0 such that 0 f(n) c.g(n) , for all n n0 Find time complexity of T(n) = (n+1) Find time complexity of T(n) = 3n+2n
CS09 303 Data Structures - Module 1
Find running time of O(n),O(n),O(n log n) Find O(max(f(n),g(n))), if f(n)={n4, if n is even; n, if n is odd} g(n)={n, if n is even; n, if n is odd}
Big O notation will always assume the upper limit where the algorithm will perform the maximum number of operations or iterations. O(1) describes an algorithm that will always execute in the same time (or space) regardless of the size of the input data set. O(1) operations run in constant time. O(N) describes an algorithm whose performance will grow linearly and in direct proportion to the size of the input data set. O(n) operations run in linear time.
O(N2) represents an algorithm whose performance is directly proportional to the square of the size of the input data set. This is common with algorithms that involve nested iterations over the data set. Deeper nested iterations will result in O(N3), O(N4) etc. O(log n) - Any algorithm which cuts the problem in half each time is O(log n). O(log n) operations run in logarithmic time. O(n log n) Performs O(log n) operation for each item in your input. O(n log n) operations run in loglinear time.
CS09 303 Data Structures - Module 1
O(2^n) - means that the time taken will double with each additional element in the input data set. O(2^n) operations run in exponential time. O(n!) - involves doing something for all possible permutations of the n elements. O(n!) operations run in factorial time.
Elementary operations:
Arithmetic operations Assignment operation Testing a condition Read operation Write operation These operations taken independently has complexity O(1).
1. Sequence of statements which is executed only once. Constant time O(1) Statement1; Statement2; : : Statement k; Total time taken = time(Statement1) + time(Statement2) + + time(Statement k) Time for each statement is a constant. Therefore, total time is a constant and hence T(n) is O(1). This has complexity O(1) as it does not depend on the number of statements in the sequence.
CS09 303 Data Structures - Module 1
Example: Algorithm to check if a number is even or odd. if n%2 = 0 then ------O(1) writeln(number is even); ------O(1) else writeln(number is odd); ------O(1) T(n) is O(1).
2. For loop Linear time O(n) for i:=1 to n do begin sequence1; end To find time complexity of an algorithm with loops, count the number of times the loop executes. Worst case: loop executes n times. Here, each of the statements in the sequence is O(1). Total no. of iterations = n Therefore, total time taken = n*O(1) = O(n). Hence, T(n) is O(n).
CS09 303 Data Structures - Module 1
Example: Linear Search or Sequential Search Algorithm to search for a number in an array. Best case: number is found at first position. Worst case: number is found at the last position or it is not present in the array( entire array is searched). for i:=1 to n do begin if num = arr[i] then ---O(1) writeln(number found); ---O(1) end T(n) is n*O(1) = O(n).
CS09 303 Data Structures - Module 1
n times
3. If-then-else statements if condition then sequence1; else sequence2; Either sequence1 or sequence2 will execute. Worst case time is the slowest of the 2 possibilities. T(n) = O(max[time(sequence1), time(sequence2)]) Suppose time(sequence1) = O(1) and time(sequence2) = O(n), then T(n) = O(n).
CS09 303 Data Structures - Module 1
Example: Algorithm to print first n numbers if choice = 1 else print wrong choice. Best case: choice not equal to 1. Worst case: choice = 1. All n numbers are printed. if choice = 1 then for i:=1 to n do begin if num = arr[i] then ---O(1) n times writeln(number found); ---O(1) end else writeln(Wrong choice!); ---O(1) T(n) is max(O(n), O(1)) = O(n).
CS09 303 Data Structures - Module 1
4. Nested for loop a)Quadratic time O(n2) for i:=1 to m do begin for j:=1 to n do begin sequence1; end end To find time complexity of an algorithm with loops, count the total number of iterations. Worst case: Inner loop executes n times and outer loop executes m times.
CS09 303 Data Structures - Module 1
Here, each of the statements in the sequence is O(1). For each iteration of outer loop, no. of iterations of inner loop = n. Therefore, T(inner loop) = n*O(1) = O(n). No. of iterations of outer loop = m. Therefore, total no. of iterations of O(1) sequence = m*n. Or There are m repetitions of O(n) sequence. Hence, T(n) = m* O(n) = O(mn)
Example: Displaying a matrix for i:=1 to n do begin for j:=1 to n do begin writeln(arr[i,j]) ; ---O(1) end end T(n) is n*(n*O(1))) = O(n2).
n times n times
4. Nested for loop b) Cubic time O(n3) Matrix multiplication for i:=1 to n do begin for j:=1 to n do begin c[i,j] := 0; for k:=1 to n do c[i,j] := c[i,j] + a[i,k]*b[k,j]; end To find time complexity of an algorithm with loops, count the total number of iterations.
Here, c[i,j] := c[i,j] + a[i,k]*b[k,j]; is O(1). The innermost loop executes n times. ie; There are n repetitions of O(1) sequence. Therefore, T(innermost loop) = n*O(1) = O(n). No. of iterations of loop with index j = n. Therefore, total no. of iterations of O(1) sequence = n*n = n2. Or There are n repetitions of O(n) sequence. Outer loop executes n times. Therefore, total no. of iterations of O(1) sequence = n2 *n = n3. Or There are n repetitions of O(n2) sequence. Hence, T(n) is O(n3). The algorithm takes Cubic time.
CS09 303 Data Structures - Module 1
5. Nested loop where inner loop index depends on outer loop index. for i:=1 to m do begin for j:= i+1 to n do sequence1;
Value of i 1 2 : n-2 n-1 No. of iterations of inner loop n-1 n-2 : 2 1
Nested loop followed by non-nested loop for i:=1 to n do for j:= 1 to n do sequence1; for k:=1 to n do sequence2; Complexity of first loop is O(n2) and second is O(n). By sum rule, runtime of the whole sequence is T(n) is O(max(n2,n)) = O(n2).
Thus, Time taken by loop body for each iteration is O(1). Time spent in the loop of line (2) - (6) = O((n-i)* 1) = O(n-i). Statement (1) is executed (n-1) times. So the total running time of the program is bounded above by some constant times.
n-1 i=1
(n-i) = (n-1)(n-2)(n-3)(1)
Most Familiar Big-Oh notations (Orders of growth in increasing order) (1) (2) (3) (4) (5) (6) (7) (8) Constant time Logarithmic Time Linear time n log n Quadratic time Cubic time Polynomial time Exponential Time O(1) O(logn) O(n) O(nlogn) O(n2) O(n3) O(nk) O(kn)
function fact (n : integer):integer; {fact(n) computes n!} begin (1) if n<=1 then (2) fact:=1; else (3) fact:=n*fact(n-1); end; {fact}
Sample Data fact(3)=3 *fact(2) fact(2)=2*fact(1) fact(1)=1*fact(0) fact(0)=1 fact(1)=1 fact(2)=2 fact(3)=6
Time Complexity
O(1) O(1)
O(1) + T(n-1)
Computing total running time of factorial procedure T(n) = c+ T(n-1) if n>1 (1) =d if n<=1 Hence, T(n-1) = c + T((n-1)-1) = c + T(n-2) Substitute (2) in (1) T(n) = c + [c + T(n-2)] T(n) = 2c + T(n-2) Similarly, T(n-2) = c + T(n-3)
(2)
(3) (4)
Substitute (4) in (3) Thus, T(n) = 3c + T(n-3) Or, For n>k, T(n) = k.c + T(n-k) Finally, when k = n-1, T(n) = c(n-1) + T(1) = c(n-1) + d = cn c + d Therefore ,T(n) is O(n).
if n>3 .(5)
Solution
Move top (n-1) disks from A to B.
1 = O(2N).
Each time we increment N, we double the amount of work. This grows incredibly fast!
CS09 303 Data Structures - Module 1
Deriving Recurrence Relation function THanoi(n, A, B, C): THanoi (n-1, A, C, B); Move larger disc from A to C; THanoi (n-1, B, A, C); T(n) = T (n-1) + O(1) + T(n-1) = 2T(n-1) + O(1) //A to B using C //B to C using A
T(n) = 2T(n-1) + O(1) = 2[2T(n-2) + O(1)] + O(1) = 2[2 [2T(n-3) + O(1)] + O(1)] + O(1) =23 T(n-3) + 22 O(1) + 2O(1) + O(1) : = 2n T(0) + (2n -1)O(1) Therefore, T(n) is O(2n). The algorithm takes exponential time.
Additional Points
Space complexity Recursive algorithm(Fibonacci) Steps in recursion Recursion Vs Iteration Disadvantages of recursion
Thank You
Resmi N.G. References: Data Structures and Algorithms: Alfred V. Aho, John E. Hopcroft, Jeffrey D. Ullman A Practical Approach to Data Structures and Algorithms: Sanjay Pahuja
Module 2
Module II (18 hours) Linear Data Structures - Stacks Queues -Lists Dequeues - Linked List - singly, doubly linked and circular lists - Application of linked lists - Polynomial Manipulation - Stack & Queue implementation using Array & Linked List - Typical problems - Conversion of infix to postfix - Evaluation of postfix expression priority queues
9/29/2012
LIST
Flexible data structure: grows and shrinks on demand. Elements can be accessed, inserted, or deleted at any position within
a list.
Lists can be concatenated or split into sub-lists. Mathematically, a list is a sequence of zero or more elements of a
given type. Represented by a comma separated sequence of elements a1,a2,an where n>=0 and each ai is of a given type.
n is the length of the list.
9/29/2012 CS 09 303 Data Structures - Module 2 3
Features Of List
Can be linearly ordered according to their position on the list. Concatenation and splitting possible Main application areas are: => Information retrieval => Programming language translation => Programming language simulation
9/29/2012
LIST OPERATIONS
INSERT(x, p,L) : Inserts x at position p in list L. Move elements at p and following positions to next higher positions. Insert x at position p in list L. If L is a1, a2, , an, then L becomes a1, a2, , ap-1, x, ap, , an . If p is END (L), then L becomes a1, a2, , an, x.
9/29/2012
END(L) : returns the last position of list L LOCATE(x, L) : returns the position of x on list L.
If x appears more than once, it returns the position of first occurrence of x. If x does not appear at all, then it returns END(L).
9/29/2012
NEXT(p, L): returns position following p on list L. PREVIOUS(p, L):returns position preceding p on list L. MAKENULL(L) : Makes the list L empty and returns the position END(L). FIRST(L) : returns first position on list L. PRINTLIST(L) :prints elements of L in order of occurrence.
9/29/2012
Implementation Of List
2 Ways Array Implementation Pointer Implementation(Linked List representation)
9/29/2012
9/29/2012
position = integer;
9/29/2012 CS 09 303 Data Structures - Module 2 10
END(L) Algorithm
function END (var L: List) : position; begin return (L.last + 1) {gives total no. of elements in the list} end; {END}
0 50 1 107 2 15 3 201 L.last
9/29/2012
11
INSERTION Algorithm
procedure INSERT(x : elementtype; p : position; var L: LIST) {INSERT places x at position p in list L} var q:position; begin if L.last >= MAXLEN then error (list is full) else if (p > L.last + 1 ) or ( p < 1) then error(position does not exist) else begin for q := L.last downto p do {shift elements at p, p+1 down by one position} L.elements[q+1] := L.elements[q]; L.last := L.last+1; L.elements[p] := x; end end;{INSERT}
9/29/2012 CS 09 303 Data Structures - Module 2 12
Insertion
Insert (47, 0, L)
0 1 2 3
L.last
CS 09 303 Data Structures - Module 2 13
Insert (59, 1, L)
0 47 L.last p 1 2 3
9/29/2012
14
Insert (65, 2, L)
0 47 1 59 L.last p 2 3
9/29/2012
15
Insert (53, 1, L)
0 47 1 59 p q 2 65 L.last 3
Shift elements down to insert at position 1. Start with q = L.last. L.elements[q+1] := L.elements[q] { Move element at position 2 to position 3. L.elements[2+1] = L.elements[2] L.elements[3] = L.elements[2] }
47 59 p
9/29/2012 CS 09 303 Data Structures - Module 2
65 q L.last q+1
16
L.elements[q+1] := L.elements[q] { Move element at position 1 to position 2. L.elements[1+1] = L.elements[1] L.elements[2] = L.elements[1] }
47 p q
9/29/2012
59 q+1 L.last
65
17
47
53 p q
59
65 L.last
18
9/29/2012
DELETION Algorithm
procedure DELETE (p:position; var L : LIST) {DELETE removes the element at position p of list L} var q:position; begin if ( p > L.last ) or ( p < 1) then error(position does not exist) else begin L.last := L.last-1; for q := p to L.last do {shift elements at p+1, p+2, up one position} L.elements[q] := L.elements[q+1]; end end;{DELETE}
9/29/2012 CS 09 303 Data Structures - Module 2 19
Deletion
Delete (1, L)
0 47 1 53 p 2 59 3 65
L.last
47
53 p
59 L.last
65
9/29/2012
20
To delete element at 1, shift elements at 2 and 3 up by one position. Start with q = p. ie. q = 1.
0 47 1 53 p q q+1 2 59 L.last 3 65
47
59 p q q+1 L.last
65
9/29/2012
21
65 q L.last
22
LOCATE Algorithm
function LOCATE( x:elementtype; var L:LIST) : position; {LOCATE returns position of x on list L} var q:position; begin for q:=1 to L.last do if L.elements[q] = x then return(q); else return( L.last + 1 ) {if not found} end;{LOCATE}
9/29/2012
23
elements are used here. Does not require contiguous memory for storage. Allocates memory dynamically Linked List is made up of cells, and each cell contains: Element of the list Pointer to next cell on the list If the list is a1,a2,an, the cell holding ai has a pointer to the cell holding ai+1, for i = 1, 2, n-1. Header holds no element but points to cell holding a1. The cell holding an has a nil pointer.
CS 09 303 Data Structures - Module 2 25
9/29/2012
9/29/2012
26
9/29/2012
27
9/29/2012
28
9/29/2012
30
9/29/2012
31
There is no work to find the location. Insertion at the beginning of the list
No Worries!
9/29/2012 CS 09 303 Data Structures - Module 2 32
//
No Worries!
9/29/2012 CS 09 303 Data Structures - Module 2 33
//
9/29/2012
34
9/29/2012
35
Explanation
(1) temp := p . next; {address of the cell next to p is stored in temp}
head
4 p
6 Temp
9/29/2012
36
(2) New(p .next); {A new cell is created and its address is assigned to p .next}
head
4 p
p . next
9/29/2012 CS 09 303 Data Structures - Module 2 37
head
4 p
9/29/2012
38
head
4 p
6 temp
head
9/29/2012
40
9/29/2012
41
9/29/2012
42
9/29/2012
43
9/29/2012
44
9/29/2012
45
9/29/2012
46
9/29/2012
47
17
42
Target = 4
9/29/2012
48
head p 6
17
42
9/29/2012
49
head p 6
17
42
Check if p.next.element = 4. NO. So, move onto next cell. p = p.next Target = 4
9/29/2012
50
head
17
42
p Check if p.next.element = 4. YES. So, perform the operation p.next = p.next.next Target = 4
9/29/2012
51
head
17
42
p .next = p.next.next
Target = 4
9/29/2012
52
head
17
42
Target = 4
9/29/2012
53
head
17
42
9/29/2012
54
9/29/2012
56
9/29/2012
57
9/29/2012
58
If Temp < > NIL and i = pos then begin New( newNode); newNode .element : = element; newNode .next : = Temp .next; newNode . previous : = Temp; Temp .next . previous : = newNode; Temp . next := newNode; end { if } Else begin error ( position not found ) end; { INSERT}
9/29/2012 CS 09 303 Data Structures - Module 2 61
Explanation
temp := L; i : =1; temp 5 15 20 head
Check if i<pos, and temp<>NIL. i=1, pos = 2 and temp<>NIL. So, move forward. temp = temp.next i = i+1
9/29/2012 CS 09 303 Data Structures - Module 2 62
head
5 temp
15
20
Check if i<pos, and temp<>NIL. i=2, pos = 2 and temp<>NIL. So, do insertion.
9/29/2012
63
If Temp < > NIL and i = pos then begin new( newNode); {Creates a new node in memory and assigns its address to newnode.}
5 temp
15
20
9/29/2012
64
head
5 temp
15
20
10
9/29/2012
65
head
5 temp
15
20
10
9/29/2012
66
5 temp
15
20
10
9/29/2012
67
head
5 temp
15
20
9/29/2012
68
head
10
15
20
9/29/2012
69
9/29/2012
70
DELETION ALGORITHM Procedure DELETE ( Var p : position); Begin if p . Previous < > nil then { deleted cell not the first } p . Previous . Next : = p . Next; if p . Next < > nil then { deleted cell not the last } p . Next . Previous : = p . Previous End { DELETE}
9/29/2012
71
9/29/2012
72
STACK
An Abstract Data Type Special List : insertions & deletions at one
end(top)
Also called LIFO(Last In First Out) list.
9/29/2012 CS 09 303 Data Structures - Module 2 73
9/29/2012
74
9/29/2012
75
Stack Implementation
Static implementation(Using arrays) Dynamic implementation(Using pointers)
9/29/2012
77
9/29/2012
78
Array Implementation
Procedure PUSH( var x:elementtype; Var s:STACK); Begin If s.top = MAXLENGTH -1 then error(stack is full) Else begin s.top:=s.top+1; s.elements[s.top]:=x; End; End;{PUSH}
9/29/2012 CS 09 303 Data Structures - Module 2 79
Initial stack
3 2 1 0
Top = -1
9/29/2012
80
Push (12, S)
s.top:=s.top+1 = -1 + 1 = 0; s.elements[s.top]:= s.elements[0] = 12; 3 2 1 0 12
Top = 0
9/29/2012
81
Push (34, S)
s.top:=s.top+1 = 0 + 1 = 1; s.elements[s.top]:= s.elements[1] = 34; 3 2 1 0 34 12
Top = 1
9/29/2012
82
Push (47, S)
s.top:=s.top+1 = 1 + 1 = 2; s.elements[s.top]:= s.elements[2] = 47; 3 2 1 0 47 34 12
Top = 2
9/29/2012
83
Push (53, S)
s.top:=s.top+1 = 2 + 1 = 2; s.elements[s.top]:= s.elements[3] = 53; 3 2 1 0 53 47 34 12
Top = 3
9/29/2012
84
Procedure POP( Var s:STACK); begin if EMPTY(s) then error(stack is empty) else s.top := s.top-1; end;{POP}
9/29/2012
85
9/29/2012
86
Function EMPTY( Var s:STACK):boolean; begin If s.top<0 then return(true); else return(false); end;{EMPTY}
9/29/2012
87
Function TOP ( Var s:STACK):elementtype; begin If EMPTY(s) then error(stack is empty) else return(s.elements[s.top]); end;{TOP}
9/29/2012
88
Applications of stack
Explicit and implicit recursive calls Conversion of Infix to postfix Postfix expression evaluation Conversion of infix to prefix Prefix expression evaluation Parenthesis checker
9/29/2012
89
Applications of stack
Expression : operands together with operator Infix notation(A+B) Prefix notation( +AB) Postfix notation( AB+) Operator precedence ^ (Exponential ) .Highest *, /(mul, div) Next highest +, - (add,sub) Lowest
9/29/2012
90
Applications of stack
Conversion from infix to postfix Rules 1. Parenthesize the expression starting from left to right 2. The operands associated with the operator having higher precedence will be parenthesized first. 3. The sub expression which has been converted to postfix is to be treated as single operand. 4. Once the expression is converted to postfix form, remove the parenthesis
9/29/2012
91
(b) Add # to stack. 5. If a left parenthesis is encountered, push it onto the stack. 6. If a right parenthesis is encountered then (a) Repeatedly pop from stack and add to P, until a left parenthesis is encountered. (b) Remove the left parenthesis. [ do not add the left parenthesis to P]. 7. Exit #
9/29/2012
9/29/2012
94
9/29/2012
95
sentinel]. (2) Scan P from left to right and repeat steps 3 and 4 for each element of P until the sentinel ) is encountered. (3) If an operand is encountered, put it on STACK. (4) If an operator # is encountered, then (a) Remove the two top elements of STACK ,where A is the top element and B is the next-to-top element. (b) Evaluate B # A. (c) Place the result onto STACK. 5) RESULT equal to the top element on stack. 6) Exit
9/29/2012 CS 09 303 Data Structures - Module 2 96
Evaluate 45+72-*
9/29/2012
97
Evaluate 59+2*65*+
9/29/2012
98
element of I until the stack is empty. (3) If an operand is encountered, add it to O. (4) If an operator # is encountered, then (a) Repeatedly pop from stack and add to O each operator (on the top of stack) which has higher precedence than #.
9/29/2012 CS 09 303 Data Structures - Module 2 99
(b) Add # to stack. 5. If a right parenthesis is encountered, push it onto the stack. 6. If a left parenthesis is encountered then (a) Repeatedly pop from stack and add to O, until a right parenthesis is encountered. (b) Remove the right parenthesis. [ do not add the right parenthesis to O]. 7. Reverse O to get the prefix expression P. 8. Exit #
9/29/2012
Convert (A*B-F*H)^D
Symbol Scanned D ^ ) H * F B * A ( ( Operator Stack ) )^ )^) )^) )^)* )^)* )^))^))^)-* )^)-* )^ (Empty) Output Expression D D D DH DH DHF DHF* DHF*B DHF*B DHF*BA DHF*BA*DHF*BA*-^
Result: ^-*AB*FHD
9/29/2012 CS 09 303 Data Structures - Module 2 101
Home Work
Convert to prefix: A+ [ B+C - D+E] * F / G +A/*+-+BCDEFG (ab)/c*(d + e f / g) */-abc-+de/fg
9/29/2012
102
Convert (ab)/c*(d + e f / g)
9/29/2012
*/-abc-+de/fg 103
as a sentinel]. (2) Scan P from right to left and repeat steps 3 and 4 for each element of P until the sentinel ( is encountered. (3) If an operand is encountered, push it onto STACK. (4) If an operator # is encountered, then (a) Remove the two top elements of STACK ,where A is the top element and B is the next-to-top element. (b) Evaluate A # B. (c) Place the result onto STACK. 5) RESULT equal to the top element on stack. 6) Exit
9/29/2012 CS 09 303 Data Structures - Module 2 104
Evaluate *+45-72
9/29/2012
105
Parenthesis Checker
To check whether a mathematical expression is properly parenthesized or not. 3 sets of grouping symbols The standard parenthesis () The braces { } The brackets []
9/29/2012
106
9/29/2012
107
9/29/2012
108
else pop a symbol s from the stack if (s does not correspond to c ) then print error incorrect nesting of symbols exit end {if} end {if} end {if} end {for}
if (stack is not empty ) then print error missing closing symbol else print input expression is OK end {if} END
9/29/2012
110
Self-Study (Homework)
Stack using Linked List Write algorithms to:
Push an element onto stack Pop an element Create an empty stack Check if stack is empty Return the top element from the stack
9/29/2012
111
QUEUES
Abstract data type which is special kind of List where items are
inserted at one end (rear) and deleted from the other end (front).
Insertion :Rear Deletion :Front Also called FIFO(First In First Out) list.
9/29/2012 CS 09 303 Data Structures - Module 2 112
front
rear
9/29/2012
113
QUEUE OPERATIONS
MAKENULL(Q) : makes queue Q an empty list FRONT(Q) : returns first element of Q ENQUEUE(x, Q) : inserts element x at end of Q DEQUEUE(Q) : deletes first element of Q EMPTY(Q) : returns true if Q is an empty queue.
9/29/2012
114
9/29/2012
115
9/29/2012
116
IMPLEMENTATION OF QUEUE
Using Arrays (Static implementation) Using Pointers (Dynamic implementation)
9/29/2012
117
Array Implementation
Type QUEUE = record elements : array[1..MAXLENGTH] of elementtype; front, rear: integer; end;
9/29/2012
118
9/29/2012
119
(1)Empty Queue
Rear := -1 Front := -1 0 rear front 1 2 3
(2)ENQUEUE(10,Q)
Front := 0 Rear := rear + 1 = 0 Q[Rear] := Q[0] = 10
10
0 rear 1 front 2 3
10
0 front 1
3
2 3 rear
120
9/29/2012
(4)ENQUEUE(17,Q)
Rear := rear + 1 = 1 + 1 =2 Q[Rear] := Q[2] = 17
10
0 1 front
17
2 rear 3
(5)ENQUEUE(29,Q)
Rear := rear + 1 = 2+1 = 3 Q[Rear] := Q[3] = 29
10
0 front 1
17
2
29
3 rear
9/29/2012
121
Deletion Operation Function DEQUEUE(Var Q: QUEUE) : elementtype; Begin if Q.rear < Q.front then begin Q.front:=0; Q.rear:= -1; error(Queue is empty); exit(); end else begin dequeue := Q.elements[Q.front]; Q.front:= Q.front+1; end; end;{DEQUEUE}
(2)
9/29/2012
122
(1)DEQUEUE(Q)
data := Q[front] = Q[0] =10 front := front + 1 = 0 + 1 = 1 0 1 front
17
2
29
3 rear
(1)DEQUEUE(Q)
data := Q[front] = Q[1] = 3 front := front + 1 = 1 + 1 = 2
17
0 1 front 2
29
3 rear
(2)DEQUEUE(Q)
data := Q[front] = Q[2] = 17 front := front + 1 = 2 + 1 = 3
29
0 1 2 3 front rear
9/29/2012
123
(4)DEQUEUE(Q)
data := Q[front] = Q[3] = 29 front := front + 1 = 3 + 1 = 4 0 1 2 3 rear front = 4
9/29/2012
124
type celltype = record element: elementtype; next : celltype; end; type QUEUE = record front, rear : celltype; end;
9/29/2012 CS 09 303 Data Structures - Module 2 125
(1) To Make a NULL QUEUE procedure MAKENULL(Var Q:QUEUE); begin new(Q.front);{create header cell } Q.front .next := nil; Q.rear := Q.front; {header is both first &last cell } end;{MAKENULL}
9/29/2012
126
(2) Check if QUEUE is empty function EMPTY(Var Q:QUEUE): boolean; begin if Q.front = Q.rear then return(true); else return(false); end;{EMPTY}
9/29/2012
127
function FRONT(Var Q:QUEUE):elementtype begin If EMPTY(Q) then error(Queue is empty); else return(Q . front . next . element) end;{FRONT}
9/29/2012
128
Procedure ENQUEUE( var x:elementtype;Var Q:Queue); begin new(Q.rear . next); {add new cell to rear of queue} Q.rear := Q.rear . next; Q. rear .element := x; Q.rear .next:=nil; end; {ENQUEUE}
9/29/2012
129
Function DEQUEUE(Var Q:Queue): elementtype; Begin if EMPTY(Q) then error(queue is empty) else begin dequeue := Q.front .element; Q.front := Q.front .next; end; end; {DEQUEUE}
9/29/2012 CS 09 303 Data Structures - Module 2 130
9/29/2012
131
Circular Queue
9/29/2012
132
CIRCULAR QUEUE
Q[1] following Q[n] Conditions of circular Queue: Insertion Condition
rear = (rear+1) % SIZE
Deletion Condition
front = (front + 1) % SIZE
QUEUE FULL
rear = front
9/29/2012 CS 09 303 Data Structures - Module 2 133
Rear=-1 Front=-1
9/29/2012
135
0
Front = 0
1 14
3
9/29/2012 CS 09 303 Data Structures - Module 2
2
136
0
Front = 0
Rear = 1
3
9/29/2012 CS 09 303 Data Structures - Module 2
2
137
0
Front = 0
20 3
9/29/2012 CS 09 303 Data Structures - Module 2
Rear = 2
138
1 14 42
Queue full
20 2
CS 09 303 Data Structures - Module 2 139
37
Rear = 3
9/29/2012
1 14 42
37
Rear = 3
9/29/2012
20 2
3
CS 09 303 Data Structures - Module 2
140
Deletion Function DEQUEUE(Var Q : QUEUE): elementtype; begin if Q.front = -1 then begin error(Queue is empty) exit(); end else dequeue := Q.elements[Q.front]; Q.front := (Q.front + 1) % SIZE; end;{DEQUEUE}
9/29/2012
141
9/29/2012
142
Operations on Dequeue
(1) Add an element at the rear end ( insert right) (2)Delete an element from the front end ( delete left ) (3) Add an element at the front end ( insert left ) (4) Delete an element from the rear end (delete right )
9/29/2012
144
if Q.left = -1 then begin Q.left := 0; Q.right := 0; end else begin if Q.right = MAX-1 then Q.right := 0; else Q.right := Q.right+1; end; Q.elements[Q.right] := data; end; {insertRightDequeue}
9/29/2012
146
l r
9/29/2012
147
9/29/2012
148
9/29/2012
149
insertRightDequeue ( 10, Q) Increment right. Right := right + 1 = 1 + 1 = 2; Insert at right. Q.elements[right] : = Q.elements[2] = 10;
0 5 left = 0 1 2 2 10 right = 2 3
9/29/2012
150
insertRightDequeue ( 17, Q) Increment right. Right := right + 1 = 2 + 1 = 3; Insert at right. Q.elements[right] : = Q.elements[3] = 17;
0 5 left = 0 1 2 2 10 3 17 right = 3
9/29/2012
151
Perform a delete operation. So, left will be incremented by one. left := left + 1 = 0 + 1 = 1;
1 2 left = 1
2 10
3 17 right = 3
9/29/2012
152
insertRightDequeue (15, Q) 15 can be inserted at 0. If left <> -1 and right = MAX 1, there are vacant positions at the beginning of the queue. Set right = 0 and insert at 0.
0 15 right = 0 1 2 left = 1 2 10 3 17
9/29/2012
153
9/29/2012
154
if Q.left = -1 then begin Q.left := 0; Q.right := 0; end else begin if Q.left = 0 then Q.left := MAX-1; else Q.left := Q.left-1; end; Q.elements[Q.left] := data; end; {insertLeftDequeue}
9/29/2012 CS 09 303 Data Structures - Module 2 155
l r
9/29/2012
156
9/29/2012
157
insertLeftDequeue ( 2, Q) If left = 0, set left = MAX - 1. left := MAX - 1 = 4 - 1 = 3; Insert at left. Q.elements[left] : = Q.elements[3] = 2;
0 5 right = 0 1 2 3 2 left = MAX- 1
9/29/2012
158
insertLeftDequeue ( 10, Q) Decrement left. left := left - 1 = 3 - 1 = 2; Insert at left. Q.elements[left] : = Q.elements[2] = 10;
0 5 right = 0 1 2 10 left = 2 3 2
9/29/2012
159
insertLeftDequeue ( 17, Q) Decrement left. left := left - 1 = 2 - 1 = 1; Insert at left. Q.elements[left] : = Q.elements[1] = 17;
0 5 right = 0 1 17 left = 1 2 10 3 2
9/29/2012
160
Perform a delete operation. Right will be set to MAX 1 so that element at zero is deleted first. right := MAX - 1 = 4 - 1 = 3;
1 17 left = 1
2 10
3 2 right = 3
9/29/2012
161
insertLeftDequeue (15, Q) 15 can be inserted at 0. If left <> -1 and left <> 0, there are vacant positions at the beginning of the queue. Set left := left - 1 = 2 -1 = 0 and insert at 0.
0 15 left = 0 1 2 2 10 3 17 right = 3
9/29/2012
162
9/29/2012
163
if (Q.left = Q.right ) then begin {only one element} Q.left:= -1; Q.right := -1; end;{if} if (Q.right = 0) then Q.right = MAX -1; else Q.right = Q.right -1; end; { deleteRightDequeue}
9/29/2012
165
deleteRightDequeue ( Q) Right = 0. So, delete the first element. data := Q.elements[right] = Q.elements[0] = 5; Set Right := MAX - 1 = 3; {Next deletion occurs at right = 3}.
0 5 right = 0 1 2 left = 1 2 10 3 17
9/29/2012
166
9/29/2012
167
9/29/2012
168
deleteRightDequeue (Q) data : = Q.elements[right] : = Q.elements[1] = 5; Left = right = 1; {only one element} Set left := -1, right := -1; {Queue becomes empty}
0 1 2 left = 1 right = 1 2 3
9/29/2012
169
deleteRightDequeue (Q) Empty Queue (left = right = -1) Cannot perform deletion
l r
9/29/2012
170
if Q.left = Q.right then begin {only one element} Q.left:= -1; Q.right := -1; end;{if} if ( Q.left = MAX - 1 ) then Q.left = 0; else Q.left = Q.left + 1; end; { deleteLeftDequeue}
9/29/2012
172
9/29/2012
173
9/29/2012
174
9/29/2012
175
deleteLeftDequeue (Q) data : = Q.elements[left] : = Q.elements[3] = 18; Left = right = 3; {only one element} Set left := -1, right := -1; {Queue becomes empty}
0 1 2 3 18 left = 3 right = 3
9/29/2012
176
deleteLeftDequeue (Q) Empty Queue (left = right = -1) Cannot perform deletion
l r
9/29/2012
177
Example
Insert right (rear)
0 21 left = 0 right = 0
9/29/2012
178
0 21 right = 0
3 42 left = 3
9/29/2012
179
0 21
1 35 right = 1
3 42 left = 3
9/29/2012
180
0 21
1 35 right = 1
2 5 left = 2
3 42
9/29/2012
181
0 21
1 35 right = 1
3 42 left = 2
9/29/2012
182
0 21 right = 0
3 42 left = 2
9/29/2012
183
3 42 left = 2 right = 3
9/29/2012
184
l ,r = -1
9/29/2012
185
PRIORITY QUEUE
Collection of elements such that each element is assigned a priority number. Rules are: An element of higher priority is processed before any element of lower priority. Elements of same priority are processed according to the order in which they were added into the queue.
9/29/2012
186
9/29/2012
187
Implementation
Implementation of priority queue: (1) One way list representation (2) Multiple Queues (one for each priority - Array representation) (3) Maximum or minimum heap
9/29/2012
188
9/29/2012
189
Operations performed: Searching an element with maximum priority Inserting an element Deleting an element with maximum priority
9/29/2012
190
Start
A 2 NULL
9/29/2012
191
Start
B 1
A 2 NULL
9/29/2012
192
Start
B 1
A 2
C 2 NULL
9/29/2012
193
Start
B 1
A 2
C 2 NULL
9/29/2012
194
Start
A 2
C 2 NULL
9/29/2012
195
of the list.
9/29/2012
196
first node }
(2) Delete first node from the list (3) Process ITEM (4) Exit
9/29/2012
197
Array Implementation
As multiple queues. Multiple queues are defined in the same array queue[] of size n. Each queue i is allocated a predefined space bounded by array indices. A queue is maintained for each priority. In order to process an element of the priority queue, element from non-empty highest priority number queue is accessed.
9/29/2012 CS 09 303 Data Structures - Module 2 198
front[0]
rear[0] front[1]
rear[1] front[2]
rear[2] front[3]
rear[3]
Queue1
Queue2
Queue3
Queue4
9/29/2012
199
9/29/2012
200
9/29/2012
201
9/29/2012
202
Applications of Queue
Round robin techniques :Process scheduling Print server routines All types of customer service software(Railway/airticket reservation)
9/29/2012
203
9/29/2012
204
9/29/2012
205
p = 2x3 + 3xy + y2 + yz Store the polynomial such that terms are arranged in descending order of powers of x first, then y and then z.
3002 1103 0201 0111
9/29/2012
206
10 x + 11 x +
9/29/2012
207
5 6 1570
ptrp
0 4 NULL 1750
1526
4 2 1660
ptrq
1500
1826
9/29/2012
208
3 2 1607
ptrp
0 4 NULL 1750
1570
1 10 1700 1660
1500
4 2 Null 1870
9/29/2012
209
3 2 1607
ptrp
0 4 NULL 1750
1570
1 10 1700
ptrq
1660
3 2 Null 1860
9/29/2012
210
1 1 1750
ptrp
0 4 NULL 1750
1607
0 1 NULL 1700
1660
p.powerx = q.powerx (Add the coefficients and increment both the pointers)
5 6 1870 1826 4 2 1860 1870 3 2 1807 1860
ptrr
1 11 Null 1807
9/29/2012
211
0 4 NULL
ptrp
1750
1700
NULL 1950
212
ptrr
9/29/2012
213
Else if the power of first polynomial > power of the second polynomial then
Insert the term from first polynomial into sum polynomial. Obtain the next term in the first polynomial.
Else
Insert the term from second polynomial into sum polynomial. Obtain the next term in the second polynomial. 4. Copy remaining terms from non-empty polynomial into sum polynomial. 5. Return the address of the sum polynomial.
9/29/2012 CS 09 303 Data Structures - Module 2 214
Thank You
9/29/2012
215
Resmi N.G. References: Data Structures and Algorithms: Alfred V. Aho, John E. Hopcroft, Jeffrey D. Ullman A Practical Approach to Data Structures and Algorithms: Sanjay Pahuja
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
9/29/2012
Trees
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/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;
9/29/2012
15
a a b c
9/29/2012
16
a b a b c
9/29/2012
17
a b d a b c
9/29/2012
18
a b d e a b c
9/29/2012
19
a b d e c a b c
9/29/2012
20
a b d e c f a b c
9/29/2012
21
a b d e c f g a b c
9/29/2012
22
A, B, D, H, I, E, C, F, J, G, K
9/29/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;
9/29/2012
24
d a b c
9/29/2012
25
d e a b c
9/29/2012
26
d e b a b c
9/29/2012
27
d e b f a b c
9/29/2012
28
d e b f g a b c
9/29/2012
29
d e b f g c a b c
9/29/2012
30
d e b f g c a a b c
9/29/2012
31
H, I, D, E, B, J, F, K, G, C, A
9/29/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;
9/29/2012 CS 09 303 Data Structures - Module 3 33
d a b c
9/29/2012
34
d b a b c
9/29/2012
35
d b e a b c
9/29/2012
36
d b e a a b c
9/29/2012
37
d b e a f a b c
9/29/2012
38
d b e a f c a b c
9/29/2012
39
d b e a f c g a b c
9/29/2012
40
H, D, I, B, E, A, J, F, C, K, G
9/29/2012 CS 09 303 Data Structures - Module 3 41
9/29/2012
42
n1 = (a + b) * (a + c)
*
n2 = (a + b)
+ a b a
n3 = (a + c)
n4 = (a)
n7 = (c)
n5 = (b) n6 = (a)
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/2012 CS 09 303 Data Structures - Module 3 47
IMPLEMENTATION OF TREE
Array Representation Linked List representation
9/29/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.
9/29/2012 CS 09 303 Data Structures - Module 3 49
9/29/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.
9/29/2012
51
9/29/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}
9/29/2012
53
9/29/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;
9/29/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}
9/29/2012
57
9/29/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)
9/29/2012
59
else i:= cellspace[i].next; end; return(0); end;{PARENT} {returns null node if parent not found}
9/29/2012
60
Shortcoming Inability to create large trees from smaller trees using CREATEi operator.
9/29/2012
61
9/29/2012
62
Definition Of Cell Space Var cellspace :array[1..maxnodes] of record label:labeltype; leftmost_child :integer; right_sibling: integer; end;
9/29/2012
63
9/29/2012
64
9/29/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.
9/29/2012 CS 09 303 Data Structures - Module 3 66
9/29/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.
9/29/2012
69
9/29/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.
9/29/2012
71
9/29/2012
72
9/29/2012
73
9/29/2012
74
9/29/2012
75
9/29/2012
76
9/29/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}
9/29/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}
9/29/2012
81
9/29/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
9/29/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}
9/29/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
9/29/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}
9/29/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
9/29/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}
9/29/2012
88
9/29/2012
90
9/29/2012
91
9/29/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
9/29/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}
9/29/2012
94
9/29/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}
9/29/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.
9/29/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}
9/29/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}
9/29/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}
9/29/2012 CS 09 303 Data Structures - Module 3 100
(1)
9/29/2012
101
(2)
Replace the node with its right child (with value 20).
9/29/2012
102
(3)
Replace the node with its inorder successor and delete the node.
9/29/2012
103
BST Traversal
9/29/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.
9/29/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 -?????
9/29/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.
9/29/2012 CS 09 303 Data Structures - Module 3 109
9/29/2012
110
9/29/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.
9/29/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.
9/29/2012
113
9/29/2012
114
Inorder: DBAEC
9/29/2012
115
9/29/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
9/29/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.
9/29/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
9/29/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.
9/29/2012
130
9/29/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.
9/29/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).
9/29/2012
133
+1
0 0
+1
-1
0 AVL Tree 2
0 AVL Tree 0
-1
0 0
9/29/2012
134
9/29/2012
135
9/29/2012
136
9/29/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.
9/29/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
9/29/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.
9/29/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.
9/29/2012 CS 09 303 Data Structures - Module 3 141
B 0 4 0 BL 2 A 0 6
BR
AR
9/29/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.
9/29/2012 CS 09 303 Data Structures - Module 3 143
B 0 8 0 A 6 0 10
AL
BL
BR
9/29/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).
9/29/2012 CS 09 303 Data Structures - Module 3 145
9/29/2012
146
9/29/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).
9/29/2012 CS 09 303 Data Structures - Module 3 148
9/29/2012
149
9/29/2012
150
9/29/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.
9/29/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
9/29/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.
9/29/2012 CS 09 303 Data Structures - Module 3 155
9/29/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.
9/29/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.
9/29/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).
9/29/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.
9/29/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.
9/29/2012
161
9/29/2012
162
9/29/2012
163
9/29/2012
164
9/29/2012
165
9/29/2012
166
9/29/2012
167
9/29/2012
168
9/29/2012
169
9/29/2012
170
9/29/2012
171
Delete H
Since H is in a leaf and the leaf has more than minimum number of keys, we just remove it.
9/29/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 .
9/29/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.
9/29/2012
174
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/2012
188
9/29/2012
189
Insert Key Value 9 Search for the location where key value 9 is to be inserted, L4. Insert key 9 into L4.
9/29/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.
9/29/2012
191
Deletion in B+ Tree
Deletion sequence: 9, 8, 12.
9/29/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.
9/29/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.
9/29/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.
9/29/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.
9/29/2012
196
B+ Trees
Reference: http://www.mec.ac.in/resources/notes/notes/ds/bplus .htm
9/29/2012
197
GRAPH
Graph G = (V,E) is a collection of vertices and edges. (1) Set of vertices - V (2) Set of Edges - E
9/29/2012
199
GRAPH TERMINOLOGIES
Directed Graph (Digraph) :A graph
called tail.
9/29/2012
200
vertex.
ADJACENT VERTICES :Two vertices are adjacent if
the vertex.
9/29/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.}
9/29/2012
204
REPRESENTATION OF GRAPH
1) Sequential Representation
9/29/2012
205
9/29/2012
207
9/29/2012
208
9/29/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
9/29/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.
9/29/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 }
9/29/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 }
9/29/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
9/29/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.
9/29/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.
9/29/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);
9/29/2012
220
9/29/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
9/29/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
9/29/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
9/29/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)
9/29/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
9/29/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
9/29/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.
9/29/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.
9/29/2012 CS 09 303 Data Structures - Module 3 229
Greedy Algorithms
9/29/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.
9/29/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.
9/29/2012
232
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.
9/29/2012
233
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).
9/29/2012
234
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.
9/29/2012 CS 09 303 Data Structures - Module 3 235
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
9/29/2012 CS 09 303 Data Structures - Module 3 236
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 237
9/29/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 }
9/29/2012
238
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);
9/29/2012 CS 09 303 Data Structures - Module 3 239
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 }
9/29/2012 CS 09 303 Data Structures - Module 3 240
9/29/2012
241
9/29/2012
242
9/29/2012
243
9/29/2012
244
9/29/2012
245
9/29/2012
246
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.
9/29/2012
247
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};
9/29/2012
248
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;
9/29/2012
249
9/29/2012
250
9/29/2012
251
9/29/2012
252
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 }
9/29/2012
254
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 }
9/29/2012 CS 09 303 Data Structures - Module 3 255
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])
9/29/2012
256
9/29/2012
257
9/29/2012
258
v3 is closest to tree . . .
9/29/2012
259
9/29/2012
260
v6 is closest to tree . . .
9/29/2012
261
9/29/2012
262
9/29/2012
263
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.
9/29/2012 CS 09 303 Data Structures - Module 3 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
9/29/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.
9/29/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.
9/29/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}
9/29/2012 CS 09 303 Data Structures - Module 3 268
9/29/2012
269
9/29/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;
9/29/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 }
9/29/2012
272
9/29/2012
273
9/29/2012
274
Thank You
9/29/2012
275