数据结构图之五(拓扑排序)

【1】拓扑排序

在一个表示工程的有向图中,有顶点表示活动,用弧表示活动之间的优先关系,这样的有向图为顶点表示活动的网,我们称为AOV网。

AOV网中的弧表示活动之间存在的某种制约关系。

所谓拓扑排序,其实就是对一个有向图构造拓扑序列的过程。

【2】拓扑排序算法

对AOV网进行拓扑排序的基本思路:

从AOV网中选择一个入度为0的顶点输出;

然后删除此顶点,并删除以次顶点为尾的弧;

继续重复此操作…..

直到输出全部顶点或AOV网中不存在入度为0的顶点为止。

由于拓扑排序过程中,需要删除顶点,显然用邻接表更加方便。

因此我们需要为AOV网建立一个邻接表。

另外,考虑到算法过程中始终需要查找入度为0的顶点?

需要在原顶点表节点结构中,增加一个入度域in,in就是入度数字。

所以结构如下图:

《数据结构图之五(拓扑排序)》

第一幅图AOV网如下:

《数据结构图之五(拓扑排序)》

第二幅图的邻接表逻辑结构

《数据结构图之五(拓扑排序)》

第三幅图邻接表数据结构:

《数据结构图之五(拓扑排序)》

【3】算法及其详解

(1)算法如下图所示:

《数据结构图之五(拓扑排序)》

(2)详解如下:

《数据结构图之五(拓扑排序)》

7. 其它的处理方式类似,直至全部打印删除。

8. 最终的拓扑排序打印结果为:

当然,从整个过程来分析,这个结果并不是唯一的一种拓扑排序方案。

分析整个算法,对一个具有n各顶点e条弧的AOV网来说:

第8-10行扫描顶点表,将入度为0的顶点入栈的时间复杂度为O(n);

而之后的while循环中,每个顶点进一次栈,出一次栈,入度减1的操作执行了e次。

所以整个算法的时间复杂度为O(n+e)。

【4】本示例代码实现

实现代码如下:

《数据结构图之五(拓扑排序)》
《数据结构图之五(拓扑排序)》

  1 #include <iostream>
  2 #include "Stack.h"
  3 #include <malloc.h>
  4 using namespace std;
  5 
  6 #define  MAXVEX   14
  7 #define  MAXEDGE  20
  8 
  9 typedef struct EdgeNode
 10 {
 11     int adjvex;    // 邻接点域,存储该顶点对应的下标
 12     struct EdgeNode* next; // 链域
 13 } EdgeNode;
 14 
 15 typedef struct VertexNode
 16 {
 17     int inNum;    // 顶点入度值
 18     int data;    // 顶点数值欲
 19     EdgeNode* firstedge; // 边表头指针
 20 } VertexNode, AdjList[MAXVEX];
 21 
 22 typedef struct
 23 {
 24     AdjList adjList;
 25     int numVertexes, numEdges; // 图中当前顶点数和边数(对于本案例,已经存在宏定义)
 26 } graphAdjList, *GraphAdjList;
 27 
 28 // 构建节点
 29 EdgeNode* BuyNode()
 30 {
 31     EdgeNode* p = (EdgeNode*)malloc(sizeof(EdgeNode));
 32     p->adjvex = -1;
 33     p->next = NULL;
 34     return p;
 35 }
 36 // 初始化图
 37 void InitGraph(graphAdjList& g)
 38 {
 39     for (int i = 0; i < MAXVEX; ++i)
 40     {
 41         g.adjList[i].firstedge = NULL;
 42     }
 43 }
 44 // 创建图
 45 void CreateGraph(graphAdjList& g)
 46 {
 47     int i = 0, begin = 0, end = 0;
 48     EdgeNode *pNode = NULL;
 49     cout << "输入14个顶点信息(顶点 入度):" << endl;
 50     for (i = 0; i < MAXVEX; ++i)
 51     {
 52         cin >> g.adjList[i].data >> g.adjList[i].inNum;
 53     }
 54     cout << "输入20条边的信息:" << endl;
 55     for (i = 0; i < MAXEDGE; ++i)
 56     {
 57         cin >> begin >> end;
 58         pNode = BuyNode();
 59         pNode->adjvex = end;
 60         pNode->next = g.adjList[begin].firstedge;
 61         g.adjList[begin].firstedge = pNode;
 62     }
 63 }
 64 // 打印输入信息的逻辑图
 65 void PrintGraph(graphAdjList &g)
 66 {
 67     cout << "打印邻接表的逻辑图:" << endl;
 68     for (int i = 0; i < MAXVEX; ++i)
 69     {
 70         cout << " " << g.adjList[i].inNum << " " << g.adjList[i].data << " ";
 71         EdgeNode* p = g.adjList[i].firstedge;
 72         cout << ": ";
 73         while (p != NULL)
 74         {
 75             int index = p->adjvex;
 76             cout << g.adjList[index].data << "  ";
 77             p = p->next;
 78         }
 79         cout << endl;
 80     }
 81 }
 82 bool TopologicalSort(graphAdjList g)
 83 {
 84     EdgeNode* pNode = NULL;
 85     int i = 0, k = 0, gettop = 0;
 86     int nCnt = 0;
 87     SeqStack<int> sQ;
 88     for (i = 0; i < MAXVEX; ++i)
 89     {
 90         if (0 == g.adjList[i].inNum)
 91             sQ.Push(i);
 92     }
 93     while (!sQ.IsEmpty())
 94     {
 95         sQ.Pop(gettop);
 96         ++nCnt;
 97         if (MAXVEX == nCnt)
 98         {    //去掉拓扑路径后面的-->
 99             cout << g.adjList[gettop].data; 
100             break;
101         }
102         cout << g.adjList[gettop].data << "-->";
103         pNode = g.adjList[gettop].firstedge;
104         while (pNode != NULL)
105         {
106             k = pNode->adjvex;
107             --g.adjList[k].inNum;
108             if (0 == g.adjList[k].inNum)
109                 sQ.Push(k);
110             pNode = pNode->next;
111         }
112     }
113     return nCnt != MAXVEX;
114 }
115 void main()
116 {
117     graphAdjList myg;
118     InitGraph(myg);
119     cout << "创建图:" << endl;
120     CreateGraph(myg);
121     cout << "打印图的邻接表逻辑结构:" << endl;
122     PrintGraph(myg);
123     cout << "拓扑排序路径:" << endl;
124     bool bAcire = TopologicalSort(myg);
125     cout << endl;
126     cout << "存在回环? " << bAcire << endl;
127 }
128 /*
129 创建图:
130 输入14个顶点信息(顶点 入度):
131 0 0
132 1 0
133 2 2
134 3 0
135 4 2
136 5 3
137 6 1
138 7 2
139 8 2
140 9 2
141 10 1
142 11 2
143 12 1
144 13 2
145 输入20条边的信息:
146 0 5
147 0 4
148 0 11
149 1 4
150 1 8
151 1 2
152 2 5
153 2 6
154 2 9
155 3 2
156 3 13
157 4 7
158 5 8
159 5 12
160 6 5
161 8 7
162 9 10
163 9 11
164 10 13
165 12 9
166 打印图的邻接表逻辑结构:
167 打印邻接表的逻辑图:
168 0 0 : 11  4  5
169 0 1 : 2  8  4
170 2 2 : 9  6  5
171 0 3 : 13  2
172 2 4 : 7
173 3 5 : 12  8
174 1 6 : 5
175 2 7 :
176 2 8 : 7
177 2 9 : 11  10
178 1 10 : 13
179 2 11 :
180 1 12 : 9
181 2 13 :
182 拓扑排序路径:
183 3-->1-->2-->6-->0-->5-->8-->12-->9-->10-->13-->11-->4-->7
184 存在回环? 0
185  */

View Code

本示例代码中的Stack.h文件可以从随笔《》中拷贝即可。

 

Good  Good  Study,  Day  Day  Up.

顺序  选择  循环  总结

    原文作者:kaizen
    原文地址: https://www.cnblogs.com/Braveliu/p/3460232.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞