VCP4DirectedLinkPredictor::VCP4DirectedLinkPredictor( const WeightedNetwork& network, const WeightedNetwork& completeNetwork ) : LinkPredictor(network,completeNetwork), connectedPairs( 0 ), amutualPairs( 0 ), mutualPairs( 0 ), unconnectedPairs( 0 ) { // compute the total number of somehow-connected pairs in the graph for( vertex_t i = 0; i < network.vertexCount(); ++i ) { neighbor_set_t::const_iterator outIt = network.outNeighbors( i ).begin(); neighbor_set_t::const_iterator outEnd = network.outNeighbors( i ).end(); neighbor_set_t::const_iterator inIt = network.inNeighbors( i ).begin(); neighbor_set_t::const_iterator inEnd = network.inNeighbors( i ).end(); while( outIt != outEnd && outIt->first <= i ) { ++outIt; } while( inIt != inEnd && inIt->first <= i ) { ++inIt; } while( outIt != outEnd && inIt != inEnd ) { ++connectedPairs; if( outIt->first < inIt->first ) { ++amutualPairs; ++outIt; } else if( outIt->first > inIt->first ) { ++amutualPairs; ++inIt; } else { ++mutualPairs; ++outIt; ++inIt; } } while( outIt != outEnd ) { ++amutualPairs; ++connectedPairs; ++outIt; } while( inIt != inEnd ) { ++amutualPairs; ++connectedPairs; ++inIt; } } unsigned long potentialConnections = (unsigned long)network.vertexCount() * (unsigned long)(network.vertexCount() - 1) / 2; unconnectedPairs = potentialConnections - connectedPairs; }
int main( int argc, char* argv[] ) { char c; while( (c = getopt( argc, argv, "+h" )) >= 0 ) { if( c == 'h' ) { usage( argv[0] ); return 0; } } if( argc != optind + 1 ) { usage( argv[0] ); return 1; } char* spec = argv[optind]; unsigned int currentDistance = 0; unsigned int distance = strlen( spec ); vector<bool> direction( distance ); while( currentDistance < distance ) { if( tolower( spec[currentDistance] ) == 'o' ) { direction[currentDistance++] = true; } else if( tolower( spec[currentDistance] ) == 'i' ) { direction[currentDistance++] = false; } else { usage( argv[0] ); return 1; } } WeightedNetwork network = WeightedNetwork::readNetwork( cin ); for( vertex_t v1 = 0; v1 < network.vertexCount(); ++v1 ) { vector<bool> found = vector<bool>( network.vertexCount() ); vector<vertex_t> search; found.at( v1 ) = true; search.push_back( v1 ); for( currentDistance = 0; currentDistance < distance; ++currentDistance ) { vector<vertex_t> newSearch; for( vector<vertex_t>::const_iterator vertexIterator = search.begin(); vertexIterator != search.end(); ++vertexIterator ) { const vertex_t searchVertex = *vertexIterator; const neighbor_set_t& neighbors = direction[currentDistance] ? network.outNeighbors( searchVertex ) : network.inNeighbors( searchVertex ); for( neighbor_set_t::const_iterator neighborIterator = neighbors.begin(); neighborIterator != neighbors.end(); neighborIterator++ ) { const vertex_t neighbor = neighborIterator->first; if( !found.at( neighbor ) ) { found.at( neighbor ) = true; if( currentDistance == distance - 1 ) { cout << network.translateIntToExt( v1 ) << " " << network.translateIntToExt( neighbor ) << "\n"; } else { newSearch.push_back( neighbor ); } } } } search.swap( newSearch ); } } return 0; }