//图-邻接表-拓扑排序
#include<Iostream>
#include<Cstdlib>
#include<malloc.h>
using namespace std;
#define INFINITY INT_MAX//最大值
#define MAX_VERTEX_NUM 20//最大定点个数
#define VRType int//定点关系类型,一般是整数表示
#define InfoType char//弧相关信息的指针类型
#define VertexType char//顶点向量类型
#define Status int//函数返回的状态
#define STACK_INIT_SIZE 100//存储空间初始分配量
#define STACKINCREMENT 10//存储空间分配增量
#define Status int
#define SElemType int
#define OVERFLOW 0
#define NONE 0
#define ERROR 0
#define OK 1
#define FALSE 0
#define TRUE 1
typedef enum {DG,DN,UDG,UDN} GraphKind;
typedef struct ArcNode//邻接表的弧结构
{
int adjvex;//该弧指向的顶点位置
struct ArcNode *nextarc;//指向下一个弧
InfoType *info;//该弧相关信息
int Power;//该弧的权值
}ArcNode;
typedef struct VNode//顶点结构
{
VertexType data;//顶点信息
ArcNode *firstarc;//指向第一条依附该顶点的弧
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct//临界表的结构
{
AdjList vertices;//定点们
int vexnum,arcnum;//顶点和弧的个数
GraphKind kind;
}ALGraph;
typedef struct//栈结构
{
SElemType *base;//在栈构造之前和销毁之后,base的值为NULL
SElemType *top;//栈顶指针
int stacksize;//当前已经分配的存储空间,以元素为单位
}SqStack;
Status InitStack(SqStack &S);//初始化栈
Status DestroyStack(SqStack &S);//销毁栈
Status Push(SqStack &S,SElemType e);//压栈
Status Pop(SqStack &S,SElemType &e);//出栈
Status StackEmpty(SqStack &S);//是否为空
Status CreateGraph(ALGraph &G);//创建一个图,并返回给G
Status CreateDN(ALGraph &G);//创建一个有向网G-采用临界表
Status PrintAL(AdjList vertices,int vexunm);//输出一个临界表
Status FindInDegree(ALGraph G,int indegree[]);//找出邻接表各节点的入度并存在数组indegree里
Status TopologicalSort(ALGraph G);//又向图的拓扑排序
void main()//主函数
{
ALGraph AG;//先上道具
CreateGraph(AG);//创建图
PrintAL(AG.vertices,AG.vexnum);//看下临界表
TopologicalSort(AG);//拓扑排序
}
Status CreateGraph(ALGraph &G)
{
int i=4;
cout<<"What kind of graph shall we create?:1.DG 2.DN 3.UDG 4.UDN"<<endl;
cin>>i;
switch(i)
{
case 1:
G.kind=DG;
return NONE;//本程序不实现先
case 2:
G.kind=DN;
return CreateDN(G);//本程序不实现先
case 3:
G.kind=UDG;
return NONE;//本程序不实现先
case 4:
G.kind=UDN;
return NONE;//只实现这个无向网的
default:
return ERROR;
}
}
Status CreateDN(ALGraph &G)//有向网
{
ArcNode *Arcp=NULL;//定义一个弧节点的指针
ArcNode *Tail=NULL;//弧链表的尾巴指针
int i,j,v,a,anum;//循环变量以及定点和弧的个数
cout<<"Please input the vexnum and the arcnum:";
cin>>v>>a;
G.arcnum=a;//弧的个数
G.vexnum=v;//定点的个数
G.kind=DN;//图的种类
for(i=0;i<G.vexnum;i++)
G.vertices[i].firstarc=NULL;//将所有的节点的指针都初始化
cout<<"Please input the vertices:";
for(i=0;i<G.vexnum;i++)
cin>>G.vertices[i].data;//输入定点
cout<<"Please input the arc's number and the arc's head location:"<<endl;//请输入每个节点弧的个数以及弧的顶点位置
for(i=0;i<G.vexnum;i++)
{
cin>>anum;//输入第i个节点的弧的个数
for(j=0;j<anum;j++)
{
Arcp=(ArcNode *)malloc(sizeof(ArcNode));//把分配的空间先给Arcp
if(G.vertices[i].firstarc==NULL)
{
G.vertices[i].firstarc=Arcp;//如果是第一个就like this 你懂得~
cin>>G.vertices[i].firstarc->adjvex;//该弧定点位置
Tail=G.vertices[i].firstarc;//Tail指向了第一个弧节点
Tail->nextarc=NULL;//初始化
}
else
{
Tail->nextarc=Arcp;//你懂得
cin>>Tail->nextarc->adjvex;//转化权值哦~
Tail=Tail->nextarc;//指向下一个节点
Tail->nextarc=NULL;//初始化
}
}
}
return OK;
}
Status PrintAL(AdjList vertices,int vexnum)//输出一个邻接表
{
int i;
ArcNode *p;
for(i=0;i<vexnum;i++)
{
cout<<"The ver:"<<vertices[i].data<<" The arc:";
p=vertices[i].firstarc;
while(p!=NULL)//输出弧节点的权值
{
cout<<p->adjvex<<" ";
p=p->nextarc;
}
cout<<endl;
}
return OK;
}
Status FindInDegree(ALGraph G,int indegree[])//找出每个定点的入度
{
ArcNode *p=NULL;
int i;
for(i=0;i<G.vexnum;i++)
{
p=G.vertices[i].firstarc;
while(p!=NULL)//输出弧节点的权值
{
indegree[p->adjvex]++;
p=p->nextarc;
}
}
return OK;
}
Status InitStack(SqStack &S)//初始化
{
//构造一个空的栈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 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;
}
Status Pop(SqStack &S,SElemType &e)//弹出
{
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}
Status DestroyStack(SqStack &S)//释放所有空间
{
SElemType *curp;
if(S.base!=S.top){
for(curp=S.base;curp<S.top;curp++)
free(curp);
free(curp);//先释放掉栈的空间
}
SqStack *sp=&S;//在释放栈结构的空间
return OK;
}
Status TopologicalSort(ALGraph G)
{
//若G无回路,则输出G的定点的一个拓扑序列并返回OK,否则返回ERROR
int i;//循环变量
int count,k;//对输入定点计数
int indegree[MAX_VERTEX_NUM];//用来存放每个定点入度个数
ArcNode *p=NULL;
for(i=0;i<MAX_VERTEX_NUM;i++)
indegree[i]=0;//全初始化为0
SqStack S;//创建一个Stack
FindInDegree(G,indegree);//找出所有入度
InitStack(S);//初始化
for(i=0;i<G.vexnum;i++)
if(!indegree[i])
Push(S,i);
count=0;
cout<<"The TopologicalSort is:"<<endl;
while(!StackEmpty(S))
{
Pop(S,i);
cout<<i<<" "<<G.vertices[i].data<<endl;//开始输出拓扑排序序列
count++;
for(p=G.vertices[i].firstarc;p;p=p->nextarc)
{
k=p->adjvex;
if(!(--indegree[k])) Push(S,k);
}
}
if(count<G.vexnum) return ERROR;
else return OK;
}
Status StackEmpty(SqStack &S)//判断是否为空栈
{
if(S.top==S.base)
return OK;
return ERROR;
}
#include<Iostream>
#include<Cstdlib>
#include<malloc.h>
using namespace std;
#define INFINITY INT_MAX//最大值
#define MAX_VERTEX_NUM 20//最大定点个数
#define VRType int//定点关系类型,一般是整数表示
#define InfoType char//弧相关信息的指针类型
#define VertexType char//顶点向量类型
#define Status int//函数返回的状态
#define STACK_INIT_SIZE 100//存储空间初始分配量
#define STACKINCREMENT 10//存储空间分配增量
#define Status int
#define SElemType int
#define OVERFLOW 0
#define NONE 0
#define ERROR 0
#define OK 1
#define FALSE 0
#define TRUE 1
typedef enum {DG,DN,UDG,UDN} GraphKind;
typedef struct ArcNode//邻接表的弧结构
{
int adjvex;//该弧指向的顶点位置
struct ArcNode *nextarc;//指向下一个弧
InfoType *info;//该弧相关信息
int Power;//该弧的权值
}ArcNode;
typedef struct VNode//顶点结构
{
VertexType data;//顶点信息
ArcNode *firstarc;//指向第一条依附该顶点的弧
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct//临界表的结构
{
AdjList vertices;//定点们
int vexnum,arcnum;//顶点和弧的个数
GraphKind kind;
}ALGraph;
typedef struct//栈结构
{
SElemType *base;//在栈构造之前和销毁之后,base的值为NULL
SElemType *top;//栈顶指针
int stacksize;//当前已经分配的存储空间,以元素为单位
}SqStack;
Status InitStack(SqStack &S);//初始化栈
Status DestroyStack(SqStack &S);//销毁栈
Status Push(SqStack &S,SElemType e);//压栈
Status Pop(SqStack &S,SElemType &e);//出栈
Status StackEmpty(SqStack &S);//是否为空
Status CreateGraph(ALGraph &G);//创建一个图,并返回给G
Status CreateDN(ALGraph &G);//创建一个有向网G-采用临界表
Status PrintAL(AdjList vertices,int vexunm);//输出一个临界表
Status FindInDegree(ALGraph G,int indegree[]);//找出邻接表各节点的入度并存在数组indegree里
Status TopologicalSort(ALGraph G);//又向图的拓扑排序
void main()//主函数
{
ALGraph AG;//先上道具
CreateGraph(AG);//创建图
PrintAL(AG.vertices,AG.vexnum);//看下临界表
TopologicalSort(AG);//拓扑排序
}
Status CreateGraph(ALGraph &G)
{
int i=4;
cout<<"What kind of graph shall we create?:1.DG 2.DN 3.UDG 4.UDN"<<endl;
cin>>i;
switch(i)
{
case 1:
G.kind=DG;
return NONE;//本程序不实现先
case 2:
G.kind=DN;
return CreateDN(G);//本程序不实现先
case 3:
G.kind=UDG;
return NONE;//本程序不实现先
case 4:
G.kind=UDN;
return NONE;//只实现这个无向网的
default:
return ERROR;
}
}
Status CreateDN(ALGraph &G)//有向网
{
ArcNode *Arcp=NULL;//定义一个弧节点的指针
ArcNode *Tail=NULL;//弧链表的尾巴指针
int i,j,v,a,anum;//循环变量以及定点和弧的个数
cout<<"Please input the vexnum and the arcnum:";
cin>>v>>a;
G.arcnum=a;//弧的个数
G.vexnum=v;//定点的个数
G.kind=DN;//图的种类
for(i=0;i<G.vexnum;i++)
G.vertices[i].firstarc=NULL;//将所有的节点的指针都初始化
cout<<"Please input the vertices:";
for(i=0;i<G.vexnum;i++)
cin>>G.vertices[i].data;//输入定点
cout<<"Please input the arc's number and the arc's head location:"<<endl;//请输入每个节点弧的个数以及弧的顶点位置
for(i=0;i<G.vexnum;i++)
{
cin>>anum;//输入第i个节点的弧的个数
for(j=0;j<anum;j++)
{
Arcp=(ArcNode *)malloc(sizeof(ArcNode));//把分配的空间先给Arcp
if(G.vertices[i].firstarc==NULL)
{
G.vertices[i].firstarc=Arcp;//如果是第一个就like this 你懂得~
cin>>G.vertices[i].firstarc->adjvex;//该弧定点位置
Tail=G.vertices[i].firstarc;//Tail指向了第一个弧节点
Tail->nextarc=NULL;//初始化
}
else
{
Tail->nextarc=Arcp;//你懂得
cin>>Tail->nextarc->adjvex;//转化权值哦~
Tail=Tail->nextarc;//指向下一个节点
Tail->nextarc=NULL;//初始化
}
}
}
return OK;
}
Status PrintAL(AdjList vertices,int vexnum)//输出一个邻接表
{
int i;
ArcNode *p;
for(i=0;i<vexnum;i++)
{
cout<<"The ver:"<<vertices[i].data<<" The arc:";
p=vertices[i].firstarc;
while(p!=NULL)//输出弧节点的权值
{
cout<<p->adjvex<<" ";
p=p->nextarc;
}
cout<<endl;
}
return OK;
}
Status FindInDegree(ALGraph G,int indegree[])//找出每个定点的入度
{
ArcNode *p=NULL;
int i;
for(i=0;i<G.vexnum;i++)
{
p=G.vertices[i].firstarc;
while(p!=NULL)//输出弧节点的权值
{
indegree[p->adjvex]++;
p=p->nextarc;
}
}
return OK;
}
Status InitStack(SqStack &S)//初始化
{
//构造一个空的栈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 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;
}
Status Pop(SqStack &S,SElemType &e)//弹出
{
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}
Status DestroyStack(SqStack &S)//释放所有空间
{
SElemType *curp;
if(S.base!=S.top){
for(curp=S.base;curp<S.top;curp++)
free(curp);
free(curp);//先释放掉栈的空间
}
SqStack *sp=&S;//在释放栈结构的空间
return OK;
}
Status TopologicalSort(ALGraph G)
{
//若G无回路,则输出G的定点的一个拓扑序列并返回OK,否则返回ERROR
int i;//循环变量
int count,k;//对输入定点计数
int indegree[MAX_VERTEX_NUM];//用来存放每个定点入度个数
ArcNode *p=NULL;
for(i=0;i<MAX_VERTEX_NUM;i++)
indegree[i]=0;//全初始化为0
SqStack S;//创建一个Stack
FindInDegree(G,indegree);//找出所有入度
InitStack(S);//初始化
for(i=0;i<G.vexnum;i++)
if(!indegree[i])
Push(S,i);
count=0;
cout<<"The TopologicalSort is:"<<endl;
while(!StackEmpty(S))
{
Pop(S,i);
cout<<i<<" "<<G.vertices[i].data<<endl;//开始输出拓扑排序序列
count++;
for(p=G.vertices[i].firstarc;p;p=p->nextarc)
{
k=p->adjvex;
if(!(--indegree[k])) Push(S,k);
}
}
if(count<G.vexnum) return ERROR;
else return OK;
}
Status StackEmpty(SqStack &S)//判断是否为空栈
{
if(S.top==S.base)
return OK;
return ERROR;
}