1 Divide-and-conquer. 2 Divide and Conquer Divideالتقسيم/: the problem into a number of...

47
1 Divide-and-conquer

Transcript of 1 Divide-and-conquer. 2 Divide and Conquer Divideالتقسيم/: the problem into a number of...

1

Divide-and-conquer

2

Divide and Conquer

Divideالتقسيم/:

the problem into a number of subproblems that are themselves smaller instances of the same type of problem.

تقس�يم المس�ألة إلى مس�ائل فرعي�ة، فتش�كل عن�دنا اج�زاء اص�غر لنفس المش�كلة التي نريد حلها.

Conquerحل المسأله/:

Recursively solving these subproblems. If the subproblems are small enough, solve them straightforward.

حل ك�ل مس�ألة فرعي�ة بش�كل منف�رد وبأس�تخدام التك�رار، ويتم التقس�يم إلى اصغر حد ممكن تقسيمة ويمكن حل المسأله بعد.

Combineالتجميع/:

the solutions to the subproblems into

the solution of original problem.

تجميع المسأله الفرعية إلى مسأله كاملة.

3

Most common usage

Break up problem of size n into two equal parts of size n/2.

.n n/2بشكل عام يتم تقسيم المسأله إلى جزئين مثل اذا كان السايز Solve two parts recursively

حل كل جزء منفصل.Combine two solutions into overall solution in linear time.

(.nعملية تجميع الجزئين وحل المسأله تأخذ وقت خطي )

4

Which are Which are more difficult?more difficult?

الحل االصعب هو؟الحل االصعب هو؟

Divided Conquer Combine

5

Sortالترتيب /

Obviously applicationتستخدم في التطبيقات / :1. Sort a list of names. ترتيب مجموعة من االسماء/2. Organize an MP3 library./تنظيم مكتبة mp3 3. Display Google PageRank results./ع�رض المواق�ع االك�ثر مش�اهده في

google4. List RSS news items in reverse chronological order./ قائم�ة اخب�ار

.(شريط االخبار) RSSال�

1. Problems become easy once items are in sorted order/ المش�اكل تص�بح:سهله عند الترتيب1. Find the median./ إيجاد الوسيط2. Find the closest pair./ ايجاد المجموعات المغلقة3. Binary search in a database./ �البحث الثنائي في ال DB4. Identify statistical outliers. / التطبيقات االحصائية5. Find duplicates in a mailing list. / ايجاد العناصر المتكررة

6

Non-obvious applications / تطبيقات ثانوية:1. Data compression. / ضغط البيانات2. Computer graphics. / رسومات الكمبيوتر3. Computational biology. / تطبيقات وراثية4. Supply chain management. / االدارة5. Book recommendations on Amazon. / الكتب في موقع أمازون6. Load balancing on a parallel computer. / تحمي�ل التطبيق�ات في

النظم المتوازنة

Searching and Sorting Arrays

7

Introduction to Search Algorithms

A search algorithm is a method of locating a specific item of information in a larger collection of data.

كبير حجمها بيانات داخل معين عنصر على للحصول .طريقة

8

The Sequential Search

This is a very simple algorithm.

.) التطبيق ) حيث من الخوارزميات انواع ابسط هي

It uses a loop to sequentially step through an array, starting with the first element.

. عنصر اول من تبدء Y دائما معينه، مصفوفه داخل تكرار تستخدم

It compares each element with the value being searched for and stops when that value is found or the end of the array is reached.

العنصر نجد عندما ونتوقف مستمر وبشكل عنصر اول من المقارنه في نبدأالمطلوب.

9

#include <iostream.h>

// Function prototypeint SeqSearch (int [], int, int);

const int arrSize = 5;

void main(void){

int tests[arrSize] = {87, 75, 98, 100, 82};int results;results = SeqSearch(tests, arrSize, 100);if (results == -1)

cout << "You did not earn 100 points on any test\n";else{

cout << "You earned 100 points on test ";cout << (results + 1) << endl;

}} 10

int SeqSearch(int list[], int numElems, int value){ int index = 0; // Used as a subscript to search array int position = -1; // To record position of search value bool found = false; // Flag to indicate if the value was found while (index < numElems && !found) {

if (list[count] == value){

found = true; position = index; } index++;

}return position;

}

11

Efficiency of the Sequential Search

The advantage is its simplicity / مميزاتها:

1. It is easy to understand / الفهم سهلة2. Easy to implement / التنفيذ سهلة3. Does not require the array to be in order / تكون ان تحتاج ال

مرتبه المصفوفه

The disadvantage is its inefficiency

If there are 20,000 items in the array and what you are looking for is in the 19,999th element, you need to search through the entire list.

12

Sequential Search Analysis

Sequential search algorithm performance / الخوارزمية :أدارةExamine worst case and average case / االسوء الحالة حسابوالمتوسطةCount number of key comparisons / المقارنه مرات عدد نحصي

Unsuccessful search / ناجح الغير :البحثSearch item not in list / القائمة في موجود غير كان اذاMake n comparisons / الى المقارنه n نحتاج عملية في المرات من

Conducting algorithm performance analysisBest case: make one key comparison / : العنصر نجد ان حالة افضل

موقع اول فيWorst case: algorithm makes n comparisons / : يكون ان حالة أسوء

موقع اخر في أو موجود غير العنصر

13

Sequential Search Analysis (cont’d.)

Determining the average number of comparisons / عدد متوسط تحديدالمقارنه :مرات

• Consider all possible cases / المحتملة االحتماالت جميع ايجاد• Find number of comparisons for each case / مرات عدد كم ايجاد

المقارنه• Add number of comparisons, divide by number of cases / تقسيم

ندرسها التي للحالة المقارنه مرات عدد على المقارنه مرات عدد

14

Sequential Search Analysis (cont’d.)

Determining the average number of comparisons (cont’d.)

15

Binary Search

The binary search is much more efficient than the linear search.ال� ال� DBكفاءة من وافضل .linearاكبر

It requires the list to be in order.. مرتبه قائمة إلى بحاجة

The algorithm starts searching with the middle element / تبدأ الخوارزميةالمتوسط من :البحث

If the item is less than the middle element, it starts over searching the first half of the list. If the item is greater than the middle element, the search starts over starting with the middle element in the second half of the list.It then continues halving the list until the item is found.

فقط االول النصف في العنصر سيبدأ المتوسط، العنصر من اقل العنصر كان إذا. القائمة من

في البحث ويتم االول الجزء إلغاء سيتم الوسيط، العنصر من اكبر كان اذا. الثاني النصف

. العنصر يجد حتى البحث يستمر

16

Binary Search

Performed only on ordered listsUses divide-and-conquer technique

17

FIGURE 1 List of length 12

FIGURE 2 Search list, list[0]...list[11]

FIGURE 3 Search list, list[6]...list[11]

// This program demonstrates the binarySearch function, which// performs a binary search on an integer array.#include <iostream.h>

// Function prototypeint binarySearch(int [], int, int);

const int arrSize = 20;

void main(void){int tests[arrSize] = {101, 142, 147, 189, 199, 207, 222, 234, 289, 296, 310, 319, 388, 394, 417, 429, 447, 521, 536, 600};int results, empID;

cout << "Enter the Employee ID you wish to search for: ";cin >> empID;results = binarySearch(tests, arrSize, empID);if (results == -1)

cout << "That number does not exist in the array.\n";else{

cout << "That ID is found at element " << results;cout << " in the array\n";

}}

18

int binarySearch(int array[], int numelems, int value){

int first = 0, last = numelems - 1, middle, position = -1; bool found = false;

while (!found && first <= last){ middle = (first + last) / 2; // Calculate mid point

if (array[middle] == value) // If value is found at mid { found = true;

position = middle; }

else if (array[middle] > value) // If value is in lower half last = middle - 1;

else first = middle + 1; // If value is in upper half

}return position;

}

19

Binary Search

20

Values of first, last, and mid and the number of comparisons for search item 89

Efficiency of the Binary Search

Much more efficient than the sequential search.

21

Number of comparisons for a list of length n

Introduction to Sorting Algorithms

Sorting algorithms are used to arrange random data into some order.

Y تنازليا أو Y تصاعديا مرتبه قائمة في عشوائية بيانات ترتيب

22

The Bubble SortAn easy way to arrange data in ascending or descending order.Pseudocode:Do Set count variable to 0 For count is set to each subscript in Array from 0 to the next-to-last

subscript If array[count] is greater than array[count+1]

swap them set swap flag to true end ifEnd forWhile any elements have been swapped.

23

Example – First Pass

Array numlist contains:

17 23 5 11

compare values17 and 23 – in correctorder, so no exchange

compare values 23 and5 – not in correct order, so exchange them

compare values 23 and11 – not in correct order,so exchange them

Example – Second Pass

After first pass, array numlist contains:

17 5 11 23

compare values 17 and 5 – not in correct order,so exchange them

compare values 17 and11 – not in correct order, so exchange them

compare values 17 and23 – in correct order,so no exchange

Example – Third Pass

After second pass, array numlist contains:

5 11 17 23

compare values 5 and 11 – in correct order,so no exchange

compare values 11 and17 – in correct order, so no exchange

compare values 17 and23 – in correct order,so no exchange

No exchanges, so array is in order

// This program uses the bubble sort algorithm to sort an // array in ascending order.#include <iostream.h>

// Function prototypesvoid bubbleSort(int [], int);void showArray(int [], int);

void main(void){

int values[6] = {7, 2, 3, 8, 9, 1};cout << "The unsorted values are:\n";showArray(values, 6);bubbleSort(values, 6);cout << "The sorted values are:\n";showArray(values, 6);

}

27

// Definition of function sortArray. This function performs an ascending // order bubble sort on Array. elems is the number of elements in the array.

void bubbleSort(int array[], int elems){

int swap, temp;do{

swap = 0;for (int count = 0; count < (elems - 1); count++){

if (array[count] > array[count + 1]){

temp = array[count];array[count] = array[count + 1];array[count + 1] = temp;swap = 1;

}}

} while (swap != 0);}

28

// Definition of function showArray.// This function displays the contents of array. elems is the// number of elements.

void showArray(int array[], int elems){

for (int count = 0; count < elems; count++)cout << array[count] << " ";

cout << endl;

}

29

The Selection Sort

The bubble sort is inefficient for large arrays because items only move by one element at a time.

bubble sort . كبيرة المصفوفه حجم كان اذا عالية غير كفاءتها تكون Y عادتا

The selection sort moves items immediately to their final position in the array so it makes fewer exchanges.

selection sort العدد حيث مباشرة، المواقع بين تكون ال التبديل عملية. نهايتها أو المصفوفه بداية في مباشرة يوضع إما الصغير

30

Selection Sort Pseudocode:For Start is set to each subscript in Array from 0 through the next-to-last

subscript

Set Index variable to Start

Set minIndex variable to Start

Set minValue variable to array[Start]

For Index is set to each subscript in Array from Start+1 through the next-to-last subscript

If array[Index] is less than minValue

Set minValue to array[Index]

Set minIndex to Index

End if

Increment Index

End For

Set array[minIndex] to array[Start]

Set array[Start] to minValue

End For31

32

FIGURE 1 List of 8 elements

FIGURE 2 Elements of list during the first iteration

FIGURE 3 Elements of list during the second iteration

void selectionSort(int array[], int elems){

int startScan, minIndex, minValue;for (startScan = 0; startScan < (elems - 1); startScan++){ minIndex = startScan;

minValue = array[startScan];for(int index = startScan + 1; index < elems; index++){ if (array[index] < minValue)

{minValue = array[index];minIndex = index;

}}array[minIndex] = array[startScan];array[startScan] = minValue;

}}

33

For selection sort in general

The number of comparisons when the array contains N elements is

Sum = (N-1) + (N-2) + . . . + 2 + 1

34

1( 1)

21

NN N

i

Sum i

(arithmetic series)

O(N2)

The Insertion Sort

Attempts to improve high selection sort key comparisonsSorts list by moving each element to its proper placeGiven list of length eight

35

The Insertion Sort (cont’d.)

Elements list[0], list[1], list[2], list[3] in order

Consider element list[4]

First element of unsorted list

36

list elements while moving list[4] to its proper place

Analysis: Insertion Sort

37

Average-case behavior of the selection sort and insertion sort for a list of length n

Sorting Algorithms and Average Case Number of Comparisons

Simple Sorts / ُم�بسط ترتيبSelection Sort

Bubble Sort

Insertion Sort

More Complex SortsQuick Sort

Merge Sort

Heap Sort

O(N2)

O(N*log N)

38

The QuickSort Algorithm

Once a pivot value has been selected, the algorithm exchanges the other values in the list until all the elements in sublist 1 are less than the pivot, and all the elements in sublist 2 are greater than the pivot.

Once this is done, the algorithm repeats the procedure on sublist 1, and then on sublist 2. The recursion stops when there is only one element in a sublist. At that point the original list is completely sorted.

The QuickSort Algorithm

The algorithm is coded primarily in two functions: QuickSort and Partition. QuickSort is a recursive function. Its pseudocode is shown below.

QuickSort:If Starting Index < Ending Index Partition the List around a Pivot. QuickSort Sublist 1. QuickSort Sublist 2.end If.

The QuickSort Algorithm

The C++ Code:void quickSort(int set[], int start, int end){

int pivotPoint; 

if (start < end){

// Get the pivot point.pivotPoint = partition(set, start, end);// Sort the first sub list.quickSort(set, start, pivotPoint - 1);// Sort the second sub list.quickSort(set, pivotPoint + 1, end);

}}

The QuickSort Algorithm

The partition Function:int partition(int set[], int start, int end){

int pivotValue, pivotIndex, mid;

 mid = (start + end) / 2;exchange(set[start], set[mid]);pivotIndex = start;pivotValue = set[start];for (int scan = start + 1; scan <= end; scan++){

if (set[scan] < pivotValue){

pivotIndex++;exchange(set[pivotIndex], set[scan]);

}}exchange(set[start], set[pivotIndex]);return pivotIndex;

}

The QuickSort Algorithm

The exchange Function:

void exchange(int &value1, int &value2){

int temp = value1;value1 = value2;value2 = temp;

}

// This program demonstrates the QuickSort Algorithm#include <iostream.h>

// Function prototypesvoid quickSort(int [], int, int);int partition(int [], int, int);void exchange(int &, int &);

void main(void){

int array[10] = {7, 3, 9, 2, 0, 1, 8, 4, 6, 5};int x; // Counter

for (x = 0; x < 10; x++)cout << array[x] << " ";

cout << endl;quickSort(array, 0, 9);for (x = 0; x < 10; x++)

cout << array[x] << " ";cout << endl;

}

Quick Sort of N elements: How many comparisons?

45

N For first call, when each of N elements is compared to the split value

2 * N/2 For the next pair of calls, when N/2 elements in each “half” of the originalarray are compared to their own split values.

4 * N/4 For the four calls when N/4 elements in each“quarter” of original array are compared to their own split values.

.

.

. HOW MANY SPLITS CAN OCCUR?

Quick Sort of N elements:How many splits can occur?

46

It depends on the order of the original array elements!

If each split divides the subarray approximately in half, there will be only log2N splits, and QuickSort is O(N*log2N).

But, if the original array was sorted to begin with, the recursive calls will split up the array into parts of unequal length, with one part empty, and theother part containing all the rest of the array except for split value itself. In this case, there can be as many as N-1 splits, and QuickSort is O(N2).

Analysis: Quicksort

47

Analysis of quicksort for a list of length n