Dynamic Programming: Data Structures and Algorithms

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

DYNAMIC PROGRAMMING

Data Structures and Algorithms

Student’s Name: Ali Asgerov


Group: PAE 17.2
Supervisor’s Name: Suleyman Suleymanzade

Baku Higher Oil School


2018
Contents
Introduction.................................................................................................................................................2
Task 1. Longest Increasing Subsequence.....................................................................................................2
Function that generates random array of n length..................................................................................2
Solution...................................................................................................................................................2
Task 2 – Coin collecting problem.................................................................................................................4
Solution...................................................................................................................................................4
Task 3- Edit distance....................................................................................................................................5
Solution...................................................................................................................................................6
Task 4- Largest sum of contiguous subarray................................................................................................6
Solution...................................................................................................................................................7

1|Page
Introduction
In this laboratory work we are going to solve problems by using one of the five
paradigms dynamic programming. In this report, we will try to explain how to solve
various problems by using dynamic approach to the given tasks.

Task 1. Longest Increasing Subsequence


In order to solve this task, we use dynamic programming.
Assume that we already have a function that gives us the length of the longest
increasing subsequence. Then we’ll try to feed some part of our input array back to it
and try to extend the result. Our base cases are: the empty list, returning 0, and an
array with one element, returning 1.
Then,
For every index i up until the second to last element, calculate LIS up to there.
We can only extend the result with the last element if our last element is greater
than arr[i](since otherwise, it’s not increasing).
Keep track of the largest result.
Function that generates random array of n length:
int *randomarr(int n)
{
int *a= new int[n];
for(int i=0; i<n;i++)
{
a[i]=rand()%50;
}
return a;
}

Solution:
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int *randomarr(int n)
{
int *a= new int[n];
for(int i=0; i<n;i++)
{
a[i]=rand()%50;
}
return a;
}
void lis(int *a, int n)
{
int *b = new int[n];
for (int i = 0; i < n; i++)
{
b[i]=1;
}
for (int i = 1; i < n; i++)
{
int j=0;
while(j!=i)
{
if(a[j]<a[i])
{
b[i]=max(b[j]+1,b[i]);
} j+
+;
}
}
vector<int> v;
int index=max_element(b,b+n)-b;
v.push_back(a[index]);
for (int i = index - 1; i >= 0 ; i--)
{
if(a[i]<a[index] && b[i]==b[index]-1)
{
v.push_back(a[i]);
index=i;
}
}
reverse(v.begin(),v.end());
cout<<*max_element(b,b+n)<<endl;
for (int i = 0; i < v.size(); i++)
{
cout<<v[i]<<" ";
}
cout<<endl;
}
int main()
{
int n;
cin>>n;
int *b;
b=randomarr(n);
for(int i=0; i<n ;i++)
{
cout<<b[i]<<" ";
}
cout<<endl;

lis(b,n);

system("pause");
return 0;
}
Task 2 – Coin collecting problem
• Let F(i,j) be the largest number of coins the robot can collect and bring to cell (i,j) in the ith
row and jth column.

• The largest number of coins that can be brought to cell (i,j):

– from the left neighbor ?

– from the neighbor above?

• The recurrence:

– F(i, j) = max{F(i-1, j), F(i, j-1)} + cij for 1 ≤ i ≤ n, 1 ≤ j ≤ m

• where cij = 1 if there is a coin in cell (i,j), and cij = 0 otherwise

– F(0, j) = 0 for 1 ≤ j ≤ m and F(i, 0) = 0 for 1 ≤ i ≤

• F(i, j) = max{F(i-1, j), F(i, j-1)} + cij for 1 ≤ i ≤ n, 1 ≤ j ≤ m

• where cij = 1 if there is a coin in cell (i,j), and cij = 0 otherwise

• F(0, j) = 0 for 1 ≤ j ≤ m and F(i, 0) = 0 for 1 ≤ i ≤ n


• F(i, j) = max{F(i-1, j), F(i, j-1)} + cij for 1 ≤ i ≤ n, 1 ≤ j ≤ m
• where cij = 1 if there is a coin in cell (i,j), and cij = 0 otherwise
• F(0, j) = 0 for 1 ≤ j ≤ m and F(i, 0) = 0 for 1 ≤ i ≤ n

Solution:
#include <bits/stdc++.h>

using namespace std;

int collect_coins(int **a, int n, int m){

for(int i=1;i<m;i++) a[0][i]+=a[0][i-1];

for(int i=1;i<n;i++) a[i][0]+=a[i-1][0];

for(int i=1;i<n;i++)

for(int j=1;j<m;j++)

a[i][j]+=max(a[i-1][j],a[i][j-1]);

return a[n-1][m-1];

int main(){

int *arr[10];

for(int i=0;i<10;i++) arr[i]=new int[10];


for(int i=0;i<10;i++)

for(int j=0;j<10;j++)

arr[i][j]=0;

for(int i=0;i<12;i++){

int x=rand()%10, y=rand()%10; if(!

arr[x][y]) arr[x][y]=1;

else i--;

for(int i=0;i<10;i++){

for(int j=0;j<10;j++)

cout<<arr[i][j]<<' ';

cout<<endl;

cout<<collect_coins(arr,10,10)<<endl;

return 0;

Task 3- Edit distance


The idea is process all characters one by one staring from either from left or right sides
of both strings.
Let us traverse from right corner, there are two possibilities for every pair of character
being traversed.
m: Length of str1 (first string)
n: Length of str2 (second string)
If last characters of two strings are same, nothing much to do. Ignore last characters
and get count for remaining strings. So we recur for lengths m-1 and n-1.
Else (If last characters are not same), we consider all operations on ‘str1’, consider all
three operations on last character of first string, recursively compute minimum cost
for all three operations and take minimum of three values.
Insert: Recur for m and n-1
Remove: Recur for m-1 and n
Replace: Recur for m-1 and n-1
Solution:
#include <bits/stdc++.h>

using namespace std;

int edit_distance(string a, string b){

int arr[256][256]; a=" "+a; b=" "+b;

for(int i=0;i<a.size();i++) arr[i][0]=i;

for(int i=0;i<b.size();i++) arr[0][i]=i;

for(int i=1;i<a.size();i++)

for(int j=1;j<b.size();j++)

if(a[i]==b[j])

arr[i][j]=arr[i-1][j-1];

else

arr[i][j]=min(arr[i-1][j-1],min(arr[i][j-1],arr[i-1][j]))+1;

return arr[a.size()-1][b.size()-1];

int main(){

string s,ss;

cin>>s>>ss;

cout<<edit_distance(s,ss)<<endl;

return 0;

Task 4- Largest sum of contiguous subarray


Kadane’s Algorithm:
Initialize:

max_so_far = 0

max_ending_here = 0

6|Page
Loop for each element of the array

(a) max_ending_here = max_ending_here + a[i]

(b) if(max_ending_here < 0)

max_ending_here = 0

(c) if(max_so_far < max_ending_here)


max_so_far = max_ending_here

return max_so_far

Simple idea of the Kadane’s algorithm is to look for all positive contiguous segments of the
array (max_ending_here is used for this). And keep track of maximum sum contiguous
segment among all positive segments (max_so_far is used for this). Each time we get a
positive sum compare it with max_so_far and update max_so_far if it is greater than
max_so_far

Solution:
#include <bits/stdc++.h>

using namespace std;

int kadane(int *a, int n){

int s=0, maxx=-1e9;

for(int i=0;i<n;i++)

s=max(a[i],s+a[i]), maxx=max(maxx,s);

return maxx;

int main(){

int *arr,n;

cin>>n; arr=new int[n];

for(int i=0;i<n;i++)

arr[i]=rand()%200-100, cout<<arr[i]<<" ";

cout<<endl<<kadane(arr,n)<<endl;

delete[] arr;

return 0;

7|Page
}

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