//shortest path by Dijkstra int vertex[7]; int ischecked[7]; int length[7]; PTNode Path[7]; //init array for (int i = 0; i < 7; i++) { vertex[i] = i; ischecked[i] = 0; length[i] = INF; Path[i] = (PTNode) malloc(sizeof(struct pathnode)); Path[i]->vertex = id[i]; Path[i]->next = NULL; }
//start from vertex 0(A) ischecked[0] = 1; length[0] = 0;
for (int i = 0; i < 7; i++) { if (G[0][i] != INF) { length[i] = G[0][i]; addvertex(&Path[i], id[0]); } } //find the shortest vertex int minpathval = INF; int minpathindex = -1; for (int i = 0; i < 7; i++) { if (ischecked[i] == 0 && length[i] < minpathval) { minpathval = length[i]; minpathindex = i; } } //update the length array int newvertex = minpathindex; while (newvertex != -1) { ischecked[newvertex] = 1; for (int i = 0; i < 7; i++) { if (ischecked[i] == 0 && G[newvertex][i] != INF) { int newpathlen = length[newvertex] + G[newvertex][i]; if (length[i] > newpathlen) { length[i] = newpathlen; //update the path copypath(Path[newvertex], Path[i]); addvertex(&Path[i], id[newvertex]); } } } //find the shortest vertex minpathval = INF; newvertex = -1; for (int i = 0; i < 7; i++) { if (ischecked[i] == 0 && length[i] < minpathval) { minpathval = length[i]; newvertex = i; } } }
printf("The shortest path from A to other vertex is:\n"); for (int i = 0; i < 7; i++) { printf("A->%c::\n", id[i]); PTNode p = Path[i]->next; printf("PATH: "); while (p != NULL) { printf("%c->", p->vertex); p = p->next; } printf("%c", id[i]); printf("\n"); printf("length: %d\n-----\n", length[i]); } return0; }
Problem A.b
Find the shortest unweighted path from B to all other vertices for the graph in Figure 9.80.
//shortest path by Dijkstra int vertex[7]; int ischecked[7]; int length[7]; PTNode Path[7]; //init array for (int i = 0; i < 7; i++) { vertex[i] = i; ischecked[i] = 0; length[i] = INF; Path[i] = (PTNode) malloc(sizeof(struct pathnode)); Path[i]->vertex = id[i]; Path[i]->next = NULL; }
//start from vertex 1(B) ischecked[1] = 1; length[1] = 0;
for (int i = 0; i < 7; i++) { if (G[1][i] != INF) { length[i] = G[1][i]; addvertex(&Path[i], id[1]); } } //find the shortest vertex int minpathval = INF; int minpathindex = -1; for (int i = 0; i < 7; i++) { if (ischecked[i] == 0 && length[i] < minpathval) { minpathval = length[i]; minpathindex = i; } } //update the length array int newvertex = minpathindex; while (newvertex != -1) { ischecked[newvertex] = 1; for (int i = 0; i < 7; i++) { if (ischecked[i] == 0 && G[newvertex][i] != INF) { int newpathlen = length[newvertex] + G[newvertex][i]; if (length[i] > newpathlen) { length[i] = newpathlen; //update the path copypath(Path[newvertex], Path[i]); addvertex(&Path[i], id[newvertex]); } } } //find the shortest vertex minpathval = INF; newvertex = -1; for (int i = 0; i < 7; i++) { if (ischecked[i] == 0 && length[i] < minpathval) { minpathval = length[i]; newvertex = i; } } }
printf("The shortest path from B to other vertex is:\n"); for (int i = 0; i < 7; i++) { printf("B->%c::\n", id[i]); PTNode p = Path[i]->next; printf("PATH: "); while (p != NULL) { printf("%c->", p->vertex); p = p->next; } printf("%c", id[i]); printf("\n"); printf("length: %d\n-----\n", length[i]); } return0; }
Problem B.a
Explain how to modify Dijkstra’s algorithm to produce a count of the number of different minimum paths from v to w.
对每个顶点构建一个前缀表(原算法是直接更改),结束后统计权重和,以前缀表向前排查出整条路径。
Problem B.b
Explain how to modify Dijkstra’s algorithm so that if there is more than one minimum path from v to w, a path with the fewest number of edges is chosen.
依照B.a,不难比较不同最短路径的边数
Problem C.a
Find a minimum spanning tree for the graph in Figure 9.82 using both Prim’s and Kruskal’s algorithms.
voidMinSpanTree_Kruskal(Graph G) { int i, j, n, m; Edge edges; int parent[n_vertex]; for (i = 0; i < n_vertex; i++) { parent[i] = 0; } edges = (Edge) malloc(sizeof(struct ENode)); edges->Next = NULL; for (i = 0; i < n_vertex; i++) { for (j = i + 1; j < n_vertex; j++) { if (G[i][j] < INF) { InsertEdge(edges, i, j, G[i][j]); } } } edges = edges->Next; while (edges != NULL) { n = Find(parent, edges->V1); m = Find(parent, edges->V2); if (n != m) { parent[n] = m; printf("(%d,%d)", edges->V1, edges->V2); } edges = edges->Next; } }
其中对Edge定义如下,
1 2 3 4 5
typedefstructedge { int begin; int end; int weight; }Edge;