-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathtemporal_network.cc
More file actions
72 lines (56 loc) · 2.87 KB
/
temporal_network.cc
File metadata and controls
72 lines (56 loc) · 2.87 KB
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
#include "temporal_network.h"
/**
*@fn TemporalNetwork::AddNetwork ( const string& filename, const string& delimiters )
*
*Adds a network structure to the end of the temporal network represented by this structure.
*
*@TODO - Add parameter to notify if the network is directed or undirected.
* - Add option to add a network in any time point of the temporal network
*
*@param filename Name of the file to load the information from. Each line in the file
* should be a single edge - node1|node2|edge_weight. If the network
* is undirected, it doesn't matter if both directions are in the
* file. If directed, the edge format is fr|to|edge_weight.
*@param delimiters Characters to be used as the delimiter for the network file
*
*/
shared_ptr < network > temporal_network::AddNetwork ( const string& filename, const string& delimiters, const bool& directed ){
ifstream fin; //Open file
openFileHarsh(&fin, filename);
vector < string > fields; //Set up parameters
shared_ptr < network > result ( new network() );
while ( fline_tr( &fin, &fields, delimiters ) ){
if (fields.size() != 3) continue; //Simple format check
for (int i = 0; i < 2; i++){ //Add new edges to list
if ( vertex_set.find(fields[i]) == vertex_set.end() ){
vertex_set.insert(fields[i]);
}
}
pair < double, bool > ret = check_str_to<double>(fields[2]); //Get edge weight
if ( !ret.second ) continue; //Wrong format
result->addEdge(shared_ptr<string>( new string ( *(vertex_set.find(fields[0])) ) ),
shared_ptr<string>( new string ( *(vertex_set.find(fields[1])) ) ),
ret.first, directed ); //Add to network
}
networks.push_back(result); //Add to time series
fin.close(); //Clean up
return ( networks[networks.size() - 1] );
}
void temporal_network::AddCommunities ( const string& filename, const string& delimiters ){
ifstream fin; //Open file
openFileHarsh(&fin, filename);
vector < string > fields; //Set up parameters
set < community, cmp_set_str > structure;
while ( fline_tr( &fin, &fields, delimiters ) ){
community next;
for (int i = 0; i < fields.size(); i++){ //Add new edges to list
if ( vertex_set.find(fields[i]) == vertex_set.end() ){
vertex_set.insert(fields[i]);
}
next.insert( shared_ptr < string > ( new string ( *(vertex_set.find(fields[i]) ) ) ) );
}
structure.insert(next);
}
communities.push_back( structure );
fin.close(); //Clean up
}