0% found this document useful (0 votes)
4 views

algo note

An algorithm is a step-by-step procedure for solving a specific problem, similar to a recipe. Asymptotic notation is used to analyze algorithm efficiency, with Big O notation describing the upper bound of running time. The document also discusses various sorting algorithms and graph concepts, including time complexity for each algorithm and definitions of paths, cycles, and minimum spanning trees.

Uploaded by

haranbasak0001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

algo note

An algorithm is a step-by-step procedure for solving a specific problem, similar to a recipe. Asymptotic notation is used to analyze algorithm efficiency, with Big O notation describing the upper bound of running time. The document also discusses various sorting algorithms and graph concepts, including time complexity for each algorithm and definitions of paths, cycles, and minimum spanning trees.

Uploaded by

haranbasak0001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

what is algorithm Worst Case 5.

Recursively apply Merge Sort to the le and


An algorithm is essen ally a step-by-step procedure or This describes the scenario where the algorithm takes right halves.
a set of rules designed to solve a specific problem or the most amount of me or space to complete its task. 6. Merge the two sorted halves into a single
perform a par cular task. It's like a recipe that guides It represents the least favorable situa on for the sorted array.
you through the process of achieving a desired algorithm. In the worst case, QuickSort has a me 7. Return the sorted array.
outcome. Algorithms are used in a wide variety of complexity of O(n^2), which happens when the pivot 8. Stop.
fields, from computer science to mathema cs and element is always the smallest or largest element in Time Complexity:
even in our everyday lives. the array.  Best Case: O(n log n)
Example: Linear Search
what is asympto c nota on  Average Case: O(n log n)
Consider a linear search algorithm that looks for a
Asympto c nota on is a way to describe the behavior specific element in an array.  Worst Case: O(n log n)
of func ons as they tend toward large input sizes, Best Case: O(1) – The element is found at the first c) Bubble Sort
o en used to analyze the efficiency of algorithms. It's posi on of the array. Bubble Sort repeatedly swaps adjacent elements if
like a mathema cal shorthand to compare how fast Average Case: O(n/2) – The element is found they are in the wrong order.
algorithms grow or shrink. somewhere in the middle of the array, but for Big O Algorithm:
There are three main types of asympto c nota on: nota on, this is simplified to O(n). 1. Start.
Big O Nota on (O): Describes the upper bound of an Worst Case: O(n) – The element is found at the last 2. Take an array as input.
algorithm's running me. It gives the worst-case posi on of the array or not found at all, requiring the 3. For i=0i = 0i=0 to size−1size - 1size−1:
scenario. For example, if an algorithm has a me en re array to be searched. For j=0j = 0j=0 to size−i−2size - i - 2size−i−2:
complexity of O(n), it means that in the worst case, the If array[j]>array[j+1]array[j] >
running me grows linearly with the size of the input, describe divide and conquer algo array[j+1]array[j]>array[j+1],
n. Divide and conquer is a powerful algorithmic swap them.
Omega Nota on (Ω): Describes the lower bound of an technique used to solve complex problems by breaking 4. Repeat un l the en re array is sorted.
algorithm's running me. It provides the best-case them down into more manageable sub-problems, 5. Return the sorted array.
scenario. For example, if an algorithm has a me solving each sub-problem individually, and then 6. Stop.
complexity of Ω(n), it means that the running me combining their solu ons to form the final solu on. Time Complexity:
grows at least linearly with the size of the input, n. Here’s a more detailed look at how it works:  Best Case: O(n)
Theta Nota on (Θ): Describes the exact bound of an Steps in Divide and Conquer  Average Case: O(n^2)
algorithm's running me. It provides a ght bound, Divide: Split the main problem into smaller,
meaning the running me grows exactly at this rate in  Worst Case: O(n^2)
independent sub-problems. This step involves breaking
the average case. For example, if an algorithm has a d) Selec on Sort
down the problem into parts that are similar in nature
me complexity of Θ(n), the running me grows Selec on Sort repeatedly selects the smallest element
to the original problem but smaller in size.
linearly with the input size, n. from the unsorted part and places it in its correct
Conquer: Solve each of the sub-problems
posi on.
independently. If the sub-problems are s ll too large,
Time Complexity Algorithm:
they are divided further using the same divide and
Time complexity refers to how the running me of an 1. Start.
conquer approach un l they become simple enough to
algorithm changes with respect to the size of the input. 2. Take an array as input.
solve directly.
It gives us an idea of how fast an algorithm runs. It is 3. For i=0i = 0i=0 to size−1size - 1size−1:
Combine: Merge the solu ons of the sub-problems to
o en expressed using Big O nota on, which describes o Set min_index=imin\_index =
get the solu on to the original problem. This step
the upper bound of the running me. For example: imin_index=i.
typically involves combining the solu ons in a way that
O(1): Constant me – the running me does not o For j=i+1j = i + 1j=i+1 to
addresses the main problem's requirements.
change with the size of the input. size−1size - 1size−1:
Example : marge short, quick short .
O(n): Linear me – the running me increases linearly Ifarray[j]<array[min_index]array[
with the input size. a) Quick Sort j] <
O(n^2): Quadra c me – the running me increases Quick Sort is a divide-and-conquer algorithm. array[min\_index]array[j]<array[
quadra cally with the input size. Algorithm: min_index], update
Space Complexity 1. Start. min_index=jmin\_index =
Space complexity refers to how much memory an 2. Take an array as input. jmin_index=j.
algorithm needs to run to comple on. It also uses Big 3. If the array has 0 or 1 elements, it is already o Swap array[i]array[i]array[i] and
O nota on to describe the upper bound of the sorted (base case). array[min_index]array[min\_ind
memory usage. For example: 4. Choose a pivot element (e.g., last element, ex]array[min_index].
O(1): Constant space – the algorithm uses a fixed random element, or median). 4. Return the sorted array.
amount of memory regardless of the input size. 5. Par on the array into two subarrays: 5. Stop.
O(n): Linear space – the memory usage grows linearly o Le subarray contains elements Time Complexity:
with the input size. ≤ pivot.  Best Case: O(n^2)
O(n^2): Quadra c space – the memory usage grows o Right subarray contains  Average Case: O(n^2)
quadra cally with the input size. elements > pivot.
 Worst Case: O(n^2)
6. Recursively apply Quick Sort to the le and
Best Case right subarrays.
This is the scenario where the algorithm performs its 7. Combine the sorted subarrays and the
task in the least amount of me or space possible. It pivot.
represents the most favorable situa on for the 8. Stop.
algorithm. For example, in the case of a sor ng Time Complexity:
algorithm like QuickSort, the best case occurs when  Best Case: O(n log n)
the pivot element splits the array into two equal halves
consistently.
 Average Case: O(n log n)
Average Case  Worst Case: O(n^2)
This represents the expected performance of the
algorithm over a typical set of inputs. It's an average b) Merge Sort
measure of efficiency when considering all possible Merge Sort splits the array, sorts each half, and merges
inputs. This is o en what we care about the most in them.
prac cal applica ons, as it gives a realis c expecta on Algorithm:
of how the algorithm will perform. For example, the 1. Start.
average-case me complexity of Quick Sort is O(n log 2. Take an array as input.
n). 3. If the array has 0 or 1 elements, it is already
sorted (base case).
4. Divide the array into two halves.
Path: A path in a graph is a sequence of ver ces 4. Repeat un l the MST includes exactly V-1
connected by edges, where each vertex is dis nct edges (where V is the number of ver ces).
(except the star ng and ending ver ces in the case of a
cycle). It represents a way to travel from one vertex to Numerical Example of Kruskal's Algorithm:
another. Let's consider the same graph with edges:

Cycle: A cycle is a path that starts and ends at the


same vertex, with no other repeated ver ces or edges.
In other words, a cycle is a closed loop in the graph.

Simple Graph : A simple graph is a type of graph in


which: 1. Sort edges by weight: A-B (1), B-C (1), C-D
There are no loops (edges that connect a vertex to (2), A-C (3), C-E (4), D-E (5), B-D (6).
itself). 2. Add edge A - B (1).
There are no mul ple edges (more than one edge 3. Add edge B - C (1).
connec ng the same pair of ver ces). 4. Add edge C - D (2).
5. Add edge C - E (4).
Connected Graph: A connected graph is a type of Both Prim's and Kruskal's algorithms produce the same
graph in which there is a path between any two MST for this graph:
ver ces. In other words, it's possible to travel from any
vertex to any other vertex within the graph.

Hashing :
Hashing is a technique used to uniquely iden fy a
specific object from a group of similar objects. It
involves conver ng an input (or 'key') into a fixed-size Time Complexity
string of bytes, typically by using a hash func on. This Minimum Spanning Tree (MST)
A Minimum Spanning Tree (MST) is a subset of edges
 Prim's Algorithm: O(E log V) using a priority
output, known as a hash code or hash value, is queue (where E is the number of edges and
typically a numeric string. Hashing is widely used in in a weighted, connected graph that connects all the
V is the number of ver ces).
various applica ons, especially in computer science. ver ces together without any cycles and with the
minimum possible total edge weight. MSTs are used in  Kruskal's Algorithm: O(E log E) due to
various applica ons, such as network design, sor ng edges, which simplifies to O(E log V)
defina on of Directed Acyclic Graph (DAG) :
clustering, and more. because E is at most V^2.
A Directed Acyclic Graph (DAG) is a graph that is both
directed and acyclic. Here's what that means: Prim's Algorithm
1. Directed: The edges between the ver ces Prim's Algorithm is a greedy algorithm that constructs
have a direc on, indica ng the rela onship an MST by star ng from a single vertex and adding the
flows in one direc on. For example, if smallest edge that connects a vertex in the MST to a
there's an edge from vertex A to vertex B, vertex outside the MST.
you can go from A to B, but not from B to A. Steps:
2. Acyclic: The graph contains no cycles. This 1. Ini alize the MST with a single vertex
means that you cannot start at a vertex, (usually the star ng vertex).
follow a sequence of edges, and return to 2. While there are ver ces not included in the
the same vertex. MST:
Example o Select the smallest edge that
Consider this simple DAG: connects a vertex in the MST to
a vertex outside the MST.
o Add the selected edge and
vertex to the MST.
3. Repeat un l all ver ces are included in the
MST.
In this graph: Numerical Example of Prim's Algorithm:
 There are directed edges from A to B, A to Let's consider a graph with ver ces A, B, C, D, E and
C, B to D, and B to E. the following edges:
 There are no cycles, meaning there's no
way to start at any vertex and return to it by
following the directed edges.

Star ng from vertex A:


1. Add edge A - B (1).
2. Add edge B - C (1).
3. Add edge C - D (2).
4. Add edge C - E (4).
Kruskal's Algorithm
Kruskal's Algorithm is another greedy algorithm that
constructs an MST by selec ng edges in increasing
order of weight, ensuring no cycles are formed.
Steps:
1. Sort all the edges in non-decreasing order
of their weights.
2. Ini alize the MST with no edges.
3. For each edge in the sorted list:
o If adding the edge does not form
a cycle, add it to the MST.

You might also like