|
|
@@ -0,0 +1,194 @@
|
|
|
+// =====================================================================================
|
|
|
+//
|
|
|
+// Filename: dimcas2txt.cpp
|
|
|
+//
|
|
|
+// Description: converter
|
|
|
+//
|
|
|
+// Version: 1.0
|
|
|
+// Created: 2017年 12月 26日 星期二 07:53:19 -00
|
|
|
+// Revision: none
|
|
|
+// Compiler: g++
|
|
|
+//
|
|
|
+// Author: Jinkun Lin, jkunlin@gmail.com
|
|
|
+// Organization: School of EECS, Peking University
|
|
|
+//
|
|
|
+// =====================================================================================
|
|
|
+#include <iostream>
|
|
|
+#include <vector>
|
|
|
+#include <fstream>
|
|
|
+#include <algorithm>
|
|
|
+using namespace std;
|
|
|
+
|
|
|
+vector<vector<int>> adjacency_list;
|
|
|
+vector<int> vertex_weight;
|
|
|
+
|
|
|
+void fast_build(string file_name) {/*{{{*/
|
|
|
+ ifstream in_file(file_name);
|
|
|
+ if (!in_file) {
|
|
|
+ cout << "in_file error" << endl;
|
|
|
+ exit(1);
|
|
|
+ }
|
|
|
+ in_file.seekg (0, in_file.end);
|
|
|
+ size_t file_len = in_file.tellg();
|
|
|
+ in_file.seekg (0, in_file.beg);
|
|
|
+ char *data = new char [file_len];
|
|
|
+
|
|
|
+ in_file.read(data, file_len);
|
|
|
+ in_file.close();
|
|
|
+
|
|
|
+
|
|
|
+ //skip comments
|
|
|
+ char *pos = data;
|
|
|
+ while (*pos == '%') {
|
|
|
+ while(*(pos++) != '\n');
|
|
|
+ }
|
|
|
+
|
|
|
+ //read vertex_count
|
|
|
+ long vertex_count = 0, edge_count = 0;
|
|
|
+ while (*pos < '0' || *pos > '9') {
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+ while (*pos != ' ') {
|
|
|
+ vertex_count = vertex_count * 10 + *pos - '0';
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+ //read edge_count
|
|
|
+ while (*pos < '0' || *pos > '9') {
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+ while (*pos >= '0' && *pos <= '9') {
|
|
|
+ edge_count = edge_count * 10 + *pos - '0';
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //read vertex_weight
|
|
|
+ vertex_weight.resize(vertex_count + 1);
|
|
|
+ long v,w;
|
|
|
+ for(vector<vector<long>>::size_type i=1; i<vertex_weight.size(); ++i){
|
|
|
+ v = w = 0;
|
|
|
+ //read vertex
|
|
|
+ while (*pos < '0' || *pos > '9') {
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+ while (*pos != ' ') {
|
|
|
+ v = v * 10 + *pos - '0';
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+ //read weight
|
|
|
+ while (*pos < '0' || *pos > '9') {
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+ while (*pos >= '0' && *pos <= '9') {
|
|
|
+ w = w * 10 + *pos - '0';
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+ vertex_weight[v]=w;
|
|
|
+ }
|
|
|
+
|
|
|
+ //read adjacency_list
|
|
|
+ adjacency_list.resize(vertex_count + 1);
|
|
|
+
|
|
|
+ vector<long> vertex_degree(vertex_count + 1, 0);
|
|
|
+ char *stash_pos = pos;
|
|
|
+ long v1, v2;
|
|
|
+ for (long i = 0; i < edge_count; ++i) {
|
|
|
+ v1 = v2 = 0;
|
|
|
+ //read v1
|
|
|
+ while (*pos < '0' || *pos > '9') {
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+ while (*pos != ' ') {
|
|
|
+ v1 = v1 * 10 + *pos - '0';
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+ //read weight
|
|
|
+ while (*pos < '0' || *pos > '9') {
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+ while (*pos >= '0' && *pos <= '9') {
|
|
|
+ v2 = v2 * 10 + *pos - '0';
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+ vertex_degree[v1]++;
|
|
|
+ vertex_degree[v2]++;
|
|
|
+ }
|
|
|
+ for (size_t v = 1; v < adjacency_list.size(); ++v) {
|
|
|
+ adjacency_list[v].reserve(vertex_degree[v]);
|
|
|
+ }
|
|
|
+ pos = stash_pos;
|
|
|
+ for (long i = 0; i < edge_count; ++i) {
|
|
|
+ v1 = v2 = 0;
|
|
|
+ //read v1
|
|
|
+ while (*pos < '0' || *pos > '9') {
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+ while (*pos != ' ') {
|
|
|
+ v1 = v1 * 10 + *pos - '0';
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+ //read weight
|
|
|
+ while (*pos < '0' || *pos > '9') {
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+ while (*pos >= '0' && *pos <= '9') {
|
|
|
+ v2 = v2 * 10 + *pos - '0';
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+ adjacency_list[v1].push_back(v2);
|
|
|
+ adjacency_list[v2].push_back(v1);
|
|
|
+ }
|
|
|
+ delete[] data;
|
|
|
+}/*}}}*/
|
|
|
+
|
|
|
+void convert(string file_name) {
|
|
|
+ ofstream out_file(file_name);
|
|
|
+ if (!out_file) {
|
|
|
+ std::cout << "out_file error" << std::endl;
|
|
|
+ abort();
|
|
|
+ }
|
|
|
+ out_file << adjacency_list.size() - 1 << endl;
|
|
|
+ for (size_t v = 1; v < adjacency_list.size(); ++v) {
|
|
|
+ out_file << " " << vertex_weight[v];
|
|
|
+ }
|
|
|
+ out_file << std::endl;
|
|
|
+
|
|
|
+ for (size_t v = 1; v < adjacency_list.size(); ++v) {
|
|
|
+ std::sort(adjacency_list[v].begin(), adjacency_list[v].end());
|
|
|
+ }
|
|
|
+ for (auto v : adjacency_list[2]) {
|
|
|
+ std::cout << v << std::endl;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (size_t v = 1; v < adjacency_list.size(); ++v) {
|
|
|
+ int cur_i = 0;
|
|
|
+ for (size_t u = 1; u < adjacency_list.size(); ++u) {
|
|
|
+ if (cur_i == (int)adjacency_list[v].size()) {
|
|
|
+ out_file << ' ' << 0;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if ((int)u == adjacency_list[v][cur_i]) {
|
|
|
+ out_file << ' ' << 1;
|
|
|
+ cur_i++;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ out_file << ' ' << 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ out_file << std::endl;
|
|
|
+ }
|
|
|
+
|
|
|
+ out_file.close();
|
|
|
+}
|
|
|
+int main(int argc, char const *argv[]) {
|
|
|
+ if (argc != 2) {
|
|
|
+ std::cout << "usage: " << std::endl;
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ string in_filename = argv[1];
|
|
|
+ fast_build(in_filename);
|
|
|
+ string out_filename (in_filename.begin(), in_filename.end() - 4);
|
|
|
+ out_filename += "txt";
|
|
|
+ convert(out_filename);
|
|
|
+ return 0;
|
|
|
+}
|