/*最短增广路径,计算路径*/ void getShortestIncrePath(Index S, Table T, Graph G) { Queue Q = createQueue(); enqueue(S, Q); /*初始化入度路径计算表时,发出节点被置0, 我们在这里的目的是计算路径最小流*/ T[S].dist = Infinity; Index V,W; while(!isEmpty(Q)) { V = dequeue(Q); T[V].known = true; Edge edge = G->TheCells[V].next; while(edge !=NULL) { W = edge->vertexIndex; if(T[W].known == false) { T[W].dist = MIN(T[V].dist, edge->weight); T[W].path = V; enqueue(W,Q); } edge = edge->next; } } disposeQueue(Q); }
int main(int argc, char **argv) { int N = (argc < 2) ? 20 : atoi(argv[1]); if (N < 20) N = 20; Queue q; q = newQueue(); int i; char x[50]; for (i = 0; i < N; i++) { if (random()%10 > 5) { if (!emptyQueue(q)) { char *str = leaveQueue(q); printf("Remove %s\n",str); free(str); } } else { randomString(x); enterQueue(q,x); printf("Insert %s\n",x); } showQueue(q); } disposeQueue(q); return 0; }
//computing the unweighted shortest path between the vertex under initIndex and other vertexs void unweighted_shortest_path(AdjTable* adj, int size, int startVertex, Queue queue) { int adjVertex; UnweightedTable* table; ElementType vertex; AdjTable temp; table = initUnweightedTable(size); enQueue(queue, startVertex-1); //if let start vertex equals to v3, then initIndex=3 and let index=2 enter the queue table[startVertex-1]->distance = 0;// update the distance table[startVertex-1]->path = 0;// update the path of starting vertex while(!isEmpty(queue)) { vertex = deQueue(queue); // if the queue is not empty, conducting departing queue table[vertex]->known = 1; // update the vertex as accessed, also responding known 1 temp = adj[vertex]->next; while(temp) { adjVertex = temp->index; // let each adjVertex adjacent to vertex enter the queue if(table[adjVertex]->path == -1) // key that judge whether corresponding element's path equals to -1 ,-1 means the element has never entered the queue { enQueue(queue, adjVertex); table[adjVertex]->distance = table[vertex]->distance + 1;// update the distance table[adjVertex]->path = vertex; //update the path of adjVertex, also responding path evaluated as vertex } temp = temp->next; } } disposeQueue(queue); //print unweighted table printUnweightedtable(table, size, startVertex); printf("\n\t"); }