-->
当前位置:首页 > 题库 > 正文内容

程序填空题:Find Unweighted Shortest Paths

Luz4年前 (2021-05-10)题库3072
The function `Unweighted` is to find the unweighted shortest path from `Vertex S` to every other vertices in a given `Graph`. The distances are stored in `dist[]`, and `path[]` records the paths. The `Graph` is defined as the following:

```
typedef struct GNode *PtrToGNode;
struct GNode{
int Nv; /* Number of vertices */
int Ne; /* Number of edges */
AdjList List; /* adjacency matrix */
};
typedef PtrToGNode Graph;
```

```
void Unweighted( Graph G, Queue Q, int dist[], int path[], Vertex S )
{
Vertex V, U;
NodePtr ptr;

dist[S] = 0;
Enqueue(S, Q);
while ( !IsEmpty(Q) ) {
V = Dequeue( Q );
for ( ptr=G->List[V].FirstEdge; ptr; ptr=ptr->Next) {
U = ptr->AdjV;
if ( dist[U] == INFINITY ) {
@@[dist[U] = dist[V] + 1](3);
path[U] = V;
@@[Enqueue(U, Q)](3);
}
}
}
}
```






答案:
第1空:dist[U] = dist[V] + 1

第2空:Enqueue(U, Q)

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。