Skip to content

Commit 2af21c0

Browse files
committed
Dijkstra and bellman
1 parent 1e33df5 commit 2af21c0

File tree

6 files changed

+171
-19
lines changed

6 files changed

+171
-19
lines changed

.idea/workspace.xml

Lines changed: 18 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

g7.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
5 8
2+
0 1 4
3+
0 2 2
4+
1 2 1
5+
1 3 2
6+
1 4 3
7+
2 3 4
8+
2 4 5
9+
3 4 1
2.4 KB
Binary file not shown.

out/production/Graph/Dijkstra.class

1.81 KB
Binary file not shown.

src/BellmanFord.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.util.Arrays;
2+
3+
public class BellmanFord {
4+
private WeightedGraph G;
5+
private int s;
6+
private int[] dis;
7+
private boolean hasNegCycle = false; // 是否包含负权环
8+
9+
public BellmanFord(WeightedGraph G, int s){
10+
11+
this.G = G;
12+
13+
G.validateVertex(s);
14+
this.s = s;
15+
16+
dis = new int[G.V()];
17+
Arrays.fill(dis, Integer.MAX_VALUE);
18+
dis[s] = 0;
19+
20+
// 进行v-1轮循环
21+
for(int pass = 1; pass < G.V(); pass ++){
22+
//对所有的边进行松弛操作
23+
for(int v = 0; v < G.V(); v ++)
24+
for(int w: G.adj(v))
25+
// 对这条边 从原点到v+ v到w的权重,小于dis[w] ,更新dis[w]
26+
if(dis[v] != Integer.MAX_VALUE && dis[v] + G.getWeight(v, w) < dis[w])
27+
dis[w] = dis[v] + G.getWeight(v, w);
28+
}
29+
30+
for(int v = 0; v < G.V(); v ++)
31+
for(int w : G.adj(v))
32+
if(dis[v] != Integer.MAX_VALUE && dis[v] + G.getWeight(v, w) < dis[w])
33+
hasNegCycle = true;
34+
35+
}
36+
37+
public boolean hasNegativeCycle(){
38+
return hasNegCycle;
39+
}
40+
41+
public boolean isConnectedTo(int v){
42+
G.validateVertex(v);
43+
return dis[v] != Integer.MAX_VALUE;
44+
}
45+
46+
public int distTo(int v){
47+
G.validateVertex(v);
48+
if(hasNegCycle) throw new RuntimeException("exist negative cycle.");
49+
return dis[v];
50+
}
51+
52+
static public void main(String[] args){
53+
54+
WeightedGraph g = new WeightedGraph("g7.txt");
55+
BellmanFord bf = new BellmanFord(g, 0);
56+
if(!bf.hasNegativeCycle()){
57+
for(int v = 0; v < g.V(); v ++)
58+
System.out.print(bf.distTo(v) + " ");
59+
System.out.println();
60+
}
61+
else
62+
System.out.println("exist negative cycle.");
63+
64+
65+
}
66+
67+
}

src/Dijkstra.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import java.util.Arrays;
2+
3+
// 单源最短路径的Dijkstra算法(从源点到其他节点的最短路径
4+
// 不能处理权重为负数
5+
public class Dijkstra {
6+
private WeightedGraph G;
7+
private int s; //源点
8+
private int[] dis ; // 原点到各个顶点的距离
9+
private boolean[] visited;
10+
11+
public Dijkstra(WeightedGraph G,int s){
12+
this.G = G;
13+
G.validateVertex(s);
14+
this.s = s;
15+
16+
dis = new int[G.V()];
17+
Arrays.fill(dis,Integer.MAX_VALUE); // 初始化路径最大
18+
dis[0] = 0 ; //源点
19+
20+
visited = new boolean[G.V()];
21+
22+
// Dijkstra
23+
while(true){
24+
int curdis = Integer.MAX_VALUE; //当前找到的最小距离值
25+
int cur = -1; //当前点
26+
System.out.println(curdis);
27+
28+
// 寻找最小值
29+
// 如第一次循环,寻找到0,其他都是等于Integer.MAX_VALUE
30+
// 第二次循环,0已经visited , 寻找他们的最小值,等等。
31+
// 如第二次找到weight2 4,那么0到weight为2的顶点肯定是最小路径,因为0经过其他点回到2会经过4
32+
// 下面同理,以weight2这个顶点开始找下面的。。
33+
for(int v=0;v<G.V();v++){
34+
if(!visited[v] && dis[v] < curdis){
35+
36+
curdis = dis[v]; //
37+
cur = v;
38+
}
39+
}
40+
if(cur == -1) break;
41+
42+
visited[cur] = true;
43+
44+
// 当前点遍历相邻,如第一次从0开始
45+
for(int w:G.adj(cur)){
46+
if(!visited[w]){
47+
// 从cur到w的长度 小于w的长度 (dis默认是无限大)
48+
// 如将无限大更新成实际值
49+
// 逐步更新dis
50+
if(dis[cur] + G.getWeight(cur,w) < dis[w] ){
51+
dis[w] = dis[cur] + G.getWeight(cur,w);
52+
53+
}
54+
}
55+
56+
}
57+
58+
}
59+
}
60+
61+
public boolean isConnectedTo(int v){
62+
G.validateVertex(v);
63+
return visited[v];
64+
}
65+
66+
public int disTo(int v){
67+
G.validateVertex(v);
68+
return dis[v];
69+
}
70+
71+
public static void main(String[] args){
72+
73+
WeightedGraph g = new WeightedGraph("g7.txt");
74+
Dijkstra d = new Dijkstra(g,0);
75+
System.out.println(d.disTo(3));
76+
}
77+
}

0 commit comments

Comments
 (0)