拓扑排序代码实现
程序员文章站
2024-03-19 11:59:28
...
main.cpp
#include<stdio.h>
#include<stdlib.h>
#include"Queue.h"
void main()
{
printf("拓扑排序是有向图\n");
LGraph Graph;
Graph = BuildGraph();
TopSort(Graph);
}
Queue.h
#ifndef QUEUE
#include"TopSort.h"
typedef struct {
int* base;
int front;
int rear;
}SqQueue;
void InitQueue(SqQueue* Q);
void getQueueLength(SqQueue Q);
void EnQueue(SqQueue* Q, int e);
void DeQueue(SqQueue* Q, Vertex* V);
bool IsEmpty(SqQueue Q);
#endif // !QUEUE
#pragma once
Queue.cpp
#include"Queue.h"
#include"stdlib.h"
#include"stdio.h"
bool IsEmpty(SqQueue Q)
{
return Q.front == Q.rear ? true : false;
}
void InitQueue(SqQueue* Q)
{
Q->base = (int*)malloc(sizeof(int) * MaxVertexNum);
if (!Q)
{
printf("error");
}
else {
Q->front = Q->rear = 0;
}
}
void getQueueLength(SqQueue Q)
{
printf("\n length = %d \n", (Q.rear - Q.front + MaxVertexNum) % MaxVertexNum);
}
void EnQueue(SqQueue* Q, int e)
{
if ((Q->rear + 1) % MaxVertexNum == Q->front)
{
printf("full");
}
else
{
Q->base[Q->rear] = e;
Q->rear = (Q->rear + 1) % MaxVertexNum;
}
}
void DeQueue(SqQueue* Q, Vertex* V)
{
if (Q->rear == Q->front)
{
printf("no element");
}
else
{
*V = Q->base[Q->front];
Q->front = (Q->front + 1) % MaxVertexNum;
}
}
TopSort.h
#ifndef TOPSORT
#define MaxVertexNum 100
typedef int Vertex; //顶点下标
typedef int WeigthType; //边的权值
typedef int DataType; //顶点的数据类型
//边的定义
typedef struct ENode {
Vertex v1, v2;
WeigthType weigth;
}*PtrToENode;
typedef PtrToENode Edge;
//邻接点定义
typedef struct AdjVNode {
Vertex AdjV; //邻接点的下标
WeigthType weight; //边的权重
struct AdjVNode* Next; //下一个邻接点
}*PtrToAdjVNode;
//表头节点定义
typedef struct VNode {
PtrToAdjVNode FirstEdge;
DataType Data; //可不用
}AdjList[MaxVertexNum];
// 图结点的定义
typedef struct GNode {
int Nv; /* 顶点数 */
int Ne; /* 边数 */
AdjList G; /* 邻接表 */
}*PtrToGNode;
typedef PtrToGNode LGraph;
//插入一条边
void InsertEdge(LGraph Graph, Edge E);
//构建一个图
LGraph BuildGraph();
//拓扑排序
void TopSort(LGraph Graph);
#endif // !TOPSORT
#pragma once
TopSort.cpp
#include"TopSort.h"
#include"Queue.h"
#include<stdio.h>
#include<stdlib.h>
Vertex TopOrder[MaxVertexNum];
// 初始化没有边的图
LGraph CreatGraph(int VertexNum)
{
Vertex V;
LGraph Graph;
Graph = (LGraph)malloc(sizeof(struct GNode));
Graph->Nv = VertexNum;
Graph->Ne = 0;
for (V = 0; V < Graph->Nv; V++)
Graph->G[V].FirstEdge = NULL;
return Graph;
}
//插入一条边 v1(出)->v2(入)
void InsertEdge(LGraph Graph, Edge E)
{
PtrToAdjVNode newNode;
newNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
newNode->AdjV = E->v2;
newNode->weight = E->weigth;
newNode->Next = Graph->G[E->v1].FirstEdge;
Graph->G[E->v1].FirstEdge = newNode;
}
//构建一个图
LGraph BuildGraph()
{
int Nv, i;
Vertex V;
Edge E;
LGraph Graph;
printf_s("请输入插入节点的个数\n");
scanf_s("%d", &Nv);
Graph = CreatGraph(Nv);
printf_s("请输入插入边的个数\n");
scanf_s("%d", &(Graph->Ne));
if (Graph->Ne != 0)
{
E = (Edge)malloc(sizeof(struct ENode));
if (!E)
{
printf("E生成失败");
exit(-1);
return NULL;
}
for (i = 0; i < Graph->Ne; i++)
{
printf("请输入第%d条边: V1,V2,权重\n", i);
scanf_s("%d %d %d", &(E->v1), &(E->v2), &(E->weigth));
InsertEdge(Graph, E);
}
}
for (V = 0; V < Graph->Nv; V++)
{
printf("请输入第%d个顶点的数据\n", V);
scanf_s("%d", &(Graph->G[V].Data));
}
return Graph;
}
void TopSort(LGraph Graph)
{
int Indegree[MaxVertexNum];//入度
int cnt = 0;
Vertex V;
PtrToAdjVNode W;
SqQueue Q;
//初始化队列
InitQueue(&Q);
//初始化入度
for (V = 0; V < Graph->Nv; V++)
{
Indegree[V] = 0;
}
//遍历图得到所有点的入度
for ( V = 0; V < Graph->Nv; V++)
{
for ( W = Graph->G[V].FirstEdge; W ; W = W->Next)
{
Indegree[W->AdjV]++;
}
}
//将所有入度为零的点入队
for (V = 0; V < Graph->Nv; V++)
{
if (Indegree[V] == 0)
{
EnQueue(&Q, V);
}
}
//出队后删除入度
while (!IsEmpty(Q))
{
DeQueue(&Q,&V);
TopOrder[cnt++] = V;
for ( W = Graph->G[V].FirstEdge; W ; W = W->Next)
{
if (--Indegree[W->AdjV] == 0)//删除入度
{
EnQueue(&Q,W->AdjV);
}
}
printf("\n");
printf("%d\t",V);
}
}
下一篇: 2017.10.28 排序 思考记录