DAA: Dynamic Programming

Dynamic Programming

Introduction

The technique of breaking a problem statement into subproblems and using the optimal result of subproblems as an optimal result of the problem statement is known as dynamic programming. This technique uses an optimized approach and results in optimal solutions.

For example, let us see the Fibonacci sequence (A sequence in which the sum of two numbers is the third number in the sequence).

0 1 1 2 3 5 ……

This can be solved using the equation:

Fibonacci (n) = Fibonacci (n-1) + Fibonacci (n-2)

Thus the problem statement is broken down into subproblems (n-1) AND (n-2), which gives the optimal result, the next number in the sequence. Hence, it is a good example of dynamic programming.

The main contrast between divide and conquer and dynamic programming is in DP, there can be overlapping subproblems, but in the case of D and C, the subproblems are independent.

Dynamic programming cannot solve all the problems. There are several algorithms that can be solved using greedy and divide and conquer techniques. The difference between recursion and DP recursion is memoization in DP. If the subproblem does not require memorization, in any case, DP cannot solve that problem.

Major components in Dynamic programming: The Following are components of dynamic programming:

  1. Recursion - To solve the problems recursively.
  2. Memoization – To store the precomputation of subproblems to use further.

DP = RECURSION + MEMOIZATION

Properties of dynamic programming strategy

There are two strategies that show whether a given problem can be done with DP or not are:

  1. Overlapping subproblems - A recursive solution that contains a small recursive solution for subproblems.
  2. Optimal substructure - An optimized solution to a problem that has optimal subproblems.

Approaches of dynamic programming: Following are the basic approaches of DP -

  1. Top-Down Approach: In this approach, the problem is broken into subproblems, and the result at each sub problem is remembered. When a result at subproblem is needed again further, it is first checked in the memory stage and then used.
  2. Bottom-Up Approach: This approach follows the idea of computing first and moves backward. It is done to avoid the process of recursion. The smaller inputs at each step in the bottom-up approach contribute to form larger ones.

Examples of DP:

  1. 0-1 Knapsack problem
  2. Traveling salesman problem
  3. Chain Matrix multiplication
  4. String algorithms – Longest common subsequence, Longest increasing sequence etc.
  5. Optimized graph algorithms and many more.

Related Topics

DAA: Density of a Binary Tree Algorithm

The Density of a Binary Tree Algorithm The density of a binary tree is defined as the ratio of the tree’s size to the tree’s height.  The height of the tree is...

2 minutes read.

Invert Binary Tree in DAA

Invert Binary Tree: A binary tree is a tree in which each node of the tree contains two children, i.e., left children and right children. Let us suppose we have...

2 minutes read.

DAA: Find the Height or Maximum Depth of a Binary Tree

Find the Height or Maximum Depth of a Binary Tree We have a binary tree structure and we need to find its height. It is defined by the distance from the...

3 minutes read.

DAA: Bottom view of a Binary Tree

Bottom view of a Binary Tree The bottom view of a binary tree is the number of nodes visible when viewed from the bottom. At every horizontal distance, there would be...

3 minutes read.

DAA: Bubble Sort Algorithm on Linked List

Bubble Sort Algorithm on Linked List In this article, we will sort a Link List using the bubble sort technique. Example: Input : 20->30->40->10 Output :10->20->30->40 Input : 20->4->3 Output : 3->4->20 Sorting Technique The bubble sort technique...

4 minutes read.

Symmetric Trees in DAA

Symmetric Trees The trees that are mirror images of themselves are known as symmetric trees. Look at the following tree image below: The tree is symmetric as the left subtree is the mirror...

4 minutes read.

DAA: Construct a Tree from Inorder and Preorder Traversals

Construct a Tree from Inorder and Preorder Traversals We are given inorder and preorder traversals of a tree. We need to generate a tree from these traversals. Example: Inorder[]   = { 3, 1,...

4 minutes read.

Introduction to Sorting in DAA

DAA: What is Sorting? The technique in which a data structure is rearranged in decreasing order, increasing order, or in a specified order is called sorting. We apply to sort in our...

4 minutes read.

DAA: Dijkstra’s Algorithm (Shortest Path)

Dijkstra’s Algorithm (Shortest Path) Dijkstra’s algorithm finds the shortest distance from a source to all the vertices in a graph. This algorithm is used in network protocols like IS-IS and OSPF(Open...

3 minutes read.

DAA: Breadth First Search (BFS) for a Graph

Breadth First Search (Bfs) For A Graph The algorithm in which all the graph nodes are traversed is known as the breadth-first search algorithm. In this algorithm, we select one node,...

5 minutes read.

DAA: Bead Sort Algorithm

Bead Sort Algorithm The bead sort is also known as the gravity sort algorithm. The algorithm is based on the natural phenomena of gravity. The phenomenon is the falling of things...

3 minutes read.

DAA: Continuous Tree

Continuous Tree A continuous tree is the one in which the nodes from root to leaf path, the two adjacent node values, have a difference of 1. Input :          3                     /   \                   ...

5 minutes read.

DAA: Bubble Sort Algorithm

Bubble Sort Algorithm The bubble sort algorithm is also known as the sinking algorithm. In this algorithm, we iterate over the array, and it takes two adjacent elements and swaps them...

3 minutes read.

DAA: Insertion Sort Algorithm on Singly Link List

Insertion Sort Algorithm on Singly Link List We will sort a singly link list using the bubble sort technique. Example: Input : 20->30->40->10 Output :10->20->30->40 Input : 20->4->3 Output : 3->4->20 Sorting Technique The insertion sort technique works...

3 minutes read.

DAA: Algorithm of Right View of a Binary Tree

Algorithm of Right View of a Binary Tree The right view of a binary tree is the visible nodes from the right side of the tree. In the given tree, the visible...

5 minutes read.

DAA: Algorithm to Find the Maximum Width of a Tree

Algorithm to Find the Maximum Width of a Tree The width of a binary tree is defined as the maximum number of nodes at a given level. The level having the...

5 minutes read.

DAA: Insert a node in Binary Search Tree

Insert a node in Binary Search Tree (BST) We have a Binary search tree and a key. Insert the key in the binary search tree if not present. In the above figure,...

4 minutes read.

DAA: Binary Tree and its Categories

Binary Tree and its Categories The binary tree is a non-linear data structure in which there are 0 or utmost 2 nodes.  Each node has two children, i.e., left and right...

4 minutes read.

DAA: KMP Algorithm

KMP ALGORITHM The KMP algorithm is abbreviated as the "Knuth Morris Pratt” algorithm. This algorithm was developed by all of them.  This algorithm searches a pattern of length m in a string...

10 minutes read.

DAA: Depth-First Search Algorithm

Depth-first search: DFS is a traversing algorithm of a graph or tree in which one node is taken as arbitrary, and with the help of that arbitrary node, all its...

6 minutes read.