加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

【算法】有向无环图的最长路径算法

(2011-02-15 16:12:20)
标签:

结点

节点

拓扑排序

算法

utf-8

it

分类: 算法数据结构
http://blog.163.com/facteur@126/blog/static/23208030200701754823140/

思想:有向无环图的节点之间是一个偏序关系,用拓扑排序获得一个全序,按拓扑序将结点加入集合S,计算从源结点v0通过S中的结点到达下一个节点vi的最长路径。算法类似图的最短路径算法,程序如下:

#include <iostream>

using namespace std;
const int rows=10;

int length[rows][rows]={
      0,    20,    10,    30,    0,    0,    0,    0,    0,      },
      0,    0,    0,    0,    20,    15,    0,    0,    0,      },
      0,    0,    0,    0,    20,    30,    20,    0,    0,      },
      0,    0,    0,    0,    0,    10,    10,    0,    0,      },
      0,    0,    0,    0,    0,    15,    0,    10,    0,      },
      0,    0,    0,    0,    0,    0,    0,    20,    10,      },
      0,    0,    0,    0,    0,    20,    0,    30,    20,      },
      0,    0,    0,    0,    0,    0,    0,    0,    0,    20    },
      0,    0,    0,    0,    0,    0,    0,    0,    0,    10    },
      0,    0,    0,    0,    0,    0,    0,    0,    0,      }
};

int getNextVertex( bool * set )
//获取下一个要加入集合S的结点:它的前驱结点都在集合S内
{
    for( int i=0; i<rows; i++ ){
        if( set[i] ) continue;
        bool found = true;
        for( int j=0; j<rows; j++ ){
            if( set[j] ) continue;
            if( j == i ) continue;
            if( length[j][i]>0 ){
                found = false;
            }
        }
        if( found ) return i;
    }
    return (-1);
}


int longestPath()
{
    bool inSourceSet[rows];
    for( int i=0; i<rows; i++ ){
        inSourceSet[i] = false;
    }
    inSourceSet[0] = true;

    int maxLength[rows]={0};//记录源结点到每个节点的最长路径长度
    string maxPath[rows];//记录源结点到每个节点的最长路径
    for( int j=1; j<rows; j++ ){
        int curVex = getNextVertex( inSourceSet );
        if( -1 == curVex ) continue;
        cout << " " << curVex << endl;
        int curMaxLen=0;
        int preVex=0;
        for( int i=0; i<rows; i++ ){//计算源结点到当前结点的最长路径长度和经过的结点并记录
            if( !inSourceSet[i] ) continue;
            if( (length[i][curVex] > 0) && (curMaxLen < maxLength[i] + length[i][curVex]) ){
                curMaxLen = maxLength[i] + length[i][curVex]; 
                preVex = i;
            }
        }
        maxLength[curVex] = curMaxLen;
        inSourceSet[curVex] = true;
        maxPath[curVex] = maxPath[preVex];
        char temp[10];
        sprintf( temp, "%d->", preVex );
        maxPath[curVex] += temp;

        curMaxLen = 0;
       
    }
    for( int i=0; i<rows; i++ ){
        cout << "Max Length to " << i << ": " << maxLength[i];
        cout << "\tPath :";
        cout << maxPath[i] << i << endl;
    }

    return maxLength[rows-1];//返回到目标结点的最长路径
}

int main( int argc, char * argv[] )
{
    int length = longestPath();
    cout << "Longest path length is " << length << endl;
    return (0);
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有