void mitk::ConnectomicsSimulatedAnnealingPermutationModularity::extractModuleSubgraph(
  ToModuleMapType *vertexToModuleMap,
  mitk::ConnectomicsNetwork::Pointer network,
  int moduleToSplit,
  mitk::ConnectomicsNetwork::Pointer subNetwork,
  VertexToVertexMapType* graphToSubgraphVertexMap,
  VertexToVertexMapType* subgraphToGraphVertexMap )
{
  const std::vector< VertexDescriptorType > allNodesVector = network->GetVectorOfAllVertexDescriptors();

  // add vertices to subgraph
  for( unsigned int nodeNumber( 0 ); nodeNumber < allNodesVector.size() ; nodeNumber++)
  {

    if( moduleToSplit == vertexToModuleMap->find( allNodesVector[ nodeNumber ] )->second )
    {
      int id = network->GetNode( allNodesVector[ nodeNumber ] ).id;
      VertexDescriptorType newVertex = subNetwork->AddVertex( id );

      graphToSubgraphVertexMap->insert(
        std::pair<VertexDescriptorType, VertexDescriptorType>(
        allNodesVector[ nodeNumber ], newVertex
        )
        );
      subgraphToGraphVertexMap->insert(
        std::pair<VertexDescriptorType, VertexDescriptorType>(
        newVertex, allNodesVector[ nodeNumber ]
      )
        );
    }
  }

  // add edges to subgraph
  VertexToVertexMapType::iterator iter = graphToSubgraphVertexMap->begin();
  VertexToVertexMapType::iterator end =  graphToSubgraphVertexMap->end();

  while( iter != end )
  {
    const std::vector< VertexDescriptorType > adjacentNodexVector
      = network->GetVectorOfAdjacentNodes( iter->first );

    for( unsigned int adjacentNodeNumber( 0 ); adjacentNodeNumber < adjacentNodexVector.size() ; adjacentNodeNumber++)
    {
      // if the adjacent vertex is part of the subgraph,
      // add edge, if it does not exist yet, else do nothing

      VertexDescriptorType adjacentVertex = adjacentNodexVector[ adjacentNodeNumber ];
      if( graphToSubgraphVertexMap->count( adjacentVertex ) > 0 )
      {
        if( !subNetwork->EdgeExists( iter->second, graphToSubgraphVertexMap->find( adjacentVertex )->second ) )
        { //edge exists in parent network, but not yet in sub network
          const VertexDescriptorType vertexA = iter->second;
          const VertexDescriptorType vertexB = graphToSubgraphVertexMap->find( adjacentVertex )->second;
          const int sourceID = network->GetNode( vertexA ).id;
          const int targetID = network->GetNode( vertexB ).id;
          const int weight = network->GetEdge( iter->first, graphToSubgraphVertexMap->find( adjacentVertex )->first ).weight;
          subNetwork->AddEdge( vertexA , vertexB, sourceID, targetID, weight );
        }
      }
    }
    iter++;
  }// end while( iter != end )
}
double mitk::ConnectomicsSimulatedAnnealingCostFunctionModularity::CalculateModularity( mitk::ConnectomicsNetwork::Pointer network, ToModuleMapType* vertexToModuleMap ) const
{
  double modularity( 0.0 );
  int numberOfModules = getNumberOfModules( vertexToModuleMap );

  if( network->GetNumberOfVertices() != vertexToModuleMap->size() )
  {
    MBI_ERROR << "Number of vertices and vertex to module map size do not match!";
    return modularity;
  }

  int numberOfLinksInNetwork( 0 );
  std::vector< int > numberOfLinksInModule, sumOfDegreesInModule;
  numberOfLinksInModule.resize( numberOfModules, 0 );
  sumOfDegreesInModule.resize( numberOfModules, 0 );
  // get vector of all vertex descriptors in the network

  const std::vector< VertexDescriptorType > allNodesVector 
    = network->GetVectorOfAllVertexDescriptors();

  for( int nodeNumber( 0 ); nodeNumber < allNodesVector.size() ; nodeNumber++)
  {
    int correspondingModule = vertexToModuleMap->find( allNodesVector[ nodeNumber ] )->second;
    const std::vector< VertexDescriptorType > adjacentNodexVector 
      = network->GetVectorOfAdjacentNodes( allNodesVector[ nodeNumber ] );
    numberOfLinksInNetwork += adjacentNodexVector.size();
    sumOfDegreesInModule[ correspondingModule ] += adjacentNodexVector.size();

    for( int adjacentNodeNumber( 0 ); adjacentNodeNumber < adjacentNodexVector.size() ; adjacentNodeNumber++)
    {
      if( correspondingModule == vertexToModuleMap->find( adjacentNodexVector[ adjacentNodeNumber ] )->second )
      {
        numberOfLinksInModule[ correspondingModule ]++;
      }
    }
  }

  // the numbers for links have to be halved, as each edge was counted twice
  numberOfLinksInNetwork = numberOfLinksInNetwork / 2;

  // if the network contains no links return 0
  if( numberOfLinksInNetwork < 1)
  {
    return 0;
  }

  for( int index( 0 ); index < numberOfModules ; index++)
  {
    numberOfLinksInModule[ index ] = numberOfLinksInModule[ index ] / 2;
  }

  //Calculate modularity M:
  //M = sum_{s=1}^{N_{M}} [ (l_{s} / L) - (d_{s} / ( 2L ))^2 ]
  //where N_{M} is the number of modules
  //L is the number of links in the network
  //l_{s} is the number of links between nodes in the module
  //s is the module
  //d_{s} is the sum of degrees of the nodes in the module
  //( taken from Guimera, R. AND Amaral, L. A. N.
  // Cartography of complex networks: modules and universal roles
  // Journal of Statistical Mechanics: Theory and Experiment, 2005, 2005, P02001 )

  for( int moduleID( 0 ); moduleID < numberOfModules; moduleID++ )
  {
    modularity += (((double) numberOfLinksInModule[ moduleID ]) / ((double) numberOfLinksInNetwork)) -
      (
      (((double) sumOfDegreesInModule[ moduleID ]) / ((double) 2 * numberOfLinksInNetwork) ) * 
      (((double) sumOfDegreesInModule[ moduleID ]) / ((double) 2 * numberOfLinksInNetwork) ) 
      );
  }

  return modularity;
}