for students and teachers of computer science , a series of lecture is provided

© All Rights Reserved

1 views

for students and teachers of computer science , a series of lecture is provided

© All Rights Reserved

- class 12 ISC Sorting Method and Algorithm in blue j
- Combined Handouts CS702 (PDF format)
- Radix
- Sorting Algorithms in C
- 99 Java Source Codes
- Epicsystems Inc Sample Technical Placement Paper Level1
- Lab Manual Cl i Vibhs(1)
- Data Structures
- lec12-oct15-09
- CSEIT - 2015
- 05DivideAndConquerI-2x2
- Advanced Data Structures_aslam
- AS-Level Math sequences (C1)
- CSEIT
- Small 05 Div and Conquer
- Computer Science Notes – Problem Solving Techniquies
- CSEIT - 2015
- Service Program1
- Arrays
- Sorting.doc

You are on page 1of 58

- Example

- Binary Search

- Merge Sort

- Quick Sort

1

Divide and Conquer

There must be base case (to stop recursion).

original problem

2

Divide-and-Conquer

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

Solve two parts recursively.

Combine two solutions into overall solution in linear time.

Consequence.

Brute force: n2.

Divide-and-conquer: n log n.

3

Closest Pair of Points

with smallest Euclidean distance between them

(closest pair).

Graphics, computer vision, geographic information systems,

molecular modeling, air traffic control.

Special case of nearest neighbor, Euclidean MST, Voronoi.

Brute force. Check all pairs of points p and q with

(n2) comparisons.

1-D version. O(n log n) easy if points are on a line.

coordinate.

4

Closest Pair of Points: First Attempt

5

Closest Pair of Points: First Attempt

Obstacle. Impossible to ensure n/4 points in each piece.

6

Closest Pair of Points

Algorithm.

Divide: draw vertical line L so that roughly n points on each

side.

7

Closest-Pair Problem:

Divide and Conquer

every other point

Given n points, we must perform 1 + 2 + 3 + + n-2 + n-1

comparisons.

n 1

(n 1) n

k

k 1 2

Brute force O(n2)

The Divide and Conquer algorithm yields O(n log n)

Reminder: if n = 1,000,000 then

n2 = 1,000,000,000,000 whereas

n log n = 20,000,000

Closest-Pair Algorithm

Closest-Pair Algorithm

Closest-Pair Algorithm

Lets sort based on the X-axis

O(n log n) using quicksort or mergesort

4

9

6 13

2

14

11

3 7 10

5

1 8

12

Closest-Pair Algorithm

Step 2: Split the points, i.e.,

Draw a line at the mid-point between 7 and 8

4

9

6 13

2

14

11

3 7 10

5

1 8

12

Sub-Problem 1 Sub-Problem 2

Closest-Pair Algorithm

Advantage: Normally, wed have to compare

each of the 14 points with every other point.

(n-1)n/2 = 13*14/2 = 91 comparisons

4

9

6 13

2

14

11

3 7 10

5

1 8

12

Sub-Problem 1 Sub-Problem 2

Closest-Pair Algorithm

Advantage: Now, we have two sub-problems of

half the size. Thus, we have to do 6*7/2

comparisons twice, which is 42 comparisons

solution d = min(d1, d2)

4

9

d1 6 13

2 d2

14

11

3 7 10

5

1 8

12

Sub-Problem 1 Sub-Problem 2

Closest-Pair Algorithm

Advantage: With just one split we cut the

number of comparisons in half. Obviously, we

gain an even greater advantage if we split the

sub-problems.

d = min(d1, d2)

4

9

d1 6 13

2 d2

14

11

3 7 10

5

1 8

12

Sub-Problem 1 Sub-Problem 2

Closest-Pair Algorithm

Problem: However, what if the closest two

points are each from different sub-problems?

4

9

d1 6 13

2 d2

14

11

3 7 10

5

1 8

12

Sub-Problem 1 Sub-Problem 2

Closest-Pair Algorithm

Here is an example where we have to compare

points from sub-problem 1 to the points in sub-

problem 2.

d1 6 13

2 d2

9 14

7 11

3 10

5 8

1

12

Sub-Problem 1 Sub-Problem 2

Closest-Pair Algorithm

However, we only have to compare points

inside the following strip.

d = min(d1, d2)

4

9

d1 d d 13

2 6

d2

14

11

3 7 10

5

1 8

12

Sub-Problem 1 Sub-Problem 2

Closest-Pair Algorithm

Step 3: But, we can continue the advantage by

splitting the sub-problems.

4

9

6 13

2

14

11

3 7 10

5

1 8

12

Closest-Pair Algorithm

Step 3: In fact we can continue to split until

each sub-problem is trivial, i.e., takes one

comparison.

4

9

6 13

2

14

11

3 7 10

5

1 8

12

Closest-Pair Algorithm

Finally: The solution to each sub-problem is

combined until the final solution is obtained

4

9

6 13

2

14

11

3 7 10

5

1 8

12

Closest-Pair Algorithm

Finally: On the last step the strip will likely

be very small. Thus, combining the two largest

sub-problems wont require much work.

4

9

6 13

2

14

11

3 7 10

5

1 8

12

Closest-Pair Algorithm

In this example, it takes 22 comparisons to find the

closets-pair.

The brute force algorithm would have taken 91

comparisons.

But, the real advantage occurs when there are millions of

points.

4

9

6 13

2

14

11

3 7 10

5

1 8

12

Closest Pair Divide & Conquer

dimension d = 2

Partition two dimensional set S into subsets S1 and S2 by a

vertical line l at the median x coordinate of S.

Solve the problem recursively on S1 and S2.

Let {p1, p2} be the closest pair in S1 and {q1, q2} in S2.

Let 1 = distance(p1,p2) and 2 = distance(q1,q2)

Let = min(1, 2)

l

S1 S2

p1

1

p2

q1

q2 2

Closest Pair Divide & Conquer

dimension d = 2

In order to merge we have to

determine if exists a pair of points {p, P1 l P2

q} where p S1, q S2 and

distance(p, q) < .

If so, p and q must both be within of

l. p1

Let P1 and P2 be vertical regions of q1

1

the plane of width on either side of l. p2 S2

If {p, q} exists, p must be within P1 S1

2

and q within P2.

However, every point in S1 and S2 may q2

be a candidate, as long as each is

within of l, which implies: O(n/2)

O(n/2) = O(n2)

Can we do better ?

Closest Pair Divide & Conquer

dimension d = 2

For a point p in P1, which portion of P1 l P2

P2 should be checked?

We only need to check the points

that are within of p.

Thus we can limit the portion of P2.

The points to consider for a point

p must lie within 2 rectangle R. S1 S2

p

At most, how many points are

there in rectangle R?

Closest Pair Divide & Conquer

dimension d = 2

rectangle R?

Since no two points can be

be at most 6 points

Therefore, 6 O(n/2)O(n)

S2

Thus, the time complexity is

S1

p

O(n log n)

R

Closest Pair Divide & Conquer

dimension d = 2

Project p and all the points of S2 within P2 onto l.

Only the points within of p in the y projection

need to be considered (max of 6 points).

After sorting the points on y coordinate we can

find the points by scanning the sorted lists. Points

are sorted by y coordinates.

To prevent resorting in O(n log n) in each merge,

two previously sorted lists are merged in O(n).

Time Complexity: O(n log n)

Counting Inversions

You rank n songs.

Music site consults database to find people with similar tastes.

My rank: 1, 2, , n.

Your rank: a1, a2, , an.

Songs i and j inverted if i <= j, but ai > aj.

Songs

A B C D E

Inversions

Me 1 2 3 4 5

3-2, 4-2

You 1 3 4 2 5

29

Counting Inversions: Divide-and-Conquer

Divide-and-conquer.

1 5 4 8 10 2 6 9 12 11 3 7

30

Counting Inversions: Divide-and-Conquer

Divide-and-conquer.

Divide: separate list into two pieces.

Divide: O(1).

1 5 4 8 10 2 6 9 12 11 3 7

1 5 4 8 10 2 6 9 12 11 3 7

31

Counting Inversions: Divide-and-Conquer

Divide-and-conquer.

Divide: separate list into two pieces.

Conquer: recursively count inversions in each half.

Divide: O(1).

1 5 4 8 10 2 6 9 12 11 3 7

1 5 4 8 10 2 6 9 12 11 3 7 Conquer: 2T(n / 2)

5-4, 5-2, 4-2, 8-2, 10-2 6-3, 9-3, 9-7, 12-3, 12-7, 12-11, 11-3, 11-7

32

Counting Inversions: Divide-and-Conquer

Divide-and-conquer.

Divide: separate list into two pieces.

Conquer: recursively count inversions in each half.

Combine: count inversions where ai and aj are in different halves,

and return sum of three quantities.

Divide: O(1).

1 5 4 8 10 2 6 9 12 11 3 7

1 5 4 8 10 2 6 9 12 11 3 7 Conquer: 2T(n / 2)

9 blue-green inversions

Combine: ???

5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7

Total = 5 + 8 + 9 = 22.

33

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

18 7 14 10 3 19 17 25 16 2 23 11

34

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 6 j = 6

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

auxiliary array

current Inversions:

Total: 0

35

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 6 j = 5

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 auxiliary array

current Inversions: 6

Total: 6

36

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 6 j = 5

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 auxiliary array

current Inversions: 6

Total: 6

37

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 5 j = 5

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 auxiliary array

current Inversions: 6

Total: 6

38

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 5 j = 5

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 auxiliary array

current Inversions: 6

Total: 6

39

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 4 j = 5

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 auxiliary array

current Inversions: 6

Total: 6

40

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 4 j = 5

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 auxiliary array

current Inversions: 6

Total: 6

41

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 3 j = 5

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 auxiliary array

current Inversions: 6

Total: 6

42

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 3 j = 5

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 auxiliary array

current Inversions: 6

Total: 6

43

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 3 j = 4

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 11 auxiliary array

Inversions: 6 + 3

Total: 9

44

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 3 j = 4

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 11 auxiliary array

Inversions: 6 + 3

Total: 9

45

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 2 j = 4

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 11 14 auxiliary array

Inversions: 6 + 3

Total: 9

46

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 2 j = 4

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 11 14 auxiliary array

Inversions: 6 + 3

Total: 9

47

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 2 j = 3

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 11 14 16 auxiliary array

Inversions: 6 + 3 + 2

Total: 11

48

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 2 j = 3

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 11 14 16 auxiliary array

Inversions: 6 + 3 + 2

Total: 11

49

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 2 j = 2

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 11 14 16 17 auxiliary array

Inversions: 6 + 3 + 2 + 2

Total: 13

50

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 2 j = 2

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 11 14 16 17 auxiliary array

Inversions: 6 + 3 + 2 + 2

Total: 13

51

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 1 j = 2

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 11 14 16 17 18 auxiliary array

Inversions: 6 + 3 + 2 + 2

Total: 13

52

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

i = 1 j = 2

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 11 14 16 17 18 auxiliary array

Inversions: 6 + 3 + 2 + 2

Total: 13

53

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

first half i = 0 j = 2

exhausted

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 11 14 16 17 18 19 auxiliary array

Inversions: 6 + 3 + 2 + 2

Total: 13

54

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

first half i = 0 j = 2

exhausted

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 11 14 16 17 18 19 auxiliary array

Inversions: 6 + 3 + 2 + 2

Total: 13

55

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

first half i = 0 j = 2

exhausted

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 11 14 16 17 18 19 23 auxiliary array

Inversions: 6 + 3 + 2 + 2 + 0

Total: 13

56

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

first half i = 0 j = 1

exhausted

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 11 14 16 17 18 19 23 auxiliary array

Inversions: 6 + 3 + 2 + 2 + 0

Total: 13

57

Merge and Count

Given two sorted halves, count number of

inversions where ai and aj are in different

halves.

Combine two sorted halves into sorted

whole.

smallest smallest

first half i = 0 second half j = 0

exhausted exhausted

3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6

2 3 7 10 11 14 16 17 18 19 23 25 auxiliary array

Inversions: 6 + 3 + 2 + 2 + 0 + 0

Total: 13

58

- class 12 ISC Sorting Method and Algorithm in blue jUploaded bySampuran De
- Combined Handouts CS702 (PDF format)Uploaded byMuhammad Umair
- RadixUploaded bysoutri
- Sorting Algorithms in CUploaded byDeeksha Shankhdher
- 99 Java Source CodesUploaded byDr Dheeraj Mehrotra
- Epicsystems Inc Sample Technical Placement Paper Level1Uploaded byplacementpapersample
- Lab Manual Cl i Vibhs(1)Uploaded byRohit
- Data StructuresUploaded byRajyaLakshmi Uppala
- lec12-oct15-09Uploaded byMohammed AL-Maaitah
- CSEIT - 2015Uploaded byCS & IT
- 05DivideAndConquerI-2x2Uploaded byChristoffer Lucas
- Advanced Data Structures_aslamUploaded bysravan.oem
- AS-Level Math sequences (C1)Uploaded byMohammadTalal277
- CSEITUploaded byCS & IT
- Small 05 Div and ConquerUploaded byadetejubello
- Computer Science Notes – Problem Solving TechniquiesUploaded bygautham28
- CSEIT - 2015Uploaded byCS & IT
- Service Program1Uploaded byapi-3851615
- ArraysUploaded byAdeel Durrani
- Sorting.docUploaded byAnonymous KotXForu
- Hellings Et Al Sigmod 2012Uploaded bymanuelq9
- Data structuresUploaded byDerek Jeromee Trilla
- AlgorithmsUploaded byAminosuma
- 6502 Chapter 3Uploaded byRoy Vesey
- assignment.pdfUploaded byMoazzam Hussain
- Sorting and SearchingUploaded byVinothsaravanan Ramakrishnan
- csUploaded byChaudry Adeel
- dso(cse)Uploaded byAARLA NITHYAPOOJAIAH
- Asymptotic NotationUploaded byJeetendra Garg
- Pre Algebra Lesson 2 3Uploaded by412137

- Mobile for Rural Connectivity Issues and Strategies BackupUploaded byturjo987
- university online course registration systemUploaded byturjo987
- Chap1 IntroUploaded byTSMFORU
- Lec 1 Data Mining IntroductionUploaded byturjo987
- Lec 1 Data Mining Introduction for ExamUploaded byturjo987
- Software Engineering - Lecture 1Uploaded byturjo987
- Sample Fall 2015 CSE 477Uploaded byturjo987
- Course Outline CSE-477Uploaded byturjo987
- Software Engineering - Lecture 2Uploaded byturjo987
- 9 Dijkstra - Algorithms (series lecture)Uploaded byturjo987
- 8 MST - Algorithms (series lecture)Uploaded byturjo987
- 15-17 Dynamic Programming - Algorithms (series lecture)Uploaded byturjo987
- 7 Sorting - Algorithms (series lecture)Uploaded byturjo987
- 2 Graph and BFS -Algorithms (series lecture)Uploaded byturjo987
- 14 Greedy (Coin, Hulfman, Activity Seclection) - Algorithms (series lecture)Uploaded byturjo987
- 4 Topological Sort - Algorithms (series lecture)Uploaded byturjo987
- 11 Warshall - Algorithms (series lecture)Uploaded byturjo987
- 10 Bellmanford and SP in DAG - Algorithms (series lecture)Uploaded byturjo987
- 6 Articulation Point - Algorithms (series lecture)Uploaded byturjo987
- 5 SCC - Algorithms (series lecture)Uploaded byturjo987
- 3 DFS - Algorithms (series lecture)Uploaded byturjo987
- 1 Asymptotic Notation -Algorithms (series lecture)Uploaded byturjo987
- Lecture 2 IP-V4Uploaded byPartho Kumar Dutta
- Lecture 1 Introduction Advanced Computer NetworksUploaded byturjo987
- schaefer ch01Uploaded byturjo987
- 19 Distributed Study on Operating Systems অপারেটিং সিস্টেমUploaded byturjo987
- 11-Memory Study on Operating Systems অপারেটিং সিস্টেমUploaded byturjo987

- Mad vs MarkowitzUploaded byyukicm
- LaplaceTransformsUploaded byParvezmahedi Ansar
- Divide and ConquerUploaded byAngel Hugo Ocaña Pineda
- Worked Examples in Dynamic Optimization Analytic and Numeric MethodsUploaded byrizanda93
- Optimization TocUploaded byVarsha Kankani
- In DecesUploaded byUma Devi Nagiah
- De Chap 2 LDE of Order OneUploaded byAndrew Nibungco
- (Student Mathematical Library 012) W. J. Kaczor, M. T. Nowak-Problems in Mathematical Analysis II_ Continuity and Differentiation-American Mathematical Society (2001).pdfUploaded byVictor Alfonso
- Quad Fn Worksheet 2Uploaded byjan12th2004
- fx 115 msUploaded byautumnelegy
- Integration TechniquesUploaded byஏம்மனுஎல்லெ செலேச்டினோ
- S.Y.B.sc. Mathematics ( MTH - 222 (B) ) Question BankUploaded byManoj Lalita Goswami
- precalculus chapter 1 learning guideUploaded byapi-241187529
- SyllabusUploaded bySShadow
- Continuum MechanicsUploaded byRohan Kar
- (CISM International Centre for Mechanical Sciences 390) Alfred Kluwick (Eds.)-Recent Advances in Boundary Layer Theory-Springer-Verlag Wien (1998)Uploaded bySara Rodriguez
- EMT Quiz -1 Type a Answers UnderlinedUploaded byAbhishekKumar
- NumericalUploaded byKaali KIng
- Lecture Week 5a - Superposition Principle.pdfUploaded byandri
- MPC PC11 Lecture1Uploaded bykaies02
- Inverse Functions Activity2Uploaded byezmoreldo
- 4. F.Y.B.sc. Com.sci. Statistics SyllabusUploaded byananddangade
- Notes on choice under UncertaintyUploaded byJosé Daniel Fernández Dávila
- 4x4 Gaussian ExampleUploaded byrhiya
- 61375 Mark Scheme Unit 4722 Core Mathematics 2 JuneUploaded byEden Hailu
- AERSYS-1001.pdfUploaded byantonellodelre
- Speech Recognition WaveletsUploaded bySouvik Sarkar
- Sophomore's Dream FunctionUploaded byJJacquelin
- Optimal ChoiceUploaded byBianca Bazin
- moduleBsolutions.docUploaded byAhmed Fahmy