| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 501 人关注过本帖
标题:关于Dijkstra算法的问题
取消只看楼主 加入收藏
shmily009
Rank: 1
等 级:新手上路
帖 子:6
专家分:5
注 册:2007-3-10
收藏
 问题点数:0 回复次数:2 
关于Dijkstra算法的问题
假定无向图为:
图片附件: 游客没有浏览图片的权限,请 登录注册


用于查的两公交站之前最少车站数,查找3到14路不正确,大家看看出了什么问题~详看注释...

程序代码:
Option Explicit

Private Sub Command1_Click()
    Dim a As Long, b As Long
    Dim w() As Long
    Dim tNum As Long
    Dim i As Long, j As Long
   
    tNum = 15
    '初始化权值,与自己的距离为0,若I与J不相邻为-1
    ReDim w(tNum - 1, tNum - 1)
    For i = 0 To tNum - 1
        For j = 0 To tNum - 1
            w(i, j) = IIf(i = j, 0, -1)
        Next
    Next
   
    '下面是手工添加的一些权值,方便测试假设各点间距离为都相同
   
    w(0, 1) = 1
    w(0, 2) = 1
    w(1, 2) = 1
    w(1, 4) = 1
    w(1, 0) = 1
    w(2, 0) = 1
    w(2, 1) = 1
    w(2, 4) = 1
    w(4, 1) = 1
    w(4, 2) = 1
    w(4, 3) = 1
    w(4, 7) = 1
    w(4, 8) = 1
    w(4, 11) = 1
    w(3, 4) = 1
    w(3, 5) = 1
    w(5, 3) = 1
    w(5, 7) = 1
    w(5, 6) = 1
    w(5, 12) = 1
    w(6, 5) = 1
    w(6, 7) = 1
    w(6, 9) = 1
    w(7, 4) = 1
    w(7, 5) = 1
    w(7, 6) = 1
    w(7, 9) = 1
    w(8, 4) = 1
    w(8, 9) = 1
    w(8, 10) = 1
    w(9, 8) = 1
    w(9, 7) = 1
    w(9, 6) = 1
    w(10, 8) = 1
    w(11, 4) = 1
    w(12, 5) = 1
    w(12, 13) = 1
    w(13, 12) = 1
    w(13, 14) = 1
    w(14, 13) = 1
   
    '下面调用显示结果
    a = 3
    b = 10
    Text1.Text = Dijkstra(a, b, tNum, w)                    '这里的结果是正常的3-->4-->8-->10
   
    '更换起点和终点
    a = 3
    b = 14
    Text2.Text = Dijkstra(a, b, tNum, w)                    '这里的结果是3-->14,明显不对,正常应该是3-->5-->12-->13-->14
   
End Sub


'改自网上的一个算法
Function Dijkstra(begin_p As Long, end_p As Long, pointNum As Long, w() As Long) As String
   
    Dim d() As Long
    Dim Bj() As Boolean
    Dim pre() As Long
   
    Dim i As Long
    Dim j As Long
    Dim n As Long
   
    n = pointNum - 1
   
    ReDim d(n)
    ReDim Bj(n)
    ReDim pre(n)
   
    For i = 0 To n
        d(i) = -1
    Next
   
    For i = 0 To n
        Bj(i) = False
    Next
   
    For i = 0 To n
        d(i) = w(begin_p, i)
        pre(i) = begin_p
    Next
   
    Dim dis As Long
    Dim p_num As Long
   
    Bj(begin_p) = True
   
    For i = 0 To n
       
        dis = -1
        p_num = -1
       
        For j = 0 To n
           
            If Not Bj(j) Then
                If (dis = -1) Or (d(j) > 0 And dis > d(j)) Then
                    dis = d(j)
                    p_num = j
                End If
            End If
        Next
       
       
        If p_num > 0 Then
           
            Bj(p_num) = True
           
            For j = 0 To n
               
                If w(p_num, j) > 0 And Not Bj(j) Then
                   
                    If (d(j) = -1) Or (d(j) > -1 And d(p_num) + w(p_num, j) < d(j)) Then
                        d(j) = d(p_num) + w(p_num, j)
                        pre(j) = p_num
                    End If
                End If
            Next
        Else
            Exit For
        End If
    Next
   
    Dim str As String
   
    i = end_p
    str = Trim(i)
    Do While (i <> begin_p)
        i = pre(i)
        str = Trim(i) & "-->" & str
    Loop
   
    Dijkstra = str
   
End Function


搜索更多相关主题的帖子: color 
2013-04-06 21:52
shmily009
Rank: 1
等 级:新手上路
帖 子:6
专家分:5
注 册:2007-3-10
收藏
得分:0 
版主不好意思哈,原来要审核,发了两次...
2013-04-06 22:12
shmily009
Rank: 1
等 级:新手上路
帖 子:6
专家分:5
注 册:2007-3-10
收藏
得分:0 
非常感谢版主认真热情的回复~

贴出来的算法已经找到问题所在,除了您说的权值没写完整,还忽略“0”结点的情况,加上就OK了~

另外,您给出的程序,如果是找出全路径,那结点3到结点9理论上应该不止三条才对吧,像3-4-7-9也算是一条存在的路径径,给出的例子截图好像没有~
2013-04-09 11:15
快速回复:关于Dijkstra算法的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027032 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved