使用C++实现有向图的邻接矩阵,以及可达矩阵的计算算法。 请完成Project05.cpp中DirectedGraph类的成员函数,具体要求如下: DirectedGraph类: 用来表示一个有向图。 成员变量: m_AdjMat:邻接矩阵 m_ReachabilityMat:可达矩阵 成员函数: DirectedGraph():默认构造函数,构造一个空图。 ~DirectedGraph():析构函数 DirectedGraph(string filepath):解析文件filepath,构造一个DirectedGraph对象。 filepath文件格式与项目四相同,但本项目的图为有向图。 DirectedGraph(const Graph & graph):复制构造函数 operator=(const Graph & graph):赋值运算符 ClearGraph():清空图的邻接矩阵和可达矩阵。 OutputGraph():输出图的邻接矩阵 operator*(const DirectedGraph & graph): 乘法运算符,用于实现可达矩阵运算中的矩阵逻辑乘 DirectedGraph Pow(int power):邻接矩阵的整数次幂。 用法如下: DirectedGraph a; a = a.Pow(5); 即a的5次幂,相当于a = a * a * a * a * a; 注意要考虑0次幂的情况。 该函数适合以递归实现。 DirectedGraph MatOr(DirectedGraph graph):矩阵逐元素的逻辑或运算。 例如: 1 0 0 1 与 0 1 1 0 运算后的结果为 1 1 1 1 void CalcReachabilityMat():计算可达矩阵,要求该函数基于*运算符和Pow函数实现 void OutputReachabilityMat():输出可达矩阵 IsConnected(int src, int dst):基于可达矩阵判断从节点src与dst之间是否存在通路,如存在返回真,否则返回假
时间: 2024-02-17 19:02:27 浏览: 126
以下是Project05.cpp的完整代码实现,包括DirectedGraph类的定义和成员函数的实现:
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
const int MAXN = 100; // 最大节点数
class DirectedGraph {
public:
DirectedGraph();
DirectedGraph(string filepath);
DirectedGraph(const DirectedGraph & graph);
~DirectedGraph();
DirectedGraph & operator=(const DirectedGraph & graph);
void ClearGraph();
void OutputGraph();
DirectedGraph operator*(const DirectedGraph & graph);
DirectedGraph Pow(int power);
DirectedGraph MatOr(DirectedGraph graph);
void CalcReachabilityMat();
void OutputReachabilityMat();
bool IsConnected(int src, int dst);
private:
int n; // 节点数
int m_AdjMat[MAXN][MAXN]; // 邻接矩阵
int m_ReachabilityMat[MAXN][MAXN]; // 可达矩阵
void ReadGraphFromFile(string filepath); // 从文件中读取图
};
DirectedGraph::DirectedGraph() {
n = 0;
memset(m_AdjMat, 0, sizeof(m_AdjMat));
memset(m_ReachabilityMat, 0, sizeof(m_ReachabilityMat));
}
DirectedGraph::DirectedGraph(string filepath) {
n = 0;
memset(m_AdjMat, 0, sizeof(m_AdjMat));
memset(m_ReachabilityMat, 0, sizeof(m_ReachabilityMat));
ReadGraphFromFile(filepath);
}
DirectedGraph::DirectedGraph(const DirectedGraph & graph) {
n = graph.n;
memcpy(m_AdjMat, graph.m_AdjMat, sizeof(m_AdjMat));
memcpy(m_ReachabilityMat, graph.m_ReachabilityMat, sizeof(m_ReachabilityMat));
}
DirectedGraph::~DirectedGraph() {}
DirectedGraph & DirectedGraph::operator=(const DirectedGraph & graph) {
if (this == &graph) {
return *this;
}
n = graph.n;
memcpy(m_AdjMat, graph.m_AdjMat, sizeof(m_AdjMat));
memcpy(m_ReachabilityMat, graph.m_ReachabilityMat, sizeof(m_ReachabilityMat));
return *this;
}
void DirectedGraph::ClearGraph() {
n = 0;
memset(m_AdjMat, 0, sizeof(m_AdjMat));
memset(m_ReachabilityMat, 0, sizeof(m_ReachabilityMat));
}
void DirectedGraph::OutputGraph() {
cout << "邻接矩阵:" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << m_AdjMat[i][j] << " ";
}
cout << endl;
}
}
DirectedGraph DirectedGraph::operator*(const DirectedGraph & graph) {
DirectedGraph ret;
ret.n = n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
ret.m_ReachabilityMat[i][j] = ret.m_ReachabilityMat[i][j] || (m_ReachabilityMat[i][k] && graph.m_ReachabilityMat[k][j]);
}
}
}
return ret;
}
DirectedGraph DirectedGraph::Pow(int power) {
DirectedGraph ret;
ret.n = n;
if (power == 0) {
for (int i = 0; i < n; i++) {
ret.m_ReachabilityMat[i][i] = 1;
}
} else if (power == 1) {
memcpy(ret.m_ReachabilityMat, m_ReachabilityMat, sizeof(m_ReachabilityMat));
} else if (power % 2 == 0) {
DirectedGraph half = Pow(power / 2);
ret = half * half;
} else {
DirectedGraph half = Pow(power / 2);
ret = half * half * (*this);
}
return ret;
}
DirectedGraph DirectedGraph::MatOr(DirectedGraph graph) {
DirectedGraph ret;
ret.n = n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ret.m_ReachabilityMat[i][j] = m_ReachabilityMat[i][j] || graph.m_ReachabilityMat[i][j];
}
}
return ret;
}
void DirectedGraph::CalcReachabilityMat() {
DirectedGraph ret;
ret.n = n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ret.m_ReachabilityMat[i][j] = m_AdjMat[i][j];
}
}
for (int i = 1; i < n; i++) {
ret = ret + Pow(i);
}
memcpy(m_ReachabilityMat, ret.m_ReachabilityMat, sizeof(m_ReachabilityMat));
}
void DirectedGraph::OutputReachabilityMat() {
cout << "可达矩阵:" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << m_ReachabilityMat[i][j] << " ";
}
cout << endl;
}
}
bool DirectedGraph::IsConnected(int src, int dst) {
return m_ReachabilityMat[src][dst];
}
void DirectedGraph::ReadGraphFromFile(string filepath) {
ifstream fin(filepath);
if (!fin) {
cout << "文件不存在!" << endl;
return;
}
fin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
fin >> m_AdjMat[i][j];
}
}
fin.close();
}
int main() {
DirectedGraph graph("input.txt");
graph.OutputGraph();
graph.CalcReachabilityMat();
graph.OutputReachabilityMat();
cout << "0和3是否连通:" << graph.IsConnected(0, 3) << endl;
cout << "1和2是否连通:" << graph.IsConnected(1, 2) << endl;
return 0;
}
```
在该代码中,我们定义了DirectedGraph类,实现了题目中要求的各种成员函数。我们首先在main函数中创建一个DirectedGraph对象,并读取输入文件中的有向图。然后,我们调用OutputGraph函数输出邻接矩阵。接着,我们调用CalcReachabilityMat函数计算可达矩阵,并使用OutputReachabilityMat函数输出可达矩阵。最后,我们使用IsConnected函数判断一些节点之间是否连通。
阅读全文