数据结构C语言-拓扑排序

#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int status;
typedef int ElemType;
typedef int bool;
#define true 1
#define false 0

#define MaxInt 32767
#define MVNum 100
typedef char VerTexType;
typedef int ArcType;

typedef int OtherInfo;

//邻接表
typedef struct ArcNode
{
int adjvex;
struct ArcNode *nextarc;
OtherInfo info;
}ArcNode;

typedef struct VNode
{
VerTexType data;
ArcNode *firstarc;
}VNode,AdjList[MVNum];

typedef struct
{
AdjList vertices;
int vexnum,arcnum;
}ALGraph;

int LocateVex_AL(ALGraph G,VerTexType v)
{
int i=0;
while(i<G.vexnum&&G.vertices[i].data!=v)
i++;
if(i>=G.vexnum)
return ERROR;
return i;
}

status CreateUDN_AL(ALGraph *G,VerTexType *V)
{
int i=0,k=0,j;
for(;i<(*G).vexnum;i++)//G->就完事了!
{
G->vertices[i].data=V[i];
G->vertices[i].firstarc=NULL;
}
char v1,v2;
for(;karcnum;k++)
{
scanf("%c %c",&v1,&v2);
getchar();
int i=LocateVex_AL(*G,v1);
int j=LocateVex_AL(*G,v2);
ArcNode *p1=(ArcNode )malloc(sizeof(ArcNode));
p1->adjvex=j;
p1->nextarc=G->vertices[i].firstarc;
G->vertices[i].firstarc=p1;
/

ArcNode *p2=(ArcNode *)malloc(sizeof(ArcNode));
p2->adjvex=i;
p2->nextarc=G->vertices[j].firstarc;
G->vertices[j].firstarc=p2;
*/
}
return OK;
}

//栈
#define MAXSIZE 100
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;

status InitStack(SqStack *s)
{
s->base=(ElemType *)malloc(sizeof(ElemType)*MAXSIZE);
if(!s->base)
exit(OVERFLOW);
s->top=s->base;
s->stacksize=MAXSIZE;
return OK;
}

status Push(SqStack *s,ElemType e)
{
if(s->top-s->base==s->stacksize)
return ERROR;
*(s->top)=e;
s->top++;
return OK;
}

status Pop(SqStack *s,ElemType *e)
{
if(s->top==s->base)
return ERROR;
s->top–;
e=(s->top);
return OK;
}

bool StackEmpty(SqStack s)
{
return s.top==s.base;
}

//拓扑排序
void FindInDegree(ALGraph G,int indegree[])
{
int i,d=0;
for(i=0;i<G.vexnum;i++)
indegree[i]=0;
for(i=0;i<G.vexnum;i++)
{
ArcNode *p=G.vertices[i].firstarc;
while(p!=NULL)
{
indegree[p->adjvex]++;
p=p->nextarc;
}
}
}

status TopologicalSort(ALGraph G,int topo[])
{
int indegree[G.vexnum],i;
FindInDegree(G,indegree);
SqStack S;
InitStack(&S);
for(i=0;i<G.vexnum;i++)
if(!indegree[i])
Push(&S,i);
int m=0;
while(!StackEmpty(S))
{
Pop(&S,&i);
topo[m]=i;
m++;
ArcNode *p=G.vertices[i].firstarc;
while(p!=NULL)
{
int k=p->adjvex;
indegree[k]–;
if(indegree[k]==0)
Push(&S,k);
p=p->nextarc;
}
}
if(m<G.vexnum)
return ERROR;
else
return OK;
}
int main()
{
ALGraph G;
G.vexnum=6;
G.arcnum=8;
VerTexType V[G.vexnum];
int i;
for(i=0;i<G.vexnum;i++)
V[i]=i+‘A’;
printf(“Create ALGraph:\n”);
CreateUDN_AL(&G,V);
int topo[G.vexnum];
printf(“Topo sort:\n”);
if(TopologicalSort(G,topo)>0)
{
for(i=0;i<G.vexnum;i++)
printf("%d ",topo[i]+1);
}
return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
拓扑排序是一种常用的图算法,用于解决有向无环图(DAG)中的排序问题。其基本思想是通过不断选择入度为0的顶点来构建排序序列。下面是一个基于邻接表的C语言实现: ``` #include <stdio.h> #include <stdlib.h> #define MAX_VERTEX_NUM 100 // 最大顶点数 #define ERROR -1 // 错误标识 // 邻接表结构体 typedef struct EdgeNode { int adjvex; // 邻接点编号 struct EdgeNode *next; // 指向下一个邻接点的指针 } EdgeNode; typedef struct VertexNode { int data; // 顶点数据 EdgeNode *firstEdge; // 指向第一个邻接点的指针 int indegree; // 顶点的入度 } VertexNode, AdjList[MAX_VERTEX_NUM]; typedef struct { AdjList adjList; // 邻接表 int vexNum, edgeNum; // 顶点数和边数 } Graph; // 初始化图 void initGraph(Graph *G, int vexNum) { G->vexNum = vexNum; G->edgeNum = 0; for (int i = 0; i < vexNum; i++) { G->adjList[i].data = i; // 顶点数据 G->adjList[i].firstEdge = NULL; // 邻接表为空 G->adjList[i].indegree = 0; // 入度为0 } } // 添加边 void addEdge(Graph *G, int u, int v) { EdgeNode *e = (EdgeNode *) malloc(sizeof(EdgeNode)); e->adjvex = v; e->next = G->adjList[u].firstEdge; G->adjList[u].firstEdge = e; G->adjList[v].indegree++; // 修改入度 G->edgeNum++; // 边数加1 } // 拓扑排序 int topSort(Graph *G, int *result) { int count = 0; // 计数器 int front = 0, rear = 0; // 队列的头尾指针 int queue[MAX_VERTEX_NUM]; // 存储入度为0的顶点 for (int i = 0; i < G->vexNum; i++) { if (G->adjList[i].indegree == 0) { queue[rear++] = i; // 入度为0的顶点入队 } } while (front != rear) { // 队列非空 int u = queue[front++]; // 出队一个顶点 result[count++] = u; // 存储排序结果 for (EdgeNode *e = G->adjList[u].firstEdge; e != NULL; e = e->next) { int v = e->adjvex; if (--G->adjList[v].indegree == 0) { queue[rear++] = v; // 入度为0的顶点入队 } } } if (count != G->vexNum) { // 存在环路 return ERROR; } return 0; } int main() { Graph G; int vexNum = 6; // 顶点数 int result[MAX_VERTEX_NUM]; // 存储排序结果 initGraph(&G, vexNum); addEdge(&G, 0, 1); addEdge(&G, 0, 2); addEdge(&G, 1, 3); addEdge(&G, 2, 3); addEdge(&G, 2, 4); addEdge(&G, 3, 5); if (topSort(&G, result) == ERROR) { printf("存在环路\n"); } else { printf("拓扑排序结果:\n"); for (int i = 0; i < vexNum; i++) { printf("%d ", result[i]); } printf("\n"); } return 0; } ``` 以上代码中,`initGraph()`函数用于初始化图,`addEdge()`函数用于添加边,`topSort()`函数用于执行拓扑排序,并将排序结果存储在`result`数组中。在`topSort()`函数中,我们使用队列来存储入度为0的顶点,并依次从队列中取出顶点来构建排序序列。如果排序序列的长度不等于顶点数,则说明存在环路,返回错误标识。否则,将排序序列输出即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值