advance data structure and algorithm cosc600 dr. yanggon kim chapter 1

24
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Upload: claire-may

Post on 29-Dec-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Advance Data Structure and AlgorithmCOSC600

Dr. Yanggon KimChapter 1

Page 2: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Introduction

• In order to analyze a given algorithm, we need to have some mathematical background. • Some of formulas that aid in analysis are as follows.

Page 3: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Exponents

• Definition• The base X raised to power N is equal to product of X multiplied N many

times. The power N in this case is exponent

XXXXX N ....

Page 4: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Exponent Rules

BABA XXX 127575 222*2

BABA XX *)(

655 222

EXAMPLE:LAW:

Page 5: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Logarithms

• Definition

if and only if ABX log

THEROM 1.1:

THEROM 1.2:

Page 6: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Series

• Let be a sequence then the sum of a sequence is called a series.• This is denoted as:

• The easiest formula to remember is:

N

N

ii AAAAA

.........3211

Page 7: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Series (Continued)

• The companion formula is:

Page 8: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Series (Continued)

Important formulas used for analysis:

• 1)

• 2)

• 3)

• 4)

22

)1(....321

2

1

NNNNi

N

i

1|1|

....3211

1

kwhere

k

NNi

kkkkk

N

i

k

Page 9: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Series (Continued)

Page 10: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Rules

Ni

ii

iexamplefor

ififif

Rule

iNfififNfNNfNf

Rule

eN

N

i

ii

i

N

i

N

i

N

Ni

N

i

N

i

N

i

N

i

log)2

1....

2

1

2

1

2

11(

1

2

11.10

2

101.100

)9....21()100....1110....21()100....121110(

)()()(

2

)(1)()()(1)()(

1

321

9

1

100

1

100

10

1

11

1111

0

0

Page 11: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Rules ( Continued)

Page 12: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Induction Method

• Principle of Mathematical Induction• Let P be a property of positive integers such that

• Base Case: P (1) is true, and• Inductive Step: if P (n) is true, then P (n+1) is true.

• Then P (n) is true for all positive integers.• The premise P (n) in the inductive step is called Induction Hypothesis.

Page 13: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Steps for proof by Induction Method• Base condition: prove the theorem for base condition. For example

N=1• Assume that the given theorem is true for N=k case• Using assumption, prove the theorem is true for N=k+1 case step by

step

Page 14: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Induction Method Example 1

• For example, proof of for any positive integer N Proof:Base Condition:

For N =1

2

)1(

1

NNi

N

i

SHR ..12

)11(1

Page 15: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Induction Method Example 1

• Assume that

• For N = k + 1 case,

• Theorem is true for n =k+1 case• Theorem is true for any positive

integer N

2

)2)(1(

)2

2)(1(

)12

)(1(

)1(2

)1(

))1(....21(1

1

kk

kk

kk

kkk

kkik

i

Page 16: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Induction Method Example 2

• For example, proof of Fibonacci number • Let be Fibonacci number

• Prove

Page 17: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Induction Method Example 2

• Proof• Base Condition• For i =1 case

• Assume that

• For i=k+1 case,

Page 18: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Counter Example

• Most theorems are counter example. They prove as “false.”

• Example: • F1 is a Fibonacci number• Fk Fk

2 is false• F0 =1 F1 =1 F2 =2 F3 =3 F4 =5 F5 =8

So we try……

• F11 = 144 112 = 121 not true• Given case is false!

Page 19: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Contradiction

• Proof by contradiction proceeds by assuming that the theorem is false and showing that this assumption implies that some known property is false, and hence the original assumption was erroneous.

• There is an infinite number of primes• EX: 2,3,5,7,11,13,17,19

• ASSUME that there is a finite number of primes• Let Pk be the largest prime• Let N = P1 * P2 * P3 * P4 * ……….. Pk +1• None of P1 , P2 ……. Pk Divides n exactly• There will be a remainder of 1 • N is a prime number

Page 20: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Contradiction (Continued)

• Contradiction (Pk is not the largest prime)• (N > Pk )• Then theorem is true

Page 21: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Recursion

• A function that is defined in terms of itself is called “recursive.”• Fundamental Rules of recursion:

• Base case – you must always have some base cases, which can be solved without recursion.

• Making progress – for the cases that are to be solved recursively, the recursive call must always be to a case that makes progress toward a base case.

• Design Rule – assume that all the recursive calls work.• Compound interest rule – never duplicate work by solving the same instance of a

problem in separate recursive calls.

• Divide & Conquer Example• Just like merge sort for traversing an array – divide array in half, then divide

both pieces in half until search item is found.

Page 22: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Recursion Example 2 – Hanoi Tower Problem• No disk can be placed on a

smaller disk. • If n=1 move the disk source to

destination• Else

T(n-1, A,C,B)Move nth disk from A to CT(n-1,B,A,C)

Page 23: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

Recursion Example

Public static int f (int n){ if (n == 0)

return 0;else

return f(n/3 +1) + n-1;}RESULT F(10) F(4) +9 F(2) +3 F(1) +2 F(1) + 0

Page 24: Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1

References

• LERMA, M. (2003, February 8). MATHEMATICAL INDUCTION. Retrieved September 6, 2014, from http://www.math.northwestern.edu/~mlerma/problem_solving/results/induction.pdf