1. Concepts
In a directed graph that represents an engineering project, vertices are used to represent activity, and arcs are used to represent priority relationships between activities. In this way, directed graphs are used to represent active networks, activity on vertex network ). The most typical example is the priority relationship between the course and the course.
If all vertices in an AOV network are in the topological sequence, the AOV network does not have a loop. The topological sequence of an aov network may not be unique.
The basic idea of topological sorting for AOV networks is:
(1) Select a vertex without a precursor from the AOV network to output it;
(2) Delete the vertex from the AOV network and delete all edges with the vertex as the end;
(3) Repeat the preceding two steps until all vertices are output, or the AOV network does not have any vertices without a precursor.
Obviously, there are two types of topological sorting results: all vertices in the AOV network are output, which indicates that there is no loop in the AOV network, and not all vertices in the AOV network are output, the remaining vertices do not have any front-end vertices, which indicates that there is a loop in the AOV network.
Storage Structure:
In the topological sorting process, you need to find all the edges at the end of a vertex, that is, you need to find all outbound edges of the vertex. Therefore, the graph should be stored in an adjacent table. In addition, you need to perform operations on the inbound degree of a vertex during the topological sorting process. For example, you need to find the vertex whose inbound degree is equal to zero and reduce the inbound degree of a vertex by 1. In general, the operations on vertex influence in the graph's adjacent table are inconvenient. Therefore, an influence field is added to the vertex table to facilitate the operations on the influence.
Ii. Code
#include <stdio.h>#include <malloc.h>#define MaxNode 26struct EdgeNode{ int adjVertex; struct EdgeNode* next;};struct VertexNode{ int in; char data; struct EdgeNode* firstEdge;};struct ALGraph{ struct VertexNode adjList[MaxNode]; int vertexAmount; int edgeAmount;};void initialize(struct ALGraph* graph, int vertexAmount, int edgeAmount){ graph->vertexAmount = vertexAmount; graph->edgeAmount = edgeAmount; int i = 0; for(i = 0; i < MaxNode; i++){ graph->adjList[i].in = 0; graph->adjList[i].data = 'A' + i; graph->adjList[i].firstEdge = NULL; }}void addEdge(struct ALGraph* graph, char prev, char next){ struct EdgeNode* edgeNode = (struct EdgeNode*)malloc(sizeof(struct EdgeNode)); edgeNode->adjVertex = next - 'A'; edgeNode->next = NULL; int vertex = prev - 'A'; if(graph->adjList[vertex].firstEdge == NULL){ graph->adjList[vertex].firstEdge = edgeNode; } else { struct EdgeNode* tEdgeNode = graph->adjList[vertex].firstEdge; while(tEdgeNode->next != NULL){ tEdgeNode = tEdgeNode->next; } tEdgeNode->next = edgeNode; } graph->adjList[next - 'A'].in++;}void topSort(struct ALGraph graph){ char outstr[MaxNode]; int k = -1; int count = 0; int queue[MaxNode] = {0}; int front = 0; int rear = -1; int i = 0; for(i = 0; i < graph.vertexAmount; i++){ if(0 == graph.adjList[i].in){ queue[++rear] = i; } } while(rear >= front){ int vertexIndex = queue[front]; outstr[++k] = graph.adjList[vertexIndex].data; struct EdgeNode* adjEdge = graph.adjList[vertexIndex].firstEdge; while(adjEdge){ int tadjVex = adjEdge->adjVertex; graph.adjList[tadjVex].in--; count++; if(graph.adjList[tadjVex].in == 0){ queue[++rear] = tadjVex; } adjEdge = adjEdge->next; } front++; } outstr[++k] = '\0'; if(count < graph.vertexAmount){ printf("%s\n", "circle exist!"); } else { printf("%s\n", outstr); }};int main(){ freopen("testData.txt", "r", stdin); int vertexAmount; int edgeAmount; while(scanf("%d%d", &vertexAmount, &edgeAmount)){ if(0 == vertexAmount && 0 == edgeAmount){ break; } getchar();/*eat the '\n'*/ /*initialize*/ struct ALGraph graph; initialize(&graph, vertexAmount, edgeAmount); int i = 0; for(i = 0; i < edgeAmount; i++){ char prev = getchar(); char next = getchar(); getchar();/*eat the '\n'*/ addEdge(&graph, prev, next); } topSort(graph); } return 0;}