我一直在尝试解决图形问题&老师给了我这个代码。我不知道如何运行它。 我的代码是:
#include <bits/stdc++.h>
using namespace std;
#define White 0
#define Gray 2
#define Black 3
int row, col;
int **Graph;
class Graph
{
private:
bool** adjacencyMatrix;
int vertexCount;
public:
Graph(int vertexCount)
{
this->vertexCount = vertexCount;
adjacencyMatrix = new bool*[vertexCount];
for (int i = 0; i < vertexCount; i++)
{
adjacencyMatrix[i] = new bool[vertexCount];
for (int j = 0; j < vertexCount; j++)
adjacencyMatrix[i][j] = false;
}
}
Graph(char filename[], int vertexCount)
{
this->vertexCount = vertexCount;
adjacencyMatrix = new bool*[vertexCount];
ifstream file;
file.open(filename, ios::in);
if( !file)
{
cout << "\nError: Cannot open file\n";
return;
}
if(file.is_open())
{
for (int i = 0; i < vertexCount; i++)
{
adjacencyMatrix[i] = new bool[vertexCount];
for (int j = 0; j < vertexCount; j++)
file>>adjacencyMatrix[i][j];
}
}
}
~Graph()
{
for (int i = 0; i < vertexCount; i++)
delete[] adjacencyMatrix[i];
delete[] adjacencyMatrix;
}
void runDFS(int u, int state[])
{
state[u] = Gray;
for (int v = 0; v < vertexCount; v++)
if (isEdge(u, v) && state[v] == White)
runDFS(v, state);
state[u] = Black;
cout<<u<<",";
}
void DFS()
{
int *state = new int[vertexCount];
for (int i = 0; i < vertexCount; i++)
state[i] = White;
runDFS(0, state);
delete [] state;
}
void display()
{
int u,v;
for(u=0; u<vertexCount; ++u)
{
cout << "\nadj[" << (char) (u+65) << "] -> ";
for(v=0; v<vertexCount; ++v)
{
cout << " " << adjacencyMatrix[u][v];
}
}
cout << "\n\n";
}
bool isEdge(int i, int j)
{
if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
return adjacencyMatrix[i][j];
else
return false;
}
void removeEdge(int i, int j)
{
if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
{
adjacencyMatrix[i][j] = false;
adjacencyMatrix[j][i] = false;
}
}
void addEdge(int i, int j)
{
if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
{
adjacencyMatrix[i][j] = true;
adjacencyMatrix[j][i] = true;
}
}
};
int main()
{
Graph = new int*[row];
for (int i = 0; i < row; i++)
{
Graph[i] = new int[col];
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
Graph[i][j] = 0;
}
}
Graph A = new Graph("Hello.txt", 2); //This line is showing the error
}
但它显示错误:
D:\Fayaz\Undirected graph.cpp|157|error: expected ';' before 'A'|
有谁能告诉我如何创建Graph类的对象并使用它的函数/方法? 提前谢谢......
答案 0 :(得分:0)
以下是代码的伪更正版本:
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
enum class BWColor
{
White,
Gray,
Black
};
class Graph
{
private:
std::map<int, std::vector<int>> graph;
int vertexCount;
private:
void dfs(std::vector<BWColor>& visit, int u)
{
std::cout << u << " ";
visit[u] = BWColor::Gray;
for(std::vector<int>::iterator it = graph[u].begin(); it != graph[u].end(); ++it)
{
if(visit[*it] == BWColor::White)
{
dfs(visit, *it);
}
}
visit[u] = BWColor::Black;
}
public:
Graph(int vertexCount) : vertexCount(vertexCount) {}
Graph(const char* filename, int vertexCount) : Graph(vertexCount)
{
std::ifstream file;
file.open(filename, std::ios::in);
if(!file)
{
std::cerr << "Error: Cannot open file\n";
return;
}
if(file.is_open())
{
int k = 0;
int u = -1;
while(file >> k)
{
++u;
if(k == 0)
{
continue;
}
graph[u / vertexCount].push_back(u % vertexCount);
}
}
}
~Graph() {}
void runDFS(int u)
{
std::vector<BWColor> visit(vertexCount, BWColor::White);
dfs(visit, u);
}
friend std::ostream& operator<<(std::ostream& stream, const Graph& g);
};
std::ostream& operator<<(std::ostream& stream, const Graph& g)
{
for(std::map<int, std::vector<int>>::const_iterator it = g.graph.cbegin(); it != g.graph.cend(); ++it)
{
for(int i = 0; i < it->second.size(); ++i)
{
stream << '(' << it->first << ", " << it->second[i] << ")\n";
}
}
return stream;
}
int main()
{
Graph gA("graph.txt", 4);
std::cout << gA << "DFS: {";
gA.runDFS(2);
std::cout << "}\n";
return 0;
}
以下是一些建议:
我正在使用第二个构造函数Graph(const char* filename, int vertexCount)
加载以下邻接矩阵:
0 1 0 0 0 0 1 0 1 0 0 1 0 0 1 1
然后,我将邻接矩阵转换为由uv
私有成员表示的弧std::map<int, std::vector<int>> graph;
的集合。
最后,我进入DFS。