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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
// Copyright 2004 The Trustees of Indiana University.
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Authors: Douglas Gregor
// Andrew Lumsdaine
// This program performs betweenness centrality (BC) clustering on the
// actor collaboration graph available at
// http://www.nd.edu/~networks/database/index.html and outputs the
// result of clustering in Pajek format.
//
// This program mimics the BC clustering algorithm program implemented
// by Shashikant Penumarthy for JUNG, so that we may compare results
// and timings.
#include <boost/graph/bc_clustering.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <fstream>
#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>
#include <boost/lexical_cast.hpp>
#include <map>
using namespace boost;
struct Actor
{
Actor(int id = -1) : id(id) {}
int id;
};
typedef adjacency_list<vecS, vecS, undirectedS, Actor,
property<edge_centrality_t, double> > ActorGraph;
typedef graph_traits<ActorGraph>::vertex_descriptor Vertex;
typedef graph_traits<ActorGraph>::edge_descriptor Edge;
void load_actor_graph(std::istream& in, ActorGraph& g)
{
std::map<int, Vertex> actors;
std::string line;
while (getline(in, line)) {
std::vector<Vertex> actors_in_movie;
// Map from the actor numbers on this line to the actor vertices
typedef tokenizer<char_separator<char> > Tok;
Tok tok(line, char_separator<char>(" "));
for (Tok::iterator id = tok.begin(); id != tok.end(); ++id) {
int actor_id = lexical_cast<int>(*id);
std::map<int, Vertex>::iterator v = actors.find(actor_id);
if (v == actors.end()) {
Vertex new_vertex = add_vertex(Actor(actor_id), g);
actors[actor_id] = new_vertex;
actors_in_movie.push_back(new_vertex);
} else {
actors_in_movie.push_back(v->second);
}
}
for (std::vector<Vertex>::iterator i = actors_in_movie.begin();
i != actors_in_movie.end(); ++i) {
for (std::vector<Vertex>::iterator j = i + 1;
j != actors_in_movie.end(); ++j) {
if (!edge(*i, *j, g).second) add_edge(*i, *j, g);
}
}
}
}
template<typename Graph, typename VertexIndexMap, typename VertexNameMap>
std::ostream&
write_pajek_graph(std::ostream& out, const Graph& g,
VertexIndexMap vertex_index, VertexNameMap vertex_name)
{
out << "*Vertices " << num_vertices(g) << '\n';
typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
for (vertex_iterator v = vertices(g).first; v != vertices(g).second; ++v) {
out << get(vertex_index, *v)+1 << " \"" << get(vertex_name, *v) << "\"\n";
}
out << "*Edges\n";
typedef typename graph_traits<Graph>::edge_iterator edge_iterator;
for (edge_iterator e = edges(g).first; e != edges(g).second; ++e) {
out << get(vertex_index, source(*e, g))+1 << ' '
<< get(vertex_index, target(*e, g))+1 << " 1.0\n"; // HACK!
}
return out;
}
class actor_clustering_threshold : public bc_clustering_threshold<double>
{
typedef bc_clustering_threshold<double> inherited;
public:
actor_clustering_threshold(double threshold, const ActorGraph& g,
bool normalize)
: inherited(threshold, g, normalize), iter(1) { }
bool operator()(double max_centrality, Edge e, const ActorGraph& g)
{
std::cout << "Iter: " << iter << " Max Centrality: "
<< (max_centrality / dividend) << std::endl;
++iter;
return inherited::operator()(max_centrality, e, g);
}
private:
unsigned int iter;
};
int main(int argc, char* argv[])
{
std::string in_file;
std::string out_file;
double threshold = -1.0;
bool normalize = false;
// Parse command-line options
{
int on_arg = 1;
while (on_arg < argc) {
std::string arg(argv[on_arg]);
if (arg == "-in") {
++on_arg; assert(on_arg < argc);
in_file = argv[on_arg];
} else if (arg == "-out") {
++on_arg; assert(on_arg < argc);
out_file = argv[on_arg];
} else if (arg == "-threshold") {
++on_arg; assert(on_arg < argc);
threshold = lexical_cast<double>(argv[on_arg]);
} else if (arg == "-normalize") {
normalize = true;
} else {
std::cerr << "Unrecognized parameter \"" << arg << "\".\n";
return -1;
}
++on_arg;
}
if (in_file.empty() || out_file.empty() || threshold < 0) {
std::cerr << "error: syntax is actor_clustering [options]\n\n"
<< "options are:\n"
<< "\t-in <infile>\tInput file\n"
<< "\t-out <outfile>\tOutput file\n"
<< "\t-threshold <value>\tA threshold value\n"
<< "\t-normalize\tNormalize edge centrality scores\n";
return -1;
}
}
ActorGraph g;
// Load the actor graph
{
std::cout << "Building graph." << std::endl;
std::ifstream in(in_file.c_str());
if (!in) {
std::cerr << "Unable to open file \"" << in_file << "\" for input.\n";
return -2;
}
load_actor_graph(in, g);
}
// Run the algorithm
std::cout << "Clusting..." << std::endl;
betweenness_centrality_clustering(g,
actor_clustering_threshold(threshold, g, normalize),
get(edge_centrality, g));
// Output the graph
{
std::cout << "Writing graph to file: " << out_file << std::endl;
std::ofstream out(out_file.c_str());
if (!out) {
std::cerr << "Unable to open file \"" << out_file << "\" for output.\n";
return -3;
}
write_pajek_graph(out, g, get(vertex_index, g), get(&Actor::id, g));
}
return 0;
}
|