实现用拓扑排序方法求有向无环图中最长路径长度的算法

编写程序,实现用拓扑排序方法求有向无环图中最长路径长度的算法。

思想:

①以maxDist[v]表示以v为结尾的最长路径,G.Edge存储的是图的边的权值,u是v的直接前驱,那么maxDist[v]=max(maxDist[v],maxDist[u]+G.Edge[u][v])表示:maxDist[v]和maxDist[u]+G.Edge[u][v]中最长的一个。

②顶点的计算顺序应该是和拓扑序列的结果一致。

③最长路径一定是从初始入度为0的顶点开始

④maxDist数组中最大值为所求maxPathvalue结果。

代码:

复制代码
typedef struct {                    // 图的定义
    int numV, numEdges;      // 图中实际的顶点数和边数
    char VerticesList[MAXV];        // 顶点表,MAXV为已定义常量
    int Edge[MAXV][MAXV];           // 邻接矩阵
}MGraph;

//获取每个结点的入度 
int *getIndegree(MGraph *G){
    int *indegree = (int*)malloc(sizeof(int)*G.numV);
    
    //初始化每个顶点的入度为0
    for(int i=0;i<G.numV;i++){
        indegree[i]=0;
    } 
    
    //遍历邻接矩阵
    for(int i=0;i<G.numV;i++){
        for(int j=0;j<G.numV;j++){
            if(G.Edge[i][j] != 0){
                indegree[j]++;
            }
        }
    } 
    return indegree
}

//拓扑遍历
bool topsort(MGraph *G){
    //topResult用来保存拓扑序列 
    int *topResult = (int*)malloc(sizeof(int)*G.numV);
    int topResultIndex = 0;
    //计算每个结点的入度 
    int *indegree=getIndegree(G);
    
    //找一个入度为0的顶点
    int stack[MAXSIZE];
    top=-1;
    
    //入度为0的顶点入队
    for(int i=0;i<G.numV;i++){
        if(indegree[i]=0){
            stack[++top]=i;
        }
    } 
    
    //栈不为空时
    while(top != -1){
        //完成拓扑排序的个数+1
        
        
        //出栈
        int v=stack[top--];
        topResult[topResultIndex++]=v;
        
        //由该顶点发出的边到到的顶点,入度均减1
        for(int i=0;i<G.numV;i++){
            if(G.Edge[v][i]==1){
                indegree[i]--;
                //出现新的入度为0的顶点
                if(indegree[i]==0){
                    //入队
                    stack[++top]; 
                } 
            }
        } 
    } 
    free(indegree);
    return topResult;
    
} 

int getMaxPath(MGraph *G){
    int topResult=topsort(G);//进行拓扑排序
    
    //maxDist[v]表示以v为结尾的最长路径 
    int *maxDist=(int*)malloc(sizeof(int)*G.numV);
    memset(maxDist,0,sizeof(int)*G.numV); 
    
    
    int maxPathvalue = -1;//最长路径长度,初始化为-1
    //按照拓扑排序的顺序进行处理
    for(int i=0;i<n;++i){
        int v=topResult[i];
        for(int u=0;u<G.numV;++u){
            if(G.Edge[u][v] != 0){//从u到v有路径 
                maxDist[v]=max(maxDist[v],maxDist[u]+G.Edge[u][v]);
                if(maxDist[v]>maxPathValue){//记录产生的最大值 
                    maxPathvalue = maxDist[v];
                }
            }
        }
    } 
    free(topRuslt);
    free(maxDist);
    
    return maxPathvalue;
     
}
相关推荐
Yingye Zhu(HPXXZYY)22 分钟前
ICPC 2023 Nanjing R L 题 Elevator
算法
苏小瀚2 小时前
[数据结构] ArrayList(顺序表)与LinkedList(链表)
数据结构
程序员Xu3 小时前
【LeetCode热题100道笔记】二叉树的右视图
笔记·算法·leetcode
笑脸惹桃花4 小时前
50系显卡训练深度学习YOLO等算法报错的解决方法
深度学习·算法·yolo·torch·cuda
阿维的博客日记4 小时前
LeetCode 48 - 旋转图像算法详解(全网最优雅的Java算法
算法·leetcode
GEO_YScsn5 小时前
Rust 的生命周期与借用检查:安全性深度保障的基石
网络·算法
程序员Xu5 小时前
【LeetCode热题100道笔记】二叉搜索树中第 K 小的元素
笔记·算法·leetcode
THMAIL6 小时前
机器学习从入门到精通 - 数据预处理实战秘籍:清洗、转换与特征工程入门
人工智能·python·算法·机器学习·数据挖掘·逻辑回归
Kevinhbr6 小时前
CSP-J/S IS COMING
数据结构·c++·算法
Armyyyyy丶6 小时前
Redis底层实现原理之五大基础结构
数据结构·redis·缓存