You are on page 1of 47

DILLA UNIVERSITY

COLLEGE OF ENGINEERING AND


TECHNOLOGY
Department of Computer Science
Course Title:- Analysis of Algorithms
Course-Code:- CoSc3131
CHAPTER Three - Divide and conquer Algorithms
By
G/medhin G.
April, 2018
Dilla, Ethiopia
0
3.1. Overview
1
 In algorithm design, the idea of divide-and-conquer is to
take a problem on a large input, break the input into
smaller pieces, solve the problem on each of the small
pieces, and then combine the piecewise solutions into a
global solution.
 But once you have broken the problem into pieces, how
do you solve these pieces?
 The answer is to apply divide-and-conquer to them, thus
further breaking them down.
 The process ends when you are left with such tiny pieces
remaining (e.g. one or two items) that it is trivial to solve
them.
Contd
2

 These algorithms typically follow a divide-and-conquer approach: they


break the problem into several sub problems that are similar to the
original problem but smaller in size, solve the sub problems recursively,
and then combine these solutions to create a solution to the original
problem.
 The divide-and-conquer paradigm involves three steps at each level of the
recursion:
 Divide the problem into a number of sub problems.
 Conquer the sub problems by solving them recursively. If the sub problem sizes
are small enough, however, just solve the sub problems in a straightforward
manner.
 Combine the solutions to the sub problems into the solution for the original
problem.
Contd
3

 Divide-and-conquer algorithms are typically recursive, since the conquer


part involves invoking the same technique on a smaller sub problem.
 Analyzing the running times of recursive programs is rather tricky, but
we will show that there is an elegant mathematical concept, called a
recurrence, which is useful for analyzing the sort of recursive programs
that naturally arise in divide-and-conquer solutions.
3.2. Merge Sort
4

 The first example of a divide-and-conquer algorithm


which we will consider is perhaps the best known. This
is a simple and very efficient algorithm for sorting a list
of numbers, called Merge Sort.
 We are given an sequence of n numbers A, which we
will assume is stored in an array A[1:::n].
 The objective is to output a permutation of this sequence,
sorted in increasing order. This is normally done by
permuting the elements within the array A.
 How can we apply divide-and-conquer to sorting? Here
are the major elements of the Merge Sort algorithm.
Contd
5

 Divide: Split A down the middle into two subsequences,


each of size roughly n=2.
 Conquer: Sort each subsequence (by calling Merge Sort
recursively on each).
 Combine: Merge the two sorted subsequences into a
single sorted list.
 The dividing process ends when we have split the
subsequences down to a single item.
 A sequence of length one is trivially sorted.
Contd
6

 The key operation where all the work is done is in the


combine stage, which merges together two sorted lists
into a single sorted list.
 It turns out that the merging process is quite easy to
implement.
 It works top-down splitting up the list into smaller sub
lists. The “conquer and combine” phases are shown on
the right.
 They work bottom-up, merging sorted lists together
into larger sorted lists.
Contd
7

 Algorithm:
1. Divide the array in to two halves.
2. Recursively sort the first n/2 items.
3. Recursively sort the last n/2 items.
4. Merge sorted items (using an auxiliary array).
 MergeSort:
 Let’s design the algorithm top-down. We’ll assume that the
procedure that merges two sorted list is available to us.
 We’ll implement it later. Because the algorithm is called
recursively on sub lists, in addition to passing in the array
itself, we will pass in two indices, which indicate the first and
last indices of the sub array that we are to sort.
Contd
8

 The call MergeSort(A, p, r)will sort the sub array


A[p::r]and return the sorted result in the same sub array.
 Here is the overview.
 If r=p, then this means that there is only one element to
sort, and we may return immediately. Otherwise (if p<r)
there are at least two elements, and we will invoke the
divide-and conquer.
 We find the index q, midway between p and r, namely q=
(p+r)=2(rounded down to the nearest integer). Then we
split the array into sub arrays A[p::q] and A[q+1::r]. (We
need to be careful here.
Contd
9

 Why would it be wrong to do A[p::q−1] and A[q::r]?


Suppose r=p+1. Call Merge Sort recursively to sort each
sub array.
 Finally, we invoke a procedure (which we have yet to
write) which merges these two sub arrays into a single
sorted array.
Contd
10
Contd
11
Contd
12

 Merging: All that is left is to describe the procedure that merges two
sorted lists. Merge(A, p, q, r) assumes that the left sub array,
A[p::q], and the right sub array, A[q+1::r], have already been sorted.
 We merge these two sub arrays by copying the elements to a
temporary working array called B. For convenience, we will
assume that the array B has the same index range A, that is, B[p::r].
 (One nice thing about pseudo-code, is that we can make these
assumptions, and leave them up to the programmer to figure out
how to implement it.)
Contd
13

 We have two indices i and j, that point to the current elements


of each sub array. We move the smaller element into the next
position of B(indicated by index k) and then increment the
corresponding index (either i or j).
 When we run out of elements in one array, then we just copy
the rest of the other array into B.
 Finally, we copy the entire contents of B back into A.
 (The use of the temporary array is a bit unpleasant, but this is
impossible to overcome entirely.
 It is one of the shortcomings of Merge Sort, compared to some
of the other efficient sorting algorithms.)
 In case you are not aware of C notation, the operator i++ returns
the current value of i, and then increments this variable by one.
Contd
14

 This completes the description of the algorithm. Observe that


of the last two while-loops in the Merge procedure, only one
will be executed. (Do you see why?)
3.3. Heap Sort
15

 Heap sort operates by first converting the list in to a heap


tree.
 Heap tree is a binary tree in which each node has a value
greater than both its children (if any).
 It uses a process called "adjust to accomplish its task
(building a heap tree) whenever a value is larger than its
parent.
 The time complexity of heap sort is O(nlogn).
Contd
16
Algorithm:
1. Construct a binary tree
 The root node corresponds to Data[0].
 If we consider the index associated with a particular node to be i, then the left child
of this node corresponds to the element with index 2*i+1 and the right child
corresponds to the element with index 2*i+2. If any or both of these elements do
not exist in the array, then the corresponding child node does not exist either.
2. Construct the heap tree from initial binary tree using "adjust"
process.
3. Sort by swapping the root value with the lowest, right most value
and deleting the lowest, right most value and inserting the deleted
value in the array in it proper position.
Contd
17
 Before performing the heap sorting first we have to find the
maximum heap tree
BUILD-MAX-HEAP(A)
{
A.heap-size=A.lenghth
For(i=[A.length/2] downto 1)
MAX-HEAPIFY(A,i) // find the maximum heap value
}
 For example, you have the following random values

 There are (1 to [n/2]) number none leaf nodes of the tree,


and ([n/2] + 1 to n) number of leafs in the tree.
Contd
18
Contd
19

Example: Sort the following list using heap sort algorithm.


Contd
20

 Swap the root node with the lowest, right most node
and delete the lowest, right most value; insert the
deleted value in the array in its proper position;
adjust the heap tree; and repeat this process until the
tree is empty.
21
Contd
Contd
22
Contd
23
Contd
24
Contd
25
Contd
26
3.4. Quick Sort
27

 Quick sort is the fastest known algorithm. It uses divide


and conquer strategy and in the worst case its
complexity is O (n2). But its expected complexity is
O(nlogn).
Algorithm:
1. Choose a pivot value (mostly the first element is taken
as the pivot value)
2. Position the pivot element and partition the list so that:
 the left part has items less than or equal to the pivot value
 the right part has items greater than or equal to the pivot
value
3. Recursively sort the left part
4. Recursively sort the right part
Contd
28

 The following algorithm can be used to position a


pivot value and create partition.
Partitioning Algorithm
Sorting Algorithm
29
30
Contd
31
Contd
32

 Running Time for quick sort

Worst case: -T(n)= T(n-1) + O(n) = O(n2)


Best case: - T(n)= 2T(n/2) + O(n) = O(nlogn)
3.5. Analyzing divide-and-conquer algorithms
33

 When an algorithm contains a recursive call to itself,


its running time can often be described by a
recurrence equation or recurrence, which describes
the overall running time on a problem of size n in
terms of the running time on smaller inputs. We can
then use mathematical tools to solve the recurrence
and provide bounds on the performance of the
algorithm.
Contd
34

 A recurrence for the running time of a divide-and-


conquer algorithm is based on the three steps of the
basic paradigm. As before, we let T(n) be the running
time on a problem of size n.
 If the problem size is small enough, say n ≤ c for some
constant c, the straightforward solution takes
constant time, which we write as Θ(1).
 Suppose that our division of the problem yields a sub
problem, each of which is 1/b the size of the original.
Contd
35

 (For merge sort, both a and b are 2, but we shall see


many divide-and-conquer algorithms in which a ≠
b.) If we take D(n) time to divide the problem into
sub problems and C(n) time to combine the
solutions to the sub problems into the solution to
the original problem, we get the recurrence.
Analysis of merge sort
36

 Although the pseudocode for MERGE-SORT works correctly

when the number of elements is not even, our recurrence-based


analysis is simplified if we assume that the original problem size
is a power of 2. Each divide step then yields two subsequences of
size exactly n/2

 We reason as follows to set up the recurrence for T(n), the worst-

case running time of merge sort on n numbers. Merge sort on


just one element takes constant time. When we have n> 1
elements, we break down the running time as follows.
Contd
37

 Divide: The divide step just computes the middle of the sub

array, which takes constant time. Thus, D(n) = Θ(1).

 Conquer: We recursively solve two sub problems, each of size

n/2, which contributes 2T(n/2) to the running time.

 Combine: We have already noted that the MERGE procedure

on an n-element sub array takes time Θ(n), so C(n) = Θ(n).


kfgjkfhgjkfhgjkfhgjf
38

[5] S.W. Kim, and S.W. Seo."Cooperative unmanned autonomous


vehicle control for spatially secure group communications."
Selected Areas in Communications, IEEE Journal on 30, Vol. 5, pp.
870-882, 2012.

[6] A.Y. Javaid, W. Sun, V. K. Devabhaktuni, and M. Alam. “Cyber


security threat analysis and modeling of an unmanned aerial
vehicle system. “In Homeland Security (HST), 2012 IEEE
Conference on Technologies for, pp. 585-590.2012.
39

[7] Yongguang Zhang and Wenke Lee” Intrusion Detection in

Wireless Adhoc Networks” in Proceedings of the Sixth Annual

International Conference on Mobile Computing and Networking

(MobiCom’2000), August 6–11, 2000, Boston, Massachusetts.

[8] M. Liu, J. Lin, and Y. Yuan. “Research of UAV cooperative

reconnaissance with self-organization path planning. “In

International Conference on Computer, Networks and

Communication Engineering (ICCNCE 2013).Atlantis Press, 2013.


40

[9] O. K. Sahingoz. "Networking models in flying Ad-hoc networks

(FANETs): Concepts and challenges." Journal of Intelligent &

Robotic Systems, Vol. 74, Issue 1-2, pp. 513-527, 2014.

[10] S. Rosati, K. Kruzelecki, G. Heitz, D. Floreano, and B. Rimoldi.

"Dynamic Routing for Flying Ad Hoc Networks." arXiv preprint

arXiv:1406.4399, 2014.
41

[11] Y. Saleem, M. H. Rehmani, and S. Zeadally, “Integration of


Cognitive Radio Technology with unmanned aerial vehicles: Issues,
opportunities, and future research challenges.” Journal of Network
and Computer Applications, Vol. 50, pp. 15-31, 2015. 76

[12]U. Khan, S. Agrawal, and S. Silakari. "A Detailed Survey on


Misbehavior Node Detection Techniques in Vehicular Ad Hoc
Networks." In Information Systems Design and Intelligent
Applications, Springer India, pp. 11-19. 2015.
42

[13] Md. Hasan Tareque, Md. Shohrab Hossain, Mohammed


Atiquzzaman, “On the Routing in Flying Ad hoc Networks” in
Proceedings of the Federated Conference on Computer Science and
Information Systems pp. 1–9, 2015.
[14] C. Yegui, F. R. Yu, L. Jun, Z. Yifeng, and L. Lamont, "Medium
Access Control for Unmanned Aerial Vehicle (UAV) Ad-Hoc
Networks With Full-Duplex Radios and Multipacket Reception
Capability," Vehicular Technology, IEEE Transactions on, vol. 62,
pp. 390-394, 2013.
[15] W.Zafar, B.M.Khan, “A reliable, delay bounded and less
complex communication protocol For multicluster FANETs”,
Digital Communications and Networks(2016),
http://dx.doi.org/10.1016/j.dcan.2016.06.001i.
43

[16] Bekmezci, Senturk, Turker, “Security Issues in Flying Ad-Hoc


Networks (FANETs)”, Journal of Aeronautics And Space
Technologies July 2016 Volume 9 Number 2(13-21).
[17] Ilker Bekmezci, Ismail Sen, and Ercan Erkalkan, “Flying Ad
Hoc Networks (FANET) Test Bed Implementation”, 978-1-4799-
7697-3/15/$31.00 ©2015 IEEE.
[18] Emerson A. Marconato, Mariana Rodrigues, Rayner M. Pires,
Daniel F. Pigatto, Luiz C.Q.Filho, Alex S.R. Pintoy and Kalinka
R.L.J.C. Branco, “AVENS – A Novel Flying Ad Hoc Network
Simulator with Automatic Code Generation for Unmanned Aircraft
System”, (Conference Paper) Proceedings of the 50th Hawaii
International Conference on System Sciences, January 2017.
44

[19] Murtaza, Ghulam, Salil Kanhere, and Sanjay Jha. "Priority-


based coverage path planning for aerial wireless sensor networks."
Intelligent Sensors, Sensor Networks and Information Processing,
2013 IEEE Eighth International Conference on. IEEE, 2013.
[20] Shilpa K G, Mr.Prakash V Parande,”Efficient Data Routing
Analysis In FANETS To Achieve QOS”, International Journal of
Innovative Science, Engineering & Technology, Vol. 3 Issue 8,
August 2016.
[21] “Omnet++.” http://www.omnet.org , Accessed due to March
16, 2017.
Questions?
Thank
You !!!

You might also like