C++数据结构邻接矩阵基本操作集(自编)

邻接矩阵基本操作

  1. 建立邻接矩阵
    CreateMGraph(MGraph &G, VertexType v[],EdgeType e[][MAXSIZE],int n)
  2. 返回顶点下标
    getVertexidx(MGraph G, VertexType x)
  3. 判断边是否存在
    Adjacent(MGraph G, VertexType x, VertexType y)
  4. 某点所连接的所有点
    Neighbors(MGraph G, VertexType x)
  5. 插入顶点
    InsertVertex(MGraph &G, VertexType x)
  6. 删除顶点
    DeleteVertex(MGraph &G, VertexType x)
  7. 插入边
    AddEdge(MGraph& G, VertexType x, VertexType y)
  8. 删除边
    RemoveEdge(MGraph& G, VertexType x, VertexType y)
  9. 第一个连接的顶点
    FirstNerghbor(MGraph G, int x)
  10. 下一个连接的顶点
    NextNeighbor(MGraph G, int x, int y)
  11. BFS
    BFSTraverse(MGraph G)
  12. DFS
    DFSTraverse(MGraph G)

其中的算法,会在个人主页单独描述

#include <iostream>
using namespace std;
#define  ERROR -1
#define  OK 1
#define  MAXSIZE 10

typedef char VertexType;
typedef int EdgeType;
typedef struct
{
	VertexType Vex[MAXSIZE];
	EdgeType Edge[MAXSIZE][MAXSIZE];
	int Vexnum, Arcnum;
}MGraph;

void Visit(MGraph G, int x);
void ArrayVisit(MGraph G);

int initMGraph(MGraph& G)
{
	G.Vexnum = 0;
	G.Arcnum = 0;
	return OK;
}

int CreateMGraph(MGraph &G, VertexType v[],EdgeType e[][MAXSIZE],int n)
{
	int i = 0;
	
	for (i; i < n; i++)
		G.Vex[i] = v[i];
	int r, c;   //r = row c = column
	for (r = 0; r < n; r++)
		for (c = 0; c < n; c++)
		{
			if (e[r][c] == 1)
				G.Arcnum++;
			G.Edge[r][c] = e[r][c];
		}
	G.Vexnum = n;
	return OK;
}

bool FindedVextex(MGraph G, VertexType x)
{
	for (int i = 0; i < G.Vexnum; i++)
		if (G.Vex[i] == x)
			return true;
	return false;
}
bool Adjacent(MGraph G, VertexType x, VertexType y)
{
	int i = 0;
	int tag1 = 0, tag2 = 0;
	int idx1, idx2;
	while (G.Vex[i] != '\0')
	{
		if (x == G.Vex[i])
		{
			tag1 = 1; idx1 = i;
			i++;
		}
		else if (y == G.Vex[i])
		{
			tag2 = 1; idx2 = i;
			i++;
		}
		else
			i++;
	}
	if (!(tag1 == 1 && tag2 == 1))
		return false;
	if (G.Edge[idx1][idx2] == 1 || G.Edge[idx2][idx1] == 1)
		return true;
	else
		return false;
}

int getVertexidx(MGraph G, VertexType x)
{
	int i = 0;
	while (G.Vex[i] != '\0')
	{
		if (G.Vex[i] == x)
			return i;
		i++;
	}
	return -1;
}
int Neighbors(MGraph G, VertexType x)
{
	if (getVertexidx(G, x) == -1)
		return ERROR;
	for (int i = getVertexidx(G, x), j = 0; j < G.Vexnum; j++)
	{
		if (G.Edge[i][j] == 1)
			cout << G.Vex[j] << " ";
	}
	return OK;
}
int InsertVertex(MGraph &G, VertexType x)
{
	if (FindedVextex(G, x))
	{
		cout << "存在该点,插入失败" << endl;
		return ERROR;
	}
	G.Vex[G.Vexnum++] = x;
	for (int i = 0; i < G.Vexnum; i++)
		G.Edge[G.Vexnum - 1][i] = 0;
	for (int i = 0; i < G.Vexnum; i++)
		G.Edge[i][G.Vexnum - 1] = 0;
	return OK;
}
int DeleteVertex(MGraph& G, VertexType x)
{
	int count = 0;
	if (!FindedVextex(G, x))
	{
		cout << "不存在该点,无法删除" << endl;
		return ERROR;
	}
	int p = getVertexidx(G, x);
	for (int i = p; i < G.Vexnum; i++)
	{
		G.Vex[i] = G.Vex[i + 1];
	}
	for (int i = 0; i < G.Vexnum; i++)
		if (G.Edge[p][i] == 1)
			count++;
	for (int i = p; i < G.Vexnum; i++)
	{	
		for (int j = 0; j < G.Vexnum; j++)
			G.Edge[i][j] = G.Edge[i + 1][j];
	}
	for (int i = p; i < G.Vexnum; i++)
	{
		for (int j = 0; j < G.Vexnum; j++)
			G.Edge[j][i] = G.Edge[j][i + 1];
	}
	G.Vexnum--; 
	G.Arcnum -= 2 * count;
}

int AddEdge(MGraph& G, VertexType x, VertexType y)
{
	if (Adjacent(G, x, y))
		return ERROR;
	int i = getVertexidx(G, x);
	int j = getVertexidx(G, y);
	G.Edge[i][j] = G.Edge[j][i] = 1;
	return OK;
}

int RemoveEdge(MGraph& G, VertexType x, VertexType y)
{
	if (!Adjacent(G, x, y))
		return ERROR;
	int i = getVertexidx(G, x);
	int j = getVertexidx(G, y);
	G.Edge[i][j] = G.Edge[j][i] = 0;
	return OK;
}
int FirstNerghbor(MGraph G, int x)
{
	for (int i = 0; i < G.Vexnum; i++)
		if (G.Edge[x][i] == 1)
			return i;
	return -1;
}
int NextNeighbor(MGraph G, int x, int y)
{
	for (int i = y + 1; i < G.Vexnum; i++)
		if (G.Edge[x][i] == 1)
			return i;
	return -1;
}

typedef struct
{
	int data[MAXSIZE];
	int front, rear;
}SqQueue;

int initSqQueue(SqQueue& Q)
{
	Q.front = Q.rear = 0;
	return OK;
}

int EnQueue(SqQueue& Q, int p)
{
	if (Q.front == (Q.rear + 1) % MAXSIZE)
		return ERROR;
	Q.data[Q.rear] = p;
	Q.rear = (Q.rear + 1) % MAXSIZE;
	return OK;
}

int DeQueue(SqQueue& Q, int& p)
{
	if (Q.front == Q.rear)
		return ERROR;
	p = Q.data[Q.front];
	Q.front = (Q.front + 1) % MAXSIZE;
	return OK;
}

bool isEmpty(SqQueue Q)
{
	if (Q.front == Q.rear)
		return true;
	else
		return false;
}

bool visited[MAXSIZE];
SqQueue Q;
void BFS(MGraph G, int x)
{
	Visit(G, x);
	visited[x] = true;
	EnQueue(Q, x);
	while (!isEmpty(Q))
	{
		DeQueue(Q, x);
		for (int w = FirstNerghbor(G, x); w >= 0; w = NextNeighbor(G, x, w))
			if (!visited[w])
			{
				Visit(G, w);
				visited[w] = true;
				EnQueue(Q, w);
			}
	}
}
void BFSTraverse(MGraph G)
{
	initSqQueue(Q);
	for (int i = 0; i < G.Vexnum; i++)
		visited[i] = false;
	for (int i = 0; i < G.Vexnum; i++)
		if (!visited[i])
			BFS(G, i);
	cout << endl;
}
void DFS(MGraph G, int x)
{
	Visit(G, x);
	visited[x] = true;
	EnQueue(Q, x);
	while (!isEmpty(Q))
	{
		DeQueue(Q, x);
		for (int w = FirstNerghbor(G, x); w >= 0; w = NextNeighbor(G, x, w))
			if (!visited[w])
				DFS(G, w);
	}
}
void DFSTraverse(MGraph G)
{
	initSqQueue(Q);
	for (int i = 0; i < G.Vexnum; i++)
		visited[i] = false;
	for (int i = 0; i < G.Vexnum; i++)
		if (!visited[i])
			DFS(G, i);
	cout << endl;
}
void Visit(MGraph G, int x)
{
	cout << G.Vex[x]<<" ";
}
void ArrayVisit(MGraph G)
{
	cout << G.Vexnum << " " << G.Arcnum / 2 << endl;
	cout << "  ";
	for (int i = 0; i < G.Vexnum; i++)
		cout << G.Vex[i] << " ";
	cout << endl;
	for (int i = 0; i < G.Vexnum; i++)
	{
		cout << G.Vex[i] << " ";
		for (int j = 0; j < G.Vexnum; j++)
			cout << G.Edge[i][j] << " ";
		cout << endl;
	}
}
发布了16 篇原创文章 · 获赞 25 · 访问量 1144

猜你喜欢

转载自blog.csdn.net/weixin_41546300/article/details/102981565