图之邻接表

本文介绍了一种使用邻接表表示有向图的方法,并实现了有向图的基本操作,包括创建有向图、计算顶点的入度和出度、深度优先遍历等。通过这些操作可以更好地理解和应用图论算法。
#ifndef _GRAPH_H_
#define _GRAPH_H
#define OK 1
#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;
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);
#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;
 }
}
//有向图的出度
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 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;
 }
}
//邻接表深度优先遍历
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);
  }
 }
}

#include"graph.h"
#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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值