-1

我想通过从 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

我不知道如何使用读取文件的输出来实现图形。

4

1 回答 1

1

这是我的建议。

首先,我决定应用@Scheff 修改(或多或少),使用 avector作为vector邻接矩阵。我认为使代码比使用row * vertexCount + column类型访问更干净。

图类

#include <unordered_map>
#include <vector>
#include <string>

class Graph {
private:
    typedef std::vector < std::vector<int> > tAdjacencyMatrix;
private:

    tAdjacencyMatrix adj;

    int vertexCount;

public:

    Graph(int vertexCount) {
        this->vertexCount = vertexCount;
        for (int i = 0; i < vertexCount; i++) {
            std::vector<int> v;
            for (int j = 0; j < vertexCount; j++)
                v.push_back(0);

            adj.push_back(v);
        }
    }   

    void addEdge(int i, int j, int value) {
        if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) {
            adj[i][j] = value;
            adj[j][i] = value;
        }
    }   

    void removeEdge(int i, int j) {
        if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) {
            adj[i][j] = 0;
            adj[j][i] = 0;
        }
    }   

    bool isEdge(int i, int j) const {
        if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
            return adj[i][j];   // 0 interpreted as false
    }

    std::string toString() const{
        std::string ret = "";
        for (int i = 0; i < vertexCount; i++) {
            ret += "V1: ";
            for (int j = 0; j < vertexCount; j++){
                ret += std::to_string(adj[i][j]);
                if (j != vertexCount - 1) ret += ", ";
            }
            ret += "\n";
        }
        return ret;
    }
};

我添加了一种toString方法来探测它是否正常工作。

注意:您可以使用adj.size()代替vertexCount.

有了这个,我的主要外观如下:

主要的

int main(){

    ifstream ip("map.csv");

    if (!ip.is_open()) std::cout << "ERROR: File Open" << '\n';
    string line;
    getline(ip, line);
    vector<string> tokens = splitString(line, ',');
    int vertexCount = tokens.size();

    Graph g(vertexCount);
    int v = 1;
    while (ip.good()){
        getline(ip, line);
        tokens = splitString(line, ',');
        for (int i = 1; i < tokens.size(); ++i){    // Note that starts in 1, avoiding "Cx" first element
            if (tokens[i].compare("-1") != 0){
                g.addEdge(v, i, atoi(tokens[i].c_str()));
            }
        }
        ++v;
    }

    ip.close();

    cout << g.toString() << endl;
    return 0;
}

拆分字符串函数:

拆分字符串

std::vector<string> splitString(const string &s, char delimiter){
    istringstream iis(s);

    vector<string> tokens;
    string aux = "";
    char c;

    while (iis.good()){
        c = iis.get();
        if (c == delimiter){
            tokens.push_back(aux);
            aux.clear();
        }
        else{
            aux.insert(aux.end(), c);
        }
    }
    tokens.push_back(aux); // Insert the last one (it has no ',' at the end)

    return tokens;
}

注2:拆分功能肯定会更好,但我认为这个更容易理解

于 2017-12-04T12:43:16.693 回答