/
matrix2pairs.cpp
90 lines (81 loc) · 2.18 KB
/
matrix2pairs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
################################################################################
# matrix2pairs
################################################################################
#
# Convert an adjacency matrix graph to an edge list graph
#
# Copyright 2014 Chris Whidden
# cwhidden@fhcrc.org
# May 2, 2014
# Version 1.0
#
# This file is part of sprspace.
#
# sprspace is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# sprspace is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with sprspace. If not, see <http://www.gnu.org/licenses/>.
################################################################################
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;
#define IN_DELIM ','
#define OUT_DELIM '\t'
int main(int argc, char **argv) {
int k = 1;//INT_MAX;
if (argc > 1) {
k = atoi(argv[1]);
}
vector<vector<int> > m = vector<vector<int> >();
string line = "";
int size = 0;
while(getline(cin, line)) {
string token = "";
m.push_back(vector<int>());
for(int i = 0; i < line.size(); i++) {
if (line[i] == IN_DELIM) {
int num = -1;
if (token != "") {
num = atoi(token.c_str());
}
m[size].push_back(num);
token = "";
}
else {
token.push_back(line[i]);
}
}
int num = -1;
if (token != "") {
num = atoi(token.c_str());
}
m[size].push_back(num);
size++;
}
vector<bool> isolated = vector<bool>(m.size(), true);
cout << "source" << OUT_DELIM << "target" << OUT_DELIM << "distance" << endl;
for(int i = 0; i < m.size(); i++) {
for(int j = i+1; j < m.size(); j++) {
if (m[i][j] > 0 && m[i][j] <= k) {
isolated[i] = false;
isolated[j] = false;
cout << i << OUT_DELIM << j << OUT_DELIM << m[i][j] << endl;
}
}
if (isolated[i]) {
cout << i << endl;
}
}
}