#include<string.h>
#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<process.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
typedef int Status;
typedef int Boolean;
#define MAX_NAME 10
#define MAXCLASS 100
int Z=0;
int X=0;
int xqzs,q=1,xfsx;
typedef int InfoType;
typedef char VertexType[MAX_NAME];
#define MAX_VERTEX_NUM 100
typedef enum {DG} GraphKind;
typedef struct ArcNode
{
int adjvex;
struct ArcNode *nextarc;
InfoType *info;
} ArcNode;
typedef struct
{
VertexType data;
ArcNode *firstarc;
} VNode,AdjList[MAX_VERTEX_NUM];
typedef struct
{
AdjList vertices,verticestwo;
int vexnum,arcnum;
int kind;
} ALGraph;
int LocateVex(ALGraph G,VertexType u)
{
int i;
for(i=0; i<G.vexnum; ++i)
if(strcmp(u,G.vertices[i].data)==0)
return i;
return -1;
}
Status CreateGraph(ALGraph *G)
{
int i,j,k;
VertexType va,vb;
ArcNode *p;
printf(" 请输入教学计划的课程数 : ");
scanf("%d",&(*G).vexnum);
printf(" 请输入拓扑排序所形成的课程先修关系的边数 : ");
scanf("%d",&(*G).arcnum);
printf(" 输入%d 个课程的代表值 (<%d 个字符):\n",(*G).vexnum,MAX_NAME);
for(i=0; i<(*G).vexnum; ++i)
{
scanf("%s",(*G).vertices[i].data);
(*G).vertices[i].firstarc=NULL;
}
printf(" 输入%d 个课程的学分值 (<%d 个符):\n", (*G).vexnum,MAX_NAME);
for(i=0; i<(*G).vexnum; ++i)
{
scanf("%s",(*G).verticestwo[i].data);
}
printf(" 请顺序输入每条弧 ( 边) 的弧尾和弧头 (以空格作为间隔 ) :\n");
for(k=0; k<(*G).arcnum; ++k)
{
scanf("%s%s",va,vb);
i=LocateVex(*G,va);
j=LocateVex(*G,vb);
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
p->info=NULL;
p->nextarc=(*G).vertices[i].firstarc;
(*G).vertices[i].firstarc=p;
}
return OK;
}
void Display(ALGraph G)
{
int i;
ArcNode *p;
switch(G.kind)
{
case DG:
printf("有向图\n");
}
printf("%d 个顶点:\n",G.vexnum);
for(i=0; i<G.vexnum; ++i)
printf("%s ",G.vertices[i].data);
printf("\n%d 条弧(边):\n",G.arcnum);
for(i=0; i<G.vexnum; i++)
{
p=G.vertices[i].firstarc;
while(p)
{
printf("%s →%s ",G.vertices[i].data,G.vertices[p->adjvex].data);
p=p->nextarc;
}
printf("\n");
}
}
void FindInDegree(ALGraph G,int indegree[])
{
int i;
ArcNode *p;
for(i=0; i<G.vexnum; i++)
indegree[i]=0;
for(i=0; i<G.vexnum; i++)
{
p=G.vertices[i].firstarc;
while(p)
{
indegree[p->adjvex]++;
p=p->nextarc;
}
}
}
typedef int SElemType;
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 2
typedef struct SqStack
{
SElemType *base;
SElemType *top;
int stacksize;
} SqStack;
Status InitStack(SqStack *S)
{
(*S).base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!(*S).base)
exit(OVERFLOW);
(*S).top=(*S).base;
(*S).stacksize=STACK_INIT_SIZE;
return OK;
}
Status StackEmpty(SqStack S)
{
if(S.top==S.base)
return TRUE;
else
return FALSE;
}
Status Pop(SqStack *S,SElemType *e)
{
if((*S).top==(*S).base)
return ERROR;
*e=*--(*S).top;
return OK;
}
Status Push(SqStack *S,SElemType e)
{
if((*S).top-(*S).base>=(*S).stacksize)
{
(*S).base=(SElemType*)realloc((*S).base,((*S).stacksize+STACKINCREMENT)*sizeof (SElemType));
if(!(*S).base)
exit(OVERFLOW);
(*S).top=(*S).base+(*S).stacksize;
(*S).stacksize+=STACKINCREMENT;
}
*((*S).top)++=e;
return OK;
}
typedef int pathone[MAXCLASS];
typedef int pathtwo[MAXCLASS];
Status TopologicalSort(ALGraph G)
{
int i,k,q=1,j=0,count,indegree[MAX_VERTEX_NUM];
SqStack S;
pathone a;
pathtwo b;
ArcNode *p;
FindInDegree(G,indegree);
InitStack(&S);
for(i=0; i<G.vexnum; ++i)
if(!indegree[i])
Push(&S,i);
count=0;
while(!StackEmpty(S))
{
Pop(&S,&i);
a[i]=*G.vertices[i].data;
b[i]=*G.verticestwo[i].data;
if(q<=6)
{
printf(" 第%d 个学期应学课程:课程 %s→学 分%s\n",q,G.vertices[i].data,G.verticestwo[i].data);
q++;
}
else
{
printf("*** 课程%s→学分%s\n",G.vertices[i].data,G.verticestwo[i].data);
}
++count;
for(p=G.vertices[i].firstarc; p; p=p->nextarc)
{
k=p->adjvex;
if(!(--indegree[k]))
Push(&S,k);
}
}
if(count<G.vexnum)
{
printf(" 此有向图有回路 \n");
return ERROR;
}
else
{
printf(" 为一个拓扑序列 。\n");
printf(" 教学编制计划已完成。 \n");
}
return 0;
}
int main()
{
ALGraph f;
printf(" 教学计划编制问题的数据模型为拓扑排序 AOV- 网结构。 \n");
printf(" 以下为教学计划编制问题的求解过程 :\n");
printf(" 请输入学期总数 :");
scanf("%d",&xqzs);
printf(" 请输入学期的学分上限 :");
scanf("%d",&xfsx);
CreateGraph(&f);
Display(f);
TopologicalSort(f);
}
教学计划编制问题的数据模型为拓扑排序 AOV- 网结构
最新推荐文章于 2023-04-25 20:59:18 发布