Skip to content

Commit bc9c752

Browse files
committed
迪杰斯特拉算法
1 parent d1e4fe7 commit bc9c752

3 files changed

Lines changed: 110 additions & 2 deletions

File tree

src/main/java/BreadthFirstSearch.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import java.util.*;
22

3+
/**
4+
* 广度优先搜索;
5+
* 图的搜索算法,对每个节点:搜索其子节点(相连节点),如果该节点被搜索过,那么就跳过,否则加入到搜索节点队列;当前节点完成后,从队列中选择
6+
* 第一个节点继续搜索.直到队列中不再有节点.
7+
*
8+
*/
39
@SuppressWarnings("unused")
410
public class BreadthFirstSearch implements AlgorithmInGraph{
511

@@ -10,7 +16,6 @@ public void showAlgorithm() {
1016
List<Integer> orders = search(graph);
1117
System.out.println("广度优先搜索的节点依次为(从0节点开始搜索):");
1218
System.out.println(Arrays.toString(orders.toArray()));
13-
1419
}
1520

1621
public List<Integer> search(int [][] graph){

src/main/java/Dijkstra.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
public class Dijkstra implements AlgorithmInGraph {
6+
7+
public void showAlgorithm() {
8+
9+
int [][] graph = new int [5][5];
10+
//不可达
11+
for(int i = 0;i<5;i++){
12+
for(int j = 0;j<5;j++){
13+
graph[i][j] = i == j? 0:-1;
14+
}
15+
}
16+
17+
graph[0][1] = 9;
18+
graph[0][2] = 2;
19+
graph[0][3] = 3;
20+
graph[1][3] = 4;
21+
graph[2][4] = 4;
22+
graph[3][2] = 2;
23+
graph[3][4] = 1;
24+
25+
GraphSearchUtil.printGraph(graph);
26+
List<Integer> nodes = doDijkstra(graph,0,4);
27+
System.out.println(Arrays.toString(nodes.toArray()));
28+
}
29+
30+
private List<Integer> doDijkstra(int [][] graph,int start,int end) {
31+
32+
int length = graph.length;
33+
int[] minimumValue = new int[length];
34+
35+
//表示当前不可达
36+
for (int i = 0; i < length; i++) {
37+
minimumValue[i] = Integer.MAX_VALUE;
38+
}
39+
40+
41+
int[] minmimumNode = new int[length];
42+
for(int i=0;i<length;i++){
43+
minmimumNode[i] = -1;
44+
}
45+
46+
List<Integer> examinedNode = new ArrayList<Integer>();
47+
48+
//存放当前最小值的节点
49+
minimumValue[start] = 0;
50+
minmimumNode[start] = start;
51+
52+
53+
54+
int currentNode = start;
55+
56+
int index = 0;
57+
boolean continueSeach = true;
58+
while (continueSeach) {
59+
continueSeach = false;
60+
for (int i = 0; i < length; i++) {
61+
if (graph[currentNode][i] < 0) {
62+
continue;
63+
}
64+
if (graph[currentNode][i] + minimumValue[currentNode] < minimumValue[i]) {
65+
minimumValue[i] = graph[currentNode][i] + minimumValue[currentNode];
66+
minmimumNode[i] = currentNode;
67+
}
68+
}
69+
//已经检查过的节点
70+
examinedNode.add(currentNode);
71+
72+
//从当前节点中选择一个最小的节点值
73+
int minmum = Integer.MAX_VALUE;
74+
for (int i = 0; i < length; i++) {
75+
if (!examinedNode.contains(i) && minimumValue[i] < minmum) {
76+
currentNode = i;
77+
minmum = minimumValue[i];
78+
continueSeach = true;
79+
}
80+
}
81+
}
82+
83+
//不可达
84+
if(minmimumNode[end] == -1){
85+
return null;
86+
}else{
87+
88+
System.out.println(Arrays.toString(minmimumNode));
89+
List<Integer> reversResult = new ArrayList<Integer>();
90+
int node = end;
91+
while(node != start){
92+
reversResult.add(minmimumNode[node]);
93+
node = minmimumNode[node];
94+
}
95+
96+
List<Integer> result = new ArrayList<Integer>();
97+
for(int i = reversResult.size() - 1;i>=0;i--){
98+
result.add(reversResult.get(i));
99+
}
100+
return result;
101+
}
102+
}
103+
}

src/test/java/AlgorithmInGraphTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class AlgorithmInGraphTest {
66

77
@Test
88
public void showAlgorithm() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
9-
AlgorithmInGraph algorithm = (AlgorithmInGraph) Class.forName("BreadthFirstSearch").newInstance();
9+
AlgorithmInGraph algorithm = (AlgorithmInGraph) Class.forName("Dijkstra").newInstance();
1010
algorithm.showAlgorithm();
1111

1212

0 commit comments

Comments
 (0)