图练习
1-1
用邻接表法存储图,占用的存储空间数只与图中结点个数有关,而与边数无关。
(1分)
作者
DS课程组
单位
浙江大学
1-1
答案正确
(1 分)
1-2
用邻接矩阵法存储图,占用的存储空间数只与图中结点个数有关,而与边数无关。
(1分)
作者
DS课程组
单位
浙江大学
1-2
答案正确
(1 分)
2-1
已知无向图G含有16条边,其中度为4的顶点个数为3,度为3的顶点个数为4,其他顶点的度均小于3。图G所含的顶点个数至少是:
(4分)
作者
考研试卷
单位
浙江大学
2-1
答案正确
(4 分)
2-2
下列选项中,不是如下有向图的拓扑序列的是:
(4分)
作者
考研真题
单位
浙江大学
2-2
答案正确
(4 分)
5-1
下列代码的功能是对一个给定的图G执行拓扑排序,其中TopNum[]从1开始记录拓扑序。
void Topsort( Graph G )
{
Queue Q;
Vertex V, W;
NodePtr ptr;
int counter = 0;
Q = CreateEmptyQueue(NumVertex);
for ( V=0; V<G->NumV; V++ )
if ( Indegree[V] == 0 )
Enqueue(V, Q);
while ( !IsEmpty(Q) ){
V = Dequeue( Q );
TopNum[V] =++counter 3分;
for ( ptr=G->List[V]; ptr; ptr=ptr->Next) {
W = ptr->Vertex;
if ( --Indegree[W] 3分 == 0 )
Enqueue(W, Q);
}
}
if ( counter != NumVertex )
printf("ERROR: Graph has a cycle.\n");
DisposeQueue(Q);
}作者
陈越
单位
浙江大学
时间限制
400 ms
内存限制
64 MB
5-1
答案正确
(6 分)
5-2
#include <iostream>
using namespace std;
#define MVNum 100
#define BDNum MVNum * (MVNum - 1)
#define OK 1
#define ERROR 0
typedef char VerTexType;
typedef struct ArcNode{
int adjvex;
int weight;
struct ArcNode *nextarc;
}ArcNode;
typedef struct VNode{
VerTexType data;
ArcNode *firstarc;
}VNode, AdjList[MVNum];
typedef struct{
AdjList vertices;
AdjList converse_vertices;
int vexnum, arcnum;
}ALGraph;
int indegree[MVNum];//顶点入度
int ve[BDNum];//事件vi的最早发生时间
int vl[BDNum];//事件vi的最迟发生时间
int topo[MVNum];//记录拓扑序列的顶点序号
int LocateVex(ALGraph G , VerTexType v);//确定点v在G中的位置
int CreateUDG(ALGraph &G);//创建邻接表和逆邻接表
void FindInDegree(ALGraph G,int indegree[]);//求出各顶点的入度存入数组indegree中
int TopologicalOrder(ALGraph G , int topo[]);//生成G的一个拓扑序列topo[]
int CriticalPath(ALGraph G){
int n , i , k , j , e , l,flag=1;
if (!TopologicalOrder(G, topo)) return ERROR;
n = G.vexnum;
for(i = 0; i < n; i++)
ve[i] = 0;
for(i = 0;i < n; i++){
k = topo[i];
ArcNode *p = G.vertices[k].firstarc;
while(p != NULL){
j = p->adjvex;
if(ve[j] < ve[k] + p->weight2分)
ve[j] = ve[k] + p->weight2分;
p = p->nextarc;
}
}
for(i=0;i<n;i++)
vl[i]=ve[n-1];
for(i = n - 1;i >= 0; i--){
k = topo[i];
ArcNode *p = G.vertices[k].firstarc;
while(p != NULL){
j = p->adjvex;
if(vl[k] > vl[j] - p->weight2分)
vl[k] = vl[j] - p->weight2分;
p = p->nextarc;
}
}
for(i = 0;i < n; i++){
ArcNode *p = G.vertices[i].firstarc;
while(p != NULL) {
j = p->adjvex;
e =ve[i]; 2分;
l =vl[j] - p->weight; 2分;
if(e == l) {
if(flag){
cout << G.vertices[i].data << "->" << G.vertices[j].data;flag=0;
}
else
cout << "," << G.vertices[i].data << "->" << G.vertices[j].data;
}
p = p->nextarc;
}
}
return OK;
}
int main(){
ALGraph G;
CreateUDG(G);
int *topo = new int [G.vexnum];
CriticalPath(G);
return 0;
}作者
王东
单位
贵州师范学院
时间限制
400 ms
内存限制
64 MB
5-2
答案正确
(12 分)