Algorithm Design
Techniques
This lesson explores essential techniques used in crafting effective
algorithms. We'll cover iterative techniques, divide and conquer, dynamic
programming, and greedy algorithms, delving into their principles, strengths,
and illustrative examples.
Iterative Techniques
Iterative Techniques Strengths
Iterative techniques involve repeating a set of instructions until Iterative techniques are often simple and straightforward to
a condition is met. This approach is often used for tasks implement. Their step-by-step approach can be easily
involving repetition and iteration. understood and debugged.
Examples:
• Looping through a list to find a specific element.
• Calculating the factorial of a number.
Divide and Conquer
Divide
1 Break down a large problem into smaller subproblems of the same type.
Conquer
2
Solve the subproblems recursively.
Combine
3 Merge the solutions of the subproblems into a solution for the
original problem.
Dynamic Programming
Overlapping Subproblems
1
Identify subproblems that are solved multiple times.
Optimal Substructure
2 The optimal solution to the problem can be constructed from optimal solutions of its
subproblems.
Memoization or Tabulation
3
Store and reuse solutions of previously solved subproblems.
Greedy Algorithms
Local Optimality
At each step, choose the best option available without looking
ahead.
Global Optimality
Hope that a series of locally optimal choices lead to a globally
optimal solution.
Not Always Guaranteed
Greedy algorithms may not always find the optimal solution,
but they are often efficient and provide a good approximation.
Illustrations for Iterative
Techniques
Finding the Maximum
A simple iterative algorithm to find the largest element in a list: loop
through each element, compare it to the current maximum, and update
the maximum if necessary.
Calculating Factorial
An iterative algorithm for calculating the factorial of a number: start with
1, multiply it by the number, then decrement the number and repeat until
reaching 1.
Illustrations for Divide and
Conquer
1 Merge Sort
Recursively divides a list into halves, sorts each half, and
merges the sorted halves.
2 Quick Sort
Picks an element as a pivot, partitions the list around the
pivot, and recursively sorts the partitions.
Illustrations for Dynamic
Programming and Greedy
Algorithms
Coin Change Shortest Path
Dynamic programming approach to A greedy approach to finding the
find the minimum number of coins shortest path in a graph, using
needed to make a given amount, Dijkstra's algorithm to iteratively
using memoization to store select the edge with the smallest
solutions for subproblems. weight.