c++实现邻接矩阵

#include<iostream>
using namespace std;

const int MAX = 10; //最大顶点数
//typedef char vertex_type; //顶点类型
//typedef int edge_weight_type; //边上权值类型

template<class vertex_type, class edge_weight_type>
class Graph
{
private:
	int vertex_num; //顶点数
	int edge_num; //边数
	vertex_type vex[MAX]; //存放顶点的数组
	edge_weight_type arc[MAX][MAX]; //邻接矩阵

public:
	Graph(); //构造函数
	//~Graph(); //析构函数
	int locate(char ch); //定位
	void print_graph(); //打印
};


// 定位
template<class vertex_type, class edge_weight_type>
int Graph<vertex_type, edge_weight_type>::locate(char ch)
{
	int i = 0;
	for (i = 0; i < this->vertex_num; i++)
	{
		if (this->vex[i] == ch)
		{
			break;
		}
	}
	return i;
}


//构造函数
template<typename vertex_type, typename edge_weight_type>
Graph<typename vertex_type, typename edge_weight_type>::Graph()
{
	int i = 0, j = 0, k = 0, weight = 0;
	cout << "输入顶点数和边数:" << endl;
	cin >> this->vertex_num >> this->edge_num;

	//存储顶点
	cout << "请输入顶点:" << endl;
	for (i = 0; i < this->vertex_num; i++)
	{
		cin >> this->vex[i];
	}

	//初始化邻接矩阵
	for (i = 0; i < this->vertex_num; i++)
	{
		for (j = 0; j < this->vertex_num; j++)
		{
			this->arc[i][j] = 0;
		}
	}

	cout << "输入两个顶点及其权值:" << endl;
	for (k = 0; k < this->edge_num; k++)
	{
		char first, second;
		cin >> first >> second >> weight;
		i = this->locate(first);
		j = this->locate(second);
		
		this->arc[i][j] = weight;
		this->arc[j][i] = weight;
	}
}

//打印
template<typename vertex_type, typename edge_weight_type>
void Graph<typename vertex_type, typename edge_weight_type>::print_graph()
{
	cout << "邻接矩阵为:" << endl;
	for (int i = 0; i < this->vertex_num; i++)
	{
		for (int j = 0; j < this->vertex_num; j++)
		{
			cout << "\t" << this->arc[i][j];
		}
		cout << endl;
	}
}

int main()
{
	Graph<char, int> g;
	g.print_graph();
	system("pause");
 	return 0;
}
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是C++实现邻接矩阵上的深度优先搜索(DFS)的代码。 在这个实现中,我们假设输入的无向图用邻接矩阵表示,其中graph[i][j]表示第i个节点到第j个节点是否有边相连(1表示有,0表示没有)。 ```cpp #include <iostream> using namespace std; void DFS(int graph[][100], int n, bool visited[], int start) { // 标记起始节点为已访问 visited[start] = true; cout << start << " "; // 递归遍历与起始节点相连的未访问节点 for (int i = 0; i < n; i++) { if (graph[start][i] == 1 && !visited[i]) { DFS(graph, n, visited, i); } } } int main() { int n; // 节点数 cin >> n; // 初始化邻接矩阵 int graph[100][100]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> graph[i][j]; } } // 初始化visited数组 bool visited[n]; for (int i = 0; i < n; i++) { visited[i] = false; } // 从第0个节点开始进行DFS DFS(graph, n, visited, 0); return 0; } ``` 在这个实现中,我们使用一个数组`visited`来标记每个节点是否已经被访问过。从起始节点开始进行DFS时,将其标记为已访问,并输出节点编号。然后递归遍历所有与起始节点相连的未访问节点,并将其标记为已访问。 您可以输入一个类似下面的邻接矩阵来测试这个程序: ``` 5 0 1 1 0 0 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 0 1 1 0 ``` 这个邻接矩阵表示一个有5个节点的无向图,其中节点0与节点1、2相连,节点1与节点0、2、3相连,节点2与节点0、1、3、4相连,节点3与节点1、2、4相连,节点4与节点2、3相连。程序的输出应该是: ``` 0 1 2 3 4 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值