Skip to content

Commit 78e2a92

Browse files
committed
bellman finish
1 parent a4dde4d commit 78e2a92

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Bellman.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,27 @@ func initEdge() {
4242
}
4343

4444
func Bellman() {
45+
//nodeNum -1 自身节点需要去除
4546
for i := 0; i < nodeNum-1; i++ {
47+
//查找已知权重节点 相连接节点 并且更新权重
4648
for j := 0; j < edgeNum; j++ {
47-
//开始节点 权值 >
4849
if dist[edge[j].v] > dist[edge[j].u]+edge[j].weight {
4950
dist[edge[j].v] = dist[edge[j].u] + edge[j].weight
5051
}
51-
fmt.Println(dist)
52+
5253
}
5354
}
55+
fmt.Println(dist)
5456

57+
//不存在负环路时,都有 v.d < = u.d + w ( u , v )
58+
for i := 0; i < edgeNum; i++ {
59+
if dist[edge[i].v] > dist[edge[i].u]+edge[i].weight {
60+
//存在负环路时,一定存在某条边使得 v.d >u.d + w ( u , v )
61+
fmt.Println("Find 负环路")
62+
return
63+
}
64+
}
65+
//另一种方案 从start出发。不断维护每个点的最短距离,如果有负权环,则会进行无数次的维护,越来越小,所以如果循环次数大于了V - 1则有负权环。
5566
}
5667

5768
func main() {

0 commit comments

Comments
 (0)