#ifndef _GRAPH_H_
#define _GRAPH_H
#define _GRAPH_H
#define OK 1
#define ERROR 0
#define INFINTY -1
#define MAX_VERTEX_NUM 20
#define ERROR 0
#define INFINTY -1
#define MAX_VERTEX_NUM 20
typedef int VertexType;
typedef int Status;
//邻接表表示
typedef struct ArcNode
{
int adjvex;
struct ArcNode *nextarc;
}ArcNode;
typedef struct VNode
{
VertexType data;
ArcNode *firstarc;
}VNode, AdjList[MAX_VERTEX_NUM];
typedef struct
{
AdjList vertices;
int vexnum, arcnum;
int kind;
}ALGraph;
typedef int Status;
//邻接表表示
typedef struct ArcNode
{
int adjvex;
struct ArcNode *nextarc;
}ArcNode;
typedef struct VNode
{
VertexType data;
ArcNode *firstarc;
}VNode, AdjList[MAX_VERTEX_NUM];
typedef struct
{
AdjList vertices;
int vexnum, arcnum;
int kind;
}ALGraph;
void CreateDG(ALGraph &G);
Status InDegree(ALGraph G, VertexType v);
Status OutDegree(ALGraph G, VertexType v);
void DFS(ALGraph G, VertexType v1);
void DFSTraverse(ALGraph G);
Status FirstAdjVex(ALGraph G, VertexType v1);
Status NextAdjVex(ALGraph G, VertexType v1, VertexType w);
Status InDegree(ALGraph G, VertexType v);
Status OutDegree(ALGraph G, VertexType v);
void DFS(ALGraph G, VertexType v1);
void DFSTraverse(ALGraph G);
Status FirstAdjVex(ALGraph G, VertexType v1);
Status NextAdjVex(ALGraph G, VertexType v1, VertexType w);
#endif
#include"graph.h"
#include<iostream>
using namespace std;
//邻接表创建有向图
void CreateDG(ALGraph &G)
{
ArcNode *p;
int num, i, v1, v2;
cout << "请输入顶点数:" << endl;
cin >> G.vexnum;
cout << "请输入弧数:" << endl;
cin >> G.arcnum;
for (i = 0; i < G.vexnum; i++)
{
G.vertices[i].firstarc = NULL;
G.vertices[i].data = i;
}
for (i = 0; i < G.arcnum; i++)
{
cout << "请输入第" << i << "边" << endl;
cin >> v1 >> v2;
p = (ArcNode*)malloc(sizeof(ArcNode));
p->adjvex = v2;
p->nextarc = G.vertices[v1].firstarc;
G.vertices[v1].firstarc = p;
}
}
#include<iostream>
using namespace std;
//邻接表创建有向图
void CreateDG(ALGraph &G)
{
ArcNode *p;
int num, i, v1, v2;
cout << "请输入顶点数:" << endl;
cin >> G.vexnum;
cout << "请输入弧数:" << endl;
cin >> G.arcnum;
for (i = 0; i < G.vexnum; i++)
{
G.vertices[i].firstarc = NULL;
G.vertices[i].data = i;
}
for (i = 0; i < G.arcnum; i++)
{
cout << "请输入第" << i << "边" << endl;
cin >> v1 >> v2;
p = (ArcNode*)malloc(sizeof(ArcNode));
p->adjvex = v2;
p->nextarc = G.vertices[v1].firstarc;
G.vertices[v1].firstarc = p;
}
}
//有向图的出度
Status OutDegree(ALGraph G, VertexType v)
{
ArcNode *p;
int OutDegree=0;
if (v >= G.vexnum)
{
return ERROR;
}
else
{
p = G.vertices[v].firstarc;
while (p)
{
OutDegree++;
p = p->nextarc;
}
return OutDegree;
}
}
Status OutDegree(ALGraph G, VertexType v)
{
ArcNode *p;
int OutDegree=0;
if (v >= G.vexnum)
{
return ERROR;
}
else
{
p = G.vertices[v].firstarc;
while (p)
{
OutDegree++;
p = p->nextarc;
}
return OutDegree;
}
}
//有向图的入度
Status InDegree(ALGraph G, VertexType v)
{
ArcNode *p;
int i,j,InDegree=0;
if (v >= G.vexnum)
{
return ERROR;
}
else
{
for (j = 0; j < G.vexnum; j++)
{
p = G.vertices[j].firstarc;
while (p)
{
if (p->adjvex == v)
{
InDegree++;
}
p = p->nextarc;
}
}
return InDegree;
}
}
Status InDegree(ALGraph G, VertexType v)
{
ArcNode *p;
int i,j,InDegree=0;
if (v >= G.vexnum)
{
return ERROR;
}
else
{
for (j = 0; j < G.vexnum; j++)
{
p = G.vertices[j].firstarc;
while (p)
{
if (p->adjvex == v)
{
InDegree++;
}
p = p->nextarc;
}
}
return InDegree;
}
}
//返回邻接表的第一个邻接点
Status FirstAdjVex(ALGraph G, VertexType v)
{
ArcNode *p;
p = G.vertices[v].firstarc;
if (p)
{
return p->adjvex;
}
else
{
return ERROR;
}
}
//邻接表 返回v相对于w的下一个邻接顶点的第一个邻接点
Status NextAdjVex(ALGraph G, VertexType v, VertexType w)
{
ArcNode *p;
p = G.vertices[w].firstarc;
if (p)
{
return p->adjvex;
}
else
{
return ERROR;
}
}
Status NextAdjVex(ALGraph G, VertexType v, VertexType w)
{
ArcNode *p;
p = G.vertices[w].firstarc;
if (p)
{
return p->adjvex;
}
else
{
return ERROR;
}
}
//邻接表深度优先遍历
bool visit[20];
void DFS(ALGraph G, VertexType v)
{
int w;
visit[v] = 1;
cout << v<< " ";
for (w = FirstAdjVex(G, v); w >= 0; w = NextAdjVex(G, v, w))
{
if (w != ERROR)
{
if (!visit[w])
{
DFS(G, w);
}
}
}
}
bool visit[20];
void DFS(ALGraph G, VertexType v)
{
int w;
visit[v] = 1;
cout << v<< " ";
for (w = FirstAdjVex(G, v); w >= 0; w = NextAdjVex(G, v, w))
{
if (w != ERROR)
{
if (!visit[w])
{
DFS(G, w);
}
}
}
}
void DFSTraverse(ALGraph G)
{
int v;
for (v = 0; v < G.vexnum; v++)
{
visit[v] = 0;
}
for (v = 0; v < G.vexnum; v++)
{
if (!visit[v])
{
DFS(G, v);
}
}
}
{
int v;
for (v = 0; v < G.vexnum; v++)
{
visit[v] = 0;
}
for (v = 0; v < G.vexnum; v++)
{
if (!visit[v])
{
DFS(G, v);
}
}
}
#include"graph.h"
#include<iostream>
using namespace std;
#include<iostream>
using namespace std;
int main()
{
int select, v;
ALGraph G;
do
{
cout << "1.创建有向图!" << endl;
cout << "2.有向图的出度(邻接表)!" << endl;
cout << "3.有向图的入度(邻接表)!" << endl;
cout << "4.邻接表深度优先遍历!" << endl;
cout << "0.结束!" << endl;
cout << "请选择:" << endl;
cin >> select;
switch (select)
{
case 1:
CreateDG(G);
break;
case 2:
cout << "请输入顶点:" << endl;
cin >> v;
if (OutDegree(G, v) == ERROR)
{
cout << "出度为0或没有该顶点!" << endl;
}
else
{
cout << v << "的出度为:" << OutDegree(G, v) << endl;
}
break;
case 3:
cout << "请输入顶点:" << endl;
cin >> v;
if (InDegree(G, v) == ERROR)
{
cout << "入度为0或没有该顶点!" << endl;
}
else
{
cout << v << "的入度为:" << InDegree(G, v) << endl;
}
break;
case 4:
DFSTraverse(G);
break;
case 0:
cout << "操作结束!" << endl;
break;
default:
cout << "输入错误!" << endl;
}
} while (select != 0);
}
{
int select, v;
ALGraph G;
do
{
cout << "1.创建有向图!" << endl;
cout << "2.有向图的出度(邻接表)!" << endl;
cout << "3.有向图的入度(邻接表)!" << endl;
cout << "4.邻接表深度优先遍历!" << endl;
cout << "0.结束!" << endl;
cout << "请选择:" << endl;
cin >> select;
switch (select)
{
case 1:
CreateDG(G);
break;
case 2:
cout << "请输入顶点:" << endl;
cin >> v;
if (OutDegree(G, v) == ERROR)
{
cout << "出度为0或没有该顶点!" << endl;
}
else
{
cout << v << "的出度为:" << OutDegree(G, v) << endl;
}
break;
case 3:
cout << "请输入顶点:" << endl;
cin >> v;
if (InDegree(G, v) == ERROR)
{
cout << "入度为0或没有该顶点!" << endl;
}
else
{
cout << v << "的入度为:" << InDegree(G, v) << endl;
}
break;
case 4:
DFSTraverse(G);
break;
case 0:
cout << "操作结束!" << endl;
break;
default:
cout << "输入错误!" << endl;
}
} while (select != 0);
}
本文介绍了一种使用邻接表表示有向图的方法,并实现了有向图的基本操作,包括创建有向图、计算顶点的入度和出度、深度优先遍历等。通过这些操作可以更好地理解和应用图论算法。
1万+

被折叠的 条评论
为什么被折叠?



