algorithms – overview (1a) · 3/29/2018  · which would include all computer programs, including...

20
Young Won Lim 3/29/18 Algorithms – Overview (1A)

Upload: others

Post on 08-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Young Won Lim3/29/18

Algorithms – Overview (1A)

Page 2: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Young Won Lim3/29/18

Copyright (c) 2017 – 2018 Young W. Lim.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".

Please send corrections (or suggestions) to [email protected].

This document was produced by using LibreOffice and Octave.

Page 3: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 3 Young Won Lim3/29/18

Informal Definitions of Algorithms

https://en.wikipedia.org/wiki/Algorithm

An informal definition could be "a set of rules that precisely defines a sequence of operations."which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only an algorithm if it stops eventually.

A prototypical example of an algorithm is the Euclidean algorithm to determine the maximum common divisor of two integers;

Page 4: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 4 Young Won Lim3/29/18

Flow Chart – Euclid Algorithms

https://en.wikipedia.org/wiki/Algorithm

flow chart of an algorithm (Euclid's algorithm)for calculating the greatest common divisor (gcd)

two numbers a and b in locations named A and B.

successive subtractions in two loops:

IF the test B ≥ A, THEN B ← B − A

Similarly, IF A > B, THEN A ← A − B.

terminates when (the contents of) B is 0, yielding the g.c.d. in A.

(Algorithm derived from Scott 2009:13; symbols and drawing style from Tausworthe 1977).

Page 5: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 5 Young Won Lim3/29/18

Euclid Algorithm

https://en.wikipedia.org/wiki/Euclidean_algorithm

Euclid's method for finding the greatest common divisor (GCD) of two starting lengths BA and DC, both defined to be multiples of a common "unit" length.

The length DC being shorter, it is used to "measure" BA, but only once because remainder EA is less than DC. EA now measures (twice) the shorter length DC, with remainder FC shorter than EA. Then FC measures (three times) length EA. Because there is no remainder, the process ends with FC being the GCD. On the right Nicomachus' example with numbers 49 and 21 resulting in their GCD of 7 (derived from Heath 1908:300).

Page 6: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 6 Young Won Lim3/29/18

Best, Worst, Average Case Complexities

https://en.wikipedia.org/wiki/Algorithm

The best, worst and average case complexity refer to three different ways of measuring the time complexity (or any other complexity measure) of different inputs of the same size.

Since some inputs of the same size n may be faster to solve than others.

This complexity is only defined with respect to a probability distribution over the inputs.

For instance, if all inputs of the same size are assumed to be equally likely to appear,

the average case complexity can be defined with respect to the uniform distribution over all inputs of size n.

Page 7: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 7 Young Won Lim3/29/18

Best, Worst, Average Case Complexities

https://en.wikipedia.org/wiki/Algorithm

Best-case complexity: the complexity of solving the problem for the best input of size n.

Worst-case complexity: the complexity of solving the problem for the worst input of size n.

Average-case complexity: the complexity of solving the problem on an average.

Page 8: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 8 Young Won Lim3/29/18

Upper and Lower Bounds

https://en.wikipedia.org/wiki/Algorithm

To classify the computation time (or similar resources, such as space consumption), one is interested in proving upper and lower bounds on the minimum amount of time required by the most efficient algorithm solving a given problem.

The complexity of an algorithm is usually taken to be its worst-case complexity, unless specified otherwise.

Analyzing a particular algorithm falls under the field of analysis of algorithms. To show an upper bound T(n) on the time complexity of a problem, one needs to show only that there is a particular algorithm with running time at most T(n).

However, proving lower bounds is much more difficult, since lower bounds make a statement about all possible algorithms that solve a given problem.

Page 9: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 9 Young Won Lim3/29/18

Upper and Lower Bounds

https://en.wikipedia.org/wiki/Algorithm

The phrase "all possible algorithms" includes not just the algorithms known today, but any algorithm that might be discovered in the future.

To show a lower bound of T(n) for a problem requires showing that no algorithm can have time complexity lower than T(n).

Upper and lower bounds are usually stated using the big O notation, which hides constant factors and smaller terms.

This makes the bounds independent of the specific details of the computational model used.

For instance, if T(n) = 7n2 + 15n + 40, in big O notation one would write T(n) = O(n2).

Page 10: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 10 Young Won Lim3/29/18

NP Problems

https://en.wikipedia.org/wiki/Algorithm

Diagram of complexity classes provided that P ≠ NP. The existence of problems in NP outside both P and NP-complete in this case was established by Ladner.[3]

Page 11: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 11 Young Won Lim3/29/18

Algorithm Analysis

https://en.wikipedia.org/wiki/Algorithm

Graphs of number of operations, N vs input size n for common complexities, assuming a coefficient of 1

Page 12: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 12 Young Won Lim3/29/18

Big-O

https://en.wikipedia.org/wiki/https://en.wikipedia.org/wiki/Big_O_notation

Page 13: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 13 Young Won Lim3/29/18

Upper and Lower Bounds

https://en.wikipedia.org/wiki/https://en.wikipedia.org/wiki/Big_O_notation`

Page 14: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 14 Young Won Lim3/29/18

Big O Notation

Discrete Mathematics, Rosen

witness (C ,k)

f (x ) ≤ C g(x ) for x > k

f (x ) is O(g(x ))

Page 15: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 15 Young Won Lim3/29/18

Big O Notation Examples

Discrete Mathematics, Rosen

witness (C ,k)

f (x ) ≤ C g(x ) for x > k

f (x ) is O(g(x ))

x2+2 x+1 is O(x3)

1 < x 1 < x2 x < x2 x2+2 x+1 < x2+2 x2+ x2 = 4 x2

x2+2 x+1 < 4 x2

Page 16: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 16 Young Won Lim3/29/18

f(x) = x2 + 2x + 1, g(x) = x2

Page 17: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 17 Young Won Lim3/29/18

The same class function examples

x→2 x

f 1(2 x )→4 f 1(x )

f 2(2 x )→4 f 2(x )

f 3(2 x )→4 f 3(x)

Page 18: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 18 Young Won Lim3/29/18

Growth Examples (1)

3n 2n n4

n3

n3n42n3n

3n 2n n4

n3

Page 19: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Algorithms – Overview (1A) 19 Young Won Lim3/29/18

Growth Examples (2)

n3

n3n42n3n

3n 2n n4

n3

(log2n)3

Page 20: Algorithms – Overview (1A) · 3/29/2018  · which would include all computer programs, including programs that do not perform numeric calculations. Generally, a program is only

Young Won Lim3/29/18

References

[1] http://en.wikipedia.org/[2]