Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

Alogrithms

Course Code: CSE125


Course Name: Discrete Mathematics

Rishad Amin Pulok


Lecturer, Dept. of CSE
What is an Algorithm?
● An algorithm is a set of step-by-step procedures, or a set of rules that
takes some value, or set of values, as input and produces some value, or
set of values, as output. Thus it can be called a sequence of computational
steps that transform input into output.

● In other words, An algorithm is a set of step-by-step procedures, or a set


of rules to follow, for completing a specific task or solving a particular
problem
Example of Algorithm
Algorithm Characteristics
Algorithm Types
Algorithms are classified based on the concepts that they use to accomplish a task.
Algorithm Types Cont.
● Divide and conquer algorithms – divide the problem into smaller
subproblems of the same type; solve those smaller problems, and combine
those solutions to solve the original problem.

Examples:
● Binary Search
● Quicksort
● Mergesort
● Counting Inversion

● Brute force algorithms – try all possible solutions until a satisfactory


solution is found. It is the simplest and most basic algorithm.
Algorithm Types Cont.
● Recursive algorithms – A problem is resolved by breaking it down into
subproblems of a similar nature and repeating the process over and over
until it is resolved with the help of a base condition. Recursion is the key to
this type of algorithm.

Examples:
● Fibonacci
● Factorial

● Backtracking algorithms – divide the problem into subproblems, each


which can be attempted to be solved; however, if the desired solution is not
reached, move backwards in the problem until a path is found that moves it
forward.
Algorithm Types Cont.
● Greedy algorithms – find an optimal solution at the local level with the
intent of finding an optimal solution for the whole problem.

Examples:
● Dijkstra’s Single Source Shortest Path
● Minimum Spanning Tree – Prim & Kruskal
● Fractional Knapsack Problem
● Huffman Coding
Algorithm Types Cont.
● Dynamic programming algorithms – break a complex problem into a
collection of simpler subproblems, then solve each of those subproblems
only once, storing their solution for future use instead of re-computing
their solutions.

Example:
● 0-1 Knapsack
● Longest Common Subsequence
● Longest Increasing Sequence
● Sum of Subset
● Warshall’s All pairs shortest path
● Bellman Ford’s Single Source Shortest Path
● Matrix Chain Multiplication
Algorithm Complexity
● Worst Case Complexity: the function defined by the maximum number
of steps taken on any instance of size n

● Best Case Complexity: the function defined by the minimum number of


steps taken on any instance of size n

● Average Case Complexity: the function defined by the average number


of steps taken on any instance of size n
Complexity Graph
Doing the Analysis
● It’s hard to estimate the running time exactly
–Best case depends on the input
–Average case is difficult to compute
–So we usually focus on worst case analysis
❏ Easier to compute
❏ Usually close to the actual running time

● Strategy: find a function (an equation) that, for large n, is an upper


bound to the actual function (actual number of steps, memory usage,
etc.)
Asymptotic Notation

Mathematical way of representing the Time Complexity of an algorithm

● Big-Oh — O() — Upper Bound/Worst Case


● Big-Omega — Ω() — Lower Bound/Best Case
● Big-Theta — Θ() — Average/Tight Bound/Average Case
Binary Search
What is Binary Search?
● A Searching algorithm to find the
target element
● Called Binary Search as it divides the
input array into two halves as part of
the algorithm
● Follows Divide and Conquer rule
Conditions

Alert !!! What are needed to perform


●Check if the input array is sorted Binary Search?
or not ● Lowest and Highest index value
●Check if there is a way to
● Mid index value (to divide the
compare any two values of the
array into two halves)
input array
● Mid = (Low + High)/2
Steps to Perform Binary Search
● Calculate Mid value using Mid = (Low + High)/2
Where, Low = 0, High = n - 1
● Compare array[Mid] with target value
○If array[mid] = target Congrats! Target value is found
○If array[mid] > target Low = Low, High = Mid - 1
○If array[mid] < target Low = Mid + 1, High = High
○If none Item is not available in the input array
Example of Binary Search
Given array R[15]

Target = 33
Example of Binary Search

Step 01: Target = 33


Low = 0 High = 15
Mid = (0+15) / 2 = 7

R[mid] = R[7] = 53 > Target

So,
Low = 0
High = Mid - 1 = 7-1 = 6
Example of Binary Search Target = 33

Step 02:
Low = 0 High = 6

Mid = (0+6) / 2 = 3

R[mid] = R[3] = 25 < Target

So,
Low = Mid + 1 = 3 + 1 = 4
High = 6
Example of Binary Search Target = 33

Step 03:

Low = 4 High = 6

Mid = (4+6) / 2 = 5

R[mid] = R[5] = 43 > Target

So,
Low = 4
High = Mid - 1 = 5 - 1 = 4
Example of Binary Search Target = 33

Step 04:

Low = 4 High = 4

Mid = (4+4) / 2 = 4

R[mid] = R[5] = 33 == Target

Congratulations!
Item Found
Merge Sort
Recursive Algorithm
What is Recursion?
● The process in which a function calls itself directly or indirectly is called
recursion and the corresponding function is called a recursive function.
Using a recursive algorithm, certain problems can be solved quite easily.

● A recursive function solves a particular problem by calling a copy of itself


and solving smaller subproblems of the original problems. Many more
recursive calls can be generated as and when required.
Properties of Recursion
● Performing the same operations multiple times with different inputs.

● In every step, we try smaller inputs to make the problem smaller.

● Base condition is needed to stop the recursion otherwise infinite loop will
occur.
Example of Recursion
Sum of Natural Numbers Using Recursion

You might also like