0% found this document useful (0 votes)
7 views

What is Dynamic Programming - Google Docs

Dynamic Programming (DP) is a problem-solving method that involves breaking problems into overlapping subproblems, solving each once, and storing results to avoid recomputation. Multistage graphs organize nodes into layers to find optimal paths with minimum costs, while the Floyd-Warshall algorithm computes shortest paths in weighted graphs. Optimal Binary Search Trees minimize search costs through balanced structures, and String Editing, or Edit Distance, involves transforming one string into another using operations like insertion, deletion, and substitution.

Uploaded by

Sukhwinder Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

What is Dynamic Programming - Google Docs

Dynamic Programming (DP) is a problem-solving method that involves breaking problems into overlapping subproblems, solving each once, and storing results to avoid recomputation. Multistage graphs organize nodes into layers to find optimal paths with minimum costs, while the Floyd-Warshall algorithm computes shortest paths in weighted graphs. Optimal Binary Search Trees minimize search costs through balanced structures, and String Editing, or Edit Distance, involves transforming one string into another using operations like insertion, deletion, and substitution.

Uploaded by

Sukhwinder Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

‭ hat is Dynamic Programming?


W
‭●‬ ‭Dynamic Programming‬‭is a method for solving problems‬‭by‬‭breaking them into‬
‭overlapping subproblems‬‭, solving each subproblem just‬‭once, and storing its result to avoid‬
‭recomputation.‬
‭Key Concepts of DP:‬
‭1.‬ ‭Optimal Substructure‬‭:‬
‭○‬ ‭A problem has‬‭optimal substructure‬‭if its optimal‬‭solution can be built from optimal‬
‭solutions of its subproblems.‬
‭2.‬ ‭Overlapping Subproblems‬‭:‬
‭○‬ ‭A problem exhibits‬‭overlapping subproblems‬‭if the‬‭same subproblem is solved multiple‬
‭times.‬
‭General method:-‬
‭Problem‬
‭|‬
‭V‬
‭Break into Subproblems (Overlapping)‬
‭|‬
‭V‬
‭Solve Subproblem and Store in Table‬
‭|‬
‭V‬
‭Combine Subproblem Solutions‬
‭|‬
‭V‬
‭Optimal Solution‬

‭ hat are Multistage Graphs?‬


W
‭●‬ ‭A‬‭multistage graph‬‭is a type of graph where the vertices‬‭(nodes) are organized into‬
‭multiple layers‬‭.‬
‭●‬ ‭In a multistage graph, each node connects only to the nodes in the next layer, representing a‬
‭sequential process or a pipeline‬‭.‬
‭●‬ ‭The goal is to find the optimal path through these nodes with the minimum cost.‬
‭●‬ ‭Often used in‬‭project scheduling, logistics, transportation‬‭planning‬‭, and‬‭process‬
‭optimization‬‭.‬
‭ dvantages of Using Multistage Graphs‬
A
‭1.‬ ‭Efficiency‬
‭●‬ ‭Reduces the complexity of sequential processes with‬‭dynamic programming‬‭techniques.‬
‭2.‬ ‭Scalability‬
‭●‬ ‭Easily scalable to more complex networks and processes.‬
‭3.‬ ‭Optimal Resource Allocation‬
‭●‬ ‭Helps in minimizing costs and optimizing resources across layers.‬
‭What is the Floyd-Warshall Algorithm?‬
‭The‬‭Floyd-Warshall algorithm‬‭is a fundamental algorithm‬‭in graph theory that finds the‬‭shortest‬
‭paths between every pair of vertices‬‭in a‬‭weighted‬‭graph‬‭(both directed and undirected).‬
‭It is particularly well-suited for‬‭dense graphs‬‭(graphs‬‭with a large number of edges). It handles‬
‭both‬‭positive and negative edge weights‬‭, making it‬‭very versatile.‬
‭ . Initialize the Distance Matrix‬
1
‭Create a 2-dimensional matrix‬‭ dist‬‭of size V×VV \times‬‭VV×V representing the shortest distance‬
‭between each pair of vertices.‬
‭For a graph with VVV vertices:‬
‭●‬ ‭Set dist[i][j]dist[i][j]dist[i][j] to the weight of the edge between iii and jjj.‬
‭●‬ ‭If there is no edge between iii and jjj, set dist[i][j]=∞dist[i][j] = \inftydist[i][j]=∞.‬
‭●‬ ‭For every vertex iii, set the diagonal element dist[i][i]=0dist[i][i] = 0dist[i][i]=0.‬
‭Initial Distance Matrix Example‬‭:‬
‭For a graph with 4 vertices A,B,C,DA, B, C, DA,B,C,D:‬
‭A ‭B ‭C ‭D
‭ . Iteratively Update the Distance Matrix‬
2
‭●‬ ‭The core of the Floyd-Warshall algorithm updates the shortest paths by considering each‬
‭vertex kkk as an‬‭intermediate node‬‭.‬
‭●‬ ‭For every pair of vertices (i,j)(i, j)(i,j):‬
‭Update Rule‬‭:‬
‭dist[i][j]=min⁡(dist[i][j],dist[i][k]+dist[k][j])dist[i][j] = \min(dist[i][j], dist[i][k] +‬
‭dist[k][j])dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j])‬
‭●‬ ‭For each possible intermediate node kkk:‬
‭○‬ ‭For every pair of nodes (i,j)(i, j)(i,j), check if the path through kkk is shorter than the direct‬
‭path from iii to jjj.‬
‭3. Repeat Until All Nodes Are Considered‬
‭●‬ ‭Continue considering each node kkk as an intermediate node in turn (i.e., for all nodes k=1k‬
‭= 1k=1 to VVV).‬
‭●‬ ‭For every node kkk, update the matrix for all pairs (i,j)(i, j)(i,j).‬

✅ Advantage‭❌ Disadvantage

to implement with a clear matrix approacomplexity O(V3)O(V^3)O(V3) for large g

es‬‭both directed and undirected graphsry-intensive with O(V2)O(V^2)O(V2) sto

vely computes all-pairs shortest paths even with‬‭negicient for very large sparse gr
weights‬
‭ hat are Optimal Binary Search Trees?‬
W
‭An‬‭Optimal Binary Search Tree‬‭minimizes the‬‭search,‬‭insert, and delete costs‬‭in a binary‬
‭search tree (BST) by ensuring that the tree is structured in such a way that these operations are as‬
‭fast and efficient as possible.‬
‭●‬ ‭In a normal binary search tree, the tree may become‬‭unbalanced‬‭, leading to inefficient‬
‭operations (up to O(V)O(V)O(V) time complexity).‬
‭●‬ ‭In an‬‭Optimal BST‬‭, the tree is‬‭balanced and well-structured‬‭,‬‭reducing the height and‬
‭ensuring better average search times.‬
‭Problem Statement‬
‭Given:‬
‭●‬ ‭A set of‬‭keys‬‭K1,K2,...,KnK_1, K_2, ..., K_nK1​,K2​,...,Kn​.‬
‭●‬ ‭For each key KiK_iKi​:‬
‭○‬ ‭pip_ipi​: the probability of accessing the key KiK_iKi​.‬
‭‬
○ ‭qiq_iqi​: the probability of accessing a non-existent key in between KiK_iKi​and‬
‭Ki+1K_{i+1}Ki+1​(representing gaps).‬
‭Objective:‬‭Construct a binary search tree with a structure‬‭that minimizes the‬‭expected search‬

📘

‭cost‬‭.‬
‭ Steps to Construct an Optimal BST‬
‭ Step 1: Define the Probabilities‬
‭For a set of‬‭n keys‬‭K1,K2,...,KnK_1, K_2, ..., K_nK1​,K2​,...,Kn​:‬
‭●‬ ‭pip_ipi​: Probability of accessing key KiK_iKi​.‬

✅ ‭●‬ ‭qiq_iqi​: Probability of accessing a‬‭non-existent‬‭key‬‭between KiK_iKi​and Ki+1K_{i+1}Ki+1​.‬


‭ Step 2: Define the Cost Formula‬
‭Let e[i][j]e[i][j]e[i][j] be the‬‭cost of the optimal‬‭BST containing keys from iii to jjj‬‭(i.e., KiK_iKi​‬‭to‬
‭KjK_jKj​).‬
‭Cost Recurrence Formula:‬
‭e[i][j]=min⁡i≤r≤j(e[i][r−1]+e[r+1][j]+w[i][j])e[i][j] = \min_{i \leq r \leq j} \left( e[i][r-1] + e[r+1][j] + w[i][j]‬
‭\right)e[i][j]=i≤r≤jmin​(e[i][r−1]+e[r+1][j]+w[i][j])‬
‭Where:‬
‭●‬ ‭rrr is the root of the tree in the subtree containing iii to jjj.‬
‭●‬ ‭w[i][j]w[i][j]w[i][j] is the sum of the probabilities of accessing the keys and the gaps between‬
‭them:‬
‭w[i][j]=∑k=ijpk+∑k=i−1jqkw[i][j] = \sum_{k=i}^j p_k + \sum_{k=i-1}^j q_kw[i][j]=k=i∑j​pk​+k=i−1∑j​qk​‬


‭ Step 3: Initialize the Cost Matrices‬
‭1.‬ ‭For a single key KiK_iKi​, the cost e[i][i]e[i][i]e[i][i] is just the probability of accessing KiK_iKi​:‬
‭e[i][i]=pi+qi−1e[i][i] = p_i + q_{i-1}e[i][i]=pi​+qi−1​‬


‭ Step 4: Use Dynamic Programming to Build Subproblems‬
‭●‬ ‭Build the matrix e[i][j]e[i][j]e[i][j] iteratively from smaller subtrees to larger ones (bottom-up‬
‭approach).‬
‭●‬ ‭For every possible subtree size j−i+1j - i + 1j−i+1, try every possible root rrr and compute the‬
‭cost using the recursive formula.‬


‭ Step 5: Reconstruct the Optimal Tree‬
‭Once the matrix e[i][j]e[i][j]e[i][j] is filled, you can backtrack to reconstruct the structure of the‬
‭optimal tree.‬

🔹
‭ Example‬
‭Let’s consider an example with‬‭3 keys‬‭:‬
‭Given Probabilities:‬
‭●‬ ‭Keys: K1,K2,K3K_1, K_2, K_3K1​,K2​,K3​‬
‭●‬ ‭p1=0.1p_1 = 0.1p1​=0.1, p2=0.5p_2 = 0.5p2​=0.5, p3=0.2p_3 = 0.2p3​=0.2‬
‭●‬ ‭Gaps: q0=0.05q_0 = 0.05q0​=0.05, q1=0.1q_1 = 0.1q1​=0.1‬
‭ bjective:‬
O
‭Construct a tree that minimizes the search cost using the dynamic programming approach.‬
‭1.‬ ‭Initialize a cost matrix e[i][j]e[i][j]e[i][j].‬
‭2.‬ ‭Use the recurrence formula to fill in the costs for all nodes by trying different roots.‬
‭3.‬ ‭Reconstruct the tree according to the optimal choices for the roots.‬

✅ Advantages of Optimal BST‬




‭ Advantages‬

nt Search Operations‬

es average search, insert, and delete costs.‬

memory management and lookup efficiency.‬

predictable time complexity,‬‭close to O(log⁡n)O(\log‬


gn)‬‭.‬
‭ hat is String Editing?‬
W
‭●‬ ‭The‬‭String Editing problem‬‭involves transforming one‬‭string into another with a series of‬
‭operations‬‭, such as‬‭Insertion, Deletion, and Substitution‬‭.‬
‭●‬ ‭Commonly known as the‬‭Edit Distance Problem‬‭in‬‭Dynamic‬‭Programming‬‭.‬
‭Operations:‬
‭1.‬ ‭Insertion‬‭: Add a character to the string.‬
‭2.‬ ‭Deletion‬‭: Remove a character from the string.‬
‭3.‬ ‭Substitution‬‭: Replace one character with another.‬
‭Dynamic Programming Approach‬
‭●‬ ‭Create a‬‭2-dimensional table‬‭to represent the edit‬‭distance between each pair of characters‬
‭in the two strings.‬
‭●‬ ‭Let’s say we have two strings‬‭AAA and BBB‬‭:‬
‭○‬ ‭dp[i][j]dp[i][j]dp[i][j] stores the minimum number of operations required to convert the first‬‭iii‬
‭characters of string AAA to the first‬‭jjj‬‭characters‬‭of string BBB.‬
‭Time Complexity:‬

📌 ‭●‬ ‭For strings of length mmm and nnn, the time complexity is O(m×n)O(m \times n)O(m×n).‬
‭ Applications of String Editing‬
‭●‬ ‭Spell Checking‬
‭●‬ ‭DNA sequence analysis‬
‭●‬ ‭Text comparison and version control‬
‭●‬ ‭Data cleaning and transformation‬

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy