我想通过从 csv 读取给定的邻接矩阵来创建它的无向加权图。我可以从 csv 读取它,但我不知道如何在图中绘制它。这是读取文件的代码。
int main(){
ifstream ip("map.csv");
if(!ip.is_open()) std::cout << "ERROR: File Open" << '\n';
string first;
string weight;
while(ip.good()){
getline(ip,first);
getline(ip,weight);
std::cout << "First: "<<first <<'\n';
std::cout << "Weight: "<<weight<< '\n';
std::cout << "-------------------" << '\n';
}
ip.close();
}
这是在图中实现它的代码。
class Graph {
private:
bool** adjacencyMatrix;
int vertexCount;
public:
Graph(int vertexCount) {
ip->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;
}
}
void addEdge(int i, int j) {
if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) {
adjacencyMatrix[i][j] = true;
adjacencyMatrix[j][i] = true;
}
}
void removeEdge(int i, int j) {
if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) {
adjacencyMatrix[i][j] = false;
adjacencyMatrix[j][i] = false;
}
}
bool isEdge(int i, int j) {
if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
return adjacencyMatrix[i][j];
else
return false;
}
~Graph() {
for (int i = 0; i < vertexCount; i++)
delete[] adjacencyMatrix[i];
delete[] adjacencyMatrix;
}
};
这就是我的文件的样子。
C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11
C1,-1,20,-1,40,-1,-1,-1,-1,-1,-1,-1
C2,20,-1,-1,-1,80,-1,-1,-1,-1,-1,-1
C3,-1,-1,-1,-1,60,-1,-1,-1,-1,-1,-1
C4,40,-1,-1,-1,-1,200,-1,-1,-1,-1,-1
C5,-1,80,60,-1,-1,-1,100,-1,43,-1,-1
C6,-1,-1,-1,200,-1,-1,33,-1,-1,-1,-1
C7,-1,-1,-1,-1,100,33,-1,-1,-1,-1,-1
C8,-1,-1,-1,-1,-1,-1,-1,-1,4,-1,55
C9,-1,-1,-1,-1,43,-1,-1,4,-1,-1,-1
C10,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,32
C11,-1,-1,-1,-1,-1,-1,-1,55,-1,32,-1
我不知道如何使用读取文件的输出来实现图形。