?? graph.cls
字號:
Next i
For i = 0 To Row
For j = 0 To Row
For k = 0 To Row
If (A(j, i) <> 0 And A(i, k) <> 0) Then
If (A(j, i) + A(i, k)) < A(j, k) Then
A(j, k) = A(j, i) + A(i, k)
sPath(j, k) = V(j) + "、" + V(i) + "、" + V(k)
End If
End If
Next k
Next j
Next i
For i = 0 To Row
For j = 0 To Row
Res(i, j) = A(i, j)
sP(i, j) = sPath(i, j)
Next j
Next i
End Sub
'**************************************************************************
'判斷入度為0的頂點,就是找鄰接矩陣行方向累計和為0的那一列 *
'**************************************************************************
Private Function Indeger(ByVal n As Integer) As Integer
Dim sPoint As Integer
Dim i As Integer
sPoint = 0
For i = 0 To Row
If A(i, n) Then sPoint = sPoint + A(i, n)
Next i
Indeger = sPoint
End Function
'**************************************************************************
'拓撲排序,基本算法就是找入度為零的頂點,然后放入一個棧中,再以此頂點找鄰接矩陣 *
'的所在行,將該行上的頂點連結值改為零(相當與擦去與這個頂點連接的?。僭谠撔?*
'上找入度為零的頂點進入棧。循環,出棧一個頂點,重復上述步驟,直到??? *
'**************************************************************************
Public Sub TopoSort(ByRef Res() As String)
Dim i As Integer
Dim j As Integer
Dim m As Integer
'找到開始的頂點,開始的頂點全部進棧
For i = 0 To Row
If Indeger(i) = 0 Then
MyStack.Push i
End If
Next i
j = 0
While Not MyStack.IsStackEmpty()
m = MyStack.Pop()
Res(j) = V(m)
j = j + 1
For i = 0 To Row
If A(m, i) <> 0 Then
A(m, i) = 0
If Indeger(i) = 0 Then MyStack.Push i
End If
Next i
Wend
End Sub
'**************************************************************************
'判斷出度為0的頂點,就是找鄰接矩陣行方向累計和為0的那一列 *
'**************************************************************************
Private Function Outdeger(ByVal n As Integer) As Integer
Dim sPoint As Integer
Dim i As Integer
sPoint = 0
For i = 0 To Row
If A(n, i) <> 0 Then sPoint = sPoint + A(n, i)
Next i
Outdeger = sPoint
End Function
Private Function GetArc(ByVal n As Integer, ByVal m As Integer) As Integer
Dim i As Integer
If m > Row Then GetArc = -1: Exit Function
For i = m To Row
If A(n, i) <> 0 Then GetArc = i: Exit Function
Next i
GetArc = -1
End Function
'關鍵路徑。
Private Function TopologicalOrder() As Boolean
Dim i As Integer
Dim j As Integer
Dim m As Integer
Dim Count As Integer
Dim AX() As Integer
ReDim AX(Row + 1, Row + 1) As Integer
For i = 0 To Row
For j = 0 To Row
AX(i, j) = A(i, j)
Next j
Next i
Count = 0
For i = 0 To Row
If Indeger(i) = 0 Then
SG.Push i
End If
Next i
While Not SG.IsStackEmpty()
m = SG.Pop()
TG.Push m
Count = Count + 1
For i = 0 To Row
If A(m, i) <> 0 Then
If (VE(m) + AX(m, i)) > VE(i) Then VE(i) = VE(m) + AX(m, i)
A(m, i) = 0
If Indeger(i) = 0 Then SG.Push i
End If
Next i
Wend
For i = 0 To Row
For j = 0 To Row
A(i, j) = AX(i, j)
Next j
Next i
If Count < Row Then
TopologicalOrder = False
Else
TopologicalOrder = True
End If
End Function
Public Function AOE() As Boolean
Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim Vi As Integer
Dim NotC As Boolean
Dim AX() As Integer
ReDim AX(Row + 1, Row + 1) As Integer
For i = 0 To Row
For j = 0 To Row
AX(i, j) = A(i, j)
Next j
Next i
'檢查是否滿足AOE網絡的條件,首先是AOE只能有一個源點、一個匯點
'其次是檢查AOE網絡是否有回路,有回路也退出
'本程序沒檢查回路問題,回路檢查實際是拓撲排序,因為有回路,則
'肯定有構成回路的頂點循環入棧,入棧次數超過一次。
'先檢查出度為0的頂點
NotC = TopologicalOrder()
If NotC = False Then AOE = NotC: Exit Function
For i = 0 To Row
VL(i) = VE(i)
Next i
While (Not TG.IsStackEmpty)
j = TG.Pop
k = 0
While (k >= 0)
m = GetArc(j, k)
If m >= 0 Then
dut = A(j, m)
If (VL(m) - dut < VL(j)) Then VL(j) = VL(m) - dut
A(j, m) = 0
End If
k = m
Wend
Wend
For i = 0 To Row
For j = 0 To Row
A(i, j) = AX(i, j)
Next j
Next i
Form1.List1.Clear
For j = 0 To Row
m = 0
k = 0
While (m >= 0)
k = GetArc(j, m)
If k >= 0 Then
dut = A(j, k)
ee = VE(j)
el = VL(k) - dut
If ee = el Then
Tag = "是"
Else
Tag = "否"
End If
A(j, k) = 0
Form1.List1.AddItem V(j) + " " + V(k) + " " + Str(dut) + " " + Str(ee) + " " + Str(el) + " " + Tag
End If
m = k
Wend
Next j
End Function
Public Sub Dijkstra(ByVal V0 As Integer, ByRef Distance() As Integer, ByRef iPath() As Integer, ByRef sPath() As String)
Dim s() As Integer
Dim tmp() As String
Dim MinDis As Integer
ReDim s(Row + 1) As Integer
ReDim tmp(Row + 1) As String
For i = 0 To Row
For j = 0 To Row
If i <> j And A(i, j) = 0 Then A(i, j) = MaxWeight
Next j
Next i
For i = 0 To Row
tmp(i) = ""
sPath(i) = V(V0) + "->"
Next i
For i = 0 To Row
Distance(i) = A(V0, i)
s(i) = 0
If i <> V0 And Distance(i) < MaxWeight Then
iPath(i) = V0
Else
iPath(i) = -1
End If
Next i
s(V0) = 1
For i = 1 To Row
MinDis = MaxWeight
For j = 0 To Row
If (s(j) = 0 And Distance(j) < MinDis) Then
u = j
MinDis = Distance(j)
End If
Next j
If MinDis = MaxWeight Then Exit Sub
s(u) = 1
For j = 0 To Row
If (A(u, j) < MaxWeight And u <> j) Then
If (s(j) = 0 And Distance(u) + A(u, j) <= Distance(j)) Then
Distance(j) = Distance(u) + A(u, j)
iPath(j) = u
If (Visited(u) = 0) Then
sPath(j) = sPath(j) + V(u) + "->" + V(j)
Else
sPath(j) = sPath(u) + "->" + V(j)
End If
Visited(j) = 1
End If
Else
If Visited(j) = 0 Then
sPath(j) = sPath(j) + V(j)
Visited(j) = 1
End If
End If
Next j
Next i
End Sub
'析構過程,刪除全部使用過的數組
Private Sub Class_Terminate()
Erase A
Erase E
Erase V
Erase Visited
Erase Result
Set ResultTree = Nothing
End Sub
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -