Algorithms and Complexity: Searching, Sorting, and Growth of Functions, Study notes of Discrete Mathematics

Definitions and algorithms for various searching and sorting techniques, including linear search, binary search, bubble sort, insertion sort, and greedy algorithms. It also covers the growth of functions and complexity analysis, including big o, big omega, and big theta notations. Additionally, it discusses the integers and division, prime numbers, and the euclidean algorithm for finding the greatest common divisor.

Typology: Study notes

Pre 2010

Uploaded on 09/02/2009

koofers-user-q7z
koofers-user-q7z 🇺🇸

5

(1)

10 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
3. The Fundamentals
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Algorithms and Complexity: Searching, Sorting, and Growth of Functions and more Study notes Discrete Mathematics in PDF only on Docsity!

The Fundamentals

Definition 13.1 Algorithms

An algorithm is a definite procedure for solving a

Problem: Searching Algorithmsproblem using a finite number of steps.

Locate an element

x

in a list of distinct elements

a 1 , a

2 ,... , a

n

or determine that it is not in the list.

Binary Search Algorithm procedure binary

search(

x :integer,

a 1 , a

2 ,... , a

n

integers in in-

  1. creasing order)

i := 1

j :=

n

  1. while

i < j

    1. begin

m

:=

i (^) +

(^) j ) / 2 ⌋

middle element

if

x > a

m

then

i :=

m

else

j

:=

m

  1. if 8. end

x

=

a i then

location

i

  1. else

location

Problem:Sorting Algorithms

Put a list

a 1 , a

2 ,... , a

n

in the increasing order.

The Bubble Sort procedure bubblesort(

a 1 ,... , a

n )

  1. for

i := 1 to

n

for

j := 1 to

n

i

if

a j

a

j

then interchange

a j

with

a j

Definition 2 3.2 The Growth of Functionswhen solving an optimization problem. Greedy algorithm makes the ”best” decision for the current stepGreedy Algorithms

Let

f

and

g

be two functions from the set of integer

(or real) numbers. We say that

f (^) ( x )

is

O

g ( x ))

(which we write

as

f (^) ( x ) =

O

g ( x ))

) if there are two constants

C

and

k

such that

f (^) ( x ) | ≤

C | g ( x ) | ,

whenever

x > k

Theorem 1

Let

f (^) ( x ) =

a n x n + a n − 1 x n − 1 +

a 0

where

a 0 ,... , a

n

are real numbers. Then

f (^) ( x ) =

O ( x n ).

Fact 2

  1. Suppose that

f 1 ( x ) =

O

g ( x ))

and

f 2 ( x ) =

O

g ( x ))

.

Then

( f 1 + f 2

x ) =

O

g ( x ))

.

  1. Suppose that

f 1 ( x ) =

O ( g 1 ( x

and

f 2 ( x ) =

O ( g 2 ( x ).

Then

f 1 f 2 )(

x ) =

O ( g 1 ( x ) g 2 ( x

3.3 Complexity

  1. Time used to solve a problem (

time complexity

  1. Memory space used (

space complexity

Time analysis:

Worst-case analysis.

Average-case analysis.

  1. Best-case analysis.

Definition 6Modular Arithmetic

Let

a

be an integer and

m

a positive integer.

We

denote by

a

mod

m

the remainder when

a

is divided by

m

.

Definition 7

If

a

and

b

are integers and

m

is a positive integer

then

a

is congruent to

b

modulo

m

if

m | ( a − b ).

Fact 4

Let

m

be a positive integer and let

a

and

b

be two inte-

gers.

a

b

(mod

m

)

if and only if

a

mod

m

b

mod

m

.

a

b

(mod

m

)

if and only if there is an integer

k

such that

a

=

b

mk

Theorem 6The Division Algorithm

Let

a

be an integer and let

d

be a positive integer.

Then there exist unique integers

q

and

r

such that

a

=

dq

r

and

r < d

Definition 9Greatest Common Divisor and Least Common Multiple

Let

a , b be two integers not both zero. The largest

integer

d

such that

d

| a

and

d

| b

is called the greatest common

divisor of

a

and

b

. It is denoted by

gcd

a, b

Definition 10

The least common multiple of the positive inte-

gers

a

and

b

is the smallest positive integer that is divisible by

both

a

and

b

. It is denoted by

lcm

a, b

Theorem 7

Let

a , b

be positive integers. Then

a (^) ·

b

=

gcd

a, b

lcm

a, b

Euclidean Algorithm3.6 Integers and Algorithms Goal:

Find

gcd

a, b

a

b .

Method:

Let

r 0

=

a, r

1

=

b

. Then

r 0 = r 1 q 1 + r 2

where 0

< r

2

< r

1

r 1 = r 2 q 2 + r 3

where 0

< r

3

< r

2

r... n − 2 = r n 1 q n − 1 + r n

where 0

< r

n

< r

n − 1

r n − 1 = r n q n

procedure gcd(Euclidean Algorithm

a, b

: positive integers)

x

:=

a

y

:=

b

  1. while(

y

6 = 0)

    1. begin

r

:=

x

mod

y

x

:=

y

y

:=

r

  1. return 8. end

gcd

a, b

x

Lemma 9

Let

a

=

bq

r

where

a, b, q

and

r

are integers.

Then

gcd

a, b

gcd

b, r