从csv中的邻接矩阵创建无向加权图 [英] creating undirected weighted graph from adjancency matrix from a csv

查看:233
本文介绍了从csv中的邻接矩阵创建无向加权图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过从csv中读取给定的邻接矩阵来创建无向加权图。我可以从csv中读取它,但我不知道如何绘制它的图形。谁能帮忙?这是读取文件的代码。

  int main(){

ifstream ip(map。 CSV);

if(!ip.is_open())std :: cout<< 错误:文件打开<< \\\
;首先是

字符串;
字符串重量;

while(ip.good()){
getline(ip,first);

getline(ip,weight);

std :: cout<< First:<< first<<'\\\
;

std :: cout<< 重量:<<重量< \\\
;
std :: cout<< -------------------<< \\\
;
}

ip.close();

$ / code>

这是用图表实现的代码。

  class图{b 
$ b private:

bool ** adjacencyMatrix;

int vertexCount;
$ b $ public:

图(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
adjacencyMatrix [i] [j] = false;

}

}



void addEdge(int i,int j){
$ b如果(i> = 0&< i< vertexCount& j> 0& j< vertexCount){

adjacencyMatrix [i] [j] = true;

adjacencyMatrix [j] [i] = true;

}

}



void removeEdge(int i,int j){
$ b如果(i> = 0&< i< vertexCount& j> 0& j< vertexCount){

adjacencyMatrix [i] [j] = false;

adjacencyMatrix [j] [i] = false;

}

}



bool isEdge(int i,int j){

如果(i> = 0&< i< vertexCount& j> 0& j< vertexCount)

return adjacencyMatrix [i] [j] ;

else

return false;




$ b $〜(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

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

解决方案

这是我的建议。

首先,我决定使用@Scheff修改(或多或少),使用 vector 向量作为邻接矩阵。我认为使代码更清洁,而不是使用 row * vertexCount + column 类型访问。



Graph class



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

class图{
private:
typedef std :: vector<的std ::矢量< INT> > tAdjacencyMatrix;
private:

tAdjacencyMatrix adj;

int vertexCount;
$ b $ public:

图(int vertexCount){
this-> vertexCount = vertexCount;
for(int i = 0; i< vertexCount; i ++){
std :: vector< int> v;
for(int j = 0; 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;



如果(i> = 0&&< ; 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解释为false
}

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

我添加了一个 toString 方法来探测它可以正常工作。



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



有了这个,我的主要看起来像:

Main



  int main(){

ifstream ip(map.csv );

if(!ip.is_open())std :: cout<< 错误:文件打开<< \\\
;
字符串行;
getline(ip,line);
vector< string> tokens = splitString(line,',');
int vertexCount = tokens.size();

图形g(vertexCount);
int v = 1;
while(ip.good()){
getline(ip,line);
tokens = splitString(line,',');
for(int i = 1; i< tokens.size(); ++ i){//注意从1开始,避免Cx第一个元素
if(tokens [i]。 compare( - 1)!= 0){
g.addEdge(v,i,atoi(tokens [i] .c_str()));
}
}
++ v;
}

ip.close();

cout<< g.toString()<< ENDL;
返回0;
}

分割字符串函数:

SplitString



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

vector< string>令牌;
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); //插入最后一个(它在末尾没有',')

返回标记;
}

:分裂函数肯定会更好,但我认为这一点更容易理解


I want to create undirected weighted graph of a given adjacency matrix by reading it from a csv. I can read it from a csv but I don't know how to draw its in graph. can anyone help? This is code for reading file.

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

This is code for implementing it in graph.

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;

      }

};

This is how my file looks like.

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

I don't know how to use my output from reading file into implementing the graph. Help will be highly appreciated.

解决方案

Here is my proposal.

First, i've decide to apply @Scheff modification (more or less), with a vector of vector as adjacency matrix. I think makes code cleaner than use row * vertexCount + column type access.

Graph class

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

I added a toString method to probe that it works fine.

Note: you can use adj.size() instead of vertexCount.

With this, my main look like:

Main

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

Split string function:

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: splitting function can surely be better, but i think this one is easier to understand

这篇关于从csv中的邻接矩阵创建无向加权图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆