From 530835874399c7c93619d273f959340663ce57e8 Mon Sep 17 00:00:00 2001 From: Xushier Date: Sat, 26 Oct 2024 23:06:57 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AE=97=E6=B3=95=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 + .idea/Algorithm-notes/Algorithm/.gitignore | 2 + .../Algorithm/.idea/.gitignore | 3 + .../Algorithm-notes/Algorithm/.idea/misc.xml | 6 + .../Algorithm/.idea/modules.xml | 8 + .../Algorithm/.idea/uiDesigner.xml | 124 +++++++++++ .idea/Algorithm-notes/Algorithm/.idea/vcs.xml | 6 + .idea/Algorithm-notes/Algorithm/Algorithm.iml | 11 + .../BinarySearchNoRecur.java | 34 +++ .../Algorithm/src/dac/Hanoitower.java | 22 ++ .../src/dynamic/KnapsackProblem.java | 67 ++++++ .../Algorithm/src/greed/Active/action.java | 47 +++++ .../Algorithm/src/greed/GreedAlgorithm.java | 76 +++++++ .../src/greed/change/MakeChange.java | 29 +++ .../Algorithm/src/kmp/KMPAlgrithm.java | 63 ++++++ .../Algorithm/src/kmp/violenceMatch.java | 37 ++++ .../Algorithm/src/kruskal/KruskalCase.java | 193 ++++++++++++++++++ .../Algorithm/src/prim/PrimAlgorithm.java | 97 +++++++++ .idea/algorithm-notes.iml | 9 + .idea/misc.xml | 6 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + 22 files changed, 862 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/Algorithm-notes/Algorithm/.gitignore create mode 100644 .idea/Algorithm-notes/Algorithm/.idea/.gitignore create mode 100644 .idea/Algorithm-notes/Algorithm/.idea/misc.xml create mode 100644 .idea/Algorithm-notes/Algorithm/.idea/modules.xml create mode 100644 .idea/Algorithm-notes/Algorithm/.idea/uiDesigner.xml create mode 100644 .idea/Algorithm-notes/Algorithm/.idea/vcs.xml create mode 100644 .idea/Algorithm-notes/Algorithm/Algorithm.iml create mode 100644 .idea/Algorithm-notes/Algorithm/src/binarysearchnorecurision/BinarySearchNoRecur.java create mode 100644 .idea/Algorithm-notes/Algorithm/src/dac/Hanoitower.java create mode 100644 .idea/Algorithm-notes/Algorithm/src/dynamic/KnapsackProblem.java create mode 100644 .idea/Algorithm-notes/Algorithm/src/greed/Active/action.java create mode 100644 .idea/Algorithm-notes/Algorithm/src/greed/GreedAlgorithm.java create mode 100644 .idea/Algorithm-notes/Algorithm/src/greed/change/MakeChange.java create mode 100644 .idea/Algorithm-notes/Algorithm/src/kmp/KMPAlgrithm.java create mode 100644 .idea/Algorithm-notes/Algorithm/src/kmp/violenceMatch.java create mode 100644 .idea/Algorithm-notes/Algorithm/src/kruskal/KruskalCase.java create mode 100644 .idea/Algorithm-notes/Algorithm/src/prim/PrimAlgorithm.java create mode 100644 .idea/algorithm-notes.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/Algorithm-notes/Algorithm/.gitignore b/.idea/Algorithm-notes/Algorithm/.gitignore new file mode 100644 index 0000000..267c321 --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/.gitignore @@ -0,0 +1,2 @@ +# 项目排除路径 +/out/ \ No newline at end of file diff --git a/.idea/Algorithm-notes/Algorithm/.idea/.gitignore b/.idea/Algorithm-notes/Algorithm/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Algorithm-notes/Algorithm/.idea/misc.xml b/.idea/Algorithm-notes/Algorithm/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/Algorithm-notes/Algorithm/.idea/modules.xml b/.idea/Algorithm-notes/Algorithm/.idea/modules.xml new file mode 100644 index 0000000..4ad0bd5 --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/Algorithm-notes/Algorithm/.idea/uiDesigner.xml b/.idea/Algorithm-notes/Algorithm/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/Algorithm-notes/Algorithm/.idea/vcs.xml b/.idea/Algorithm-notes/Algorithm/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/Algorithm-notes/Algorithm/Algorithm.iml b/.idea/Algorithm-notes/Algorithm/Algorithm.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/Algorithm.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/Algorithm-notes/Algorithm/src/binarysearchnorecurision/BinarySearchNoRecur.java b/.idea/Algorithm-notes/Algorithm/src/binarysearchnorecurision/BinarySearchNoRecur.java new file mode 100644 index 0000000..eeaec83 --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/src/binarysearchnorecurision/BinarySearchNoRecur.java @@ -0,0 +1,34 @@ +package binarysearchnorecurision; + +public class BinarySearchNoRecur { + public static void main(String[] args){ + int[] arr = {1,2,3,4,5,6,7,8,9}; + int index = binarySearch(arr,7); + System.out.println("找到了" + index); + + } + + //非递归二分查找 + + /** + * + * @param arr 待查找数组 + * @param target 需要查找的数 + * @return 返回对应下标,-1表示没有找到 + */ + public static int binarySearch(int[] arr, int target){ + int left = 0; + int right = arr.length - 1; + while(left <= right){ + int mid = (left + right)/2; + if(arr[mid] == target){ + return mid; + }else if(arr[mid] > target){//需要向左边查找 + right = mid - 1; + }else { + left = mid + 1;//需要向右边查找 + } + } + return -1; + } +} diff --git a/.idea/Algorithm-notes/Algorithm/src/dac/Hanoitower.java b/.idea/Algorithm-notes/Algorithm/src/dac/Hanoitower.java new file mode 100644 index 0000000..1b2941e --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/src/dac/Hanoitower.java @@ -0,0 +1,22 @@ +package dac; + +public class Hanoitower{ + public static void main(String[] args){ + hanoiTower(5, 'A', 'B', 'C'); + } + //汉诺塔移动问题 + //使用分治算法 + public static void hanoiTower(int num, char a, char b, char c){ + if(num == 1){ + System.out.println("第1个盘从" + a + "->" + c); + }else { + //先把最上面的所有盘A->B,移动过程会使用到C + hanoiTower(num - 1, a, c, b); + //2.把最下面的盘A-C + System.out.println("第" + num + "个盘从" + a + "->" + c); + //3.把B塔上的所有盘从B-》C,移动过程用到A塔 + hanoiTower(num - 1, b, a, c); + } + } + +} diff --git a/.idea/Algorithm-notes/Algorithm/src/dynamic/KnapsackProblem.java b/.idea/Algorithm-notes/Algorithm/src/dynamic/KnapsackProblem.java new file mode 100644 index 0000000..43e790e --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/src/dynamic/KnapsackProblem.java @@ -0,0 +1,67 @@ +package dynamic; + +public class KnapsackProblem { + public static void main(String[] args){ + int[] w = {1,4,3};//物品重量 + int[] val = {1500,3000,2000};//物品价值 + int m = 4;//背包容量 + int n = val.length; + + //创建二维数组 + //v[i][j]表示前i个物品中能够装入容量为j的背包中的最大价值 + int[][] v = new int[n + 1][m + 1]; + int[][] path = new int[n + 1][m + 1]; + + for(int i =0; i j) {//如果商品重量>背包容量 + v[i][j] = v[i - 1][j]; + } else { + /** + * 方法一 + */ + //v[i][j] = Math.max(v[i - 1][j], val[i - 1] + v[i-1][j - w[i - 1]]); + // j - w[i - 1]背包剩余容量 + //在之前放进去的最大价值的物品中,与所有物品中选择两个以上物品放入加起来的价值相比,取较大值; + + /** + * 加入标记 + */ + if (v[i - 1][j] < val[i - 1] + v[i - 1][j - w[i - 1]]) { + v[i][j] = val[i - 1] + v[i - 1][j - w[i - 1]]; + path[i][j] = 1; + } else { + v[i][j] = v[i - 1][j]; + } + } + } + } + for(int i= 0 ; i< v.length; i++){ + for(int j = 0; j< v[0].length; j++){ + System.out.print(v[i][j] + " "); + } + System.out.println(); + } + + //输出放入的商品 + int i= path.length - 1; + int j = path[0].length - 1; + while(i > 0 && j > 0){ + if(path[i][j] == 1){ + System.out.printf("第%d个商品放入背包\n",i); + j -= w[i - 1]; + } + i--; + } + } +} diff --git a/.idea/Algorithm-notes/Algorithm/src/greed/Active/action.java b/.idea/Algorithm-notes/Algorithm/src/greed/Active/action.java new file mode 100644 index 0000000..58c08c8 --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/src/greed/Active/action.java @@ -0,0 +1,47 @@ +package greed.Active; + +import java.util.Arrays; +import java.util.Scanner; + +/** + * 将数组按照结束时间早晚升序排序 + * 每次选择结束时间最早的活动(贪心策略) + */ +class Time{ + int start; + int end; + + public Time(int start, int end) { + this.start = start; + this.end = end; + } +} + +public class action { + public static void main(String[] args) { + Scanner cin = new Scanner(System.in); + int N = cin.nextInt(); + Time[] arr = new Time[N + 1]; + for(int i=1; i<=N; i++){ + int start = cin.nextInt(); + int end = cin.nextInt(); + arr[i] = new Time(start,end); + } + + Arrays.sort(arr,1,N+1,(a,b)->a.end - b.end); + greed_action(arr,N); + + + + } + public static void greed_action(Time[] arr, int N){ + int i=1,count=1; + for(int j=2; j<=N; j++){ + if(arr[j].start>= arr[i].end){ + i=j; + count++; + } + } + System.out.println(count); + } +} diff --git a/.idea/Algorithm-notes/Algorithm/src/greed/GreedAlgorithm.java b/.idea/Algorithm-notes/Algorithm/src/greed/GreedAlgorithm.java new file mode 100644 index 0000000..bed9f4b --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/src/greed/GreedAlgorithm.java @@ -0,0 +1,76 @@ +package greed; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; + +public class GreedAlgorithm { + public static void main(String[] args){ + HashMap> broadcasts = new HashMap>(); + //将各个电台放入到broadcast + HashSethashSet1 = new HashSet(); + hashSet1.add("北京"); + hashSet1.add("上海"); + hashSet1.add("天津"); + + HashSethashSet2 = new HashSet(); + hashSet2.add("广州"); + hashSet2.add("北京"); + hashSet2.add("深圳"); + + HashSethashSet3 = new HashSet(); + hashSet3.add("成都"); + hashSet3.add("上海"); + hashSet3.add("杭州"); + + HashSethashSet4 = new HashSet(); + hashSet4.add("上海"); + hashSet4.add("天津"); + + HashSethashSet5 = new HashSet(); + hashSet5.add("杭州"); + hashSet5.add("大连"); + + HashSetallAreas = new HashSet(); + allAreas.add("北京"); + allAreas.add("上海"); + allAreas.add("天津"); + allAreas.add("广州"); + allAreas.add("深圳"); + allAreas.add("成都"); + allAreas.add("杭州"); + allAreas.add("大连"); + + //创建ArrayList,存放选择的电台集合 + ArrayListselects = new ArrayList(); + + //定义一个临时集合,在遍历过程中,存放遍历过程中的电台覆盖的地区和当前还没有覆盖的地区的交集 + HashSet tempSet = new HashSet(); + + //定义maxKey,保存再一次遍历过程中,能够覆盖最大未覆盖地区对应的电台key + //如果maxkey,不为null,则会加入到selects + String maxKey = null; + while(allAreas.size() != 0){ + maxKey = null; + //遍历broadcasts,取出对应key + for(String key : broadcasts.keySet()){ + tempSet.clear(); + //得到当前这个key可以覆盖的区域 + HashSet areas = broadcasts.get(key); + tempSet.addAll(areas); + //求出tempSet 和 allAreas 集合的交集; + tempSet.retainAll(allAreas); + if(tempSet.size() > 0 && (maxKey == null || tempSet.size() > broadcasts.get(maxKey).size())){ + maxKey = key;//贪心算法的核心,每次都选最好的 + } + } + if(maxKey != null){ + selects.add(maxKey); + //将已经选择的地区,从allAreaas中去掉,避免重复遍历 + allAreas.removeAll(broadcasts.get(maxKey)); + + } + } + System.out.println("得到的结果是" + selects); + } +} diff --git a/.idea/Algorithm-notes/Algorithm/src/greed/change/MakeChange.java b/.idea/Algorithm-notes/Algorithm/src/greed/change/MakeChange.java new file mode 100644 index 0000000..cb1b486 --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/src/greed/change/MakeChange.java @@ -0,0 +1,29 @@ +package greed.change; + +import java.util.Scanner; + +public class MakeChange { + public static void main(String[] args) { + int N = 7; + Scanner scanner = new Scanner(System.in); + System.out.println("请输入需要找零的money"); + int money = scanner.nextInt(); + int[] count = {3,0,2,1,0,3,5}; + int[] value = {1,2,5,10,20,50,100}; + int answer = solve(N,money,count,value); + System.out.println(answer); + } + public static int solve(int N, int money,int[] count, int value[]){ + int answer = 0; + for(int i=N-1; i>=0; i--){ + int temp = Math.min(money/value[i],count[i]); + money = money - temp*value[i]; + answer += temp; + /** + * 注意,找零的纸币可能不够 + */ + } + if(money > 0) answer = -1; + return answer; + } +} diff --git a/.idea/Algorithm-notes/Algorithm/src/kmp/KMPAlgrithm.java b/.idea/Algorithm-notes/Algorithm/src/kmp/KMPAlgrithm.java new file mode 100644 index 0000000..956068f --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/src/kmp/KMPAlgrithm.java @@ -0,0 +1,63 @@ +package kmp; + +import java.util.Arrays; +//动态规划算法 +public class KMPAlgrithm { + public static void main(String[] args){ + String str1 = "BBC ABCDAB ABCDABCDABDE"; + String str2 = "ABCDABD"; + int[] next = kmpNext("ABCDABD"); + System.out.println("next=" + Arrays.toString(next)); + + int index = kmpSearch(str1,str2,next); + System.out.println("index=" + index); + + } + + /** + * + * @param str1 源字符串 + * @param str2 子串 + * @param next 部分匹配表 + * @return 没有找到返回-1,找到返回第一个位置 + */ + public static int kmpSearch(String str1,String str2,int[] next){ + //遍历 + for(int i = 0, j = 0; i < str1.length(); i++){ + //需要处理str1.chatAt(i) != str2.chatAt(j) + while(j > 0 && str1.charAt(i) != str2.charAt(j)){ + j = next[j - 1]; + } + if(str1.charAt(i) == str2.charAt(j)){ + j++; + } + if(j == str2.length()){//找到了 + return i - j + 1; + } + } + return -1; + } + + /** + * + * @param dest 需要查找的字符串 + * @return 需要查找的字符串的匹配表,一个数组 + */ + public static int[] kmpNext(String dest){ + //创建一个next数组保存部分匹配值 + int[] next = new int[dest.length()]; + next[0] = 0;//一个字符既没有前缀也没有后缀,部分匹配值为0 + for(int i = 1,j = 0; i < dest.length(); i++){ + //当dest.charAt(i) != dest.charAt(j),我们需要从next[j - 1]获取新的j; + while(j > 0 && dest.charAt(i) != dest.charAt(j)){ + j = next[j - 1]; + } + //当dest.charAt(i) == dest.charAt(j)满足时,即前缀和后缀相同,部分匹配值+1; + if(dest.charAt(i) == dest.charAt(j)){ + j++; + } + next[i] = j; + } + return next; + } +} diff --git a/.idea/Algorithm-notes/Algorithm/src/kmp/violenceMatch.java b/.idea/Algorithm-notes/Algorithm/src/kmp/violenceMatch.java new file mode 100644 index 0000000..6646ff5 --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/src/kmp/violenceMatch.java @@ -0,0 +1,37 @@ +package kmp; + +public class violenceMatch { + public static void main(String[] args){ + String str1 = "I like like like like you"; + String str2 = "like you~"; + int index = violenceMatch(str1,str2); + System.out.println("index=" + index); + } + + //暴力匹配算法 + public static int violenceMatch(String str1, String str2){ + char[] s1 = str1.toCharArray();//将字符串转化为字符数组 + char[] s2 = str2.toCharArray(); + + int s1len = s1.length; + int s2len = s2.length; + + int i = 0; + int j = 0; + while(i < s1len && j < s2len){ + if(s1[i] == s2[j]){ + i++; + j++; + }else{ + i = i-(j - 1);//从匹配的第一个字符的后一个位置重新开始匹配 + j = 0; + } + } + //判断是否匹配成功 + if(j == s2len){ + return i - j; + }else{ + return - 1; + } + } +} diff --git a/.idea/Algorithm-notes/Algorithm/src/kruskal/KruskalCase.java b/.idea/Algorithm-notes/Algorithm/src/kruskal/KruskalCase.java new file mode 100644 index 0000000..9dab807 --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/src/kruskal/KruskalCase.java @@ -0,0 +1,193 @@ +package kruskal; + +import java.util.Arrays; + +public class KruskalCase { + private int edgeNum;// + private char[] vertexs; + private int[][] matrix; + + private static final int INF = Integer.MAX_VALUE; + + public static void main(String[] args) { + char[] vertexs = {'A', 'B', 'C', 'D', 'E', 'F', 'G'}; + int matrix[][] = { + {0, 12, INF, INF, INF, 16, 14}, + {12, 0, 10, INF, INF, 7, INF}, + {INF, 10, 0, 3, 5, 6, INF}, + {INF, INF, 3, 0, 4, INF, INF}, + {INF, INF, 5, 4, 0, 2, 8}, + {16, 7, 6, INF, 2, 0, 9}, + {14, INF, INF, INF, 8, 9, 0}, + }; + KruskalCase kruskalCase = new KruskalCase(vertexs, matrix); + kruskalCase.print(); + EDate[] edges = kruskalCase.getEdges(); + System.out.println("xx=" + Arrays.toString(edges)); + kruskalCase.sortEdge(edges); + System.out.println("排序=" + Arrays.toString(edges)); + kruskalCase.kruskal(); + + + } + + //构造器 + + /** + * @param vertexs 顶点数组 + * @param matrix 邻接矩阵 + */ + public KruskalCase(char[] vertexs, int[][] matrix) { + int velen = vertexs.length; + this.vertexs = new char[velen]; + //初始化节点 + for (int i = 0; i < vertexs.length; i++) { + this.vertexs[i] = vertexs[i]; + } + //初始化邻接矩阵 + this.matrix = new int[velen][velen]; + for (int i = 0; i < velen; i++) { + for (int j = 0; j < velen; j++) { + this.matrix[i][j] = matrix[i][j]; + } + } + //统计边,不统计自己通向自己的边 + for (int i = 0; i < velen; i++) { + for (int j = i+1; j < velen; j++) { + if (this.matrix[i][j] != INF) { + edgeNum++; + } + } + } + } + + public void kruskal() { + int index = 0; + int[] ends = new int[edgeNum]; + //创建结果数组保存最后的最小生成树 + EDate[] rets = new EDate[edgeNum]; + //获取图中所有边的集合 + EDate[] edges = getEdges(); + //排序 + sortEdge(edges); + //遍历edges数组,判断是否构成回路,如果没有加入rets,否则不能加入 + for(int i=0; i < edgeNum; i++){ + int p1 = getPosition(edges[i].star); + int p2 = getPosition(edges[i].end); + //获取p1这个顶点在已有最小生成树中的终点 + int m = getEnd(ends,p1); + int n = getEnd(ends,p2); + if(m != n){//没有构成回路 + ends[m] = n;//设置m在最小生成树中的终点 + rets[index++] = edges[i]; + } + } + System.out.println("最小生成树="); + for(int i=0;i edges[j + 1].weight) { + EDate temp = edges[j]; + edges[j] = edges[j + 1]; + edges[j + 1] = temp; + } + } + } + } + + /** + * @param ch 顶点对应的值,如'A' + * @return ch对应的下标,找不到返回-1 + */ + + private int getPosition(char ch) { + for (int i = 0; i < vertexs.length; i++) { + if (vertexs[i] == ch) { + return i; + } + } + return -1; + } + + /** + * 功能:获取图中的边,放进EData数组中,后面需要遍历该数组 + * 通过邻接矩阵获取 + * EDate[]形式['A','B',12] + * + * @return + */ + private EDate[] getEdges() { + int index = 0; + EDate[] edges = new EDate[edgeNum]; + for (int i = 0; i < vertexs.length; i++) { + for (int j = i+1; j < vertexs.length; j++) { + if (matrix[i][j] != INF) { + edges[index++] = new EDate(vertexs[i], vertexs[j], matrix[i][j]); + } + } + } + return edges; + } + + /** + * + * @param ends 数组记录各个顶点的终点是哪个 + * @param i 传入顶点对应的下标 + * @return 返回顶点下标为i的顶点对应的终点下标 + */ + private int getEnd(int[] ends, int i){ + while(ends[i] !=0){ + i = ends[i]; + } + return i; + } +} + + + + class EDate { + char star;//边的一个点 + char end;//边的另外一个点 + int weight;//边的权值 + //构造器 + public EDate(char star,char end, int weight){ + this.star = star; + this.end = end; + this.weight = weight; + } + //重写ToString,便于输出 + @Override + public String toString() { + return "EDate{" + + "star=" + star + + ", end=" + end + + ", weight=" + weight + + '}'; + } + + + } + diff --git a/.idea/Algorithm-notes/Algorithm/src/prim/PrimAlgorithm.java b/.idea/Algorithm-notes/Algorithm/src/prim/PrimAlgorithm.java new file mode 100644 index 0000000..996a170 --- /dev/null +++ b/.idea/Algorithm-notes/Algorithm/src/prim/PrimAlgorithm.java @@ -0,0 +1,97 @@ +package prim; + +import java.util.Arrays; + +public class PrimAlgorithm { + public static void main(String[] args){ + char[] data = new char[]{'A','B','C','D','E','F','G'}; + int verxs = data.length; + int[][]weight = new int[][]{ + {10000,5,7,10000,10000,10000,2}, + {5, 10000, 10000, 9, 10000, 10000, 3}, + {7,10000,10000,10000,8,10000,10000}, + {10000,9,10000,10000,10000,4,10000}, + {10000,10000,8,10000,10000,5,4}, + {10000, 10000, 10000, 4, 5, 10000, 6}, + {2,3,10000,10000,4,6,10000}, + }; + MGraph graph = new MGraph(verxs); + MinTree minTree = new MinTree(); + minTree.createGraph(graph,verxs,data,weight); + minTree.showGraph(graph); + minTree.prim(graph,1); + } +} + +class MinTree{ + /** + * + * @param graph 图对象 + * @param verxs 图对像对应的顶点个数 + * @param data 图的各个顶点的值 + * @param weight 图的邻接矩阵 + */ + public void createGraph(MGraph graph, int verxs, char data[], int[][]weight){ + for(int i = 0; i + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..07115cd --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..fe61067 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file -- Gitee From 1ed2e3d005bf9d807d00cde50327a76f6952846c Mon Sep 17 00:00:00 2001 From: Xushier Date: Sat, 26 Oct 2024 23:17:41 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AE=97=E6=B3=95=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/Algorithm-notes/Algorithm/.gitignore | 2 - .../Algorithm/.idea/.gitignore | 3 - .../Algorithm-notes/Algorithm/.idea/misc.xml | 6 - .../Algorithm/.idea/modules.xml | 8 - .../Algorithm/.idea/uiDesigner.xml | 124 ----------- .idea/Algorithm-notes/Algorithm/.idea/vcs.xml | 6 - .idea/Algorithm-notes/Algorithm/Algorithm.iml | 11 - .../BinarySearchNoRecur.java | 34 --- .../Algorithm/src/dac/Hanoitower.java | 22 -- .../src/dynamic/KnapsackProblem.java | 67 ------ .../Algorithm/src/greed/Active/action.java | 47 ----- .../Algorithm/src/greed/GreedAlgorithm.java | 76 ------- .../src/greed/change/MakeChange.java | 29 --- .../Algorithm/src/kmp/KMPAlgrithm.java | 63 ------ .../Algorithm/src/kmp/violenceMatch.java | 37 ---- .../Algorithm/src/kruskal/KruskalCase.java | 193 ------------------ .../Algorithm/src/prim/PrimAlgorithm.java | 97 --------- 17 files changed, 825 deletions(-) delete mode 100644 .idea/Algorithm-notes/Algorithm/.gitignore delete mode 100644 .idea/Algorithm-notes/Algorithm/.idea/.gitignore delete mode 100644 .idea/Algorithm-notes/Algorithm/.idea/misc.xml delete mode 100644 .idea/Algorithm-notes/Algorithm/.idea/modules.xml delete mode 100644 .idea/Algorithm-notes/Algorithm/.idea/uiDesigner.xml delete mode 100644 .idea/Algorithm-notes/Algorithm/.idea/vcs.xml delete mode 100644 .idea/Algorithm-notes/Algorithm/Algorithm.iml delete mode 100644 .idea/Algorithm-notes/Algorithm/src/binarysearchnorecurision/BinarySearchNoRecur.java delete mode 100644 .idea/Algorithm-notes/Algorithm/src/dac/Hanoitower.java delete mode 100644 .idea/Algorithm-notes/Algorithm/src/dynamic/KnapsackProblem.java delete mode 100644 .idea/Algorithm-notes/Algorithm/src/greed/Active/action.java delete mode 100644 .idea/Algorithm-notes/Algorithm/src/greed/GreedAlgorithm.java delete mode 100644 .idea/Algorithm-notes/Algorithm/src/greed/change/MakeChange.java delete mode 100644 .idea/Algorithm-notes/Algorithm/src/kmp/KMPAlgrithm.java delete mode 100644 .idea/Algorithm-notes/Algorithm/src/kmp/violenceMatch.java delete mode 100644 .idea/Algorithm-notes/Algorithm/src/kruskal/KruskalCase.java delete mode 100644 .idea/Algorithm-notes/Algorithm/src/prim/PrimAlgorithm.java diff --git a/.idea/Algorithm-notes/Algorithm/.gitignore b/.idea/Algorithm-notes/Algorithm/.gitignore deleted file mode 100644 index 267c321..0000000 --- a/.idea/Algorithm-notes/Algorithm/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# 项目排除路径 -/out/ \ No newline at end of file diff --git a/.idea/Algorithm-notes/Algorithm/.idea/.gitignore b/.idea/Algorithm-notes/Algorithm/.idea/.gitignore deleted file mode 100644 index 26d3352..0000000 --- a/.idea/Algorithm-notes/Algorithm/.idea/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml diff --git a/.idea/Algorithm-notes/Algorithm/.idea/misc.xml b/.idea/Algorithm-notes/Algorithm/.idea/misc.xml deleted file mode 100644 index 0548357..0000000 --- a/.idea/Algorithm-notes/Algorithm/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/Algorithm-notes/Algorithm/.idea/modules.xml b/.idea/Algorithm-notes/Algorithm/.idea/modules.xml deleted file mode 100644 index 4ad0bd5..0000000 --- a/.idea/Algorithm-notes/Algorithm/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/Algorithm-notes/Algorithm/.idea/uiDesigner.xml b/.idea/Algorithm-notes/Algorithm/.idea/uiDesigner.xml deleted file mode 100644 index e96534f..0000000 --- a/.idea/Algorithm-notes/Algorithm/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/Algorithm-notes/Algorithm/.idea/vcs.xml b/.idea/Algorithm-notes/Algorithm/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/Algorithm-notes/Algorithm/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/Algorithm-notes/Algorithm/Algorithm.iml b/.idea/Algorithm-notes/Algorithm/Algorithm.iml deleted file mode 100644 index c90834f..0000000 --- a/.idea/Algorithm-notes/Algorithm/Algorithm.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/Algorithm-notes/Algorithm/src/binarysearchnorecurision/BinarySearchNoRecur.java b/.idea/Algorithm-notes/Algorithm/src/binarysearchnorecurision/BinarySearchNoRecur.java deleted file mode 100644 index eeaec83..0000000 --- a/.idea/Algorithm-notes/Algorithm/src/binarysearchnorecurision/BinarySearchNoRecur.java +++ /dev/null @@ -1,34 +0,0 @@ -package binarysearchnorecurision; - -public class BinarySearchNoRecur { - public static void main(String[] args){ - int[] arr = {1,2,3,4,5,6,7,8,9}; - int index = binarySearch(arr,7); - System.out.println("找到了" + index); - - } - - //非递归二分查找 - - /** - * - * @param arr 待查找数组 - * @param target 需要查找的数 - * @return 返回对应下标,-1表示没有找到 - */ - public static int binarySearch(int[] arr, int target){ - int left = 0; - int right = arr.length - 1; - while(left <= right){ - int mid = (left + right)/2; - if(arr[mid] == target){ - return mid; - }else if(arr[mid] > target){//需要向左边查找 - right = mid - 1; - }else { - left = mid + 1;//需要向右边查找 - } - } - return -1; - } -} diff --git a/.idea/Algorithm-notes/Algorithm/src/dac/Hanoitower.java b/.idea/Algorithm-notes/Algorithm/src/dac/Hanoitower.java deleted file mode 100644 index 1b2941e..0000000 --- a/.idea/Algorithm-notes/Algorithm/src/dac/Hanoitower.java +++ /dev/null @@ -1,22 +0,0 @@ -package dac; - -public class Hanoitower{ - public static void main(String[] args){ - hanoiTower(5, 'A', 'B', 'C'); - } - //汉诺塔移动问题 - //使用分治算法 - public static void hanoiTower(int num, char a, char b, char c){ - if(num == 1){ - System.out.println("第1个盘从" + a + "->" + c); - }else { - //先把最上面的所有盘A->B,移动过程会使用到C - hanoiTower(num - 1, a, c, b); - //2.把最下面的盘A-C - System.out.println("第" + num + "个盘从" + a + "->" + c); - //3.把B塔上的所有盘从B-》C,移动过程用到A塔 - hanoiTower(num - 1, b, a, c); - } - } - -} diff --git a/.idea/Algorithm-notes/Algorithm/src/dynamic/KnapsackProblem.java b/.idea/Algorithm-notes/Algorithm/src/dynamic/KnapsackProblem.java deleted file mode 100644 index 43e790e..0000000 --- a/.idea/Algorithm-notes/Algorithm/src/dynamic/KnapsackProblem.java +++ /dev/null @@ -1,67 +0,0 @@ -package dynamic; - -public class KnapsackProblem { - public static void main(String[] args){ - int[] w = {1,4,3};//物品重量 - int[] val = {1500,3000,2000};//物品价值 - int m = 4;//背包容量 - int n = val.length; - - //创建二维数组 - //v[i][j]表示前i个物品中能够装入容量为j的背包中的最大价值 - int[][] v = new int[n + 1][m + 1]; - int[][] path = new int[n + 1][m + 1]; - - for(int i =0; i j) {//如果商品重量>背包容量 - v[i][j] = v[i - 1][j]; - } else { - /** - * 方法一 - */ - //v[i][j] = Math.max(v[i - 1][j], val[i - 1] + v[i-1][j - w[i - 1]]); - // j - w[i - 1]背包剩余容量 - //在之前放进去的最大价值的物品中,与所有物品中选择两个以上物品放入加起来的价值相比,取较大值; - - /** - * 加入标记 - */ - if (v[i - 1][j] < val[i - 1] + v[i - 1][j - w[i - 1]]) { - v[i][j] = val[i - 1] + v[i - 1][j - w[i - 1]]; - path[i][j] = 1; - } else { - v[i][j] = v[i - 1][j]; - } - } - } - } - for(int i= 0 ; i< v.length; i++){ - for(int j = 0; j< v[0].length; j++){ - System.out.print(v[i][j] + " "); - } - System.out.println(); - } - - //输出放入的商品 - int i= path.length - 1; - int j = path[0].length - 1; - while(i > 0 && j > 0){ - if(path[i][j] == 1){ - System.out.printf("第%d个商品放入背包\n",i); - j -= w[i - 1]; - } - i--; - } - } -} diff --git a/.idea/Algorithm-notes/Algorithm/src/greed/Active/action.java b/.idea/Algorithm-notes/Algorithm/src/greed/Active/action.java deleted file mode 100644 index 58c08c8..0000000 --- a/.idea/Algorithm-notes/Algorithm/src/greed/Active/action.java +++ /dev/null @@ -1,47 +0,0 @@ -package greed.Active; - -import java.util.Arrays; -import java.util.Scanner; - -/** - * 将数组按照结束时间早晚升序排序 - * 每次选择结束时间最早的活动(贪心策略) - */ -class Time{ - int start; - int end; - - public Time(int start, int end) { - this.start = start; - this.end = end; - } -} - -public class action { - public static void main(String[] args) { - Scanner cin = new Scanner(System.in); - int N = cin.nextInt(); - Time[] arr = new Time[N + 1]; - for(int i=1; i<=N; i++){ - int start = cin.nextInt(); - int end = cin.nextInt(); - arr[i] = new Time(start,end); - } - - Arrays.sort(arr,1,N+1,(a,b)->a.end - b.end); - greed_action(arr,N); - - - - } - public static void greed_action(Time[] arr, int N){ - int i=1,count=1; - for(int j=2; j<=N; j++){ - if(arr[j].start>= arr[i].end){ - i=j; - count++; - } - } - System.out.println(count); - } -} diff --git a/.idea/Algorithm-notes/Algorithm/src/greed/GreedAlgorithm.java b/.idea/Algorithm-notes/Algorithm/src/greed/GreedAlgorithm.java deleted file mode 100644 index bed9f4b..0000000 --- a/.idea/Algorithm-notes/Algorithm/src/greed/GreedAlgorithm.java +++ /dev/null @@ -1,76 +0,0 @@ -package greed; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; - -public class GreedAlgorithm { - public static void main(String[] args){ - HashMap> broadcasts = new HashMap>(); - //将各个电台放入到broadcast - HashSethashSet1 = new HashSet(); - hashSet1.add("北京"); - hashSet1.add("上海"); - hashSet1.add("天津"); - - HashSethashSet2 = new HashSet(); - hashSet2.add("广州"); - hashSet2.add("北京"); - hashSet2.add("深圳"); - - HashSethashSet3 = new HashSet(); - hashSet3.add("成都"); - hashSet3.add("上海"); - hashSet3.add("杭州"); - - HashSethashSet4 = new HashSet(); - hashSet4.add("上海"); - hashSet4.add("天津"); - - HashSethashSet5 = new HashSet(); - hashSet5.add("杭州"); - hashSet5.add("大连"); - - HashSetallAreas = new HashSet(); - allAreas.add("北京"); - allAreas.add("上海"); - allAreas.add("天津"); - allAreas.add("广州"); - allAreas.add("深圳"); - allAreas.add("成都"); - allAreas.add("杭州"); - allAreas.add("大连"); - - //创建ArrayList,存放选择的电台集合 - ArrayListselects = new ArrayList(); - - //定义一个临时集合,在遍历过程中,存放遍历过程中的电台覆盖的地区和当前还没有覆盖的地区的交集 - HashSet tempSet = new HashSet(); - - //定义maxKey,保存再一次遍历过程中,能够覆盖最大未覆盖地区对应的电台key - //如果maxkey,不为null,则会加入到selects - String maxKey = null; - while(allAreas.size() != 0){ - maxKey = null; - //遍历broadcasts,取出对应key - for(String key : broadcasts.keySet()){ - tempSet.clear(); - //得到当前这个key可以覆盖的区域 - HashSet areas = broadcasts.get(key); - tempSet.addAll(areas); - //求出tempSet 和 allAreas 集合的交集; - tempSet.retainAll(allAreas); - if(tempSet.size() > 0 && (maxKey == null || tempSet.size() > broadcasts.get(maxKey).size())){ - maxKey = key;//贪心算法的核心,每次都选最好的 - } - } - if(maxKey != null){ - selects.add(maxKey); - //将已经选择的地区,从allAreaas中去掉,避免重复遍历 - allAreas.removeAll(broadcasts.get(maxKey)); - - } - } - System.out.println("得到的结果是" + selects); - } -} diff --git a/.idea/Algorithm-notes/Algorithm/src/greed/change/MakeChange.java b/.idea/Algorithm-notes/Algorithm/src/greed/change/MakeChange.java deleted file mode 100644 index cb1b486..0000000 --- a/.idea/Algorithm-notes/Algorithm/src/greed/change/MakeChange.java +++ /dev/null @@ -1,29 +0,0 @@ -package greed.change; - -import java.util.Scanner; - -public class MakeChange { - public static void main(String[] args) { - int N = 7; - Scanner scanner = new Scanner(System.in); - System.out.println("请输入需要找零的money"); - int money = scanner.nextInt(); - int[] count = {3,0,2,1,0,3,5}; - int[] value = {1,2,5,10,20,50,100}; - int answer = solve(N,money,count,value); - System.out.println(answer); - } - public static int solve(int N, int money,int[] count, int value[]){ - int answer = 0; - for(int i=N-1; i>=0; i--){ - int temp = Math.min(money/value[i],count[i]); - money = money - temp*value[i]; - answer += temp; - /** - * 注意,找零的纸币可能不够 - */ - } - if(money > 0) answer = -1; - return answer; - } -} diff --git a/.idea/Algorithm-notes/Algorithm/src/kmp/KMPAlgrithm.java b/.idea/Algorithm-notes/Algorithm/src/kmp/KMPAlgrithm.java deleted file mode 100644 index 956068f..0000000 --- a/.idea/Algorithm-notes/Algorithm/src/kmp/KMPAlgrithm.java +++ /dev/null @@ -1,63 +0,0 @@ -package kmp; - -import java.util.Arrays; -//动态规划算法 -public class KMPAlgrithm { - public static void main(String[] args){ - String str1 = "BBC ABCDAB ABCDABCDABDE"; - String str2 = "ABCDABD"; - int[] next = kmpNext("ABCDABD"); - System.out.println("next=" + Arrays.toString(next)); - - int index = kmpSearch(str1,str2,next); - System.out.println("index=" + index); - - } - - /** - * - * @param str1 源字符串 - * @param str2 子串 - * @param next 部分匹配表 - * @return 没有找到返回-1,找到返回第一个位置 - */ - public static int kmpSearch(String str1,String str2,int[] next){ - //遍历 - for(int i = 0, j = 0; i < str1.length(); i++){ - //需要处理str1.chatAt(i) != str2.chatAt(j) - while(j > 0 && str1.charAt(i) != str2.charAt(j)){ - j = next[j - 1]; - } - if(str1.charAt(i) == str2.charAt(j)){ - j++; - } - if(j == str2.length()){//找到了 - return i - j + 1; - } - } - return -1; - } - - /** - * - * @param dest 需要查找的字符串 - * @return 需要查找的字符串的匹配表,一个数组 - */ - public static int[] kmpNext(String dest){ - //创建一个next数组保存部分匹配值 - int[] next = new int[dest.length()]; - next[0] = 0;//一个字符既没有前缀也没有后缀,部分匹配值为0 - for(int i = 1,j = 0; i < dest.length(); i++){ - //当dest.charAt(i) != dest.charAt(j),我们需要从next[j - 1]获取新的j; - while(j > 0 && dest.charAt(i) != dest.charAt(j)){ - j = next[j - 1]; - } - //当dest.charAt(i) == dest.charAt(j)满足时,即前缀和后缀相同,部分匹配值+1; - if(dest.charAt(i) == dest.charAt(j)){ - j++; - } - next[i] = j; - } - return next; - } -} diff --git a/.idea/Algorithm-notes/Algorithm/src/kmp/violenceMatch.java b/.idea/Algorithm-notes/Algorithm/src/kmp/violenceMatch.java deleted file mode 100644 index 6646ff5..0000000 --- a/.idea/Algorithm-notes/Algorithm/src/kmp/violenceMatch.java +++ /dev/null @@ -1,37 +0,0 @@ -package kmp; - -public class violenceMatch { - public static void main(String[] args){ - String str1 = "I like like like like you"; - String str2 = "like you~"; - int index = violenceMatch(str1,str2); - System.out.println("index=" + index); - } - - //暴力匹配算法 - public static int violenceMatch(String str1, String str2){ - char[] s1 = str1.toCharArray();//将字符串转化为字符数组 - char[] s2 = str2.toCharArray(); - - int s1len = s1.length; - int s2len = s2.length; - - int i = 0; - int j = 0; - while(i < s1len && j < s2len){ - if(s1[i] == s2[j]){ - i++; - j++; - }else{ - i = i-(j - 1);//从匹配的第一个字符的后一个位置重新开始匹配 - j = 0; - } - } - //判断是否匹配成功 - if(j == s2len){ - return i - j; - }else{ - return - 1; - } - } -} diff --git a/.idea/Algorithm-notes/Algorithm/src/kruskal/KruskalCase.java b/.idea/Algorithm-notes/Algorithm/src/kruskal/KruskalCase.java deleted file mode 100644 index 9dab807..0000000 --- a/.idea/Algorithm-notes/Algorithm/src/kruskal/KruskalCase.java +++ /dev/null @@ -1,193 +0,0 @@ -package kruskal; - -import java.util.Arrays; - -public class KruskalCase { - private int edgeNum;// - private char[] vertexs; - private int[][] matrix; - - private static final int INF = Integer.MAX_VALUE; - - public static void main(String[] args) { - char[] vertexs = {'A', 'B', 'C', 'D', 'E', 'F', 'G'}; - int matrix[][] = { - {0, 12, INF, INF, INF, 16, 14}, - {12, 0, 10, INF, INF, 7, INF}, - {INF, 10, 0, 3, 5, 6, INF}, - {INF, INF, 3, 0, 4, INF, INF}, - {INF, INF, 5, 4, 0, 2, 8}, - {16, 7, 6, INF, 2, 0, 9}, - {14, INF, INF, INF, 8, 9, 0}, - }; - KruskalCase kruskalCase = new KruskalCase(vertexs, matrix); - kruskalCase.print(); - EDate[] edges = kruskalCase.getEdges(); - System.out.println("xx=" + Arrays.toString(edges)); - kruskalCase.sortEdge(edges); - System.out.println("排序=" + Arrays.toString(edges)); - kruskalCase.kruskal(); - - - } - - //构造器 - - /** - * @param vertexs 顶点数组 - * @param matrix 邻接矩阵 - */ - public KruskalCase(char[] vertexs, int[][] matrix) { - int velen = vertexs.length; - this.vertexs = new char[velen]; - //初始化节点 - for (int i = 0; i < vertexs.length; i++) { - this.vertexs[i] = vertexs[i]; - } - //初始化邻接矩阵 - this.matrix = new int[velen][velen]; - for (int i = 0; i < velen; i++) { - for (int j = 0; j < velen; j++) { - this.matrix[i][j] = matrix[i][j]; - } - } - //统计边,不统计自己通向自己的边 - for (int i = 0; i < velen; i++) { - for (int j = i+1; j < velen; j++) { - if (this.matrix[i][j] != INF) { - edgeNum++; - } - } - } - } - - public void kruskal() { - int index = 0; - int[] ends = new int[edgeNum]; - //创建结果数组保存最后的最小生成树 - EDate[] rets = new EDate[edgeNum]; - //获取图中所有边的集合 - EDate[] edges = getEdges(); - //排序 - sortEdge(edges); - //遍历edges数组,判断是否构成回路,如果没有加入rets,否则不能加入 - for(int i=0; i < edgeNum; i++){ - int p1 = getPosition(edges[i].star); - int p2 = getPosition(edges[i].end); - //获取p1这个顶点在已有最小生成树中的终点 - int m = getEnd(ends,p1); - int n = getEnd(ends,p2); - if(m != n){//没有构成回路 - ends[m] = n;//设置m在最小生成树中的终点 - rets[index++] = edges[i]; - } - } - System.out.println("最小生成树="); - for(int i=0;i edges[j + 1].weight) { - EDate temp = edges[j]; - edges[j] = edges[j + 1]; - edges[j + 1] = temp; - } - } - } - } - - /** - * @param ch 顶点对应的值,如'A' - * @return ch对应的下标,找不到返回-1 - */ - - private int getPosition(char ch) { - for (int i = 0; i < vertexs.length; i++) { - if (vertexs[i] == ch) { - return i; - } - } - return -1; - } - - /** - * 功能:获取图中的边,放进EData数组中,后面需要遍历该数组 - * 通过邻接矩阵获取 - * EDate[]形式['A','B',12] - * - * @return - */ - private EDate[] getEdges() { - int index = 0; - EDate[] edges = new EDate[edgeNum]; - for (int i = 0; i < vertexs.length; i++) { - for (int j = i+1; j < vertexs.length; j++) { - if (matrix[i][j] != INF) { - edges[index++] = new EDate(vertexs[i], vertexs[j], matrix[i][j]); - } - } - } - return edges; - } - - /** - * - * @param ends 数组记录各个顶点的终点是哪个 - * @param i 传入顶点对应的下标 - * @return 返回顶点下标为i的顶点对应的终点下标 - */ - private int getEnd(int[] ends, int i){ - while(ends[i] !=0){ - i = ends[i]; - } - return i; - } -} - - - - class EDate { - char star;//边的一个点 - char end;//边的另外一个点 - int weight;//边的权值 - //构造器 - public EDate(char star,char end, int weight){ - this.star = star; - this.end = end; - this.weight = weight; - } - //重写ToString,便于输出 - @Override - public String toString() { - return "EDate{" + - "star=" + star + - ", end=" + end + - ", weight=" + weight + - '}'; - } - - - } - diff --git a/.idea/Algorithm-notes/Algorithm/src/prim/PrimAlgorithm.java b/.idea/Algorithm-notes/Algorithm/src/prim/PrimAlgorithm.java deleted file mode 100644 index 996a170..0000000 --- a/.idea/Algorithm-notes/Algorithm/src/prim/PrimAlgorithm.java +++ /dev/null @@ -1,97 +0,0 @@ -package prim; - -import java.util.Arrays; - -public class PrimAlgorithm { - public static void main(String[] args){ - char[] data = new char[]{'A','B','C','D','E','F','G'}; - int verxs = data.length; - int[][]weight = new int[][]{ - {10000,5,7,10000,10000,10000,2}, - {5, 10000, 10000, 9, 10000, 10000, 3}, - {7,10000,10000,10000,8,10000,10000}, - {10000,9,10000,10000,10000,4,10000}, - {10000,10000,8,10000,10000,5,4}, - {10000, 10000, 10000, 4, 5, 10000, 6}, - {2,3,10000,10000,4,6,10000}, - }; - MGraph graph = new MGraph(verxs); - MinTree minTree = new MinTree(); - minTree.createGraph(graph,verxs,data,weight); - minTree.showGraph(graph); - minTree.prim(graph,1); - } -} - -class MinTree{ - /** - * - * @param graph 图对象 - * @param verxs 图对像对应的顶点个数 - * @param data 图的各个顶点的值 - * @param weight 图的邻接矩阵 - */ - public void createGraph(MGraph graph, int verxs, char data[], int[][]weight){ - for(int i = 0; i