Rank:
The rank of an element is its position in the linear order of the set.
Select:
Select is the operation of retrieving an element of a given rank from a
linearly ordered set.
Median:
Median is a special case of select. It is the operation of retrieving an
item with rank [n/2] in a set of ‘n’
ordered elements.
The augmented red-black tree contains two parts: the value and the size of the subtree containing the node as the root.
Operations:
Retrieving an element with a given rank. OS-SELECT(x,i)
Determining the rank of an element. OS-RANK(x,
y)
Here is the recursive version of the OS-Rank Algorithm.
Given the value of a node OS-Rank (x, y) gives the rank of that element in the tree.
OS-RANK(x,y)
1 if x = y
2 then return size[left[y]] + 1
3 else if ( key[x] < key[y] )
4 then return OS-Rank(x,left[y])
5 else return OS-Rank ( x,right[y]) + size[left[y]]
+ 1
o What is the key value in an interval tree?
Interval trees support the following three operations.
Interval-Insert: Interval-Insert (T,x) adds the element x, whose int field is assumed to contain an interval, to the interval tree T.
Interval-Delete: Interval-Delete(T,x) removes the element x from the interval tree T.
Interval-Search(T,i): Interval-Search(T,i) returns a pointer to an element x in the interval tree T such that int[x] overlaps interval i, or NIL if no such element is in the set. Note that this operation cannot be efficiently implemented without the augmented information.
Refer to p 292 of [CLR] for algorithms for the above operations.
Dynamic Programming is an expensive method. It uses more space but it is very powerful and solves many fairly complicated problems.
Dynamic Programming is typically applied to optimization problems. In such problems there can be many possible solutions and the objective is to find a solution with the optimal value. There may be more than one optimal solution to problem.
Description: A subsequence of a given sequence is just the given sequence with some elements left out.
Ex: Given a sequence X = < x1, x2, … xm > and the sequence Z = < z1, z2, …,z3 > is a sub sequence of X if there exists a strictly increasing sequence (i1,i2,…,ik) of indices of x such that for all j = 1,2,…,k there is a xij such that xij = zj.
Note: Subsequence should always go from left to right. And the elements in the common subsequence need not be adjacent.
Common Subsequence: Given two sequences X of length ‘m’ and Y of length ‘n’, the sequence Z is said to be common subsequence of X and Y if Z is a subsequence of both X and Y.
Ex: X = < A, B, R, A, C, A, D, A, B, R, A>
Y = < R, A, B, A, D, A, B >
The sequence <A, B, A, D> is a Common subsequence of both X and Y. But it is not the Longest Common Subsequence.
In the Longest Common Subsequence problem two sequences X and Y are given and the objective is to find the longest common subsequence of X and Y.
For the above example
Input: The two sequences X and Y.
Output: < A, B, A, D, A, B >
For Algorithms refer to pages 316 and 317 of [CLR].
The recursive version for the algorithm LCS-Length(x,y,m,n) is as follows. Where X and Y are the given sequences. And m and n are the lengths of the two sequences X and Y respectively.
C | A | B | R | A | C | A | D | A | B | R | A |
R |
0 |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
A |
\
1 \ |
1 |
1 |
2 |
2 |
2 |
2 |
2 |
2 |
2 |
2 |
B |
1 |
\
2 \ |
¬
2 |
2 |
2 |
2 |
2 |
2 |
2 |
2 |
2 |
A |
1 |
2 |
2 |
\
3 \ |
¬
3 |
¬
3 |
3 |
3 |
3 |
3 |
4 |
D |
1 |
2 |
2 |
3 |
3 |
3 |
\
4 \ |
4 |
4 |
4 |
4 |
A |
1 |
2 |
2 |
3 |
3 |
4 |
4 |
\
5 \ |
5 |
5 |
5 |
B |
1 |
2 |
2 |
3 |
3 |
4 |
4 |
5 |
\
6 \ |
¬
6 |
¬
6 |
To print the Longest Common Subsequence from the above table the following algorithm is used.
1 if i =0 or j =0
2 then return
3 if b[i,j] = "\"
4 then PRINT-LCS(b,X,i-1,j-1)
5 print xi
6 elseif b[i,j]
= " "
7 then PRINT-LCS(b,X,i-1,j)
8 else PRINT-LCS(b,X,i,j-1)
This algorithm simply follows
the directional arrows starting from the entry in the last row, last column.
ffd