Skip to content

Commit df5c393

Browse files
committed
BloomFilter and DP code added
1 parent 108e6ea commit df5c393

12 files changed

+310
-35
lines changed

Competitive Programming.iml

+26
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,31 @@
1414
<orderEntry type="library" name="chelper" level="project" />
1515
<orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
1616
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
17+
<orderEntry type="library" name="Maven: com.google.apis:google-api-services-youtube:v3-rev194-1.23.0" level="project" />
18+
<orderEntry type="library" name="Maven: com.google.api-client:google-api-client:1.23.0" level="project" />
19+
<orderEntry type="library" name="Maven: com.google.oauth-client:google-oauth-client:1.23.0" level="project" />
20+
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:1.3.9" level="project" />
21+
<orderEntry type="library" name="Maven: com.google.http-client:google-http-client-jackson2:1.23.0" level="project" />
22+
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.1.3" level="project" />
23+
<orderEntry type="library" name="Maven: com.google.guava:guava-jdk5:17.0" level="project" />
24+
<orderEntry type="library" name="Maven: com.google.api-client:google-api-client-extensions:1.6.0-beta" level="project" />
25+
<orderEntry type="library" name="Maven: com.google.oauth-client:google-oauth-client-extensions:1.6.0-beta" level="project" />
26+
<orderEntry type="library" name="Maven: com.googlecode.jsontoken:jsontoken:1.0" level="project" />
27+
<orderEntry type="library" name="Maven: com.google.code.gson:gson:1.4" level="project" />
28+
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.4" level="project" />
29+
<orderEntry type="library" name="Maven: com.google.collections:google-collections:1.0" level="project" />
30+
<orderEntry type="library" name="Maven: joda-time:joda-time:1.6" level="project" />
31+
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.0.1" level="project" />
32+
<orderEntry type="library" name="Maven: javax.servlet:servlet-api:2.5" level="project" />
33+
<orderEntry type="library" name="Maven: javax.jdo:jdo2-api:2.3-eb" level="project" />
34+
<orderEntry type="library" name="Maven: javax.transaction:transaction-api:1.1" level="project" />
35+
<orderEntry type="library" name="Maven: com.google.http-client:google-http-client-extensions:1.6.0-beta" level="project" />
36+
<orderEntry type="library" name="Maven: com.google.http-client:google-http-client:1.6.0-beta" level="project" />
37+
<orderEntry type="library" name="Maven: com.google.guava:guava:r09" level="project" />
38+
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.0.3" level="project" />
39+
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.1" level="project" />
40+
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-core-asl:1.9.1" level="project" />
41+
<orderEntry type="library" name="Maven: xpp3:xpp3:1.1.4c" level="project" />
42+
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:2.2.0" level="project" />
1743
</component>
1844
</module>

pom.xml

+10-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@
2525
<artifactId>junit</artifactId>
2626
<version>4.12</version>
2727
</dependency>
28+
<dependency>
29+
<groupId>com.google.apis</groupId>
30+
<artifactId>google-api-services-youtube</artifactId>
31+
<version>v3-rev194-1.23.0</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.google.api-client</groupId>
35+
<artifactId>google-api-client-extensions</artifactId>
36+
<version>1.6.0-beta</version>
37+
</dependency>
2838
</dependencies>
29-
30-
3139
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main.java.course;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.function.Function;
6+
7+
class BloomFilterMain {
8+
public static void main(String[] args) {
9+
BloomFilter bloomFilter = new BloomFilter(1000);
10+
bloomFilter.mark(141432);
11+
bloomFilter.mark(2354364);
12+
System.out.println(bloomFilter.isMarked(141432));
13+
System.out.println(bloomFilter.isMarked(723));
14+
//This should be false, but bloom filters provide no guarantee on definite existence
15+
System.out.println(bloomFilter.isMarked(1432));
16+
}
17+
}
18+
19+
public class BloomFilter<T> {
20+
private final long[] points;
21+
private final int size;
22+
private final List<Function<T, Integer>> hashFunctions;
23+
24+
BloomFilter(final int size) {
25+
this.size = size;
26+
points = new long[(size + 63) >> 6];
27+
hashFunctions = Arrays.asList(multiplyWith(29), multiplyWith(19), multiplyWith(17));
28+
}
29+
30+
private Function<T, Integer> multiplyWith(final int i) {
31+
return t -> (t.hashCode() * i) % size;
32+
}
33+
34+
public void mark(int hash) {
35+
hash %= size;
36+
points[hash >> 6] |= (1 << (hash & 63));
37+
}
38+
39+
public boolean isMarked(int hash) {
40+
hash %= size;
41+
return (points[hash >> 6] & (1 << (hash & 63))) != 0;
42+
}
43+
44+
public void add(final T value) {
45+
hashFunctions.stream().map(function -> function.apply(value)).forEach(this::mark);
46+
}
47+
48+
public boolean exists(final T value) {
49+
return hashFunctions.stream().map(function -> function.apply(value)).allMatch(this::isMarked);
50+
}
51+
}

src/main/java/main/java/course/BloomFilterMain.java

-33
This file was deleted.
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main.java.dp;
2+
3+
public class CoinChange {
4+
private static long countWays(int coins[], int amount) {
5+
final long[] table = new long[amount + 1];
6+
table[0] = 1;
7+
for (final int value : coins) {
8+
for (int j = value; j <= amount; j++) {
9+
table[j] += table[j - value];
10+
}
11+
}
12+
return table[amount];
13+
}
14+
15+
public static void main(String args[]) {
16+
System.out.println(countWays(new int[]{1, 2, 3}, 4));
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main.java.dp;
2+
3+
public class LongestCommonSubsequence {
4+
int lcs(char[] A, char[] B, int m, int n) {
5+
final int dp[][] = new int[m + 1][n + 1];
6+
for (int x = 1; x <= m; x++) {
7+
for (int y = 1; y <= n; y++) {
8+
if (A[x - 1] == B[y - 1]) {
9+
dp[x][y] = dp[x - 1][y - 1] + 1;
10+
} else {
11+
dp[x][y] = Math.max(dp[x - 1][y], dp[x][y - 1]);
12+
}
13+
}
14+
}
15+
return dp[m][n];
16+
}
17+
18+
public static void main(String[] args) {
19+
final char[] A = "AGGTAB".toCharArray(), B = "GXTXAYB".toCharArray();
20+
System.out.println("LCS: " + new LongestCommonSubsequence().lcs(A, B, A.length, B.length));
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main.java.dp;
2+
3+
public class LongestCommonSubstring {
4+
5+
private static int findLCS(char A[], char B[], int m, int n) {
6+
int dp[][] = new int[m + 1][n + 1];
7+
int result = 0;
8+
for (int x = 1; x <= m; x++) {
9+
for (int y = 1; y <= n; y++) {
10+
if (A[x - 1] == B[y - 1]) {
11+
dp[x][y] = dp[x - 1][y - 1] + 1;
12+
result = Integer.max(result, dp[x][y]);
13+
}
14+
}
15+
}
16+
return result;
17+
}
18+
19+
public static void main(String[] args) {
20+
final String a = "www.gkcs.tech", b = "www.youtube.gkcs";
21+
System.out.println("Length of Longest Common Substring is "
22+
+ findLCS(a.toCharArray(), b.toCharArray(), a.length(), b.length()));
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main.java.dp;
2+
3+
public class LongestIncreasingSubsequence {
4+
private static int lis(int arr[], int n) {
5+
int dp[] = new int[n];
6+
int x, y, max = 0;
7+
for (x = 0; x < n; x++) {
8+
dp[x] = 1;
9+
}
10+
for (x = 1; x < n; x++) {
11+
for (y = 0; y < x; y++) {
12+
if (arr[x] > arr[y] && dp[x] < dp[y] + 1) {
13+
dp[x] = dp[y] + 1;
14+
}
15+
}
16+
}
17+
for (x = 0; x < n; x++) {
18+
if (max < dp[x]) {
19+
max = dp[x];
20+
}
21+
}
22+
return max;
23+
}
24+
25+
public static void main(String args[]) {
26+
int arr[] = {10, 22, 9, 33, 21, 50, 41, 60};
27+
System.out.println("LIS: " + lis(arr, arr.length));
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main.java.dp;
2+
3+
import java.util.Arrays;
4+
5+
public class LongestRepeatingSubsequence {
6+
static int findLongestRepeatingSubSeq(String str) {
7+
int n = str.length();
8+
int[][] dp = new int[n + 1][n + 1];
9+
for (int x = 1; x <= n; x++) {
10+
for (int y = 1; y <= n; y++) {
11+
if (str.charAt(x - 1) == str.charAt(y - 1) && x != y)
12+
dp[x][y] = 1 + dp[x - 1][y - 1];
13+
else
14+
dp[x][y] = Math.max(dp[x][y - 1], dp[x - 1][y]);
15+
}
16+
}
17+
for (final int[] row : dp) {
18+
System.out.println(Arrays.toString(row));
19+
}
20+
return dp[n][n];
21+
}
22+
23+
public static void main(String[] args) {
24+
System.out.println(findLongestRepeatingSubSeq("aabpqrarqa"));
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main.java.dp;
2+
3+
import java.util.Arrays;
4+
5+
public class MatrixChain {
6+
static int MatrixChainOrder(int p[], int n) {
7+
final int dp[][] = new int[n][n];
8+
int start, end, partition, blockLength, q;
9+
for (start = 1; start < n; start++) {
10+
dp[start][start] = 0;
11+
}
12+
for (blockLength = 2; blockLength < n; blockLength++) {
13+
for (start = 1; start < n - blockLength + 1; start++) {
14+
end = start + blockLength - 1;
15+
dp[start][end] = Integer.MAX_VALUE;
16+
for (partition = start; partition <= end - 1; partition++) {
17+
q = dp[start][partition]
18+
+ dp[partition + 1][end]
19+
+ p[start - 1] * p[partition] * p[end];
20+
if (q < dp[start][end]) {
21+
dp[start][end] = q;
22+
}
23+
}
24+
}
25+
}
26+
System.out.println(Arrays.deepToString(dp));
27+
return dp[1][n - 1];
28+
}
29+
30+
public static void main(String args[]) {
31+
int arr[] = new int[]{7, 2, 1, 4, 5};
32+
int size = arr.length;
33+
34+
System.out.println("Minimum number of multiplications is " +
35+
MatrixChainOrder(arr, size));
36+
}
37+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main.java.dp;
2+
3+
public class OptimalBST {
4+
static int optimalSearchTree(final int freq[], final int n) {
5+
final int cost[][] = new int[n + 1][n + 1];
6+
final int prefixSums[] = new int[n];
7+
prefixSums[0] = freq[0];
8+
for (int i = 1; i < n; i++) {
9+
prefixSums[i] = prefixSums[i - 1] + freq[i];
10+
}
11+
for (int i = 0; i < n; i++) {
12+
cost[i][i] = freq[i];
13+
}
14+
for (int lengthOfBlock = 2; lengthOfBlock <= n; lengthOfBlock++) {
15+
for (int start = 0; start <= n - lengthOfBlock + 1; start++) {
16+
final int end = start + lengthOfBlock - 1;
17+
cost[start][end] = Integer.MAX_VALUE;
18+
for (int root = start; root <= end; root++) {
19+
final int currentCost = ((root > start) ? cost[start][root - 1] : 0)
20+
+ ((root < end) ? cost[root + 1][end] : 0) +
21+
prefixSums[Math.min(end, prefixSums.length - 1)] - (start > 0 ? prefixSums[start - 1] : 0);
22+
if (currentCost < cost[start][end]) {
23+
cost[start][end] = currentCost;
24+
}
25+
}
26+
}
27+
}
28+
return cost[0][n - 1];
29+
}
30+
31+
public static void main(String[] args) {
32+
int keys[] = {10, 12, 20};
33+
int freq[] = {34, 8, 50};
34+
int n = keys.length;
35+
System.out.println("Optimal BST cost " + optimalSearchTree(freq, n));
36+
}
37+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main.java.dp;
2+
3+
public class SubsetSum {
4+
private static boolean isSubsetSum(final int set[], int n, int sum) {
5+
final boolean subset[][] = new boolean[sum + 1][n + 1];
6+
for (int i = 0; i <= n; i++) {
7+
subset[0][i] = true;
8+
}
9+
for (int i = 1; i <= sum; i++) {
10+
for (int j = 1; j <= n; j++) {
11+
subset[i][j] = subset[i][j - 1];
12+
if (i >= set[j - 1]) {
13+
subset[i][j] = subset[i][j] ||
14+
subset[i - set[j - 1]][j - 1];
15+
}
16+
}
17+
}
18+
return subset[sum][n];
19+
}
20+
21+
public static void main(String args[]) {
22+
int set[] = {3, 34, 4, 12, 5, 2};
23+
if (isSubsetSum(set, set.length, 9)) {
24+
System.out.println("Found a subset with given sum");
25+
} else {
26+
System.out.println("No subset with given sum");
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)