Skip to content

Commit bd00a5c

Browse files
authored
Add files via upload
1 parent a02fb8a commit bd00a5c

4 files changed

Lines changed: 266 additions & 0 deletions

File tree

‎DP/Edit_Distance.java‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import java.util.Arrays;
2+
3+
public class Edit_Distance {
4+
5+
/*Edit (Levenshtein Distance) Algorithm
6+
7+
Problem (informal): Given two strings, find minimum number of deletions, insertions, or replacements required to transform
8+
9+
Algorithm: If characters are same, continue, else, consider removal, deletion, and replacement and consider minimum recursively
10+
11+
Complexity:
12+
* Time - O(n^2)
13+
* Space - O(n^2) with memoization and O(1) without
14+
15+
Functions Defined:
16+
* editDistanceIterative() - Bottom up implementation, finds minimum iteratively
17+
* editDistanceRecursive() - Top down implementation, recursive
18+
19+
*/
20+
21+
static int[][] dp;
22+
public static void main(String[] args) {
23+
String a = "hey if you like this repository, you should give it a star!";
24+
String b = "I'd appreciate any feedback or requests -- just fork this repo and add to the README.md";
25+
26+
dp = new int [a.length()+1][b.length()+1];
27+
28+
for(int[] row :dp){
29+
Arrays.fill(row, -1);
30+
}
31+
32+
System.out.println(editDistanceIterative(a, b));
33+
System.out.println(editDistanceTD(a, b));
34+
35+
}
36+
37+
public static int editDistanceIterative(String a, String b){
38+
int[][] dp = new int[a.length()+1][b.length()+1];
39+
for(int i=0; i <= a.length();i++){
40+
for(int j=0; j <= b.length();j++){
41+
if(i==0 || j==0)
42+
dp[i][j] = i+j;
43+
else if(a.charAt(i-1) == b.charAt(j-1))
44+
dp[i][j] = dp[i-1][j-1];
45+
else
46+
dp[i][j] = 1 + Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]);
47+
}
48+
}
49+
return dp[a.length()][b.length()];
50+
}
51+
52+
//minimal # of insertions, substitutions, deletions required to change String a to b
53+
public static int editDistanceTD(String a, String b){
54+
int m = a.length();
55+
int n = b.length();
56+
if(dp[m][n] !=-1){
57+
return dp[m][n];
58+
}
59+
if(m==0 || n==0){
60+
dp[m][n] = m+n;
61+
return dp[m][n];
62+
}
63+
if(a.charAt(m-1) == b.charAt(n-1)){
64+
dp[m][n] = editDistanceTD(a.substring(0, m-1), b.substring(0, n-1));
65+
return dp[m][n];
66+
}
67+
dp[m][n] = 1 + Math.min(Math.min(editDistanceTD(a.substring(0, m-1), b), editDistanceTD(a, b.substring(0, n-1))), editDistanceTD(a.substring(0, m-1), b.substring(0, n-1)));
68+
return dp[m][n];
69+
}
70+
71+
}

‎DP/Knapsack.java‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import cern.colt.Arrays;
2+
import java.util.*;
3+
4+
public class Knapsack {
5+
6+
/*0-1 Knapsack Probelm
7+
8+
Problem (informal): Given set of items with cost and weight, find subset of items such that net weight does not exceed W and set has maximum total cost
9+
10+
Algorithm: Recursive -- try each possible item and calculate max of remaining
11+
12+
Complexity:
13+
* Time - O(nW) where W is the maximum weight possible
14+
* Space - O(n) only one array for number of items.
15+
16+
Functions Defined:
17+
* knapsackTD() - Recursively consideres one less item and maximum of remaining subset
18+
* fractionalknapsack() - Pretty trivial -- greedy algorithm suffices. Solved in O(nlogn) but also possible in O(n) via weighted medians
19+
20+
*/
21+
static int[][] items;
22+
static int maxweight;
23+
public static void main(String[] args){
24+
int[][] itemsx = {{60, 10}, {100, 20}, {120, 30}};
25+
//{cost, weight}
26+
items = itemsx;
27+
maxweight = 50;
28+
boolean[] insert = new boolean[items.length];
29+
//System.out.println(knapsackBU(items, maxweight));
30+
System.out.println(knapsackTD(maxweight, insert));
31+
}
32+
33+
public static int knapsackTD(int maxweight, boolean[] insert){
34+
int max = 0;
35+
for(int i=0;i<items.length;i++){
36+
if(!insert[i] && maxweight >= items[i][1]){
37+
boolean[] insertclone = insert.clone();
38+
insertclone[i] = true;
39+
max = Math.max(items[i][0] + knapsackTD(maxweight - items[i][1], insertclone), max);
40+
}
41+
}
42+
return max;
43+
}
44+
45+
public static void fractionalknapsack(){
46+
//greedy
47+
//I don't think it's necessary to implement this since it's pretty straightforward.
48+
//Just find the item with largest cost and take as much of it as possible. If you take all of it, continue to next largest.
49+
}
50+
51+
}

‎DP/LCS.java‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import java.util.*;
2+
import cern.colt.Arrays;
3+
4+
public class LCS {
5+
6+
/*Longest Common Subsequence (LCS)
7+
8+
Problem (informal): Given two arrays, find largest subsequence of both arrays
9+
10+
Algorithm: If elements at position i are equal, find LCS of subarrays [0, i-1], else find max of both possible cases
11+
12+
Complexity:
13+
* Time - O(n^2)
14+
* Space - O(n^2) with memoization and O(1) without
15+
16+
Functions Defined:
17+
* iterativeLCS() - Bottom up implementation, finds longest length and reconstructs sequence
18+
* recursiveLCS() - Top down implementation, only outputs length of sequence
19+
20+
*/
21+
22+
static int[] arr1;
23+
static int[] arr2;
24+
static int[][] dp;
25+
26+
static Stack<Integer> solution = new Stack<Integer>();
27+
public static void main(String[] args) {
28+
int[] arrA = {1, 2, 3, 4, 7, 5, 9};
29+
int[] arrB = {1, 4, 3, 7, 9, 5};
30+
31+
arr1 = arrA;
32+
arr2 = arrB;
33+
34+
System.out.println(Arrays.toString(iterativeLCS(arrA, arrB)));
35+
36+
}
37+
38+
public static int[] iterativeLCS(int[] a, int[] b){
39+
int[][] dp = new int[a.length+1][b.length+1];
40+
for(int i = 1; i <= a.length; i++){
41+
for(int j = 1; j <= b.length; j++){
42+
if(a[i-1] == b[j-1]){
43+
dp[i][j] = 1 + dp[i-1][j-1];
44+
}
45+
else{
46+
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
47+
}
48+
}
49+
}
50+
51+
int[] lcs = new int[dp[a.length][b.length]];
52+
int index = lcs.length-1;
53+
int i=a.length; int j = b.length;
54+
55+
while(i > 0 && j > 0){
56+
if(a[i-1] == b[j-1]){
57+
lcs[index] = a[i-1];
58+
i--; j--; index--;
59+
}
60+
else if(dp[i-1][j] >= dp[i][j-1])
61+
i--;
62+
else
63+
j--;
64+
}
65+
66+
return lcs;
67+
}
68+
69+
public static int recursiveLCS(int a, int b){
70+
if(a < 0 || b < 0) return 0;
71+
72+
if(arr1[a] == arr2[b]){
73+
solution.push(arr1[a]);
74+
dp[a][b] = 1 + recursiveLCS(a-1, b-1);
75+
return dp[a][b];
76+
}
77+
int opt1 = recursiveLCS(a, b-1);
78+
int opt2 = recursiveLCS(a-1, b);
79+
80+
if(opt1 >= opt2){
81+
dp[a][b] = opt1;
82+
}
83+
84+
else{
85+
dp[a][b] = opt2;
86+
}
87+
88+
return dp[a][b];
89+
}
90+
91+
}

‎DP/maxDonations.java‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
public class maxDonations {
3+
4+
/*Maximum Donations (Topcoder)
5+
6+
Problem (informal): Given set S of values, find subset Q of values with maximum sum such that no two values in Q are adjacent in S
7+
8+
Algorithm: Recursive -- two possibilities at each branch, test both
9+
10+
Complexity:
11+
* Time - O(n^2) - each branches off into two possibilities
12+
* Space - O(n^2) due to DP array
13+
14+
Functions Defined:
15+
* maxDonations() - main algorithm
16+
*/
17+
18+
static int[] arr;
19+
static int len;
20+
static int[][] dp;
21+
22+
public static void main(String[] args) {
23+
int[] arr2 = {10, 3, 2, 5, 7, 8};
24+
//{10, 4, 5}
25+
arr = arr2;
26+
len = arr.length;
27+
dp = new int[len][len];
28+
29+
30+
31+
32+
}
33+
34+
public static int maxDonations(int start, int end){
35+
if(end - start ==2) return Math.max(Math.max(arr[start], arr[end]), arr[start+1]);
36+
if(end - start <=1) return Math.max(arr[start], arr[end]);
37+
38+
int start1 = (start+1)%len;
39+
40+
int val = arr[start];
41+
int start2 = start, end2 = end;
42+
if(start == 0){
43+
start2+=2;
44+
45+
}
46+
else{
47+
start2+=2;
48+
}
49+
dp[start][end] = Math.max(maxDonations(start1, end), val + maxDonations(start2, end2));
50+
return dp[start][end];
51+
}
52+
53+
}

0 commit comments

Comments
 (0)