填空题
A graph with 30 vertices and 40 edges must have at most 21 connected component(s).
A graph with 15 vertices and 38 edges must have at most 6 connected component(s).
程序填空题
Bubble sort is a simple sorting algorithm. Suppose we have a list of integers and want to sort them in ascending order. Bubble sort repeatedly scans the list from the head to the tail, and swaps two adjacent numbers if they are in the wrong order. Please complete the following program to implement bubble sort.
typedef struct node *nodeptr;
struct node{
int value;
nodeptr next;
/* some other fields */
};
nodeptr BubbleSort (nodeptr h)
{/* h is the head pointer of the list with a dummy head node */
nodeptr p, q;
int flag_swap;
if (!h->next) return h;
do{
flag_swap = 0;
p = h;
while (p->next->next){
if ( **p->next->value > p->next->next->value**){
flag_swap++;
q = p->next;
**p->next = q->next;**
**q->next = p->next->next;**
**p->next->next = q;**
}
else p = p->next;
}
} while (flag_swap > 0);
return h;
}
Please fill the array with the results after the following union/find operations.
union( find(2), find(7) )
union( find(3), find(5) )
union( find(0), find(2) )
union( find(5), find(7) )
union( find(5), find(6) )
Note: Assume union-by-size (if two sets are equal-sized, the first root will be the root of the result) and find-with-path-compression. S[i] is initialized to be −1 for all 0≤i≤7.
The function is to sort the list { r[1] … r[n] } in non-decreasing order. Unlike selection sort which places only the minimum unsorted element in its correct position, this algorithm finds both the minimum and the maximum unsorted elements and places them into their final positions.
void sort( list r[], int n )
{
int i, j, mini, maxi;
for (i=1; i<n-i+1; i++) {
mini = maxi = i;
for( j=i+1;**j<=n-i+1**; ++j ){
if( **r[j]->key < r[mini]->key** ) mini = j;
else if(r[j]->key > r[maxi]->key) maxi = j;
}
if( mini != i ) swap(&r[mini], &r[i]);
if( maxi != n-i+1 ){
if( **maxi == i**) swap(&r[mini], &r[n-i+1]);
else swap(&r[maxi], &r[n-i+1]);
}
}
}
The function is to find the K-th largest element in a list A of N elements. The function BuildMinHeap(H, K) is to arrange elements H[1] … H[K] into a min-heap. Please complete the following program.
ElementType FindKthLargest ( int A[], int N, int K )
{ /* it is assumed that K<=N */
ElementType *H;
int i, next, child;
H = (ElementType *)malloc((K+1)*sizeof(ElementType));
for ( i=1; i<=K; i++ ) H[i] = A[i-1];
BuildMinHeap(H, K);
for ( next=K; next<N; next++ ) {
H[0] = A[next];
if ( H[0] > H[1] ) {
for ( i=1; i*2<=K; i=child ) {
child = i*2;
if ( child!=K && **H[child+1]<H[child]** child++;
if ( **H[0]>H[child]** )
H[i] = H[child];
else break;
}
H[i] = H[0];
}
}
return H[1];
}
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, W;
NodePtr ptr;
dist[S] = 0;
Enqueue(S, Q);
while ( !IsEmpty(Q) ) {
V = Dequeue( Q );
for ( ptr=G->List[V].FirstEdge; ptr; ptr=ptr->Next) {
W = ptr->AdjV;
if ( dist[W] == INFINITY ) {
**dist[W] = dist[V] + 1**);
path[W] = V;
**Enqueue(W, Q)**);
}
}
}
}
None-recursive Quick Selection algorithm
The function is to find the K-th smallest element in a list A of N elements. The initial function call is QSelect(A, N, K). Please complete the following program.
ElementType QSelect( ElementType A[], int N, int K )
{
ElementType Pivot;
int L, R, Left, Right, K1;
Left = 0; Right = N-1; K1 = K;
for(;; ) {
L = Left, R = Right+1;
Pivot = A[Left];
while (1) {
while ( A[++L] < Pivot ) ;
**while ( A[--R] > Pivot );**
if ( L < R ) Swap( &A[L], &A[R] );
else break;
}
Swap( &A[Left], &A[R] );
if ( K1 < (L-Left) )
Right = R-1;
else if ( K1 > (L-Left) ){
**K1 = K1-(L-Left)**;
Left = L;
}else
return Pivot;
}
}
Concatenation of lists is an operation where the elements of one list are added at the end of another list. For example, if we have a linked list L1→1→2→3 and another one L2→4→5→6. The function ListConcat is to return the head pointer of the list L→1→2→3→4→5→6.
The list structure is defined as the following:
typedef struct Node *PtrToNode;
struct Node{
int Data;
PtrToNode Next;
};
typedef PtrToNode List;
Please fill in the blanks.
List ListConcat( List L1, List L2 )
{
List Tmp = L1;
if ( !L1 ) return L2;
while ( Tmp->Next )
**Tmp = Tmp->Next**;
**Tmp->Next = L2**;
return **L1**);
}
The function is to sort N elements in non-decreasing order by Insertion Sort.
void InsertionSort( ElementType A[ ], int N )
{
int P, i;
ElementType Tmp;
for ( P=1; P<N; P++ ) {
Tmp = A[P];
for ( i=P; **i>0 && A[i-1]>Tmp**; i-- )
A[i] = A[i-1];
**A[i] = Tmp** ;
}
}