从csv

时间:2017-11-30 09:20:35

标签: c++ csv graph adjacency-matrix undirected-graph

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

我不知道如何使用读取文件的输出来实现图形。帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

这是我的建议。

首先,我决定应用@Scheff修改(或多或少),vector 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;
}

拆分字符串功能:

SplitString

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;
}

Note2 :分割功能肯定会更好,但我觉得这个更容易理解