static void breadth_first_search(const GraphType& graph, 
                                 const VertexIndexType& source,
                                 const int& k_steps,
                                 AssociativeContainer& visited_vertices) {
  /* CHECK: AssociativeContainer::value_type == VertexIndexType */

  std::queue<VertexIndexType> even_epoch_queue;
  std::queue<VertexIndexType> odd_epoch_queue;
  std::queue<VertexIndexType>& current_queue = even_epoch_queue;
  std::queue<VertexIndexType>& next_queue = odd_epoch_queue;

  current_queue.push (source);
  int step_number = 0;
  while (step_number<k_steps) {
    while (!current_queue.empty ()) {
      const int vertex = current_queue.front ();
      current_queue.pop ();
      visited_vertices.insert (vertex);
      for (int target_index=graph.begin(vertex); 
           target_index<graph.end(vertex);
           ++target_index) {
        const int target = graph.get_target (target_index);
        if (visited_vertices.end()==visited_vertices.find (target)) {
          next_queue.push (target);
        }
      }
    }
    ++step_number;
    current_queue = (step_number&0x10)?even_epoch_queue:odd_epoch_queue;
    next_queue = (step_number&0x01)?even_epoch_queue:odd_epoch_queue;
  }
}
Example #2
0
bool outputTextEdgeData(const char* ofile, GraphType& G) {
  std::ofstream file(ofile);
  for (typename GraphType::iterator ii = G.begin(),
         ee = G.end(); ii != ee; ++ii) {
    unsigned src = G.getData(*ii).id;
    // FIXME: Version in include/Galois/Graphs/Serialize.h is wrong.
    for (typename GraphType::edge_iterator jj = G.edge_begin(*ii),
           ej = G.edge_end(*ii); jj != ej; ++jj) {
      unsigned dst = G.getData(G.getEdgeDst(jj)).id;
      file << src << ' ' << dst << ' ' << G.getEdgeData(jj) << '\n';
    }
  }
  return true;
}
static void construct_adjacency_matrix 
                        (const GraphType& graph,
                         const AssociativeContainer& vertices,
    __gnu_cxx::hash_map<VertexIndexType, VertexIndexType>& label_map,
    __gnu_cxx::hash_map<VertexIndexType, VertexIndexType>& reverse_label_map,
                         std::vector<EdgeWeightType>& adjacency_matrix) {
  typedef typename AssociativeContainer::const_iterator 
                              AssociativeContainerConstIterator;
                                        
  /* resize the adjacency matrix to hold the adjacencies */
  const int num_vertices = vertices.size ();
  adjacency_matrix.resize (num_vertices*num_vertices, 0.0);

  /* Create a map, which remaps the old vertices into new vertices */
  VertexIndexType new_label=0;
  AssociativeContainerConstIterator iter = vertices.begin();
  AssociativeContainerConstIterator end = vertices.end();

  while (iter!=end) {
    const VertexIndexType current_vertex = *iter++;
    label_map [current_vertex] = new_label;
    reverse_label_map [new_label] = current_vertex;
    ++new_label;
  }
  assert (new_label==num_vertices);
  
  /* now do the adjacencies */
  iter = vertices.begin();
  while (iter!=end) {
    const VertexIndexType old_vertex = *iter++;
    const VertexIndexType new_vertex = label_map [old_vertex];
    for (int target_index=graph.begin(old_vertex); 
         target_index<graph.end(old_vertex);
         ++target_index) {
      const VertexIndexType adjacent_vertex=graph.get_target (target_index);

      /* only add the adjacency if the target is one of "vertices". */
      if (vertices.end()!=vertices.find (adjacent_vertex)) {
        const int element_offset = 
          (num_vertices*new_vertex) + label_map [adjacent_vertex];
        adjacency_matrix [element_offset] = graph.get_weight (target_index);
      }
    }
  }
}