程序填空题:Find Unweighted Shortest Paths
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)
```
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)