• Tidak ada hasil yang ditemukan

PDF Data structure and algorithm in Python - Recursion

N/A
N/A
Protected

Academic year: 2023

Membagikan "PDF Data structure and algorithm in Python - Recursion"

Copied!
116
0
0

Teks penuh

One way to describe repetition in a computer program is to use loops, such as Python's while-loop and for-loop constructs. Recursion is a technique by which a function makes one or more calls to itself during runtime, or by which a data structure relies on smaller instances of the same type of structure in its representation. When one function call makes a recursive call, that call is suspended until the recursive call completes.

The Factorial Function

In Python, every time a function is called, a structure known as a trigger record or frame is created to store information about the progress of that function call. This trigger record contains a namespace to store the parameters of the function call and local variables, and information about which command in the body of the function is currently executing. When the execution of a function results in a nested function call, the execution of the previous call is suspended and the trigger record stores the place in the source code where the control flow should continue after the return of the nested call.

This process is used both in the standard case of a function calling another function or in the recursive case where a function calls itself.

The Factorial Function: Algorithm Analysis

English Ruler

English Ruler I

English Ruler: Algorithm Analysis

Binary Search

When the sequence is unsorted, the standard way to search for a target value is to use a loop to examine each element until you either find the target or exhaust the data set.

Binary Search I

Binary Search: Algorithm Analysis

File Systems

It is equal to the immediate disk space used by the entry plus the sum of the cumulative disk space usage of each entry that is stored directly within the entry.

Python’s os Module

File Systems: Algorithm Analysis

Poor Implementation of Recursion

Element uniqueness problem

For n≥2, the elements are unique if and only if the first−1 elements are unique, the last−1 elements are unique, and the first and last elements are different. Ifn=1, the runtime of unike3isO(1), since there are no recursive calls for this case.

Computing Fibonacci Numbers

Computing Fibonacci Numbers: An Inefficient Recursion

Computing Fibonacci Numbers: An Efficient Recursion

Maximum Recursive Depth in Python

To combat infinite recursions, Python's designers made a deliberate decision to limit the total number of function activations that can be simultaneously active. The exact value of this limit depends on the Python distribution, but a typical default value is 1000. If this limit is reached, the Python interpreter raises aRuntimeError with a message Maximum recursion depth exceeded.

Further Examples of Recursion

If a recursive function is designed so that each call to the body makes at most one new recursive call, it is known as linear recursion. This includes a case analysis with two branches that lead to recursive calls, but only one of those calls can be reached during a particular execution of the body.

Linear Recursion

Linear Recursion: Summing the Elements of a Sequence

Linear Recursion: Reversing a Sequence

Reverse the n elements of a sequence S, so that the first element becomes the last, the second element becomes the second to last, and so on. Returning a sequence can be achieved by swapping the first and last elements and then recursively returning the remaining elements. When we call recursion, we are guaranteed to make progress toward a base case, since the change-stop-start is decremented by two with each call.

The above argument implies that the recursive algorithm is guaranteed to terminate after a total of 1+⌊n2⌋ recursive calls.

Linear Recursion: Computing Powers

We will consider two different recursive formulations for the problem that lead to algorithms with very different performance.

Binary Recursion

With two or more elements, we can recursively calculate the sum of the first half and the sum of the second half and add these sums together. The size of the array is halved at each recursive call, and so the depth of the recursion is 1+log2n,.

Multiple Recursion

Designing Recursive Algorithms

Parameterizing a Recursion

If we had insisted on binary_search(data, target), the only way to perform a search on half the list would have been to create a new list instance with only those elements that should be the first parameter are sent. However, making a copy of half the list would already take O(n)time, negating the whole advantage of the binary search algorithm. If we want to provide a cleaner public interface for an algorithm like binary search, without bothering a user with the extra parameters, a standard technique is to make one public use function with the cleaner interface, like binary_search(data, target), and then by having its body call a non-public utility function with the desired recursive parameters.

Eliminating Tail Recursion

In particular, the Python interpreter must maintain activation records that track the state of each nested call. When computer memory is limited, it is useful in some cases to be able to derive non-recursive algorithms from recursive ones. In general, we can use the data stack structure to convert a recursive algorithm to a non-recursive algorithm by managing the nesting of the recursive structure ourselves, instead of relying on an interpreter.

Although this only moves memory usage from the interpreter to our stack, we may be able to reduce memory usage by storing only the minimum necessary information. A recursion is atail recursion if any recursive call made from one context is the very last operation in that context, with the return value of the recursive call (if any) immediately returned by the enclosing recursion. Any tail recursion can be reimplemented nonrecursively by wrapping the body in a loop for iteration and replacing a recursive call with new parameters with a reassignment of the existing parameters to those values.

Referensi

Dokumen terkait

Judul yang penulis ajukan adalah “ANALISIS KINERJA KEUANGAN DENGAN MENGGUNAKAN RATIO RETURN ON ASSET (ROA) DAN RETURN ON EQUITY (ROE) PADA PT.BANK RAKYAT