图的邻接矩阵

图的特点:非线性多对多


#ifndef _QUEUE_H_
#define _QUEUE_H_
#define MAXSIZE 4
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int ElemType;
typedef int Status;
typedef struct
{
 ElemType *base;
 int front;
 int rear;
}SqQueue;
Status InitQueue(SqQueue &Q);
Status DestoryQueue(SqQueue &Q);
Status ClearQueue(SqQueue &Q);
Status EmptyQueue(SqQueue &Q);
Status EnterQueue(SqQueue &Q, ElemType e);
Status DeleteQueue(SqQueue &Q, ElemType &e);
#endif

#ifndef _GRAPH_H_
#define _GRAPH_H
#define OK 1
#define ERROR -1
#define MAX_VERTEX_NUM 20
typedef int VertexType;
typedef int Status;
//邻接矩阵表示
typedef enum{DG,DN,UDG,UDN}GraphKind;//有向图、有向网、无向图、无向网
typedef struct
{
 VertexType vexs[MAX_VERTEX_NUM];
 int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
 int vexnum, arcnum;
 GraphKind kind;
}Mgraph;
void  CreateUDG(Mgraph &G);
Status Degree(Mgraph G,VertexType v1);
void BFESTraverse(Mgraph G);
Status FirstAdjVex(Mgraph G, VertexType v1);
Status NextAdjVex(Mgraph G, VertexType v1, VertexType v2);
#endif

#include"queue.h"
#include<iostream>
using namespace std;
//构造空队列
Status InitQueue(SqQueue &Q)
{
 Q.base = (ElemType*)malloc(MAXSIZE*sizeof(ElemType));
 if (!Q.base)
 {
  return OVERFLOW;
 }
 else
 {
  Q.front = Q.rear = 0;
  return OK;
 }
}
//销毁队列
Status DestoryQueue(SqQueue &Q)
{
 free(Q.base);
 return TRUE;
}
//清空队列
Status ClearQueue(SqQueue &Q)
{
 Q.rear = Q.front;
 return OK;
}
//置空队列
Status EmptyQueue(SqQueue &Q)
{
 if (Q.rear != Q.front)
 {
  return FALSE;
 }
 else
 {
  return TRUE;
 }
}
//入队
Status EnterQueue(SqQueue &Q, ElemType e)
{
 if ((Q.rear + 1) % MAXSIZE == Q.front)
 {
  return ERROR;
 }
 else
 {
  Q.base[Q.rear] = e;
  Q.rear = (Q.rear + 1) % MAXSIZE;
  return OK;
 }
}
//出队
Status DeleteQueue(SqQueue &Q, ElemType &e)
{
 if (Q.front == Q.rear)
 {
  return ERROR;
 }
 else
 {
  e = Q.base[Q.front];
  Q.front = (Q.front + 1) % MAXSIZE;
  return OK;
 }
}


#include"Graph.h"
#include"queue.h"
#include<iostream>
using namespace std;
//邻接矩阵创建无向图
void  CreateUDG(Mgraph &G)
{
 int i,j,k, v1, v2;
 cout << "请输入顶点数:" << endl;
 cin >> G.vexnum;
 cout << "请输入弧数:" << endl;
 cin >> G.arcnum;
 for (i = 0; i < G.vexnum; i++)
 {
   G.vexs[i]=i;
 }
 for (i = 0; i < G.vexnum; i++)
 {
  for (j = 0; j < G.vexnum; j++)
  {
   G.arcs[i][j] = -1;
  }
 }
 cout << "请输入边:" << endl;
 for (k = 0; k < G.arcnum; k++)
 {
  cin >> v1 >> v2 ;
  G.arcs[v1][v2] = 1;
  G.arcs[v2][v1] = G.arcs[v1][v2];
 }
 for (i = 0; i < G.vexnum; i++)
 {
  for (j = 0; j < G.vexnum; j++)
  {
   if (G.arcs[i][j] == -1)
   {
    cout <<"0" << " ";
   }
   else
   {
    cout<<G.arcs[i][j]<<" ";
   }
   
  }
  cout << endl;
 }
 
}
//无向图顶点的度
Status Degree(Mgraph G, VertexType v1)
{
 int j, Degree = 0;
 if (v1 >= G.vexnum)
 {
  return ERROR;
 }
 else
 {
  for (j = 0; j < G.arcnum; j++)
  {
   if (G.arcs[v1][j] != 0)
   {
    Degree++;
   }
  }
  return Degree;
 }
}

//返回v的第一个邻接点
Status FirstAdjVex(Mgraph G, VertexType v1)
{
 int i;
 for (i = 0; i < G.vexnum; i++)
 {
  if (G.arcs[v1][i] != 0)
  {
   return i;
  }
 }
 return ERROR;
}
//返回v1相对于v2的下一个邻接顶点
Status NextAdjVex(Mgraph G, VertexType v1, VertexType v2)
{
 int i;
 for (i = v2 + 1; i < G.vexnum; i++)
 {
  if (G.arcs[v1][i] != 0)
  {
   return i;
  }
 }
 return ERROR;
}
//邻接矩阵广度优先遍历
void BFESTraverse(Mgraph G)
{
 int v, w,u,visit[20];
 SqQueue Q;
 for (v = 0; v < G.vexnum; v++)
 {
  visit[v] = 0;
 }
 InitQueue(Q);
 for (v = 0; v < G.vexnum; v++)
 {
  if (!visit[v])
  {
   visit[v] = 1;
   cout << G.vexs[v]<<" ";
   EnterQueue(Q, v);
   while (!EmptyQueue(Q))
   {
    DeleteQueue(Q, u);
    for (w = FirstAdjVex(G, u); w >= 0; w = NextAdjVex( G, u, w))
    {
     if (!visit[w])
     {
      visit[w] = 1;
      cout << G.vexs[w]<<" ";
      EnterQueue(Q, w);
     }
    }
   }
  }
 }
 cout << endl;
}


#include"Graph.h"
#include"queue.h"
#include<iostream>
using namespace std;
int main()
{
 int select, v;
 Mgraph G;
 do
 {
  cout << "1.创建无向图!" << endl;
  cout << "2.无向图顶点的度(邻接矩阵)!" << endl;
  cout << "3.邻接矩阵广度优先遍历!" << endl;
  cout << "0.结束!" << endl;
  cout << "请选择:" << endl;
  cin >> select;
  switch (select)
  {
  case 1:
   CreateUDG(G);
   break;
  case 2:
   cout << "请输入顶点:" << endl;
   cin >> v;
   if (Degree(G, v) == ERROR)
   {
    cout << "顶点不存在!" << endl;
   }
   else
   {
    cout << v << "的度为:" << Degree(G, v) << endl;
   }
   break;
  case 3:
   BFESTraverse(G);
   break;
  case 0:
   cout << "操作结束!" << endl;
   break;
  default:
   cout << "输入错误!" << endl;
  }
 } while (select != 0);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值