Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions graphtool/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,3 +866,41 @@ bool Graph::isForest() {
isForestFlag = true;
return true;
}


Graph *Graph::loadAdjacencyFile(string file) {
string line;
ifstream myfile(file);

vector<vector<string>> result;
if (myfile.is_open()) {
while (getline(myfile, line)) {

vector<std::string> cols;
boost::split(cols, line, boost::is_any_of(","));
result.push_back(cols);
}
myfile.close();


// check if the first column is a name
bool isFirstColName = result.at(0).size() != result.size();

//Load into vector
vector<vector<int>> adjacencyMatrix = getZeroizedMatrix((int) result.size(), (int) result.size());
for (unsigned long rIndex = 0; rIndex < result.size(); rIndex++) {
// add values to matrix
for (unsigned long cIndex = 0; cIndex < result.size(); cIndex++) {
unsigned long tmpCIndex = isFirstColName ? cIndex + 1 : cIndex;
try {
adjacencyMatrix.at(rIndex).at(cIndex) = stoi(result.at(rIndex)[tmpCIndex]);
} catch (const invalid_argument &e) {
throw invalid_argument("matrix entries must be and contain at least one integer.");
}
}
}
return new Graph(adjacencyMatrix);
} else {
return nullptr;
}
}
10 changes: 8 additions & 2 deletions graphtool/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class Graph {
*
* @return a zeroized matrix
*/
vector<vector<int>> getZeroizedMatrix(int rows, int cols);
static vector<vector<int>> getZeroizedMatrix(int rows, int cols);

/**
* Checks whether matrix A (m x n) and B (m x n) are identical.
Expand All @@ -181,9 +181,9 @@ class Graph {
const vector<vector<int>> &getAdjacencyMatrix() const;



public:
Graph(vector<vector<int>> adjacencyMatrix);

Graph(vector<vector<int>> adjacencyMatrix, vector<string> node_names);

/**
Expand Down Expand Up @@ -362,4 +362,10 @@ class Graph {
*/
string exportDot(vector<int> path);

/**
* Load the Graph from a file within an adjacency matrix.
* @param file Path to the file
* @return New Graph object
*/
static Graph *loadAdjacencyFile(string file);
};
8 changes: 7 additions & 1 deletion gui/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ int main(int argc, char **argv) {
*/

// Get input from csv file
// TODO @ForrestFalcon issue #10, vgl. Implementierung inputMatlab
vector<string> inputCSV = getValues("-i", allArgs);
for (int i = 0; i < inputCSV.size(); i++) {
Graph *g = Graph::loadAdjacencyFile(inputCSV[i]);
printv("Graph %d (CSV-Datei) \"%s\" hinzugefügt.", graphs.size(), inputCSV[i].c_str());
graphs.push_back(g);
}
printv("%d CSV-Dateien gefunden.", inputCSV.size());

// Get input from matlab string
vector<string> inputMatlab = getValues("-iml", allArgs);
Expand Down
61 changes: 61 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "gtest/gtest.h"
#include <string>
#include <boost/algorithm/string.hpp>
#include <fstream>
#include "Graph.h"

TEST(graph_check, test_directed) {
Expand Down Expand Up @@ -453,4 +454,64 @@ TEST(graph_check, test_forest_undirected) {
bool forest = g->isForest();

ASSERT_FALSE(forest);
}
TEST(graph_check, test_adjazenzmatrix_load_file) {
ofstream myfile ("tmp.txt");
if (myfile.is_open()) {
myfile << "1,0,1\n";
myfile << "1,1,1\n";
myfile << "0,0,1\n";
myfile.close();
} else FAIL();

Graph *g = Graph::loadAdjacencyFile("tmp.txt");
string mat = g->getAdjacencyMatrixString();
remove("tmp.txt");

std::vector<std::string> strs;
boost::split(strs, mat, boost::is_any_of("\n"));

ASSERT_EQ(strs[0], "1,0,1");
ASSERT_EQ(strs[1], "1,1,1");
ASSERT_EQ(strs[2], "0,0,1");
}
TEST(graph_check, test_adjazenzmatrix_load_file_space) {
ofstream myfile ("tmp.txt");
if (myfile.is_open()) {
myfile << "1 ,0,1\n";
myfile << "1,1 ,1\n";
myfile << "0,0, 1\n";
myfile.close();
} else FAIL();

Graph *g = Graph::loadAdjacencyFile("tmp.txt");
string mat = g->getAdjacencyMatrixString();
remove("tmp.txt");

std::vector<std::string> strs;
boost::split(strs, mat, boost::is_any_of("\n"));

ASSERT_EQ(strs[0], "1,0,1");
ASSERT_EQ(strs[1], "1,1,1");
ASSERT_EQ(strs[2], "0,0,1");
}
TEST(graph_check, test_adjazenzmatrix_load_file_graph_name) {
ofstream myfile ("tmp.txt");
if (myfile.is_open()) {
myfile << "Demo,1,0,1\n";
myfile << "Test,1,1,1\n";
myfile << "42,0,0,1\n";
myfile.close();
} else FAIL();

Graph *g = Graph::loadAdjacencyFile("tmp.txt");
string mat = g->getAdjacencyMatrixString();
remove("tmp.txt");

std::vector<std::string> strs;
boost::split(strs, mat, boost::is_any_of("\n"));

ASSERT_EQ(strs[0], "1,0,1");
ASSERT_EQ(strs[1], "1,1,1");
ASSERT_EQ(strs[2], "0,0,1");
}