Algorithm
Algorithm
Examples:
● Binary Search
● Quicksort
● Mergesort
● Counting Inversion
Examples:
● Fibonacci
● Factorial
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
Target = 33
Example of Binary Search
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
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
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
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.
● Base condition is needed to stop the recursion otherwise infinite loop will
occur.
Example of Recursion
Sum of Natural Numbers Using Recursion