Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ljfirst committed Sep 22, 2018
1 parent e84c5fa commit e5a2404
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 5 deletions.
71 changes: 71 additions & 0 deletions algorithm/backpack/Backpack_dynamic.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,79 @@
*@date: 2018-7-19 上午11:55:33
*@author—Email:ljfirst@mail.ustc.edu.cn
*@description:
*1.状态转换方程
*2.如何找出所选物品
*
*扩展:
*1.是否还有其他方法?回溯法?分支限界法?贪心?
*2.然后可以重复选择呢?
*@version 1.0
*/
public class Backpack_dynamic {

public Backpack_dynamic(Integer[] goodsvalue, Integer[] weight, int packageweight) {

//价值矩阵,列:背包的重量,行:加入的物品
int[][] bestvalue = new int [packageweight + 1][goodsvalue.length + 1];

//逐层规划,外层循环表示背包重量增加
for (int i = 1; i <= packageweight; i++) {
//内层循环,遍历物品
for (int j = 1; j <= goodsvalue.length; j++) {

//如果放入值比背包总重量还大,放弃
if(weight[j-1] > i){
bestvalue[i][j] = bestvalue[i][j - 1];
}else{
bestvalue[i][j] = bestvalue[i][j - 1] > bestvalue[i - weight[j - 1]][j - 1] + goodsvalue[j - 1] ?
bestvalue[i][j - 1] : bestvalue[i - weight[j - 1]][j - 1] + goodsvalue[j - 1] ;
}
}
}

//打印矩阵值
for (int i = 1; i <= packageweight; i++) {
for (int j = 1; j <= goodsvalue.length; j++){
if (bestvalue[i][j] < 10) {
System.out.print(" " + bestvalue[i][j] + " ");
}else{
System.out.print(bestvalue[i][j] + " ");
}
}
System.out.println();
}


//存放选择的物品
int x[] = new int[goodsvalue.length];
//背包实际装载重量
int real_weight = 0;
//找出所选物品
int i = packageweight;
int j = x.length;

while(j >= 1 && i >= 1){
if(bestvalue[i][j] != bestvalue[i][j - 1]){
x[j - 1] = 1;//装入第i个物品
i -= weight[j - 1];
real_weight += weight[j - 1];
j--;
}else {
x[j - 1] = 0;//不装入第i个物品
j--;
}
}

System.out.println("背包实际转载重量为:"+real_weight);
System.out.print("背包最大价值为:");
System.out.println(bestvalue[packageweight - 1][goodsvalue.length - 1]);
System.out.print("背包包含物品为:");

//输出选择的物品
for(int y = 0;y < x.length; y++){
if(x[y] != 0){
System.out.print(y+" ");
}
}
}
}
21 changes: 21 additions & 0 deletions algorithm/backpack/Package_Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package backpack;
/**
*@author liujun
*@date: 2018-9-21 下午08:18:31
*@author—Email:ljfirst@mail.ustc.edu.cn
*@description:
*@version 1.0
*/
public class Package_Test {

public static void main(String[] args) {

//背包的重量
int packageweight = 40;
//物品价值、物品重量
Integer[] goodsvalue = {6,6,1,5,2,1,1,9,4,9,9,5,4,5,2};
Integer[] weight = {3,8,3,9,4,6,6,3,9,2,9,8,1,9,7,};

new Backpack_dynamic(goodsvalue, weight, packageweight);
}
}
Binary file modified bin/backpack/Backpack_dynamic.class
Binary file not shown.
Binary file added bin/backpack/Package_Test.class
Binary file not shown.
Binary file modified bin/graph/CompleteMultipartiteGraph.class
Binary file not shown.
9 changes: 8 additions & 1 deletion data_structure/graph/CompleteMultipartiteGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ public static void main(String[] args) {
}

}

//找出全相连的点
private Map find(int[][] matrix) {
// TODO Auto-generated method stub

int flag = 1;
//使用map存放全相连的点
Map<Integer, Integer> map = new HashMap<Integer, Integer>();

//因为矩阵从1开始存储,所以从1开始遍历
for (int i = 1; i < matrix.length; i++) {
for (int j = 1; j < matrix[0].length; j++) {
//默认自身不检查,其他均相连
if (j != i && matrix[i][j] != 1 ) {
break;
}
Expand All @@ -63,11 +67,14 @@ private Map find(int[][] matrix) {
if (flag == matrix[0].length) {
map.put(i, 1);
}
//flag在循环时应该被清零
flag = 1;
}

return map;
}

//判断剩下元素是否存在关联
private boolean judge(int[][] matrix) {

CompleteMultipartiteGraph m = new CompleteMultipartiteGraph();
Expand Down
4 changes: 2 additions & 2 deletions dynamic_program/lcs/LCS.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import java.util.List;

/**
*@author liujun
*@author
*@date: 2018-7-19 Time:上午10:15:35
*@author—Email:ljfirst@mail.ustc.edu.cn
*@author—Email:@mail.ustc.edu.cn
*@description:LCS算法,找出两个字符串最大匹配子串。
*@version 1.0
*/
Expand Down
4 changes: 2 additions & 2 deletions dynamic_program/lcs/LCS_optimize.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package lcs;
/**
*@author liujun
*@author
*@date: 2018-7-19 上午10:52:22
*@author—Email:ljfirst@mail.ustc.edu.cn
*@author—Email:@mail.ustc.edu.cn
*@description:lcs改进算法,只需一行数组即可以获取匹配数最大值,节省了空间,
*但是无法获取匹配匹配的字符串。
*@version 1.0
Expand Down

0 comments on commit e5a2404

Please sign in to comment.