Algorithms: Efficiency and Analysis Lecture Notes and Analysis Study of Search and Fibonacci Sequences

Fundamental Definitions of Algorithms and Problems

  • Algorithm Definition: An algorithm is a step-by-step procedure used to solve a problem. It is the core logic that dictates how a task is performed.

  • Crucial Nature of Efficiency: Ensuring a developer uses the most efficient algorithm is critical because the efficiency of the logic remains a bottleneck no matter how fast computer hardware becomes or how inexpensive memory gets.

  • Algorithm Analysis: This is the process of determining how efficiently an algorithm solves a specific problem. It involves studying the resource consumption of the algorithm.

  • Algorithm Grouping (Order): The concept of "order" is used to categorize algorithms based on their eventual behavior or scalability as input sizes increase.

  • Program Composition: A computer program is made up of individual modules. Each module is designed to be understandable by a computer and solves a specific task, such as sorting a list.

  • Focus of Study: The primary concentration is on the design and analysis of individual modules (specific tasks) rather than the design of entire, large-scale software programs.

  • Formal Definition of a Problem: A problem is defined as a specific question to which an answer is sought.

  • Parameters: These are variables within a problem statement that have not yet been assigned specific values. Parameters allow a single problem statement to represent a entire class of related problems.

  • Problem Instance: A specific assignment of values to the parameters of a problem defines an instance of that problem.

  • Algorithms as General Solutions: For an algorithm to properly solve a problem, it must provide a general step-by-step procedure that can produce a solution for every possible instance of that problem.

The Importance of Developing Efficient Algorithms

  • Resource Constraints: Efficiency is an essential consideration in algorithm design. A solution is categorized as efficient only if it can solve a problem within the specific resource constraints required by the system or environment.

  • Comparative Efficiency: Because a single problem can often be solved by multiple different algorithms, computer scientists must determine which algorithm is the most efficient choice for a given scenario.

  • Example Case: Searching for data in an array can be accomplished via Sequential Search or Binary Search, each possessing different levels of efficiency.

Sequential Search vs. Binary Search Methodologies

  • Sequential Search Mechanism: This algorithm begins at the first position of an array and examines each subsequent value in order until the target item is located or the end of the array is reached.

  • Binary Search Mechanism: This algorithm is used for locating the position of an element within a sorted list.

    • Initially, the algorithm compares the target variable xx with the middle item of the array.

    • If xx equals the middle item, the search is complete.

    • If xx is smaller than the middle item, it must reside in the first half of the array (if present), and the procedure repeats on only that first half.

    • If xx is larger than the middle item, the search is repeated on the second half of the array.

    • The process continues until xx is found or it is determined that xx is not present in the array.

  • Search Reduction Strategy: Binary search reduces the number of elements that need to be examined by half (two-way division) during each step of the process.

Quantitative Efficiency Comparison: Search Algorithms

  • Sequential Search Performance:

    • To determine that an item xx is not in an array of size nn, the algorithm must perform exactly nn comparisons.

    • If xx is present, the number of comparisons is less than or equal to nn.

  • Worst-Case Comparison Table: The following data demonstrates the number of comparisons required for the worst-case scenario (where the item is not found or is at the end of the search path).

Array Size (nn)

Sequential Search Comparisons

Binary Search Comparisons

3232

3232

66

6464

6464

77

128128

128128

88

1,0241,024

1,0241,024

1111

1,048,5761,048,576

1,048,5761,048,576

2121

General Case (nn)

nn

log2(n)+1\log_{2}(n) + 1

  • Inference: Binary Search is significantly more efficient than Sequential Search based on the drastically lower number of comparisons required as the array size increases.

Recursive vs. Iterative Fibonacci Implementations

  • Fibonacci Sequence Definition: The sequence is defined as 0,1,1,2,3,5,8,0, 1, 1, 2, 3, 5, 8, \dots

  • Mathematical Formula:

    • Fib(n)=Fib(n1)+Fib(n2)\text{Fib}(n) = \text{Fib}(n-1) + \text{Fib}(n-2) for n2n \geq 2

    • Base cases: Fib(0)=0\text{Fib}(0) = 0 and Fib(1)=1\text{Fib}(1) = 1

  • Recursive Algorithm Approach:

    • An integer function fib(int n) checks if n1n \leq 1. If true, it returns nn.

    • Otherwise, it returns the sum of fib(n-1) and fib(n-2).

  • Iterative Algorithm Approach:

    • An integer function fib2(int n) uses an index ii and an array f[0..n]f[0..n].

    • It initializes f[0]=0f[0] = 0.

    • If n > 0, it sets f[1]=1f[1] = 1 and uses a for loop starting at i=2i = 2 up to nn to calculate f[i]=f[i1]+f[i2]f[i] = f[i-1] + f[i-2].

    • Finally, it returns the value stored at f[n]f[n].

Quantitative Efficiency Comparison: Fibonacci

  • Computation Statistics: The following data compares the total number of operations/computations performed by the Recursive and Iterative approaches.

nn

Recursive Computations

Iterative Computations

00

11

11

11

11

22

22

33

33

33

55

44

44

99

55

4040

> 1,048,576

4141

General Case (nn)

> 2^{n/2}

n+1n + 1

  • Observation: The Iterative method is vastly more efficient than the Recursive method for calculating Fibonacci numbers as nn increases, specifically avoiding the exponential computation growth seen in the recursive version.