Computing Fibonacci Numbers - Lecture Slides | CSCE 350, Study notes of Computer Science

Material Type: Notes; Professor: Huang; Class: DATA STRUCTR&ALGORITHMS; Subject: Computer Science & Engineering; University: University of South Carolina - Columbia; Term: Spring 2009;

Typology: Study notes

Pre 2010

Uploaded on 09/02/2009

koofers-user-13a
koofers-user-13a 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CSCE 350:
Data Structures and Algorithms
Chin-Tser Huang
University of South Carolina
02/04/2009 2
Announcement
Reading assignment: Chapter 3.2
Homework 2 due on Monday, February
9in class
Have your answers neatly typed
Explain your answer clearly and adequately
by showing the steps
Midterm Exam 1 will be held on Friday,
February 13
02/04/2009 3
Computing Fibonacci numbers
1. Definition-based recursive algorithm (exponential)
2. Nonrecursive definition-based algorithm (linear)
3. Explicit formula algorithm
4. Logarithmic algorithm based on formula:
F
F(
(n
n-
-1)
1) F
F(
(n
n)
)
F
F(
(n
n)
)F
F(
(n
n+1)
+1)
0
0 1
1
1 1
1 1
=
=n
n
for n1, assuming an efficient way of computing matrix powers.
02/04/2009 4
Another Example
Construct an algorithm for computing
a
n
, where
a
>0
is a constant and the integer
n
0 is the input size.
Then analyze its time efficiency
Basic operation: a multiplication of two numbers
Two choices
Nonrecursive algorithm
Recursive algorithm
Any idea to make it faster, e.g., with a better time
efficiency?
Bonus: Can we solve this problem in time?
)(lognΘ
02/04/2009 5
What’s next
From now on, we are going to learn some
basic and general strategies in designing
algorithms to solve some typical computing
problems
We will analyze the efficiency of these
algorithms using the tools learned in the past
several classes
We will learn how to design algorithms with
better efficiency
Chapter 3: Brute Force
pf3

Partial preview of the text

Download Computing Fibonacci Numbers - Lecture Slides | CSCE 350 and more Study notes Computer Science in PDF only on Docsity!

CSCE 350:

Data Structures and Algorithms

Chin-Tser Huang

[email protected]

University of South Carolina

02/04/2009 2

Announcement

„ Reading assignment: Chapter 3.

„ Homework 2 due on Monday, February 9 in class

„ Have your answers neatly typed

„ Explain your answer clearly and adequately

by showing the steps

„ Midterm Exam 1 will be held on Friday, February 13

02/04/2009 3

Computing Fibonacci numbers

  1. Definition-based recursive algorithm (exponential)
  2. Nonrecursive definition-based algorithm (linear)
  3. Explicit formula algorithm
  4. Logarithmic algorithm based on formula: F F (( nn - -1)1) FF (( nn )) FF (( nn )) FF (( nn +1)+1)

nn

for n≥ 1, assuming an efficient way of computing matrix powers.

02/04/2009 4

Another Example

„ Construct an algorithm for computinga n^ , wherea>

is a constant and the integern≥0 is the input size.

Then analyze its time efficiency „ Basic operation: a multiplication of two numbers „ Two choices „ Nonrecursive algorithm „ Recursive algorithm „ Any idea to make it faster, e.g., with a better time efficiency? „ Bonus: Can we solve this problem in Θ(log^ n ) time?

02/04/2009 5

What’s next

„ From now on, we are going to learn some

basic and general strategies in designing

algorithms to solve some typical computing

problems

„ We will analyze the efficiency of these

algorithms using the tools learned in the past

several classes

„ We will learn how to design algorithms with

better efficiency

Chapter 3: Brute Force

02/04/2009 7

Brute Force

A straightforward approach, usually based directly

on the problem’s statement and definitions of

the concepts involved

Examples:

1. Computinga n^ (a > 0,n is a nonnegative integer)

2. Computingn!

  1. Multiplying two matrices
  2. Sequentially searching for a key of a given value in a list 02/04/2009 8

Brute-Force Strengths and

Weaknesses

„ Strengths

„ wide applicability „ simplicity „ yields reasonable algorithms for some important problems (e.g., matrix multiplication, sorting, searching, string matching)

„ Weaknesses

„ rarely yields efficient algorithms „ some brute-force algorithms are unacceptably slow „ not as constructive as some other design techniques

02/04/2009 9

Brute-Force Sorting Algorithms:

Selection Sort

Selection Sort Scan the array to find its smallest element

and swap it with the first element. Then, starting with the second element, scan the elements to the right of it to find the smallest among them and swap it with the

second elements. Generally, on passi (0 ≤ i ≤ n-2), find

the smallest element inA[i..n-1] and swap it withA[i]:

A[0] ≤... ≤ A[i-1] |A[i],... ,A[min],.. .,A[n-1] in their final positions

02/04/2009 10

Selection Sort Example

Sorting [89 45 68 90 29 34 17]

02/04/2009 11

Analysis of Selection Sort

Time efficiency: Space efficiency: Stability: Number of key swaps: 02/04/2009 12

Brute-Force Sorting Algorithms:

Bubble Sort

Bubble Sort Compare adjacent elements of the list and

exchange them if they are out of order. The first pass “bubbles up” the largest element to last position, the next pass bubbles up the second largest element, until

after n-1 passes, the list is sorted. Generally, passi (0 ≤

i ≤ n-2) can be represented as follows:

A[0],... ,A[j]↔ A[j-1].. .,A[n-i-1] |A[n-i] ≤... ≤ A[n-1] in their final positions

?