ALGO++: Recursion & Backtracking
ALGO++: Recursion & Backtracking
ALGO++: Recursion & Backtracking
Recursion &
Backtracking
ALGO++
Recursion & Backtracking
Problems
Prateek Narang
Problem Solving Techniques
Brute Force/ Complete Search
Greedy Methods
Divide and Conquer
Dynamic Programming
ALGO++ 2
Brute Force Search
Linear Search
Finding the largest/smallest number in an array
Finding a pair in an array with sum as K
Substrings of an array
Subsequences of an array
N-Queen using recursion
Sudoku Solver using Recursion
Rat in a Maze using Recursion
ALGO++ 3
Divide and Conquer
Problems have dont have overlapping subproblems property.
Divide the original problem into subproblems.
Solve these subproblems.
If needed, combine the sub-solutions to get complete solution.
Examples-
Binary Search
Merge Sort
Quick Sort
Inversion Count
Fast Power
Square Root of Number
ALGO++ 4
Greedy Strategy
- Greedy Algorithms make the choice that looks
best at the moment.
- You hope that by choosing a local optimum at
each step, you will end at a global optimum.
Examples -
Counting Money Greedy (Indian Currency)
Ith Largest Element in sorted array
Activity Selection Problems
Fractional Knapsack
Load Balancing
Some Graph Algorithms
ALGO++ 5
Dynamic Programming
Problems having an optimal substructure and
overlapping subproblems.
Fibonacci Problem
Ladders Problem
Knapsack Problem
And much more
ALGO++ 6
Lets start
Pre-requisities
Variables
Data types
Loops & Conditional Statements
Functions
Pointers
Call by value & reference
Basic algorithms searching, sorting, merging
etc.
ALGO++ 7
Reference Variables
Call by reference !
ALGO++ 8
Static vs Dynamic Memory
ALGO++ 9
Never Return Address of
Local Variable !
ALGO++ 10
Call Stack!
ALGO++
12
ALGO++
13
ALGO++
14
What is Recursion?
ALGO++
Parts of Recursive Algorithm
Base Case (i.e., when to stop)
Work toward Base Case
Recursive Call (i.e., call ourselves)
ALGO++
Problems
Fibonacci Number
ALGO++ 16
Problems
Replace PI
Tower of Hanoi
Merge Sort
ALGO++ 17
Problems
Subsequences of a String
ALGO++ 18
June 11,
192017
ALGO++
June 11, 2017
ALGO++
June 11, 2017
STL
Vectors
StringClass
Sorting & Comparators
ALGO++
N-Queen Problem
ALGO++ 22
Permutations of a String
ALGO++ 23
Sudoku Solver
Create a Sudoku Solver and Checker
ALGO++ 24
Sample Input
int mat[9][9] =
{{5,3,0,0,7,0,0,0,0},
{6,0,0,1,9,5,0,0,0},
{0,9,8,0,0,0,0,6,0},
{8,0,0,0,6,0,0,0,3},
{4,0,0,8,0,3,0,0,1},
{7,0,0,0,2,0,0,0,6},
{0,6,0,0,0,0,2,8,0},
{0,0,0,4,1,9,0,0,5},
{0,0,0,0,8,0,0,7,9}};
ALGO++ 25
Rat in a Maze
ALGO++ 26
Phone Keypad
ALGO++ 27
HomeWork
Read and Implement QuickSort
ALGO++ 28
Thank you
ALGO++
Prateek Narang