Computer-Science

Analysis of Algorithms

1. Recursion

μš°λ¦¬λŠ” for λ°˜λ³΅λ¬Έμ΄λ‚˜ while 반볡문과 같은 λ°˜λ³΅λ¬Έμ„ μž‘μ„±ν•΄μ„œ λ°˜λ³΅μ„ κ΅¬ν˜„ν•  수 μžˆμŒμ„ λ³΄μ•˜λ‹€. λ°˜λ³΅μ„ μ–»λŠ” 또 λ‹€λ₯Έ 방법은 ν•¨μˆ˜κ°€ μžμ‹ μ˜ μ •μ˜ λ‚΄μ—μ„œ μžμ‹ μ„ ν˜ΈμΆœν•  λ•Œ λ°œμƒν•˜λŠ” μž¬κ·€(recursion)λ₯Ό ν†΅ν•΄μ„œμ΄λ‹€.

General Procedure

  1. Initialize the algorithm with a value to start with (e.g.,viaparameters).
  2. If the current value(s) being processed matches the base case, solve it and return the value.
  3. If not,redefine the answer in terms of a smaller or simpler sub-problem(s).
  4. Run the algorithm on the sub-problem.
  5. Combine the results in the formulation of th eanswer.
  6. Return the results.

Characteristic of Recursion

All recursive methods have the following characteristics:

Designing Recursive Functions

ex1> Counting

#include <iostream>
using namespace std;

int counting(int n, int val);

int main()
{
    int n, val = 0, res;
    cout << "Enter a positive integer : ";
    cin >> n;

    res = counting(n, val);
    cout << "The " << n << "th integer is " << res << endl;
}

int counting(int n, int val)
{
    if (n == 0)
    {
        cout << "Base case" << endl;
        return 0;
    }
    else
    {
        cout << n << endl;
        val = val + 1;
    }
    return counting(n - 1, val) + 1;
}

ex2> Factorial

Iterative

#include <iostream>
using namespace std;

int factorial(int n);

int main()
{
    int n = 5, res;
    res = factorial(n);
    cout << "Factorial of " << n << " is " << res << endl;

    return 0;
}

int factorial(int n)
{
    int fact = 1;
    for (int i = 1; i <= n; i++)
    {
        fact = fact * i;
    }
    return fact;
}

Recursive

IMG_243E92A1E3AD-1

// iteration & recursive factorial

#include <iostream>
using namespace std;

long long int iteration_factorial(unsigned long long int n)
{
    unsigned long long int value = 1;
    for (unsigned long long int i = 1; i <= n; i++)
    { // n번 반볡
        value = value * i;
    }
    return value;
}

unsigned long long int factorial(unsigned long long int n)
{ // recursive
    if (n <= 1)
    {
        return 1;
    }
    else
    {
        return n * factorial(n - 1);
    }
}

int main()
{
    unsigned long long int n = 5, result;
    result = iteration_factorial(n);
    // result = factorial(n);

    cout << result;
    return 0;
}

ex3> Fibonacci

Recursive

#include <iostream>
using namespace std;

int fibonacci(int n); // recursive fibonacci implementation

int main()
{
    int n;
    cout << "Enter an integer n for fibonacci(n): ";
    cin >> n;
    cout << "Fibonacci(" << n << ") = " << fibonacci(n) << endl;
}

int fibonacci(int n)
{
    if ((n == 0) || (n == 1))
    {
        return n;
    }
    else
    {
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
}

Iterative

#include <iostream>
using namespace std;

int fibonacci(int n); // iterative fibonacci implementation

int main()
{
    int n;
    cout << "Enter an integer n for fibonacci(n): ";
    cin >> n;
    cout << "Fibonacci(" << n << ") = " << fibonacci(n) << endl;
}

int fibonacci(int n)
{
    if (n == 0)
    {
        return 0;
    }
    int fib0 = 0;
    int fib1 = 1;

    for (int i = 2; i <= n; ++i)
    {
        int temp = fib1;
        fib1 = fib0 + fib1;
        fib0 = temp;
    }

    return fib1;
}

Downside of Recursion

IMG_734251D8B1E4-1

2. Analysis of Algorithms

Seven functions that often appear in algorithm analysis:

IMG_DDB67E86BA1E-1

Functions Graphed Using β€œNormal” Scale

IMG_7BC97DC6891E-1

Why Growth Rate Matters

IMG_F8748871339F-1

3. Big-Oh Notation

4. Exception Handling

#include <iostream>
using namespace std;

int main()
{
    try
    {
        int age;
        cout << "Enter your age: ";
        cin >> age;
        if (age >= 18)
        {
            cout << "Access granted - you are old enough.";
        }
        else
        {
            throw(age);
        }
    }
    catch (int myNum)
    {
        cout << "Access denied - You must be at least 18 years old.\n";
        cout << "Age is: " << myNum << endl;
    }
}