typedef struct{
int last_one;
int length_of_way;
}NODE;
Status Reclear_Queue(Queue &Q,int* indegree,int N){
ClearQueue(Q);
for(i = 0;i < N;i++)
if(!indegree[i]) EnQueue(Q,i);
return OK;
}//Reclear_Queue
Status Find_longest_way_DAG(MGraph G){
//求以邻接矩阵存储的有向无环图中的最长路径
NODE way[G.vexnum];
FindInDegree(G,indegree);//求入度序列
for(i = 0;i < G.vexnum;i++){//路径初始化
way[i].last_one = -1
if(!indegree[i]) way[i].length_of_way = 0;
else way[i].length_of_way = -1;
}//for
InitQueue(Q);
int processed = 0;
while(processed < G.vexnum){//找路
Reclear_Queue(Q,indegree,G.vexnum);
int Queuel = QueueLength(Q);
processed += Queuel;
for(i = 0;i < Queuel;i++){
DeQueue(S,start);
for(j = 0;j < G.vexnum;j++){
if(G.arcs[start][j]){//这里的假设是相邻则为关
求以邻接矩阵存储的有向无环图中的最长路径
最新推荐文章于 2021-08-09 10:14:40 发布
该博客介绍了如何使用邻接矩阵存储的有向无环图(DAG)找到最长路径的方法。通过计算每个节点的入度,初始化路径长度,并使用队列进行遍历更新最长路径。最终复杂度分析为O(v^2),其中v是图的顶点数。
摘要由CSDN通过智能技术生成