《数据结构与算法》第六次 图及图的遍历(上)

《数据结构与算法那》第六次课实验内容

图及图的遍历(上)

实验目的:

  1. 熟悉图的两种存储结构:邻接矩阵和邻接链表。
  2. 掌握在图的邻接表存储结构上的遍历算法的实现。

实验内容:

  1. 开发c++类adjacencyGraph,用邻接矩阵描述一个无向图,要求可以手动输入图;可删除一条边;输出邻接矩阵等成员函数,并编写测试函数。
  2. 开发c++类linkedGraph,用邻接链表描述一个无向图,要求可以手动输入图;可删除一条边;输出邻接链表等成员函数,并编写测试函数。
  3. 两个存储方式任选其一完成。

参考提示:

可以参考《《数据结构与算法》第六次 图及图的遍历(上)》《《数据结构与算法》第六次 图及图的遍历(上)》中相关成员的实现方法,完成本次实验内容。

《《数据结构与算法》第六次 图及图的遍历(上)》

以上图中的无向图为例,建立邻接矩阵并输出矩阵,然后删除一条边再输出矩阵,程序执行结果如下图。

《《数据结构与算法》第六次 图及图的遍历(上)》

实验代码:

江米条想说的:

看图,上面是   无向图    的邻接链表,邻接矩阵

           下面是   有向图    的邻接链表,邻接矩阵

《《数据结构与算法》第六次 图及图的遍历(上)》

 

前面已经用过链表的插入,产出等等的啦,今天咱们用矩阵操作一下。

图和矩阵的关系看这个:(图摘自《算法:C语言实现》

 

《《数据结构与算法》第六次 图及图的遍历(上)》

头文件:

#pragma once
#include<iostream>
using namespace std;

class adjacencyGraph
{
public:
	adjacencyGraph(int n, int e);//构造函数
	void setElement(int n, int value);//设置元素
	int getElement(int value);//取到元素
	void insertEdge(int vertex1, int vertex2);//插入边,参数是边的两个点
	void eraseEdge(int vertex1, int vertex2);//擦出边
	void outputGraph();//输出
private:
	int **matrix;//图得用到二维矩阵
	int *element;
	int numberOfVertices;//顶点数量
	int numberOfEdges;//边数量
};
adjacencyGraph::adjacencyGraph(int n, int e)
{
	numberOfVertices = n;
	numberOfEdges = e;
	matrix = new int *[numberOfVertices];
	for (int i = 0; i < numberOfVertices; i++)//两个for循环是把二维矩阵置零
		matrix[i] = new int[numberOfVertices];
	for (int i = 0; i < numberOfVertices; i++)
		for (int j = 0; j < numberOfVertices; j++)
			matrix[i][j] = 0;
	element = new int[numberOfVertices];
}
void adjacencyGraph::setElement(int n, int value)//
{
	element[n - 1] = value;//存入顶点的内容,element[0]是第1个顶点的数据,那里用到n-1了,看清楚!
}
int adjacencyGraph::getElement(int value)//输入值,查它是第几个顶点,这里因为后面用到插入数组,所以输出的第几个顶点没+1,如果加了就越界,具体的看下面的主函数的插入时候的两个参数,你就明白了
{
	for (int i = 0; i < numberOfVertices; i++)
		if (element[i] == value)
			return i;
}
void adjacencyGraph::insertEdge(int vertex1, int vertex2)
{
	matrix[vertex1][vertex2] = 1;
	matrix[vertex2][vertex1] = 1;//二维矩阵,插入一个边的话得在矩阵里面置两个1
}
void adjacencyGraph::eraseEdge(int vertex1, int vertex2)
{
	matrix[vertex1][vertex2] = 0;
	matrix[vertex2][vertex1] = 0;//二维矩阵,删除一个边的话得在矩阵里面置两个0
}
void adjacencyGraph::outputGraph()
{
	for (int i = 0; i < numberOfVertices; i++)
	{
		for (int j = 0; j < numberOfVertices; j++)
			cout << matrix[i][j] << " ";
		cout << endl;
	}
}

主函数:

#include<iostream>
#include"adjacencyGraph.h"
using namespace std;
int main()
{
	adjacencyGraph *temp;
	temp = new adjacencyGraph(0, 0);
	while (1)
	{
		cout << "1.用邻接矩阵描述一个无向图" << endl;
		cout << "2.删除无向图的一条边" << endl;
		cout << "0.退出" << endl;
		int option;
		cin >> option;
		switch (option)
		{
		case 1:
		{
			int n, e;
			cout << "请输入顶点的数目:";
			cin >> n;
			cout << "请输入边的数目:";
			cin >> e;
			temp = new adjacencyGraph(n, e);
			for (int i = 1; i <= n; i++)
			{
				int value;
				cout << "请输入第" << i << "个顶点";
				cin >> value;
				temp->setElement(i, value);
			}
			for (int i = 1; i <= n; i++)
			{
				int value1, value2;
				cout << "请输入第" << i << "条边的第1个顶点:";
				cin >> value1;
				cout << "请输入第" << i << "条边的第2个顶点:";
				cin >> value2;
				temp->insertEdge(temp->getElement(value1), temp->getElement(value2));
			}
			temp->outputGraph();
		}
			break;
		case 2:
		{
			int value1, value2;
			cout << "请输入要删除边的第一个顶点:";
			cin >> value1;
			cout << "请输入要删除边的第二个顶点:";
			cin >> value2;
			temp->eraseEdge(temp->getElement(value1), temp->getElement(value2));
			temp->outputGraph();
		}
			break;
		case 0:
			return 0;
			break;
		}
	}
}

因为很简单,就不上代码链接啦!一共就这两个,上面的是.h头文件,下面的是主函数。

完全ODK!

今天无限火力最后一天,明天再更新别的!

共勉!

 

    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/LZJSTUDY/article/details/81169219
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞