analysis of algorithmsds172/wiki.files/01-analysis.pdf · data structures and algorithms in java....

60
Analysis of algorithms Analysis of algorithms

Upload: others

Post on 29-Mar-2020

27 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Analysis of algorithms

Analysis of algorithms

Page 2: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Textbooks

Main textbook.

Introduction to Algorithms. Cormen, Leiserson, Rivestand Stein.

Additional textbooks.

Algorithm design: foundations, analysis, and Internetexamples. Michael T. Goodrich, Roberto Tamassia.

Data structures and algorithms in Java. Michael T.Goodrich, Roberto Tamassia.

Data structures and algorithms. Alfred V. Aho, John E.Hopcroft, Jeffrey D. Ullman.

Data structures & their algorithms. Harry R. Lewis, LarryDenenberg.

Algorithms in C++. Robert Sedgewick.

Analysis of algorithms

Page 3: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Algorithms

An algorithm is a well-defined computational procedurethat takes some value, or set of values, as input andproduces some value, or set of values, as output.

Algorithm is a tool for solving a well-specifiedcomputational problem. A computational problem isdefined by an input/output relationship.

Example

In the sorting problem,

Input A sequence of n number a1, . . . , an.

Output A permutation a′1, . . . , a′n of the input numbers

which is ordered, namely a′1 ≤ a′2 ≤ · · · ≤ a′n.

Analysis of algorithms

Page 4: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Insertion sort

Insertion-Sort(A)

(1) for j = 2 to A.length(2) key← A[j ]

% Insert A[j ] into the sorted sequence A[1..j − 1](3) i ← j − 1(4) while i > 0 and A[i ] > key(5) A[i + 1]← A[i ](6) i ← i − 1(7) A[i + 1]← key

45 726 1

Analysis of algorithms

Page 5: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Insertion sort

Insertion-Sort(A)

(1) for j = 2 to A.length(2) key← A[j ]

% Insert A[j ] into the sorted sequence A[1..j − 1](3) i ← j − 1(4) while i > 0 and A[i ] > key(5) A[i + 1]← A[i ](6) i ← i − 1(7) A[i + 1]← key

45 726 1 j = 2

Analysis of algorithms

Page 6: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Insertion sort

Insertion-Sort(A)

(1) for j = 2 to A.length(2) key← A[j ]

% Insert A[j ] into the sorted sequence A[1..j − 1](3) i ← j − 1(4) while i > 0 and A[i ] > key(5) A[i + 1]← A[i ](6) i ← i − 1(7) A[i + 1]← key

45 726 1 j = 2

Analysis of algorithms

Page 7: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Insertion sort

Insertion-Sort(A)

(1) for j = 2 to A.length(2) key← A[j ]

% Insert A[j ] into the sorted sequence A[1..j − 1](3) i ← j − 1(4) while i > 0 and A[i ] > key(5) A[i + 1]← A[i ](6) i ← i − 1(7) A[i + 1]← key

45 72 6 1 j = 3

Analysis of algorithms

Page 8: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Insertion sort

Insertion-Sort(A)

(1) for j = 2 to A.length(2) key← A[j ]

% Insert A[j ] into the sorted sequence A[1..j − 1](3) i ← j − 1(4) while i > 0 and A[i ] > key(5) A[i + 1]← A[i ](6) i ← i − 1(7) A[i + 1]← key

45 72 6 1 j = 3

Analysis of algorithms

Page 9: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Insertion sort

Insertion-Sort(A)

(1) for j = 2 to A.length(2) key← A[j ]

% Insert A[j ] into the sorted sequence A[1..j − 1](3) i ← j − 1(4) while i > 0 and A[i ] > key(5) A[i + 1]← A[i ](6) i ← i − 1(7) A[i + 1]← key

45 72 6 1 j = 4

Analysis of algorithms

Page 10: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Insertion sort

Insertion-Sort(A)

(1) for j = 2 to A.length(2) key← A[j ]

% Insert A[j ] into the sorted sequence A[1..j − 1](3) i ← j − 1(4) while i > 0 and A[i ] > key(5) A[i + 1]← A[i ](6) i ← i − 1(7) A[i + 1]← key

45 72 6 1 j = 5

Analysis of algorithms

Page 11: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Insertion sort

Insertion-Sort(A)

(1) for j = 2 to A.length(2) key← A[j ]

% Insert A[j ] into the sorted sequence A[1..j − 1](3) i ← j − 1(4) while i > 0 and A[i ] > key(5) A[i + 1]← A[i ](6) i ← i − 1(7) A[i + 1]← key

45 72 61 j = 6

Analysis of algorithms

Page 12: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Insertion sort

Insertion-Sort(A)

(1) for j = 2 to A.length(2) key← A[j ]

% Insert A[j ] into the sorted sequence A[1..j − 1](3) i ← j − 1(4) while i > 0 and A[i ] > key(5) A[i + 1]← A[i ](6) i ← i − 1(7) A[i + 1]← key

4 5 72 61

Analysis of algorithms

Page 13: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Insertion sort — Correctness

At the start of each iteration of the for loop, A[1..j − 1]contains the elements that are initially in A[1..j − 1] insorted order.

At the end of the algorithm (j = n + 1), the array issorted.

45 72 6 1

Analysis of algorithms

Page 14: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Analysis of algortihms

We want to measure the resources used by an algorithm:

Running time.Memory usage.

The complexity of an algorithm is a function of the inputsize.

In the sorting problem — the number of values.In graph problems — the number of nodes and edges.

For a fixed input size, the algorithm complexity dependson the specific input. We can consider several options.

Best case.Worst case.Average case.

Analysis of algorithms

Page 15: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Analysis of insertion sort

Insertion-Sort(A)

(1) for j = 2 to A.length(2) key← A[j ](3) i ← j − 1(4) while i > 0 and A[i ] > key(5) A[i + 1]← A[i ](6) i ← i − 1(7) A[i + 1]← key

cost timesc1 nc2 n − 1c3 n − 1c4

∑nj=2 tj

c5∑n

j=2(tj − 1)

c6∑n

j=2(tj − 1)

c7 n − 1

T (n) = c1n + c2(n − 1) + c3(n − 1) + c4

n∑j=2

tj+

c5

n∑j=2

(tj − 1) + c6

n∑j=2

(tj − 1) + c7(n − 1)

Analysis of algorithms

Page 16: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Insertion sort — Best case

The best case is when the array is sorted. In this case,case tj = 1 for all j .

The running time is

T (n) = c1n + c2(n − 1) + c3(n − 1) + c4(n − 1) + c7(n − 1)

= (c1 + c2 + c3 + c4 + c7)n − (c2 + c3 + c4 + c7)

= a′n − b′

Analysis of algorithms

Page 17: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Insertion sort — Worst case

The worst case is when the array is sorted in reverseorder. In this case tj = j for all j .∑n

j=2 j = n(n+1)2− 1 and

∑nj=2(j − 1) = n(n−1)

2.

The running time is

T (n) = c1n + c2(n − 1) + c3(n − 1) + c4

(n(n + 1)

2− 1

)+ c5

(n(n − 1)

2

)+ c6

(n(n − 1)

2

)+ c7(n − 1)

=(c4

2+

c52

+c62

)n2

+(c1 + c2 + c3 +

c42− c5

2− c6

2+ c7

)n

− (c2 + c3 + c4 + c7)

= an2 + bn − c

Analysis of algorithms

Page 18: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Analysis of algorithms

The worst-case running time of insertion sort isan2 + bn + c for some constants a, b, c .

The exact values of a, b, c depend on the computer onwhich we run the algorithm.

In theoretical computer science, we are interested in theasymptotic complexity, namely the behavior of theworst-case complexity function as n approaches infinity.

In the expression an2 + bn + c , the terms bn and cbecome negligible compared to an2 when n approachesinfinity.

Thus, the time complexity of insertion sort is ≈ an2.

We are not interested in the constant a. Thus, we saythat the running time of insertion sort grows like n2 whenn approaches infinity.

Analysis of algorithms

Page 19: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Order of growth

n n log n n2 n3 2n

10 33.2 100 1,000 1,02420 86.4 400 8,000 1,048,57630 147.2 900 27,000 1,073,741,824

100 664.4 10,000 1,000,000200 1528.8 40,000 8,000,000300 2468.6 90,000 27,000,000

1000 9965.8 1,000,000 1,000,000,0002000 21931.6 4,000,000 8,000,000,000

Analysis of algorithms

Page 20: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Θ

Θ(g) is the set of all function that grow like g when napproaches infinity.

Formally,

Θ(g) = f : there are positive constants c1, c2, and n0

such that 0 ≤ c1g(n) ≤ f (n) ≤ c2g(n) for all n ≥ n0

Analysis of algorithms

Page 21: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Example

Claim12n2 − 3n ∈ Θ(n2).

Proof.

We need to show that there are c1, c2, n0 such that for n ≥ n0,

c1n2 ≤ 1

2n2 − 3n ≤ c2n

2.

Clearly, 12n2 − 3n ≤ 1

2n2, so we can take c2 = 1/2.

Furthermore,

c1n2 ≤ 1

2n2 − 3n⇐⇒ 3n ≤

(1

2− c1

)n2 ⇐⇒ 3

12− c1

≤ n

We can take c1 = 1/4 and n0 = 12.

Analysis of algorithms

Page 22: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Example — Polynomials

Claim

Let f (n) = bknk + bk−1n

k−1 + · · ·+ b1n + b0, where bk > 0,be a polynomial of degree k . Then f (n) ∈ Θ(nk).

Proof.

Let ai = |bi |. Then,

f (n) ≤ aknk + ak−1n

k−1 + · · ·+ a1n + a0

≤ aknk + ak−1n

k + · · ·+ a1nk + a0n

k

= (ak + ak−1 + · · ·+ a0)nk

f (n) ≥ aknk − ak−1n

k−1 − · · · − a1n − a0

≥ aknk − ak−1n

k−1 − · · · − a1nk−1 − a0n

k−1

= aknk − (ak−1 + · · ·+ a1 + a0)nk−1

so f (n) ≥ 12akn

k for large enough n.Analysis of algorithms

Page 23: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Example

Some functions in Θ(n2) are

n2 + 10n

3n2 + 100n + 5

3n2 +√n + log n

Analysis of algorithms

Page 24: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

O

O(g) is the set of all function that grow like g or slowerwhen n approaches infinity.

Formally,

O(g) = f : there are positive constants c and n0

such that 0 ≤ f (n) ≤ cg(n) for all n ≥ n0

Analysis of algorithms

Page 25: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Example

Some functions in Θ(n2) are

n2 + 10n

3n2 + 100n + 5

3n2 +√n + log n

Some functions in O(n2) are

n2 + 10n

3n2 + 100n + 5

3n2 +√n + log n

n

n1.999

log n

log log n

Analysis of algorithms

Page 26: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Ω

Ω(g) is the set of all function that grow at least as fastas g when n approaches infinity.

Formally,

Ω(g) = f : there are positive constants c and n0

such that 0 ≤ cg(n) ≤ f (n) for all n ≥ n0

Θ(g) = O(g) ∩ Ω(g).

Analysis of algorithms

Page 27: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Example

Some functions in Ω(n2) are

n2 + 10n

3n2 + 100n + 5

3n2 +√n + log n

n2 log log n

4n3

2n

Analysis of algorithms

Page 28: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Asymptotic notation in equations and inequalities

Stands alone on right-hand side means set membership.Example: n = O(n2) means n ∈ O(n2).

In general, asymptotic notation on the right side standsfor some anonymous function.Example: 2n2 + 3n + 1 = 2n2 + Θ(n) means that2n2 + 3n+ 1 = 2n2 + f (n) for some function f (n) in Θ(n).

On the left-hand side stands for any anonymous function.Example: 2n2 + Θ(n) = Θ(n2) means that for anyfunction g(n) in Θ(n) there is a function f (n) in Θ(n2)such that 2n2 + g(n) = f (n).

Analysis of algorithms

Page 29: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Asymptotic notations intuition

Intuitively,

O is like ≤.Ω is like ≥.Θ is like =.

Unlike real numbers, some functions cannot be compared.

For example, f (n) = n and g(n) =

n2 if n is even

1 if n is odd.

Analysis of algorithms

Page 30: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Properties

Transitivity

f ∈ Θ(g) and g ∈ Θ(h) implies f ∈ Θ(h).

Same for O,Ω.

Reflexivity

f ∈ Θ(f ).

Same for O,Ω.

Symmetry

f ∈ Θ(g) if and only if g ∈ Θ(f ).

Transpose symmetry

f ∈ O(g) if and only if g ∈ Ω(f ).

Analysis of algorithms

Page 31: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

The Divide and Conquer Paradigm

Divide

Divide the problem into several (smaller) sub-problems.

Solve the sub-problems recursively.

Stop the recursion when the size of a sub-problem is smallenough. Then, either the sub-problem does not requiresolving, or apply some naive algorithm on thesub-problem.

Conquer

Combine the solutions of the sub-problems into thesolution for the original problem.

Analysis of algorithms

Page 32: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Devide

Divide the array into two sub-array of equal sizes.

Sort the two sub-arrays recursively.

Stop the recursion when the size of the sub-array is 1.An array of size 1 is always sorted, so no work is needed.

Conquer

Merge the two sorted sub-arrays to produce the sortedarray.

34 825 1 2 6

Analysis of algorithms

Page 33: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Devide

Divide the array into two sub-array of equal sizes.

Sort the two sub-arrays recursively.

Stop the recursion when the size of the sub-array is 1.An array of size 1 is always sorted, so no work is needed.

Conquer

Merge the two sorted sub-arrays to produce the sortedarray.

34 825 1 2 6

Analysis of algorithms

Page 34: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Devide

Divide the array into two sub-array of equal sizes.

Sort the two sub-arrays recursively.

Stop the recursion when the size of the sub-array is 1.An array of size 1 is always sorted, so no work is needed.

Conquer

Merge the two sorted sub-arrays to produce the sortedarray.

34 825 1 2 6

Analysis of algorithms

Page 35: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Devide

Divide the array into two sub-array of equal sizes.

Sort the two sub-arrays recursively.

Stop the recursion when the size of the sub-array is 1.An array of size 1 is always sorted, so no work is needed.

Conquer

Merge the two sorted sub-arrays to produce the sortedarray.

34 825 1 2 6

Analysis of algorithms

Page 36: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Devide

Divide the array into two sub-array of equal sizes.

Sort the two sub-arrays recursively.

Stop the recursion when the size of the sub-array is 1.An array of size 1 is always sorted, so no work is needed.

Conquer

Merge the two sorted sub-arrays to produce the sortedarray.

34 825 1 2 6

merge

Analysis of algorithms

Page 37: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Devide

Divide the array into two sub-array of equal sizes.

Sort the two sub-arrays recursively.

Stop the recursion when the size of the sub-array is 1.An array of size 1 is always sorted, so no work is needed.

Conquer

Merge the two sorted sub-arrays to produce the sortedarray.

34 82 5 1 2 6

Analysis of algorithms

Page 38: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Devide

Divide the array into two sub-array of equal sizes.

Sort the two sub-arrays recursively.

Stop the recursion when the size of the sub-array is 1.An array of size 1 is always sorted, so no work is needed.

Conquer

Merge the two sorted sub-arrays to produce the sortedarray.

34 82 5 1 2 6

Analysis of algorithms

Page 39: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Devide

Divide the array into two sub-array of equal sizes.

Sort the two sub-arrays recursively.

Stop the recursion when the size of the sub-array is 1.An array of size 1 is always sorted, so no work is needed.

Conquer

Merge the two sorted sub-arrays to produce the sortedarray.

34 82 5 1 2 6

merge

Analysis of algorithms

Page 40: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Devide

Divide the array into two sub-array of equal sizes.

Sort the two sub-arrays recursively.

Stop the recursion when the size of the sub-array is 1.An array of size 1 is always sorted, so no work is needed.

Conquer

Merge the two sorted sub-arrays to produce the sortedarray.

34 82 5 1 2 6

Analysis of algorithms

Page 41: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Devide

Divide the array into two sub-array of equal sizes.

Sort the two sub-arrays recursively.

Stop the recursion when the size of the sub-array is 1.An array of size 1 is always sorted, so no work is needed.

Conquer

Merge the two sorted sub-arrays to produce the sortedarray.

34 82 5 1 2 6

merge

Analysis of algorithms

Page 42: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Devide

Divide the array into two sub-array of equal sizes.

Sort the two sub-arrays recursively.

Stop the recursion when the size of the sub-array is 1.An array of size 1 is always sorted, so no work is needed.

Conquer

Merge the two sorted sub-arrays to produce the sortedarray.

34 82 5 1 2 6

Analysis of algorithms

Page 43: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Devide

Divide the array into two sub-array of equal sizes.

Sort the two sub-arrays recursively.

Stop the recursion when the size of the sub-array is 1.An array of size 1 is always sorted, so no work is needed.

Conquer

Merge the two sorted sub-arrays to produce the sortedarray.

34 82 5 1 2 6

Analysis of algorithms

Page 44: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Devide

Divide the array into two sub-array of equal sizes.

Sort the two sub-arrays recursively.

Stop the recursion when the size of the sub-array is 1.An array of size 1 is always sorted, so no work is needed.

Conquer

Merge the two sorted sub-arrays to produce the sortedarray.

34 82 5 1 2 6

merge

Analysis of algorithms

Page 45: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Devide

Divide the array into two sub-array of equal sizes.

Sort the two sub-arrays recursively.

Stop the recursion when the size of the sub-array is 1.An array of size 1 is always sorted, so no work is needed.

Conquer

Merge the two sorted sub-arrays to produce the sortedarray.

34 82 5 1 2 6

Analysis of algorithms

Page 46: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Devide

Divide the array into two sub-array of equal sizes.

Sort the two sub-arrays recursively.

Stop the recursion when the size of the sub-array is 1.An array of size 1 is always sorted, so no work is needed.

Conquer

Merge the two sorted sub-arrays to produce the sortedarray.

34 82 5 1 2 6

merge

Analysis of algorithms

Page 47: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Devide

Divide the array into two sub-array of equal sizes.

Sort the two sub-arrays recursively.

Stop the recursion when the size of the sub-array is 1.An array of size 1 is always sorted, so no work is needed.

Conquer

Merge the two sorted sub-arrays to produce the sortedarray.

3 4 82 51 2 6

Analysis of algorithms

Page 48: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge sort

Merge-Sort(A, left, right)

(1) if left < right(2) mid← b(left + right)/2c(3) Merge-Sort(A, left,mid)(4) Merge-Sort(A,mid + 1, right)(5) Merge(A, left,mid, right)

The array A is sorted by calling Merge-Sort(A, 1,A.length).

Analysis of algorithms

Page 49: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge

Merge(A, left,mid, right)(1) n1 ← mid− left + 1(2) n2 ← right−mid(3) Create array L[1..n1 + 1](4) Create array R[1..n2 + 1](5) for i = 1 to n1(6) L[i ]← A[left + i − 1](7) for j = 1 to n2(8) R[j ]← A[mid + j ](9) L[n1 + 1]←∞(10) R[n2 + 1]←∞

(11) i ← 1(12) j ← 1(13) for k = left to right(14) if L[i ] ≤ R[j ](15) A[k]← L[i ](16) i ← i + 1(17) else(18) A[k]← R[j ](19) j ← j + 1

9 10 11 12 13 14 15 16

A 25 842 1 3 6

Merge(A, 9, 12, 16)

Analysis of algorithms

Page 50: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge

Merge(A, left,mid, right)(1) n1 ← mid− left + 1(2) n2 ← right−mid(3) Create array L[1..n1 + 1](4) Create array R[1..n2 + 1](5) for i = 1 to n1(6) L[i ]← A[left + i − 1](7) for j = 1 to n2(8) R[j ]← A[mid + j ](9) L[n1 + 1]←∞(10) R[n2 + 1]←∞

(11) i ← 1(12) j ← 1(13) for k = left to right(14) if L[i ] ≤ R[j ](15) A[k]← L[i ](16) i ← i + 1(17) else(18) A[k]← R[j ](19) j ← j + 1

9 10 11 12 13 14 15 16

A

L 25 842 1 3 6∞ R ∞

25 842 1 3 6

Merge(A, 9, 12, 16)

Analysis of algorithms

Page 51: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge

Merge(A, left,mid, right)(1) n1 ← mid− left + 1(2) n2 ← right−mid(3) Create array L[1..n1 + 1](4) Create array R[1..n2 + 1](5) for i = 1 to n1(6) L[i ]← A[left + i − 1](7) for j = 1 to n2(8) R[j ]← A[mid + j ](9) L[n1 + 1]←∞(10) R[n2 + 1]←∞

(11) i ← 1(12) j ← 1(13) for k = left to right(14) if L[i ] ≤ R[j ](15) A[k]← L[i ](16) i ← i + 1(17) else(18) A[k]← R[j ](19) j ← j + 1

9 10 11 12 13 14 15 16

A

L 25 842 1 3 6∞ R ∞

25 842 1 3 6

i j

k

Merge(A, 9, 12, 16)

Analysis of algorithms

Page 52: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge

Merge(A, left,mid, right)(1) n1 ← mid− left + 1(2) n2 ← right−mid(3) Create array L[1..n1 + 1](4) Create array R[1..n2 + 1](5) for i = 1 to n1(6) L[i ]← A[left + i − 1](7) for j = 1 to n2(8) R[j ]← A[mid + j ](9) L[n1 + 1]←∞(10) R[n2 + 1]←∞

(11) i ← 1(12) j ← 1(13) for k = left to right(14) if L[i ] ≤ R[j ](15) A[k]← L[i ](16) i ← i + 1(17) else(18) A[k]← R[j ](19) j ← j + 1

9 10 11 12 13 14 15 16

A

L 25 842 1 3 6∞ R ∞

25 841 1 3 6

i j

k

Merge(A, 9, 12, 16)

Analysis of algorithms

Page 53: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge

Merge(A, left,mid, right)(1) n1 ← mid− left + 1(2) n2 ← right−mid(3) Create array L[1..n1 + 1](4) Create array R[1..n2 + 1](5) for i = 1 to n1(6) L[i ]← A[left + i − 1](7) for j = 1 to n2(8) R[j ]← A[mid + j ](9) L[n1 + 1]←∞(10) R[n2 + 1]←∞

(11) i ← 1(12) j ← 1(13) for k = left to right(14) if L[i ] ≤ R[j ](15) A[k]← L[i ](16) i ← i + 1(17) else(18) A[k]← R[j ](19) j ← j + 1

9 10 11 12 13 14 15 16

A

L 25 842 1 3 6∞ R ∞

25 821 1 3 6

i j

k

Merge(A, 9, 12, 16)

Analysis of algorithms

Page 54: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge

Merge(A, left,mid, right)(1) n1 ← mid− left + 1(2) n2 ← right−mid(3) Create array L[1..n1 + 1](4) Create array R[1..n2 + 1](5) for i = 1 to n1(6) L[i ]← A[left + i − 1](7) for j = 1 to n2(8) R[j ]← A[mid + j ](9) L[n1 + 1]←∞(10) R[n2 + 1]←∞

(11) i ← 1(12) j ← 1(13) for k = left to right(14) if L[i ] ≤ R[j ](15) A[k]← L[i ](16) i ← i + 1(17) else(18) A[k]← R[j ](19) j ← j + 1

9 10 11 12 13 14 15 16

A

L 25 842 1 3 6∞ R ∞

22 821 1 3 6

i j

k

Merge(A, 9, 12, 16)

Analysis of algorithms

Page 55: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge

Merge(A, left,mid, right)(1) n1 ← mid− left + 1(2) n2 ← right−mid(3) Create array L[1..n1 + 1](4) Create array R[1..n2 + 1](5) for i = 1 to n1(6) L[i ]← A[left + i − 1](7) for j = 1 to n2(8) R[j ]← A[mid + j ](9) L[n1 + 1]←∞(10) R[n2 + 1]←∞

(11) i ← 1(12) j ← 1(13) for k = left to right(14) if L[i ] ≤ R[j ](15) A[k]← L[i ](16) i ← i + 1(17) else(18) A[k]← R[j ](19) j ← j + 1

9 10 11 12 13 14 15 16

A

L 25 842 1 3 6∞ R ∞

22 321 1 3 6

i j

k

Merge(A, 9, 12, 16)

Analysis of algorithms

Page 56: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge

Merge(A, left,mid, right)(1) n1 ← mid− left + 1(2) n2 ← right−mid(3) Create array L[1..n1 + 1](4) Create array R[1..n2 + 1](5) for i = 1 to n1(6) L[i ]← A[left + i − 1](7) for j = 1 to n2(8) R[j ]← A[mid + j ](9) L[n1 + 1]←∞(10) R[n2 + 1]←∞

(11) i ← 1(12) j ← 1(13) for k = left to right(14) if L[i ] ≤ R[j ](15) A[k]← L[i ](16) i ← i + 1(17) else(18) A[k]← R[j ](19) j ← j + 1

9 10 11 12 13 14 15 16

A

L 25 842 1 3 6∞ R ∞

22 321 4 3 6

i j

k

Merge(A, 9, 12, 16)

Analysis of algorithms

Page 57: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge

Merge(A, left,mid, right)(1) n1 ← mid− left + 1(2) n2 ← right−mid(3) Create array L[1..n1 + 1](4) Create array R[1..n2 + 1](5) for i = 1 to n1(6) L[i ]← A[left + i − 1](7) for j = 1 to n2(8) R[j ]← A[mid + j ](9) L[n1 + 1]←∞(10) R[n2 + 1]←∞

(11) i ← 1(12) j ← 1(13) for k = left to right(14) if L[i ] ≤ R[j ](15) A[k]← L[i ](16) i ← i + 1(17) else(18) A[k]← R[j ](19) j ← j + 1

9 10 11 12 13 14 15 16

A

L 25 842 1 3 6∞ R ∞

52 321 4 3 6

i j

k

Merge(A, 9, 12, 16)

Analysis of algorithms

Page 58: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge

Merge(A, left,mid, right)(1) n1 ← mid− left + 1(2) n2 ← right−mid(3) Create array L[1..n1 + 1](4) Create array R[1..n2 + 1](5) for i = 1 to n1(6) L[i ]← A[left + i − 1](7) for j = 1 to n2(8) R[j ]← A[mid + j ](9) L[n1 + 1]←∞(10) R[n2 + 1]←∞

(11) i ← 1(12) j ← 1(13) for k = left to right(14) if L[i ] ≤ R[j ](15) A[k]← L[i ](16) i ← i + 1(17) else(18) A[k]← R[j ](19) j ← j + 1

9 10 11 12 13 14 15 16

A

L 25 842 1 3 6∞ R ∞

52 321 4 6 6

i j

k

Merge(A, 9, 12, 16)

Analysis of algorithms

Page 59: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Merge

Merge(A, left,mid, right)(1) n1 ← mid− left + 1(2) n2 ← right−mid(3) Create array L[1..n1 + 1](4) Create array R[1..n2 + 1](5) for i = 1 to n1(6) L[i ]← A[left + i − 1](7) for j = 1 to n2(8) R[j ]← A[mid + j ](9) L[n1 + 1]←∞(10) R[n2 + 1]←∞

(11) i ← 1(12) j ← 1(13) for k = left to right(14) if L[i ] ≤ R[j ](15) A[k]← L[i ](16) i ← i + 1(17) else(18) A[k]← R[j ](19) j ← j + 1

9 10 11 12 13 14 15 16

A

L 25 842 1 3 6∞ R ∞

52 321 4 6 8

i j

k

Merge(A, 9, 12, 16)

Analysis of algorithms

Page 60: Analysis of algorithmsds172/wiki.files/01-analysis.pdf · Data structures and algorithms in Java. Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms. Alfred V

Analysis of merge sort

Merge-Sort(A, left, right)

(1) if left < right(2) mid← b(left + right)/2c(3) Merge-Sort(A, left,mid)(4) Merge-Sort(A,mid + 1, left)(5) Merge(A, left,mid, right)

costΘ(1)Θ(1)T (n/2)T (n/2)Θ(n)

T (n) =

Θ(1) if n = 1

2T (n/2) + Θ(n) if n > 1

Analysis of algorithms