Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 32655aa

Browse files
committed
add some dp problem
1 parent ff72192 commit 32655aa

11 files changed

+470
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.wcong.test.algorithm.basic;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
/**
7+
* represent a graph
8+
* adjacent list
9+
* Created by wcong on 2017/3/29.
10+
*/
11+
public class Graph {
12+
13+
14+
public static class Edge {
15+
16+
public Vertex from;
17+
18+
public Vertex to;
19+
20+
public int distance;
21+
22+
}
23+
24+
public static class Vertex {
25+
public int value;
26+
27+
public int distance;
28+
29+
public Vertex last;
30+
31+
@Override
32+
public boolean equals(Object o) {
33+
if (o == null || !(o instanceof Vertex)) {
34+
return false;
35+
}
36+
return value == ((Vertex) o).value;
37+
}
38+
}
39+
40+
public List<Edge> edges;
41+
42+
public Map<Vertex, List<Vertex>> adjacentMap;
43+
44+
public void addEdge(Edge edge) {
45+
edges.add(edge);
46+
adjacentMap.get(edge.from).add(edge.to);
47+
}
48+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package org.wcong.test.algorithm.basic;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* a heap data structure
7+
* 1 property max-heap there is max-heap property and min-heap there is min-heap property
8+
* 2 operation
9+
* 1. max-heapify : give a index,assume it's children is all satisfied ,make sure index is satisfied
10+
* 2. build max-heap : make self a max-heap
11+
* 3. heap-sort
12+
* 4. max-heap-insert
13+
* 5. heap-extract-max
14+
* 6. heap-increase-key
15+
* 7. heap-maximum
16+
* Created by wcong on 2017/3/29.
17+
*/
18+
public class Heap {
19+
20+
private int[] array;
21+
22+
public Heap(int[] array) {
23+
assert array != null;
24+
this.array = Arrays.copyOf(array, array.length);
25+
}
26+
27+
/**
28+
* initialization:priory to the first iteration of loop,i=[n/2],each node[n/2]+1,[n/2]+2,n is a leaf and thus the root
29+
*/
30+
public void buildMaxHeap() {
31+
if (array == null || array.length <= 1) {
32+
return;
33+
}
34+
int end = array.length / 2;
35+
for (int i = end - 1; i >= 0; i++) {
36+
maxHeapify(i, array.length - 1);
37+
}
38+
}
39+
40+
public int maximun() {
41+
return array[0];
42+
}
43+
44+
public int heapExtractMax() {
45+
return 0;
46+
}
47+
48+
public void maxHeapInsert(int add) {
49+
int[] newArray = new int[array.length + 1];
50+
for (int i = 0; i < array.length; i++) {
51+
newArray[i] = array[i];
52+
}
53+
heapIncreaseKey(newArray.length - 1, add);
54+
}
55+
56+
57+
public void heapIncreaseKey(int index, int k) {
58+
array[index] = k;
59+
while (index > 0) {
60+
int parent = (index + 1) / 2 - 1;
61+
if (array[parent] < array[index]) {
62+
int temp = array[parent];
63+
array[parent] = array[index];
64+
array[index] = temp;
65+
index = parent;
66+
} else {
67+
break;
68+
}
69+
}
70+
}
71+
72+
public void maxHeapify(int index, int length) {
73+
int left = index * 2 + 1;
74+
int right = left + 1;
75+
int maxIndex = index;
76+
if (left <= length && array[left] > array[maxIndex]) {
77+
maxIndex = left;
78+
}
79+
if (right <= length && array[right] > array[maxIndex]) {
80+
maxIndex = right;
81+
}
82+
if (maxIndex == index) {
83+
return;
84+
}
85+
int temp = array[index];
86+
array[index] = array[maxIndex];
87+
array[maxIndex] = temp;
88+
maxHeapify(maxIndex, length);
89+
}
90+
91+
public static void heapSort(int[] array) {
92+
Heap heap = new Heap(array);
93+
heap.buildMaxHeap();
94+
int end = array.length - 1;
95+
while (end > 0) {
96+
int temp = array[0];
97+
array[0] = array[end];
98+
array[end] = temp;
99+
end -= 1;
100+
heap.maxHeapify(0, end);
101+
}
102+
}
103+
104+
105+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.wcong.test.algorithm.basic;
2+
3+
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.PriorityQueue;
8+
import java.util.Queue;
9+
import java.util.Set;
10+
11+
/**
12+
* test for graph
13+
* Created by wcong on 2017/3/29.
14+
*/
15+
public class MinimumSpanningTree {
16+
17+
18+
public List<Graph.Edge> kruskal(Graph graph) {
19+
List<Graph.Edge> mini = new ArrayList<>();
20+
Set<Graph.Vertex> vertexSet = new HashSet<>();
21+
Queue<Graph.Edge> priorityQueue = new PriorityQueue<>(graph.edges.size(), new Comparator<Graph.Edge>() {
22+
@Override
23+
public int compare(Graph.Edge o1, Graph.Edge o2) {
24+
return o2.distance - o1.distance;
25+
}
26+
});
27+
priorityQueue.addAll(graph.edges);
28+
while (!priorityQueue.isEmpty()) {
29+
Graph.Edge edge = priorityQueue.poll();
30+
if (!(vertexSet.contains(edge.from) && vertexSet.contains(edge.to))) {
31+
mini.add(edge);
32+
vertexSet.add(edge.from);
33+
vertexSet.add(edge.to);
34+
}
35+
}
36+
return mini;
37+
}
38+
39+
public List<Graph.Edge> prim(Graph graph) {
40+
PriorityQueue<Graph.Vertex> priorityQueue = new PriorityQueue<>(graph.adjacentMap.keySet().size(), new Comparator<Graph.Vertex>() {
41+
@Override
42+
public int compare(Graph.Vertex o1, Graph.Vertex o2) {
43+
return o2.distance - o1.distance;
44+
}
45+
});
46+
for (Graph.Vertex vertex : graph.adjacentMap.keySet()) {
47+
vertex.distance = Integer.MAX_VALUE;
48+
priorityQueue.add(vertex);
49+
}
50+
Graph.Vertex first = priorityQueue.poll();
51+
first.distance = 0;
52+
priorityQueue.add(first);
53+
while (!priorityQueue.isEmpty()) {
54+
Graph.Vertex min = priorityQueue.poll();
55+
56+
}
57+
return null;
58+
}
59+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.wcong.test.algorithm.dp;
2+
3+
/**
4+
* give a string of unique character ,return all permutation
5+
* Created by wcong on 2017/3/31.
6+
*/
7+
public class AllPermutation {
8+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.wcong.test.algorithm.dp;
2+
3+
/**
4+
* given a string S with n characters and an array containing the break points, compute
5+
* the lowest cost for a sequence of breaks, along with a sequence of breaks that
6+
* achieves this cost.
7+
* length of 20 points 2,8,10
8+
* Created by wcong on 2017/3/31.
9+
*/
10+
public class BreakingAString {
11+
public static void main(String[] args) {
12+
13+
}
14+
15+
public static int minimumCost(String a, int[] point) {
16+
return minimumCost(a, 0, a.length() - 1, point, 0, point.length - 1);
17+
}
18+
19+
public static int minimumCost(String a, int start, int end, int[] point, int pointStart, int pointEnd) {
20+
if (start >= end || pointStart >= pointEnd) {
21+
return 0;
22+
}
23+
int length = end - start + 1;
24+
int left = minimumCost(a, point[pointStart], end, point, pointStart + 1, pointEnd);
25+
int right = minimumCost(a, start, point[pointEnd], point, pointStart, pointEnd - 1);
26+
return left < right ? left + length : right + length;
27+
}
28+
29+
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.wcong.test.algorithm.dp;
2+
3+
/**
4+
* give two strings a,b calculate the distance for b transform to b
5+
* use
6+
* add insert one char
7+
* delete delete one char
8+
* replace replace one char
9+
* return the minimum distance
10+
* Created by wcong on 2017/3/31.
11+
*/
12+
public class EditDistance {
13+
14+
public static void main() {
15+
}
16+
17+
public static int mininumEditDistance(String a, String b) {
18+
int[][] result = new int[a.length()][b.length()];
19+
for (int i = 0; i < a.length(); i++) {
20+
for (int j = 0; j < b.length(); j++) {
21+
if (a.charAt(i) == b.charAt(j)) {
22+
result[i + 1][j + 1] = result[i][j] + 1;
23+
} else {
24+
result[i + 1][j + 1] = 1 + Math.min(Math.min(result[i + 1][j], result[i][j + 1]), result[i][j]);
25+
}
26+
}
27+
}
28+
return result[a.length()][b.length()];
29+
}
30+
31+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.wcong.test.algorithm.dp;
2+
3+
/**
4+
* give two string a,b find the longest common sub sequence
5+
* Created by wcong on 2017/3/31.
6+
*/
7+
public class LongestCommonSubSequence {
8+
9+
public static void main(String[] args) {
10+
11+
}
12+
13+
public static int longest(String a, String b) {
14+
int[][] result = new int[a.length() + 1][b.length() + 1];
15+
if (a.charAt(0) == b.charAt(0)) {
16+
result[1][1] = 1;
17+
}
18+
for (int i = 0; i < b.length(); i++) {
19+
if (a.charAt(0) == b.charAt(i)) {
20+
result[1][i + 1] = 1;
21+
}
22+
}
23+
for (int i = 1; i < a.length(); i++) {
24+
for (int j = 0; j < b.length(); j++) {
25+
if (a.charAt(i) == b.charAt(j)) {
26+
result[i][j] = result[i - 1][j] + 1;
27+
}
28+
}
29+
}
30+
int max = 0;
31+
for (int i = 0; i < a.length(); i++) {
32+
for (int j = 0; j < b.length(); j++) {
33+
if (result[i][j] > max) {
34+
max = result[i][j];
35+
}
36+
}
37+
}
38+
return max;
39+
}
40+
41+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.wcong.test.algorithm.dp;
2+
3+
import org.eclipse.core.runtime.Assert;
4+
5+
/**
6+
* give a string find the longest palindrome sub sequence
7+
* Created by wcong on 2017/3/31.
8+
*/
9+
public class LongestPalindromeSubsequence {
10+
11+
public static void main(String[] args) {
12+
Assert.isTrue(palindrome("cabdbae") == 5);
13+
}
14+
15+
public static int palindrome(String a) {
16+
int[][] result = new int[a.length()][a.length()];
17+
for (int i = 0; i < a.length(); i++) {
18+
result[i][i] = 1;
19+
}
20+
for (int i = 1; i < a.length(); i++) {
21+
for (int j = 0; j < i; j++) {
22+
if (a.charAt(i) == a.charAt(j)) {
23+
if (i - j - 1 == 1) {
24+
result[i][j] = 3;
25+
} else if (i - 1 > 0 && j + 1 < i - 1) {
26+
if (result[i - 1][j + 1] > 0) {
27+
result[i][j] = result[i - 1][j + 1] + 2;
28+
}
29+
} else {
30+
result[i][j] = 1;
31+
}
32+
}
33+
}
34+
}
35+
int max = 1;
36+
for (int i = 0; i < a.length(); i++) {
37+
for (int j = 0; j < a.length(); j++) {
38+
if (result[i][j] > max) {
39+
max = result[i][j];
40+
}
41+
}
42+
}
43+
return max;
44+
}
45+
46+
}

0 commit comments

Comments
 (0)