Adjacency Matrix -- c++实现

3 篇文章 0 订阅
1 篇文章 0 订阅

1. Adjacency Matrix (Undirected Graph)

i. Header (Graph.h)

#ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED

class Graph
{
private:
	bool** adjMat;			// implement the matrix by a 2-D bool array
	int countV;			// number of vertices in the graph
public:
	Graph(int);
	~Graph();
	void addEdge(int, int);
	void removeEdge(int, int);
	bool isConnected(int, int);	// check if two vertices are connected
	void showConnected(int);	// show all connected vertices of one vertex
};

#endif // GRAPH_H_INCLUDED

ii. Implementation (Graph.cpp)

#include"Graph.h"
#include<iostream>
using namespace std;

Graph::Graph(int N)
{
	countV = N;
	/* initialize the adjacency matrix */
	adjMat = new bool *[countV];
	for (int i = 0; i < countV; ++i){
		adjMat[i] = new bool[countV];
		for (int j = 0; j < countV; ++j)
			adjMat[i][j] = false;
	}
}

Graph::~Graph()
{
	for (int i = 0; i < countV; ++i)
		delete[] adjMat[i];
	delete[] adjMat;
}

void Graph::addEdge(int i, int j)
{
	/* since the graph is undirected, set both a[i][j]
	and a[j][i] to 1 while adding edge between i and j */
	if (i >= 0 && i < countV && j >= 0 && j < countV){
		adjMat[i][j] = true;
		adjMat[j][i] = true;
	}
}

void Graph::removeEdge(int i, int j)
{
	/* as in addEdge(), set both a[i][j] and a[j][i]
	to 0 while removing edge between i and j*/
	if (i >= 0 && i < countV && j >= 0 && j < countV){
		adjMat[i][j] = false;
		adjMat[j][i] = false;
	}
}

bool Graph::isConnected(int i, int j)
{
	/* since the graph is undirected, we only need to
	check either a[i][j] or a[j][i], here check a[i][j]*/
	if (i >= 0 && i < countV && j >= 0 && j < countV)
		return adjMat[i][j];
	else return false;
}

void Graph::showConnected(int i)
{
	cout << "The connection of vertex " << i << ":" << endl;
	cout << i;
	for (int j = 0; j < countV; ++j){
		if (adjMat[i][j])
			cout << " -> " << j;
	}
	cout << endl;
}

iii. Client (main.cpp)

#include<iostream>
#include"Graph.h"
using namespace std;

int main(int argc, char* argv[])
{
	int V = atoi(argv[1]);
	Graph *g = new Graph(V);
	char YN;

	for (int i = 0; i < V; ++i){
		cout << "Enter the connections of vertex " << i << ":" << endl;
		int j;
		while (cin >> j){
			if (j >= 0 && j < V && j != i){
				if (g->isConnected(i, j)){
					cout << "Vertices " << i << " and " << j << " have been connected." << endl;
				}
				else g->addEdge(i, j);
			}
			/* input two same number to quit the loop */
			else if (j == i){
				break;
			}
			else cout << "Out of range." << endl;
		}
	}
	
	for (int i = 0; i < V; ++i)
		g->showConnected(i);

	cout << "Do you wanna remove edges?";
	cin >> YN;
	if (YN == 'Y'){
		int i, j;
		cout << "Enter the edge you want to remove: " << endl;
		while (cin >> i >> j){
			if (i >= 0 && i < V && j >= 0 && j < V && j != i){
				if (!g->isConnected(i, j)){
					cout << "Vertices " << i << " and " << j << " are not connected." << endl;
				}
				else {
					g->removeEdge(i, j);
				}
			}
			else if (j == i){
				break;
			}
			else cout << "Out of range.";
		}
	}
	else if (YN == 'N')
		cout << "No edges to remove." << endl;

	for (int i = 0; i < V; ++i)
		g->showConnected(i);

	return 0;
}

执行:

Enter the connections of vertex 0:
1 4 0
Enter the connections of vertex 1:
0 2 3 4 1
Vertices 1 and 0 have been connected.
Enter the connections of vertex 2:
1 3 2
Vertices 2 and 1 have been connected.
Enter the connections of vertex 3:
1 2 4 3
Vertices 3 and 1 have been connected.
Vertices 3 and 2 have been connected.
Enter the connections of vertex 4:
0 1 3 4
Vertices 4 and 0 have been connected.
Vertices 4 and 1 have been connected.
Vertices 4 and 3 have been connected.
The connection of vertex 0:
0 -> 1 -> 4
The connection of vertex 1:
1 -> 0 -> 2 -> 3 -> 4
The connection of vertex 2:
2 -> 1 -> 3
The connection of vertex 3:
3 -> 1 -> 2 -> 4
The connection of vertex 4:
4 -> 0 -> 1 -> 3
Do you wanna remove edges?Y
Enter the edge you want to remove:
1 4
0 0
The connection of vertex 0:
0 -> 1 -> 4
The connection of vertex 1:
1 -> 0 -> 2 -> 3
The connection of vertex 2:
2 -> 1 -> 3
The connection of vertex 3:
3 -> 1 -> 2 -> 4
The connection of vertex 4:
4 -> 0 -> 3


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值