algorithems
algorithems
Contents
1
CH1 : Analyzing the complexity of Algorithms
Algorithm: A finite sequence of instructions for solving a problem.
2. Machine Reason :
Time , Space.
Cost of algorithm :
** How do you compare the efficiency of two algorithms ( for one problem ) ?
1. Compare the execution time.
2. Compare the size of program or (algorithm)
Size of program or algorithm is dependent on :
- The number of lines.
- The number of instructions.
** It’s better \
to measure the complexity of algorithm, that means (count number of basic
operations).
2
2. Worst case complexity :
n >= 0 , I (instances of problem) define :
W(n) = the maximum value of T(I) ,
where T(I) the number of basic operations for instance I.
3. Average complexity :
A(n) = ∑ ( ) ( )
Where P(I) is the probability that the instance I will occur and
T(I) the number of basic operations for instance I.
Examples :
Suppose we have a one dim array length 10 containing different int keys
19 22 13 45 34 31 100 90 75 60
1 2 3 4 5 6 7 8 9 10
3
Example2 :
Analyze and find the worst case complexity of the following algorithm :
t=1;
while ( t <= n ) // n is the input size
{
for ( int i = 1 ; i < n ; ++i )
{
Add += i % t ;
for ( int j = n ; j > 1 ; --j )
P = P *3 ;
}
if ( X > 2)
S.O.P(Y - 1);
t = t + 1;
Basic Op Count
<= N
< n2
++ n2
+= n2
% n2
> n3
-- n3
* n3
> N
- <= n
+ N
4
Example3 :
Given following :
solved
……..
…….
5
3. suppose the input size : n > 25
In general :
n : P1 better than P2 , ( using the same computer )
Definitions :
Examples :
Ex1 :
Given two positive real functions W1(n) and W2(n) , where
6
1- W1(n) is O(W2(n)) ?
Solution :
Suppose k = 1 and n0 = 25 , g(n) = W1(n) and f(n) = W2(n)
g(n) ≤ k*f(n) ? n ≥ n0
W1(n) ≤ 1*W2(n) ? n ≥ 25
100n ≤ 1*4n2 ? n ≥ 25
25 ≤ n ? Yes n ≥ 25
W1(n) is O(W2(n))
2- W2(n) is O(W1(n)) ?
Solution :
Suppose k = 1 and n0 = 25 , g(n) = W2(n) and f(n) = W1(n)
g(n) ≤ k*f(n) ? n ≥ n0
W2(n) ≤ 1*W1(n) ? n ≥ 25
4n2 ≤ 100n ? n ≥ 25
n ≤ 25 ? NO n ≥ 25
W2(n) is NOT O(W1(n)) W2(n) is Ω (W1(n))
Ex2 :
W2(n) is Ω (W1(n)) ?
Solution :
Suppose k = 1 and n0 = 25 , g(n) = W2(n) and f(n) = W1(n)
g(n) ≥ k*f(n) ? n ≥ n0
W2(n) ≥ 1*W1(n) ? n ≥ 25
4n2 ≥ 100n ? n ≥ 25
n ≥ 25 ? YES n ≥ 25 W2(n) is Ω (W1(n))
7
Other Definitions ( using limit ) :
8
1. lim ( any constant function) / log2(n) = 0 ,
likes g(n) = 1/2 ,g(n) = 1000 , g(n) = 10200
2. lim ( log2 (n) / n ) =0
3. lim (n / (n*log2 (n)) ) = 0
4. lim ((n*log2 (n)) / n2) = 0
5. lim ( np / nq ) =0 … If ( p < q ) and p , q >= 3
p n
6. lim ( n / 2 ) =0 … positive integer indices p
Efficiency of algorithms :
1. O(1) [ constant functions ] is better than O(log2n)
2. O( log2 n) [log functions ] is better than O(n)
3. O( n) [ linear functions ] is better than O(n log2n)
4. O( n log2n) [ log linear functions ]is better than O(n2)
5. O( np) [ polynomial functions ] is better than O(nq) …
if ( p < q) and p,q >= 2
6. O( n ) [polynomial functions ] is better than O(2n) …
p
OR : \
O(1) < O( log2n) < O(n) < O( n log2n) < O(n2) < O(np) (p > 2) < O(2n)
.
Given following :
solved
9
Solution :
P1 : W1(n) = 100n
lim W1(n)/n = lim 100n/n = 100 ≠ ∞ W1(n) is O(n)
P2 : W2(n) = 4n 2
lim W2(n)/n2 = lim 4n2/n2 = 4 ≠ ∞ W2(n) is O(n2)
solved
Example :
Suppose we have an algorithm P with worst case complexity W(n) ,
Every basic operation costs times ( the Algorithm written as Program
runs on a machine )
T the used time to run the algorithm for the input n ,
T = W(n)*
when we solve the equation , we can know the maximum input size ,
which can be handled in T time .
Examples :
Ex1 :
Suppose τ
= 1 ms ,
W(n) = n2 ,
T = 1 hour
T = W(n)*
60*60*sec = n2 * 10-3*sec
11
n2 = 6* 105
n = 600 * √ 10 ≈ 1897 input size
Ex2 :
Given an algorithm with W(n) = 2n runs on two different machines so
that the time for execution a basic operation for the first machine equal to
and for the other one equal to /k , where k >= 2.
Calculate n1 , n2 maximum input size for two machines which can be
handled in T time (Same time interval)
Problem?
Solved
Algorithm
W(n) = 2n
Write a program
Runs on two different machines (Computers) using the same time (T)
calculate n1 ? claculate n2 ?
Solution :
Using the equation T = W(n)*
n2 = log2k + n1
n2 > n1
11
CH2 : Sorting Algorithms
Two types for sorting algorithms :
1. Internal sorting algorithms
2. External sorting algorithms
Declarations :
If E small enough to fit into internal memory (algorithm called internal sorting
algorithm).
Otherwise E too large sorting the elements of E in a file saved in external memory
like hard disk,… (algorithm called external sorting algorithm).
12
Internal sorting Algorithms
Declaration :
N = … ( the size of the array A to be sorted )
Index = 1… N;
Bubble sort :
The elements in the array will be sorted in (N-1) passes beginning with i = 2, in
the first pass comparing A[N] with A[N-1] , if ( A[N].key < A[N-1]) swapping
until we reach the comparing of A[i] with A[i-1].
Body of algorithm :
For i = 2 to N do
{
for j := N down to i do
if A[j].key < A[j-1] then
Swap( A[j-1], A[j]) ;
}
Example:
Sort the following array of integers using Bubble sort.
8 2 6 4
1 2 3 4
1. Round (Outer loop ) :
i=2 j = N to 2
j = 4 swap (A[j],A[j-1])
8 2 4 6
1 2 3 4
j = 3 nothing to do
8 2 4 6
1 2 3 4
j = 2 swap (A[j],A[j-1])
2 8 4 6
1 2 3 4
13
2. Round (Outer loop ) :
i = 3 j = 4 to 3
j = 4 nothing to do
2 8 4 6
1 2 3 4
j = 3 swap (A[j],A[j-1])
2 4 8 6
1 2 3 4
3. Round (Outer loop ) :
i = 4 j = 4 to 4
j = 4 swap (A[j],A[j-1])
2 4 6 8
1 2 3 4
Value of i = 2 , 3, 4 , … , N-1 , N
……….
No of comparisons for each round : N-1 , N-2 , N-3 … , 2 , 1
14
Insertion sort:
Idea :
We begin with for i = 2 to N ( N the number of elements )
Comparing i-th element with the preceding entries with index (i-1) , (i-2) ,…,2 , 1
in the array until either we reach a smaller element or reach the left hand end of the
array .
Body of algorithm :
Example :
Sort the following array of integers using insertion sort.
8 2 6 4
1 2 3 4
1. Round :
i=2
x = A[2] = 2
A[0] = 2
2 8 2 6 4
0 1 2 3 4
j = i-1 = 1
while : A[j] > x
8 > 2 YES
A[j+1] = A[j]
A[2] = A[1] = 8
A[1]=2.
2 2 8 6 4
0 1 2 3 4
j := j - 1 = 0 ,
A[0] comparing with x A[0] = x out of the loop.
15
2. Round :
i=3
x = A[3] = 6
A[0] = 6
6 2 8 6 4
0 1 2 3 4
j = i -1 = 2
while : A[j] > x
8 > 6 YES
A[j+1] = A[j]
A[3] = A[2] = 8 ,
A[2] = 6.
6 2 6 8 4
0 1 2 3 4
j = j -1 = 1;
j=1
A[1] > x
2 > 6 NO
out of the loop
3. Round :
i=4
x = A[4] = 4
A[0] := 4
4 2 6 8 4
0 1 2 3 4
j=i–1=3
while : A[j] > x
8 > 4 YES
A[j+1] = A[j]
A[4] = A[3] = 8
A[3] = 4
4 2 6 4 8
0 1 2 3 4
j=j–1=2
while : A[j] > x
6 > 4 YES
A[j+1] = A[j]
A[3] = A[2] = 6
A[2] = 4
4 2 4 6 8
0 1 2 3 4
16
j=j–1=1
while : A[j] > x
2 > 4 NO
Out of while (STOP)
2 4 6 8
1 2 3 4
Value of i = 2 , 3 , 4 , … , N-1 , N
17
Selection sort :
Idea :
For i = 1 to N-1 :
In the i_th round comparing the key in i_th position with the keys in the (i+1)_th,
(i+2)_th ,…, N_th positions, each time we find a key less than the key in i_th
position (swap).
Body of algorithm :
Example:
Sort the following array of integers using selection sort.
8 2 6 4
1 2 3 4
1. Round :
i=1
k=1
x = A[1] = 8
inner loop :
j = i + 1 = 2 A[2] < x 2 < 8 YES k=j=2
x = A[2] = 2
18
k=2
x = A[2] = 8
inner loop :
j = i + 1 = 3 A[3] < x 6 < 8 YES
k=j=3
x = A[3] = 6
inner loop :
j = i + 1 = 4 A[4] < x 8 < 6 NO
(Nothing to do )
19
Quick sort : (RECURSION)
Left hand subarray with elements Less Right hand subarray with elements
than PIVOT
greater equal PIVOT
Idea : Partition
Two stages :
1. Function find_pivot( i , j ) ; // i is the index of the first element in the
array and j is the last element in the array
2. Method : Partition ( i , j , p , k );
the Method returns the index of the first element of the right hand subarray
21
1. First Stage : How to Find Pivot
Comparing the elements A[i],..,A[j] until we find two elements with different keys
choosing the larger of these as pivot .
Problem :
- The array contains only one element ?
- The keys of the array are the same ?
We do nothing , because the array is sorted
21
public int partition ( int A[] , int i , int j , int p)
{ int left , right , tmp;
left = I;
right = j;
do
{ while ( A[left] < p)
Left = left+1;
while (A[right] >= p)
right = right-1;
if (left<right)
{ tmp = A[left];
A[left] = A[right];
A[right] = tmp;
}
} while(left > right);
return left;
}
22
class QuickSort
{
public int pivotIndex( int A[] , int i , int j )
{ int z , p , q ;
boolean found = false ;
p = i-1;
q=i;
do
{ p = p + 1;
q = q+1;
if(A[p] != A[q])
found = true;
if(A[p] < A[q])
z = q;
else
z = p;
} while ( p!=j-1) && ( !found));
if (!found) z = 0;
return z;
}
23
Example : right
left
n=2
p = A[n] = A[2] = car
k = partition(A,i,j,p) = partition ( A, 1 , 8 , car )
partition ( A, 1 , 8 , car ) :
1- left = 1 , right = 8
2- while ( A[left] < car )
beg < car yes left = left +1 =2
car < car no stop
3- while (A[right] >= car )
pie >= car yes right = right -1 = 7
sum >= car yes right = 7 - 1 = 6
bee >= car no stop
4- if (left < right )
2 < 6 yes swap ( A[left] , A[right]) = swap( car , bee )
left right
Again :
24
5- while ( left < right ) [ means : No Crossing ] yes goto step 2
Again :
6- k = left = 4
Recursion :
n=1 n=5
p = A[n] = A[1] = beg p = A[n] = A[5] = the
k = partition(A,i,j,p) = partition ( A, 1 , 3 , beg ) k = partition(A,i,j,p) = partition ( A, 4 , 8 , the )
……. …….
…… ….
25
Complexity of quick sort
1.Worst case Complexity (General):
The number of comparisons [ needed to partition an array of length N ] :
is either N ( if pivot is NOT one of the entries in the array)
or N-1 (if pivot is one of the entries in the array)
First Instance :
Apply quick sort using an array with following properties :
- Number of keys is equal to N
- Sorted keys.
- Different keys.
- The pivot is larger of the first two entries.
…………
………..
Input 2 entries
…………………………
Sorted N-1 N
Output
Number of comparisopns = 1
26
Second Instance :
Apply quick sort using an array with following properties :
m
- Number of keys is equal to N = 2 , where m >=1
- Unsorted keys.
- After each parition, the array will be divided into exactly equal parts.
- pivot is not one of the elements.
N = 2m ( Unsorted )
Input
Output
m-1
2 2m-1
No of Comparisons =20*2m
=2m
Input Input
2m-1 2m-1
Output Output
…………………………………. …………………….
…………………………………….. …………………….
…………………………………… ……………………..
1 1 1 1 …………………… 1 1 1 1
27
m
m m m m N=2
Worst case complexity = 2 +2 + ……. + 2 = m*2 = N*log2N
m = log2N
m times
w(N) = N*log2N is O(Nlog2N)
N = 1 A(1) = 0
N>1:
Number of Instances = N -1
P(I) = 1/(N-1) // The probability
B(N-1) - B(1) = 3/(N-1) - 1/(N-2) + 3/(N-2) - 1/(N-3) + ..+ 3/3 -1/2+3/2-1 .(9)
29
In (9) Replacing (N-1) N :
31
Heap sort:
A Heap Array H is a one dimensional array with length N. (refer to the definition in
Data Structure )
Example :
H 96 90 70 80 75 42 60 17 44 10 72 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
31
Example:
Sort the following array of integers using Heap sort.
5 10 27 60 59 62 14 73
1 2 3 4 5 6 7 8
73 60 62 10 59 27 14 5
1 2 3 4 5 6 7 8
5 60 62 10 59 27 14 73
1 2 3 4 5 6 7 8
62 60 27 10 59 5 14 73
1 2 3 4 5 6 7 8
14 60 27 10 59 5 62 73
1 2 3 4 5 6 7 8
60 59 27 10 14 5 62 73
1 2 3 4 5 6 7 8
5 59 27 10 14 60 62 73
1 2 3 4 5 6 7 8
…
…
…
Then the array is sorted :
5 10 14 27 59 60 62 73
1 2 3 4 5 6 7 8
32
Complexity of Heap Sort :
Merge sort :
Idea:
1. A , B two sorted arrays with length N , M
2. Define an array C of length N + M
3. Comparing each element of A with each element of B :
if A[i] ≤ B[j] then
{
C[k] = A[i];
i = i +1;
}
else
{
C[k] = B[j];
j = j +1;
}
k=k+1;
Where i = 1 to M , j = 1 to N and k = 1 to N+M
4. A empty and B not empty copy the remains of B to C
else B empty and A not empty copy remains of A to C
A B
1 2 .. M 1 2 ……. N
C
1 2 ……. ……. …… M+N
Examples :
A 11 13 15 B 12 14 16
1 2 3 1 2 3
C 11 12 13 14 15 16
1 2 3 4 5 5
A 10 14 16 20 21 B 14 14 25 26
1 2 3 4 5 1 2 3 4
C 10 14 14 14 16 20 21 25 26
1 2 3 4 5 6 7 8 9
33
// Algorithm
i = 1; j = 1 ; k = 1;
while ( (i ≤ M) and (j ≤ N) )
{ if ( A[i] ≤ B[j] )
{ C[k] = A[i];
i = i + 1;
}
else
{ C[k] := B[j];
j = j+1; }
k = k+1;
}
if ( i > M )
for l = j to N do
{
C[k] = B[l];
k = k+1; }
if ( j > N)
for l := i to M do
{
C[k] := A[l];
k := k+1; }
}
34
Example :
Sort the following array using merge sort
6 22 26 36 45 66 74 79
A 66 22 36 6 79 26 45 74
1 2 3 4 5 6 7 8
Low = 1 , high = 8
Mid = (low+high)/2 = (1+8)/2 = 4
SortByMerge(1,4) SortByMerg(5,8)
6 22 36 66 26 45 74 79
66 22 36 6 79 26 45 74
1 2 3 4 5 6 7 8
22 66 6 36 26 79 45 74
66 22 36 6 79 26 45 74
1 2 3 4 5 6 7 8
66 22 36 6 79 26 45 74
1 2 3 4 5 6 7 8
A B
1 2 .. L 1 2 ……. L
C
1 2 ……. ……. …… 2L
Unsorted N = 2k
Sorted
35
W(N) = W(1/2N) + W(1/2N) + cN , constant
W(N) = 21*W(1/21N) + 1cN
To calculate W(1/2N) :
Unsorted 1/2N
Sorted
W(N) = 2 [2W(1/4N) + 1/2cN] + cN
= 22 W(1/4N) + cN + cN
= 22 W(1/22N) + 2cN
36
Other internal sorting algorithm
** Sorting the keys of a queue with values between 0..99
First pass:
Test the key by MOD function, then enqueuing this key in Qu indexed by the
least significant digit of its key,where Qu is defined as one array length 10
containing 10 queues.
` While ( !empty(Q) )
{
dequeue (Q , x) ;
j = x % 10;
engueue (x ,Qu[j]) ;
}
Second pass :
Test the key by DIV function, then enqueuing this key in Qu indexed by the
most significant digit of its key .
While ( !empty(Q) )
{
dequeue (Q , x) ;
j = x / 10;
engueue (x ,Qu[j]) ;
}
37
Example :
50 / 63 / 03 / 09 / 77 / 67 / 20
Front Rear
50 63 3 9 77 67 20
First Pass :
Qu
0 50 20
1
2
3 63 3
4
5
6
7 77 67
8
9 9
Front Rear
50 20 63 3 77 67 9
Second Pass :
Qu
0 3 9
1
2 20
3
4
5 50
6 63 67
7 77
8
9
38
Concatenation the 10 queues Qu[0] , Qu[1] , … , Qu[9] to Q
Front Rear
3 9 20 50 63 67 77
Complexity :
39
External Sorting Algorithms
1) Blanaced Merge Sort algorithm :
Idea :
- Define 5 files :
F as master file containing the keys, and
F1 , F2 , F3 , F4 as help files
- Number of keys in F is equal to N
- Define one dim array ( called RUN ) length M , where M << N
- Define r = N/M , number of RUNS
- Use any internal Sort Algorithms ( heapSort….., with worst case in O(Nlog2N))
1- Distribution stage :
1- Open F read only , open F1 , F2 rewriting : RESET(F) ; REWRITE(F1 , F2)
2- Read from F , M keys in one dim array (RUN) in the internal memory
3- Sort the keys in this array ( using any internal algorithm, like heapSort, … )
4- Write the sorted keys in this array in F1 or F2 ( alternately )
5- Goto step (2) until F eof.
2- Merge Stage :
1- Open F1,F2 read only , rewriting F3 , F4 : RESET (F1, F2) ; REWRITE(F3 , F4).
2- While not eof(F1) and not eof(F2) do
- Read 2 RUNS of F1 and F2
- Merge these and write the new RUN to F3 or F4 ( using F3 , F4 files alternately )
- Goto step (2)
- If one of the files (F1,F2) empty and the other still contains the last RUN
Read this RUN from the file (F1 or F2) then write it to (F3 or F4)
3- Again : Using (F1,F2) and (F3,F4) alternately for reading and rewriting until
there is only on RUN in one file of (F1,F2,F3,F4)
One of these files contains the sorted keys .
41
Example :
N = 35
Define M = 4 : r = N/M = 9
MERGE STAGE :
First round :
RESET ( F1 , F2 ) , REWRITE ( F3 , F4 )
/
F3 : 3 5 8 9 11 17 20 21 5 10 13 15 16 19 21 22 4 7 15 /
/
F4 : 2 6 14 15 15 18 23 24 1 5 8 12 18 20 25 26
Second round :
RESET ( F3 , F4 ) , REWRITE( F1 , F2 )
F1 : 2 3 5 6 8 9 11 14 15 15 17 18 20 21 23 24 4 7 15 /
F2 : 1 5 5 8 10 12 13 15 16 18 19 20 21 22 25 26
Third round :
RESET ( F1 , F2 ) , REWRITE( F3 , F4)
F3 : 1 2 3 5 5 5 6 8 8 9 10 11 12 13 14 15 15 15 16 17 18 18 19 20 20
21 21 22 23 24 25 26
F4 : 4 7 15
Fourth round :
RESET ( F3 , F4 ) , REWRITE( F1 , F2 )
F1 : 1 2 3 4 5 5 5 6 7 8 8 9 10 11 12 13 14 15 15 15 15 16 17 18 18
19 20 20 21 21 22 23 24 25 26
F2 : empty
41
Complexity of Balanced Merge sort :
N number of keys in F to be sorted
M length of RUNS in distribution stage
N/M = r number of RUNS at the distribution stage
suppose r = 2k
1. Round :
Merge r/21 pairs of RUNS length 20M
2. Round :
Merge r/22 pairs of RUNS length 21M
3. Round :
Merge r/23 pairs of RUNS length 22M
………………
………………
k. Round :
Merge r/2k pairs of RUNS length 2k-1M
42
Complexity by Merge Stage :
Multiply with A
Cost of Merge Stage = A*N* log2 r
43
2) Polyphase Sorting algorithm :
Fib number :
Fib : N+ N+
Fib(n) = n , if n = 0 or n = 1
Fib(n) = Fib(n-1) + Fib(n-2) n>= 2
Fib(0) = 0 .
Fib(1) = 1 .
Fib(2) = F(1) + F(0) = 1 .
…………..
………….
F(n)= F(n-1) + F(n-2).
N 0 1 2 3 4 5 6 7 8 9 10 ….
FIB(n) 0 1 1 2 3 5 8 13 21 34 55 ….
PreCondition :
Suppose T a file contains r sorted RUNS (using any internal algorithm), where r any
fib number ( r = FIB(n) = FIB(n-1) + FIB(n-2) ) :
Semi Example :
Let T contains r = FIB(8) = 21 RUNS ( after sorting the record in T using any
internal sort algorithm )
Distribution Stage :
REWRITE (T1) , REWRITE(T2) , RESET(T)
T1 : FIB(7) = 13 RUNS
T2 : FIB(6) = 8 RUNS
Merge Stage :
1.Round :
44
2.Round
3.Round
4.Round
5.Round
6.Round
45
Example :
Given following File T containing following keys :
T : 20-8-5-17-21-9-3-11-18-15-23-22-14-6-15-24-10-21-8-15-18-13-16- 6-6-25-24
-11-5
Pre-Calculations :
N = 29
M = ? ( needs algorithm )
r = N/M = any Fib() number (depends on M )
How to find M :
M=2 r = N/M = 29/2 = 15 is not a Fib number
M=3 r = N/M = 29/3 = 10 is not a Fib number
M=4 r = N/M = 29/4 = 8 is a Fib number 8= Fib(6) = Fib(5) + Fib(4)
T : 5-8-17-20/3-9-11-21/15-18-22-23/6-14-15-24/8-10-15-21/6-13-16-18/6-11-24-
25/5
(Runwise sorted using any internal algorithm)
Distribution Stage :
RESET(T) , REWRITE(T1,T2)
T1 : 5-8-17-20/3-9-11-21/15-18-22-23/6-14-15-24/8-10-15-21/ Fib(5) = 5 RUNS
T2 : 6-13-16-18/6-11-24-25/5 Fib(4) = 3 RUNS
Merge Stage :
1.Round :
RESET(T1,T2); REWRITE(T3)
T1 : 6-14-15-24/8-10-15-21/
T2 : EMPTY
T3 : 5-6-8-13-16-17-18-20/3-6-9-11-11-21-24-25/5-15-18-22-23/
2.Round :
RESET(T1,T3), REWRITE(T2)
T1 : EMPTY
T2 :5-6-6-8-13-14-15-16-17-18-20-24/3-6-8-9-10-11-11-15-21-21-24-25/
T3 : 5-15-18-22-23/
46
3.Round :
RESET(T2,T3) , REWRITE(T1)
T1 :5-5-6-6-8-13-14-15-15-16-17-18-18-20-22-23-24/
T2 : 3-6-8-9-10-11-11-15-21-21-24-25/
T3 : EMPTY
4.Round :
RESET(T1,T2) , REWRITE(T3)
T1 : EMPTY
T2 : EMPTY
T3 :3-5-5-6-6-6-8-8-9-10-11-11-13-14-15-15-15-16-17-18-18-20-21-21-22-23-24-24-25
47
CH3 : Graph Algorithms ( Shortest Path Algorithms )
Shorted path algorithms:
** Type of graphs:
1- Undirected graph , { vi, vi+1 } E symmetric
2- Directed graph , (vi, vi+1 ) E not symmetric
3- Undirected weighted graph
4- Directed weighted graph
Example :
1 200 2
100
40
300 5 10
70
20
4 20 3
Weight :
W : E R ( or any other informations)
w(p) = w({ v0, v1}) + w({ v1, v2}) + ………..+ w({ vk-1, vk}) ( undirected)
w(p) = w(( v0, v1)) + w(( v1, v2)) + ………..+ w(( vk-1, vk)) ( directed )
shorted path p from a to b is a path such that for all ṕ from a to b : w(p) <= w(ṕ )
48
(1) Dijkstra's Algorithm : ( For single source problem )
4- for i = 1 to n-1 do
{
choose a vertex v not in S , for which d[v] is least
S = S U {v} ;
for each vertex x not in S (inner loop )
d[x] = min { d[x],d[v]+w((v,x))} // if ( d[v]+w((v,x))< d[x] )
d[x] = d[v]+w((v,x))
}
Example :
1 200 2
100
40
300 5 10
70
20
4 20 3
1- Initialized suppose a = 1 is the source vertex
S ={1}
2- d[2]= 200
d[3] = ∞
d[4] = 300
d[5] = 100 unchanged
d[5] = min { d[2] , d[3] , d[4] ,d[5] } = min {200 , ∞ , 300 , 100} v = 5
3- Join vertex 5 to S S = S:= S U {v} = { 1 , 5}
49
6- Again with step (4) : x = 3 , 4 all are not in S ( inner loop )
d[x] = min {d[x],d[2]+w(2,x))}
d[3] = min {170,140+10}=150 changed
d[4] = min { 300,140+∞ } = 300 unchanged
7- Again with outer loop : for all v not in S :
d[3] = min { d[3] , d[4] } = min { 150 , 300} v = 3
S = S U {3} S = {1 , 5 , 2 , 3}
51
Example :
A
250 C
500 20
200
100 D
175 200
B
20 E
Start vertex : A
1- Init S ={A};
d[B] = 100 unchanged
d[C] = 250
d[D] =500
d[E] =200
2- Init p with source A
p[B] = A
p[C] = A
p[D] = A
p[E] = A
Body of algorithm :
Outer loop : for all verices v not in S :
d[B] = min { d[B] , d[C] , d[D] , d[E] } = min {100 , 250 , 500 , 200 }= 100
v=B
S = S U {B} = {A,B}
51
Again Outer loop : for all vertices v not in S :
d[C] = min { d[C] , d[D] } = min { 250 , 275 }= 250 v = C
S =S U {C} = { A, B, E, C }
52
All pairs problem :
3- float D[nxn] ;
Body of Algorthim :
For k = 1 to n do
For i = 1 to n do
For j = 1 to n do
D [i , j] = min { D[i , j],D[i , k]+D[k , j]};
OR
For k = 1 to n do
For i = 1 to n do
For j = 1 to n do
if (D[i , k]+D[k , j] < D [i , j])
then D[i , j] = D[i , k]+D[k , j];
Example :
3
1 2
7
10
2
1
Initialization : 3
D0 = D
j
1 2 3
1 0 3 10
i 2 7 0 1
3 2 ∞ 0
53
Body of Algorithm :
Outer loop : k =1
* keep the 1. Row , 1. Column and the diagonal of D0 to D1 unchanged
* Use statement : D[i , j ] = min { D[i , j] , D[i , k] + D[k , j] } to calculate
Outer loop : k =2
* keep the 2. Row , 2. Column and the diagonal of D1 to D2 unchanged
Outer loop : k = 3
* keep the 3. Row , 3. Column and the diagonal of D2 to D3 unchanged
54
4- Body of Algorithm :
For k = 1 to n do
For i = 1 to n do
For j = 1 to n do
If ( D[i , k] + D[k , j] < D[i , j])
{
D[i , j] = d[i , k]+ d[k , j] ;
P[i , j] = k
}
55
5- P[i , j] = 0 there is a shortest path (i , j) direct between i and j
Other wise using a procedure path(i, j) to define all intermediate vertices
between i
and j
path (i , j )
{
x 0..n;
x = p[i , j] ;
if (x <> 0)
{
Path (i , x);
S.O.P (x);
Path (x , j);
}
}
Example :
40 0 5 10 P = 0 0 0 0
7 ∞ 0 4 0 0 0 0
20 10 7 0 0 0 0 0
0 90 100 70 0 0 0 0
D1 = 40 0 5 10 P = 0 0 0 0
7 97 0 4 0 1 0 0
20 10 7 0 0 0 0 0
56
k = 2 : find D[i,j] = min { D[i,j] , D[i,k] + D[k,j] } , if changed P[i,j] = k
0 90 95 70 0 0 2 0
D2 = 40 0 5 10 P = 0 0 0 0
7 97 0 4 0 1 0 0
20 10 7 0 0 0 0 0
0 90 95 70 0 0 2 0
D3 = 12 0 5 9 P = 3 0 0 3
7 97 0 4 0 1 0 0
14 10 7 0 3 0 0 0
0 80 77 70 0 4 4 0
D4 = 12 0 5 9 P = 3 0 0 3
7 14 0 4 0 4 0 0
14 10 7 0 3 0 0 0
57
All pairs The weights of the The shortest path
of vertices shortest path using using the matrix P
the matrix D
1,1 0 1-1
1,2 80 1-4-2
1,3 77 1-4-3
1,4 70 1-4
2,1 12 2-3-1
2,2 0 2-2
2,3 5 2-3
2,4 9 2-3-4
3,1 7 3-1
3,2 14 3-4-2
3,3 0 3-3
3,4 4 3-4
4,1 14 4-3-1
4,2 10 4-2
4,3 7 4-3
4,4 0 4-4
58
CH4 : Spanning Tree Algorithms
G = (V,E) , V={ v1… vn}
P : v1 , v2 , ……… , vk a path from v1 to vk
Defintions :
1- Simple path : if all intermediate vertices between v1 and vk are distinct.
2- Cycle Path : if source v1 = vk sink and there are at least 3 different
vertices.
3- Simple Cycle : if the path is simple + cycle .
3 4
Cycle : 1,2,4,1 or
4 , 3 , 1 , 2 , 4 or
1,4,2,3,4,1
Simple cycle : 1 , 2 , 4 , 1 or
4,3,1, 2 , 4
59
Making min spanning tree :
Idea :
Given a connected graph by a cycle remove one edge such that the graph still have
a connected graph repeating this procedure until no cycle exists.
Example :
A 1 B Spanning tree
0 with minimal
1 7 9 weights
2
D 6 C
→ 1 1 1
0 0 0
7 9 1 1 9 1
2 7
6 2 2
6
Sum = 26 Sum = 25
Sum = 31 Sum = 28
1 1
0 0
1 9 9 7 1
7 9
2 6 6 2
6
Sum = 27 Sum = 25 Sum = 23 Sum = 28
IDEA :
Colouring the edges :
2 sets of E one contains the blue edges the other contains red edges
Min spanning tree of G which includes all the blue edges none of the red edges.
61
Example : ( protruded edges )
1 2
3 4
Blue rule :
1- Choose a non empty subset X of V .
2- Among the uncoloured edges protruded from X choose one of minimum weight
and colour it blue
Red rule :
1- Choose a simple cycle K which includes no red edges
2- Among the uncoloured edges of K choose one of maximum weight and colour it
red
61
1- Boruvka's Algorithm :
G = ( V, E ) connected weighted graph all edges distinct
Idea :
Body of algorithm :
Example :
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
Initialize :
F = { ({A},) ,({B},) ,({C},) ,({D},) ,({E},) ,({F},) ,({G},) , ({H}, ) }
X = {A}
→ AC(25),AB(55),AD(45) prodruded form X
→ Min edge from {{A}, } is AC(25)
X = {B} .. …………………………… …… Min edge from {{B}, } is BE(5)
X = {C}……………………………………. Min edge from {{C}, } is CA(25)
X = {D}…………………………………..…Min edge from {{D}, } is DE(20)
X = {E}………………………………...….. Min edge from {{E}, } is EB(5)
X = {F}……….. …………………………... Min edge from {{F}, } is FG(10)
X = {G}…………………………………..... Min edge from {{G}, } is GF(10)
X = {H}………………………………….... .Min edge from {{H}, } is HD(30)
62
Eliminate doublicates :
Min edge from {{A}, } is AC(25)
Min edge from {{B}, } is BE(5)
Min edge from {{D}, } is DE(20)
Min edge from {{F}, } is FG(10)
Min edge from {{H}, } is HD(30)
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35 10
Eliminate doublicates :
Min edge from T1 is CE(40)
Min edge from T2 is EG(15)
63
We choose F1 subset of F as follows:
F1 = { T1 , T2}
Colouring CE , EG blue
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35 10
F = { (A , B , C , D , E , F , G , H) , AC , BE , CE , ED , EG , FG , DH }
Number of blue edges is equal to n – 1 = 7
STOP
2- Kruskal’s algorithm :
Given G = (V,E) , V ={v1, … , vn} , E = { e1, … ,em }
Initialization :
- F collection of blue tree initialized with ( n single vertex trees )
- Ordering the edges in increasing order of weights : w(e1) <= … <= w(em)
Body of algorithm :
i=1;
REPEAT
IF ( both ends of ei are in the same blue tree )
THEN colouring ei RED
ELSE
colouring ei BLUE
i=i+1;
UNTIL ( there are n-1 BLUE edges )
64
Example :
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
F
INITIALIZE :
- F = {({A},), ({B},), ({C},), ({D},), ({E},), ({F},), ({G},), ({H},) }
Body of algorithm :
i = 1;
e1 = BE(5) ;
One end in the blue tree ({B} , ) the other in the blue ({E}, ) {different blue
trees}
Colouring BE(5) BLUE
F = { ({A},) , ({B ,E}, BE) , ({C},) , ({D},) , ({F},) , ({G},) ,({H}, ) }
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
65
i = 2:
e2 = FG(10) ;
One end in the blue tree ({F} , ) the other in the blue ({G}, ){different blue
trees}
Colouring FG(10) BLUE
F = { ({A}, ) , ({B , E }, BE) , ({C}, ), ( {D},) , ({F, G}, FG) , ({H}, ) }
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
F
i = 3:
e3 = EG(15) ;
One end in the blue tree ({B ,E},BE) the other in the blue ({F , G}, FG) {different
blue trees}
Colouring EG(15) BLUE
F = { ({A},),({B ,E ,F,G}, BE,EG,FG) ,({C},),( {D},) ,({H},)}
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
F
66
i = 4:
e4 = DE(20) ;
One end in the blue tree ({B,E, F,G},BE , EG , FG) the other in the blue ({D}, )
{different blue trees} Colouring DE(20) BLUE
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
F
i = 5:
e5 = AC(25) ;
One end in the blue tree ({A} , ) the other in the blue ({C}, ){different blue
trees}
Colouring AC(25) BLUE
F = { ({A , C}, AC) , ({B , D , E , F , G }, BE , EG , FG , DE) , ({H}, ) }
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
F
67
i = 6:
e6 = DH(30) ;
One end in the blue tree ({B ,D,E ,F,G }, BE , EG , FG , DE) the other in the blue
({H}, ) {different blue trees}
Colouring DH(30) BLUE
F={
({A , C}, AC) ,({B , D , E , F , G ,H }, BE , EG , FG , DE , DH)
}
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
F
i = 7:
e7 = EF(35) ;
Both ends in the blue tree ({B , D , E , F , G , H }, BE , EG , FG , DE , DH)
{same blue trees}
Colouring EF(35) RED
F={
({A , C}, AC) ,({B , D , E , F , G ,H }, BE , EG , FG , DE , DH)
}
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
F
68
i = 8:
e8 = CE(40) ;
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
F
69
3- PRIM's algorithm :
- Initialization :
T = { One vertex blue tree }
- Body of algorithm :
For i =1 to n-1 do
Begin
1- Apply the BLUE rule to the set of edges protruded from T; // min edge blue
colouring
T := T U { the new blue edge };
2- Suppose e , e` two edges are protruded from T with v as common endpoint
not in T
and e , e` form a cycle K , a simple cycle without red edges
`
Apply RED rule to max { e , e };
END;
Example :
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
F
Suppose T = {(A) , }
1- AB(55) , AC(25) , AD(45) protruded from T
AC = min { AB(55) , AC(25) , AD(45) }
Colouring AC(25) BLUE
T = {( A,C) , AC}
71
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
71
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
72
Colouring EF(35) RED , where EF(35) = max{EF(35) , FG(10)}
T = {( A , B , C , E , F , G) , EB , AC , CE , FG , EG}
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
F
A
55
45
25
B 30
C D H
5
20
40
50
15
E G
35
10
F
73
T = {( A , B , C , D , E , F , G , H) , EB , AC , DH , ED , CE , FG , EG}
A
55
45
25
B 30
C D H
5
20
40
15 50
E G
35
10
STOP!!!
74
CH5 : Storing in Binary Search Tree
Review :
Example :
Level = 0
4
Level = 1 6
2
Level = 2 1 3 5
- Height : h = 2
- No of Nodes <= 22+1 – 1 = 7
- No of leaves <= 22 = 4
Data structure :
public class TreeNode
{
protected int info; //key
protected TreeNode left;
protected TreeNode right;
public TreeNode ()
{ }
75
Search: ( RECURSION )
void treeSearch (int x ,TreeNode T )
{
if ( T == null) stop not found;
else
if ( T.info == x) stop found;
else
if (T.info > x ) treeSearch (x ,T.left);
else
treeSearch (x , T.right);
}
( NON-RECURSION )
begin
repeat
if (T == null)
stop; done = true;
else
if (T.info == x)
stop; done = true;
else
done = false;
if ( x < T.info) T = T.left
else
T = T.right;
until done
end;
76
Complexity of binary search tree :
The max number of key comparisons is one more than the Height of the tree.
Example :
Cost of looking for an element in a binary search tree consists the elements
1,2,3,4,5,6,7
CASE 1
7
A - Looking for the element 0
costs 7 key comparisons
6
→ h = N-1 = 6 5
→ no of comparsions = h + 1 = 6+1 =7 4
→ no of comparsions = h + 1 = 6+1 =7
4
77
CASE 2
→ no of comparisons =
h+1=2+1=3 1 3 5 7
CASE 3
1
2 5
3 6
1 2
1
4
7
1 3
5
4
6
……………………………………………………………..
…………………………………………………………….
…………………………………………………………
…………………………………………………………
78
** The Height of a binary search tree : trunc (log2N) <= h <= N –1 ,
where N number of keys
Perfectly balanced tree is a binary search tree where number of nodes in the left and
the right
Subtrees differ by at most 1.
79
Example :
construct a balanced tree (perfectly ) of the following keys :
1,2,3,4,5,6,7
4
(1 , 2 ,3 ) (5 , 6 , 7)
6
2
1 3 5 7
81
Storing in AVL_trees :
Name : Adel'son Velskii Landis .
AVL tree is a binary search tree in which for every node the Heights of the left and
right subtrees differ by at most 1 .
Examples :
8 0 8 0
4 0 10 0 4 1 11 1
0 1
2 0 6 0 9 0 11 0 2 -1 6 0 10 1 12 0
0 0 0 1 0
3 0 9 0
Not AVL 0
4 -2
2 0 6 -1
5 0 7 -1
9 0
81
INSERTION IN AVL TREES :
The AVL tree must have its properties after each insertion a new element.
Examples :
1- Insert the element 3 in the following AVL tree :
8 0
4 0 10 0
2 0 6 0 9 0 11 0
0
8 1
12111
4 1 1 10 0 After insertion 3 still AVL tree
1
2 -1 6 0 9 0 11 0
0 0
3 0
8 1
1
4 0 10 0
0
2 0 6 0
0 0
8 2
2 After insertion 3 not AVL tree
4 -1 10 0
0
2 0 6 1
0 1
5 0
82
** How will be reconstructed the AVL tree after insertion ?
1- L-Rotation :
with two versions :
a- LL-Rotation
b- LR-Rotation
2- R-Rotation :
with two versions :
a- RL-Rotation
b- RR-Rotation
L-Rotation :
Suppose we have AVL tree in which we would like to insert a new element with the
following two conditions :
- A is pivot node with balance = 1 , where A the last node with balance ≠ 0 in
the search path
- Insert in Left subtree of A ,where the root of the left subtree is B with
balance = 0
1
A
0
B
Tr(A)
Tl(B) Tr(B)
Where the Heights of Tl(B) , Tr(B) and Tr(A) are the same
83
Now there are two cases to consider :
2
A
1
B
Tr(A)
~ Tr(B)
T l(B)
0
B
0
A
~
T l(B)
Tr(B) Tr(A)
Example :
Insert 2 in the following tree :
8 1
A
5 1 10 0
B
3 0 6 0 9 0 11 0
1 0 4 0
84
8 2
A 2
5 2 10 0
B 0
3 1 6 0 9 0 11 0
0 0 0
1 -1 4 0
0
2 0
Restructuring
8 1
B 1
3 0 10 0
A 0
1 -1 5 0 9 0 11 0
0 0
2 0 4 0 6 0
0 0
1
A
0
B
Tr(A)
Tl(B) Tr(B)
85
a- Tr(B) = NULL Tl(B) = Tr(A) = NULL
1
A
0
A
A
B
Example :
Insert 7 in the following tree :
10 1
5 0 12 0
A
2 0 8 1 11 0 13 0
1 0 3 0 6 0 B
86
10 2
5 -1 12 0
A
2 0 8 2 11 0 13 0
1 0 3 0 6 -1 B
C
7 0
Restructuring
10 1
5 0 12 0
C
2 0 7 0 11 0 13 0
1 0 3 0 6 0 B 8 0 A
b- Right subtree of B not NULL with root C (left and right subtree of C possibly
NULL )
- Insert in the left subtree of C ( in Tl(C) )
1
A
0
B
0
Tr(A)
Tl(C) Tr(C)
Tl(B)
87
After insertion
2
A
-1
B
1
C
Tr(A)
Tr(C)
Tl(B)
~
T l(C)
RESTRUCTURING
1- The pointer , which points to A becomes a pointer to C
2- Right pointer of C becomes a pointer to A
3- Left pointer of C becomes a pointer to B
4- Left pointer of A becomes a pointer to Tr(C)
~
5- Right pointer of B becomes a pointer to T l(C)
A
B
Tr(C) Tr(A)
Tl(B) T~l(C
)
88
c-Right subtree of B not NULL with root C ( left and right subtree of C possibly
NULL )
* Insert in the right subtree of C ( in Tr(C) )
1
A
0
B
0
Tl(C) Tr(A)
Tl(B)
Tr(C)
After insertion
2
A
-1
B
-1
Tr(A)
Tl(B)
Tl(C)
T~r(C)
89
RESTRUCTURING
1- The pointer , which points to A becomes a pointer to C
2- Right pointer of C becomes a pointer to A
3- Left pointer of C becomes a pointer to B
~
4- Left pointer of A becomes a pointer to T r(C)
5- Right pointer of B becomes a pointer to Tl(C)
B A
Example :
Insert 12 or 14 in the following tree :
9 -1
A
5 0 16 1
B
2 0 8 0 11 0 17 0
C
B 10 0 13 0
9 -2
A
5 0 16 2
B
2 0 8 0 11 -1 17 0
C
B 10 0 13 -1
14 0
91
Restructuring
9 -1
C
5 0 13 0
B A
2 0 8 0 11 1 16 0
B 10 0 17 0
14 0
Suppose we have AVL tree in which we would like to insert a new element with the
following conditions :
- A is pivot node with balance = -1 , where A the last node with balance ≠ 0 in
the search path
- Insert in Right subtree of A ,where the root of the right subtree is B with
balance = 0
-1
A
0
B
Tl(A)
Tl(B)
Tr(B)
Where the Heights of Tl(B) , Tr(B) and Tl(A) are the same
-2
A
-1
B
Tl(A)
Tl(B)
~
T r(B)
91
Restructuring the above tree as follows :
1- The pointer , which points to A becomes a pointer to B
2- Left pointer of B becomes a pointer to A
3- The pointer , which points to B as (right pointer of A) becomes a pointer to
Tl(B)
Tr(A) Tl(B) ~
T r(B)
Example :
Insert 12 or 16 in the following tree :
A
8 -1
B
5 0 10 0
9 0 14 0
-1
A
0
B
Tl(A)
Tl(B)
Tr(B)
92
a- Tl(B) = NULL Tr(B) = Tl(A) = NULL
-1
A
0
A
B
A
Example :
Insert 7 in the following tree :
10 0
A
5 -1 12 0
B
8 0 11 0 13 0
93
b- Left subtree of B not NULL with root C (left and right subtree of C possibly
NULL )
- Insert in the right subtree of C ( in Tr(C) )
-1
A
0
B
After insertion
-2
A
1
B
Tl(A) Tl(C)
~ Tr(B)
T r(C)
94
RESTRUCTURING
1- The pointer , which points to A becomes a pointer to C
2- Left pointer of C becomes a pointer to A
3- Right pointer of C becomes a pointer to B
4- Right pointer of A becomes a pointer to Tl(C)
~
5- Left pointer of B becomes a pointer to T r(C)
RESTRUCTURING
C
A B
Tl(C)
~
T r(C) Tr(B)
Tl(A)
c-Left subtree of B not NULL with root C (left and right subtree of C possibly
NULL )
- Insert in the left subtree of C ( in Tl(C) )
-1
A
0
B
Tr(B)
Tl(A) Tl(C) Tr(C)
95
After insertion
-2
A
1
B
Tr(B)
Tl(A) Tr(C)
~
T l(C)
RESTRUCTURING
1- The pointer , which points to A becomes a pointer to C
2- Left pointer of C becomes a pointer to A
3- Right pointer of C becomes a pointer to B
~
4- Right pointer of A becomes a pointer to T l(C)
5- Left pointer of B becomes a pointer to Tr(C)
RESTRUCTURING
C
A B
Tr(B)
Tl(A) ~ Tr(C)
T l(C
)
96
Example :
Insert 17 or 19 in the following tree :
9 -1
A
5 0 16 - 1
B
2 0 8 0 11 0 20 0
C
22 0
18 0
97
Complexity of searching in AVL-tree :
Suppose we have a binary search tree contains N elements with height h with
THEN the worst case complexity of B.S.T. could be one of the following :
Example :
- Determine all B.S.T. with height h = 2 :
………..
………….
- Determine all AVL trees with least number of nodes and with height h = 2 :
Nh = N2 = N1 + N0 + 1 = 2 + 1 + 1 = 4 Nodes
98
Using FIB number and induction
h >= 0 : Nh = 1 + Nh-1 + Nh-2 = FIB(h+3) - 1
N > 1/5*Xh+3 - 2
………………..
………………
h < 1.4404log2(N+2) - 1.328 …………………….. 2
FROM 1 and 2 log2(N+1) - 1 <= h < 1.4404log2(N+2) - 1328
99
CH6: STORING IN MULTIWAY TREES
MULTIWAY SEARCH TREE:
M.W.S.T of order n , n >= 2 defined as follows :
class TreeNode
{
Object element[m] ;
TreeNode child[m+1] ; // child[n] OR child[order]
int number ; //number of keys in any node
……
……
}
child[1] = S1 child[i] = Si
child[m+1] = Sn
k1 k2 … ki-1 ki ki+1 …. …. km
S1 Si Sn
111
Searching in M.W.S.T. :
Two conditions must be satisfied to find an element :
1- The node, which hold the element
2- The position in the node : 1<= i <= order -1
*FUNCTION place(key , T )
: :
: :
if i = 1 : k <= T.Element[1].key place(k,T) = 1
if 1 < i < order : T.Element[i-1].key < k <= T.Element[i].key place(k,T) =
i
if i = n : T.Element[order] < k place(k,T) = order;
Examples :
Build up a M.W.S.T. of order 5 using the following inputs :
a- 25 , 17 , 31 , 42 , 21 , 19 , 26 , 33 , 47 , 44 , 45 , 43 , 8 , 9
25
17 25
17 25 31
17 25 31 42
19 21
…………………
…………………….
17 25 31 42
8 9 19 21 26 33 43 44 45 47
111
b- 20 , 19 , 18 , 17 , 16 , 15 , 14 , 13 , 12 , 11 ,10 , 9 , 8 ,7 , 6 , 5 , 4 , 3 ,
2 ,1
17 18 19 20
13 14 15 16
9 10 11 12
5 6 7 8
1 2 3 4
112
B-TREE OF ORDER n >= 2 :
Insertion in B-tree :
1- N NOT full with number of elements < n-1 insert and then sort the elements
in the Node
b- Divide the elements in N’ into two new leaves (left and right) as follows :
Left : X1 , X2 ,…….,Xq Right : Xq+2, ………,Xn ,
where q = (n-1) DIV 2
And the Xq+1 will be inserted (recursively) to the parent P of N
Example :
Insert with n = 5 :
41 , 61 , 36 , 53 , 55 , 52 , 49 , 43 , 67 , 45 , 69 , 71 , 63 , 65 , 57
in the following B-tree , where the order of the tree is n = 5 :
21 31 51
SOLUTION :
1- Insert 41
113
21 31 41 51
21 31 41 51 61
Left to P right
41
21 31 51 61
3- Insert 36 , 53 , 55
41
21 31 36 51 53 55 61
61
P 41
N
21 31 36 51 53 55 61
61
N is FULL and P parent of N is not full :
51 52 53 55 61
Left to P right
P 41 53
21 31 36 51 52 55 61
114
5- Insert 49 , 43 , 67
41 53
21 31 36 43 49 51 52 55 61 67
P 41 53
N
21 31 36 43 49 51 52 55 61 67
43 45 49 51 52
Left to P right
41 49 53
21 31 36 43 45 51 52 55 61 67
7- Insert 69 , 71
41 49 53
21 31 36 43 45 51 52 55 61 67 69
115
41 49 53 67
21 31 36 43 45 51 52 55 61 69 71
8- Insert 63 , 65
41 49 53 67
21 31 36 43 45 51 52 55 61 63 65 69 71
P 41 49 53 67
N
21 31 36 43 45 51 52 55 61 63 65 69 71
41 49 53 61 67 53
41 49 61 67
21 31 36 43 45 51 52 55 57 63 65 69 71
116
Deletion from B-Tree of order n >= 2 :
Otherwise : ( replace the element x [which will be deleted ] with the element y in the
mostleft Node in the right subtree of x )
Means :
Suppose P defined as pointer to the Node N , where we wish to delete from with
element P.element[i] , but N is NON-leaf-Node , so we search in P.child[i+1]
(right child of P.element[i] ) to find the least element in the leftmost leaf, replace
this element with P.element[i] and then restructuring the B-Tree after replacing
using the followingalgorithm ( as Example )
32
20 28 40 46
10 11 15 22 24 29 30 34 36 37 38 42 44 48 50
Solution :
Replacing x = 32 with y = 34 the B-Tree becomes
34
20 28 40 46
10 11 15 22 24 29 30 36 37 38 42 44 48 50
4 7
4
117
2- Delete from N , N is leaf
A- N has > q = (n-1) DIV 2 = (5-1)DIV 2 = 2 elements No problem
Example delete 34 from following B-Tree :
33 34 37 38
33 37 38
B.1- Left or Right or ( BOTH ) sibling(s) with elements > q = (n-1) DIV 2
Choose the one of the siblings which has more than q elements merge it
with the rest of N and the element which defined as parent of N and
then restructuring as follows :
Make temp array likes insertion algorithm contaning the chosen
Elements with X1, X2, ……, Xq, Xq+1, Xq+2,….
32
Parent
20 28 40 46
10 11 15 22 24 29 30 36 37 42 44 48 50 52
RESTRUCTURING
Explaining the case of delete 22 :
- Merge 10 , 11 , 15 , 20 , 24 temp. and sort in increasing order
10 11 15 20 24
118
10 11 15 20 24
Left leaf to P right leaf
Parent
Left leaf 32
15 28 40 48
10 11 20 24 29 30 36 37 44 46 50 52
Right leaf
Parent
32
3 6 38 42 51
33 35 40 41 44 50 52 60
119
RESTRUCTURING
40 41 42 50
N' as new leaf
Parent
32
3 6 38 51
33 35 40 41 42 50 52 60
N’
Example2 : ( Delete 41 from the following B-Tree)
B.2.2- Choose right sibling of N (Parent of N Root with elements >1 elements):
(Recursive ) restructuring same as in B.2.1
Parent
38 42
10 20 40 41 44 50
RESTRUCTURING
Merge 40 , 42 , 44 , 50 in new Node N’ in increasing order :
40 42 44 50
N' as new leaf
Parent
38
10 20 40 42 44 50
N’
111
Example3 : ( Delete 40 from the following B-Tree)
B.2.3- Choose left [there is only left sibling] of N
( Parent of N Root with elements exactly 1 element ) :
Parent
30
10 20 40 50
RESTRUCTURING
Merge 10 , 20 , 30 , 50 in new Node N’ as leaf in increasing order :
40 42 44 50
N’
111
41 49 53 61
N' new leaf
53 Parent
41 49 61 67
21 31 36 43 45 51 52 55 57 63 65 69 71
63 65 67 71
N' as new leaf
RESTRUCTURING
41 49 53 61
21 31 36 43 45 51 52 55 57 63 65 67 71
71
N’
112
B*-TREE OF ORDER n :
Algorithm's idea :
113
Example :
Suppose we have a B*-Tree of order n = 7
- Root-Node contains between 1 and 8 elements [ between 1 and 2(2n-
2)DIV3) ]
- NON-Root-Node contains between 4 and 6 [ between (2n-2)DIV3 and (n-
1) ]
10 15 20 25 30 35 40 45
1 2 3 4 5 6 7 8
dim array
10 15 20 25 30 35 40 45 50
1 2 3 4 5 6 7 8 9
RESTRUCTURING
- Construct left Node with the first 4 elements
- Construct a Node as Root with the next element
- Construct right Node with the last 4 elements
30
10 15 20 25 35 40 45 50
Insert 55 and 60
30
10 15 20 25 35 40 45 50 55 60
Insert 65 and 70 looking for sibling ( left ), not full, make left shifting as
following :
65
35
10 15 20 25 30 40 45 50 55 60 65
70 40
10 15 20 25 30 35 45 50 55 60 65 70
114
HOMEWORK AT HOME : INSERT first 29 and then 9
30
10 15 20 25 27 35 40 45 50
28
10 15 20 25 30 35 45 50 55 60 65 70
N is NON-Root-Node ( N and it’s sibling is full )
restructuring as follows :
- Separate the elements (after insertion ) in N and sibling (which chosen ) and
parent
- of N we have 2n elements
- Create 3 new leaves with following :
First leaf with (2n-2) Div 3 elements (as left ) , the next element to parent P
Second leaf with (2n-1) DIV 3 elements (as mid leaf),the next element to
parent P
Third leaf with 2n DIV 3 elements ( as right )
30 55
10 15 20 25 35 40 45 50 60 65 70 75
115
Example2 :
Insert the key with the value 33 into the following B*-Tree of order n = 5 :
10 20 30 40
2 4 12 14 22 24 26 28 32 34 36 38 42 44 46 48
116