标签:gre 数组 queue mamicode oid clu topsort com dex
拓扑排序是对有向无圈图的一种排序,它使得如果存在一条从vi到vj的路径,那么在排序中vj出现在vi后面。
首先,对每一个顶点计算它的入度,记录在一个数组中,然后,将所有入度为0的顶点放入一个初始为空的队列中。当队列不空时,删除一个顶点v,并将与v邻接的所有的顶点的入度减1,只要一个顶点的入度降为0,则将其放入队列中。
1 #include <iostream> 2 using namespace std; 3 #define MAX_VERTEX_NUM 10//最大顶点个数 4 5 //创建队列 6 struct Queue 7 { 8 int rear; 9 int front; 10 int size; 11 int cap; 12 int *array; 13 }; 14 Queue* CreateQueue() 15 { 16 Queue *Q = (Queue*)malloc(sizeof(Queue)); 17 Q->rear = -1; Q->front = 0; 18 Q->size = 0; Q->cap = MAX_VERTEX_NUM ; 19 Q->array = (int*)malloc(sizeof(int)*MAX_VERTEX_NUM); 20 return Q; 21 } 22 bool isempty(Queue* Q)//队列是否为空 23 { 24 return Q->size==0; 25 } 26 bool isfull(Queue* Q) 27 { 28 return Q->size == Q->cap; 29 } 30 void Enqueue(Queue *Q,int v)//入队 31 { 32 if (isfull(Q)) 33 return; 34 Q->rear++; Q->size++; 35 Q->array[Q->rear] = v; 36 } 37 int Dequeue(Queue* Q)//出队 38 { 39 if (isempty(Q)) 40 return 0; 41 int a = Q->array[Q->front]; 42 Q->front++; Q->size--; 43 return a; 44 } 45 46 //创建图(邻接表) 47 struct ENode//边结点 48 { 49 int index;//边所指向的点的序号 50 int info;//记录权值 51 struct ENode* next;//指向下一条边 52 }; 53 struct VNode//顶点结点 54 { 55 char data;//顶点名称 56 ENode* first;//指向顶点的第一个邻边 57 }; 58 struct AGraph//记录图的信息 59 { 60 int vexnum, arcnum;//顶点数和边数 61 VNode p[MAX_VERTEX_NUM];//顶点数组 62 }; 63 64 int LocateVex(AGraph *G, char ch) 65 { 66 for (int i = 0; i < G->vexnum; i++) 67 if (ch == G->p[i].data) 68 { 69 return i; 70 break; 71 } 72 return -1; 73 } 74 //创建邻接表 75 void CreateAGraph(AGraph *G) 76 { 77 int i, j, info; 78 char v1, v2; 79 cout << "请输入顶点个数:" << endl; cin >> G->vexnum; 80 cout << "请输入边的个数:" << endl; cin >> G->arcnum; 81 cout << "构造顶点:" << endl; 82 for (int i = 0; i < G->vexnum; i++)//构造顶点数组 83 { 84 cin >> G->p[i].data; 85 G->p[i].first = NULL; 86 } 87 cout << "构造邻接表:" << endl; 88 for (int k = 0; k < G->arcnum; k++) 89 { 90 cin >> v1 >> v2 >> info; 91 i = LocateVex(G, v1); 92 j = LocateVex(G, v2); 93 ENode *E = (ENode*)malloc(sizeof(ENode)); 94 E->index = j; 95 E->info = info; 96 E->next = G->p[i].first;//头插法 97 G->p[i].first = E; 98 } 99 } 100 //求每个顶点的入度 101 int Indegree[MAX_VERTEX_NUM] = { 0 };//记录入度 102 void VexIndegree(AGraph *G) 103 { 104 for (int i = 0; i < G->vexnum; i++) 105 { 106 if (G->p[i].first != NULL) 107 { 108 ENode* e = G->p[i].first; 109 while (e != NULL) 110 { 111 Indegree[e->index]++; 112 e = e->next; 113 } 114 } 115 } 116 } 117 118 //拓扑排序 119 void Topsort(AGraph *G) 120 { 121 Queue *Q = CreateQueue(); 122 VexIndegree(G); 123 for (int i = 0; i < G->vexnum; i++)//先将入度为0的点全部入队 124 { 125 if (Indegree[i] == 0) 126 Enqueue(Q, LocateVex(G,G->p[i].data)); 127 } 128 while (!isempty(Q))//当队列不空 129 { 130 131 int v=Dequeue(Q);//出队 132 if (G->p[v].first != NULL)//若v的存在邻接点 133 { 134 ENode* e = G->p[v].first; 135 while (e != NULL) 136 { 137 if (--Indegree[e->index] == 0)//将v的邻接点的入度皆减1,若为0,则入队 138 Enqueue(Q, e->index); 139 e = e->next; 140 } 141 } 142 } 143 cout << "拓扑排序:" << endl; 144 for (int i = 0; i < G->vexnum; i++) 145 cout << Q->array[i] << " "; 146 } 147 int main() 148 { 149 AGraph* G = (AGraph*)malloc(sizeof(AGraph)); 150 CreateAGraph(G); 151 Topsort(G); 152 system("pause"); 153 return 0; 154 }
运行结果:
标签:gre 数组 queue mamicode oid clu topsort com dex
原文地址:https://www.cnblogs.com/cs0915/p/12239969.html