RO_(01)_Introduzione.mp4
Introduction to Algorithms and Complexity Analysis
What is an Algorithm?
An algorithm is a well-defined, step-by-step procedure or set of rules designed to solve a specific problem or perform a particular task. Algorithms are fundamental to computer science and are used in various applications, from simple calculations to complex artificial intelligence systems.
Finiteness: An algorithm must always terminate after a finite number of steps.
Definiteness: Each step of an algorithm must be precisely defined; the actions to be carried out must be rigorously and unambiguously specified for each case.
Input: An algorithm has zero or more inputs, taken from a specified set of objects.
Output: An algorithm has one or more outputs, which have a specified relation to the inputs.
Effectiveness: All operations to be performed must be sufficiently basic that they can be done exactly and in a finite length of time by a person using pencil and paper.
Example: Linear Search
Linear search is a simple searching algorithm that checks each element of a list sequentially until the desired element is found or the end of the list is reached.
\(-1\)
Complexity Analysis of Linear Search
Best Case: The target element is found at the beginning of the list. The algorithm performs only one comparison. The best-case time complexity is \(O(1)\).
Worst Case: The target element is at the end of the list or not present at all. The algorithm performs \(n\) comparisons. The worst-case time complexity is \(O(n)\).
Average Case: Assuming the target element is equally likely to be anywhere in the list, the average number of comparisons is \(\frac{n}{2}\). The average-case time complexity is \(O(n)\).
Example: Binary Search
Binary search is an efficient algorithm for finding a target value within a sorted list. It works by repeatedly dividing the search interval in half.
\(low \gets 0\) \(high \gets n - 1\) \(-1\)
Complexity Analysis of Binary Search
Best Case: The target element is found in the middle of the list in the first step. The best-case time complexity is \(O(1)\).
Worst Case: The target element is not in the list, or is at one of the ends. The search space is halved each time until it becomes empty. The worst-case time complexity is \(O(\log n)\).
Average Case: Similar to the worst case, the average number of comparisons required is logarithmic with respect to the number of elements. The average-case time complexity is \(O(\log n)\).
Example: Bubble Sort
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
Complexity Analysis of Bubble Sort
Best Case: The list is already sorted. The algorithm still performs \(n-1\) passes, but no swaps are made. The best-case time complexity is \(O(n^2)\), although it can be optimized to \(O(n)\) with a flag to check if any swaps were made in a pass.
Worst Case: The list is in reverse order. The algorithm performs the maximum number of comparisons and swaps. The worst-case time complexity is \(O(n^2)\).
Average Case: The average number of comparisons and swaps is proportional to \(n^2\). The average-case time complexity is \(O(n^2)\).
Example: Insertion Sort
Insertion Sort is a simple sorting algorithm that builds the final sorted array one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
Complexity Analysis of Insertion Sort
Best Case: The list is already sorted. For each element, the algorithm only makes one comparison and no shifts. The best-case time complexity is \(O(n)\).
Worst Case: The list is in reverse order. For each element, the algorithm needs to shift all previous elements. The worst-case time complexity is \(O(n^2)\).
Average Case: The average number of comparisons and shifts is proportional to \(n^2\). The average-case time complexity is \(O(n^2)\).