Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
graph_util.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file core/graph/graph_util.hh
11 /// @brief generic graph class header
12 /// @author Andrew Leaver-Fay (aleaverfay@gmail.com)
13 
14 // Unit Headers
15 #include <core/graph/graph_util.hh>
16 
17 // Package Headers
18 #include <core/graph/Graph.hh>
19 
20 //#include <iostream>
21 
22 namespace core {
23 namespace graph {
24 
25 bool
26 all_visited( utility::vector1< bool > const & visited );
27 
28 /// @details DFS search to identify connected components
29 /// O(V) memory, O(V+E) time.
32 {
33  using namespace utility;
34  vector1< bool > reached( g.num_nodes(), false );
35 
36  /// CC description
37  vector1< platform::Size > representative;
38  vector1< platform::Size > cc_nelements;
39  representative.reserve( g.num_nodes() ); // O(N) -- at most N cc's.
40  cc_nelements.reserve( g.num_nodes() ); // O(N) -- at most N cc's.
41 
42  /// DFS data
43  vector1< platform::Size > to_explore( g.num_nodes(), 0 ); // insert at most N
44  platform::Size n_to_explore = 0;
45 
46  for ( platform::Size ii = 1; ii <= (platform::Size) g.num_nodes(); ++ii ) {
47  if ( reached[ ii ] ) continue;
48 
49  // new connected component
50  // run a dfs from this node
51  representative.push_back( ii );
52  cc_nelements.push_back( 1 );
53  n_to_explore = 0;
54  to_explore[ ++n_to_explore ] = ii;
55 
56  while ( n_to_explore != 0 ) {
57  platform::Size const exploring = to_explore[ n_to_explore ];
58  to_explore[ n_to_explore ] = 0; // for sanity -- unneccesary
59  --n_to_explore;
60 
61  Node const * node_exploring = g.get_node( exploring );
63  eiter = node_exploring->const_edge_list_begin(),
64  eiter_end = node_exploring->const_edge_list_end();
65  eiter != eiter_end; ++eiter ) {
66  platform::Size neighbor = (*eiter)->get_other_ind( exploring );
67  if ( ! reached[ neighbor ] ) {
68  to_explore[ ++n_to_explore ] = neighbor; // dfs
69  ++cc_nelements[ cc_nelements.size() ];
70  reached[ neighbor ] = true;
71  }
72  }
73  }
74  }
75 
76  assert( all_visited( reached ) );
77 
78  // Prepare output descriptions
79  vector1< std::pair< platform::Size, platform::Size > > cc_descriptions( representative.size() );
80  for ( platform::Size ii = 1; ii <= representative.size(); ++ii ) {
81  cc_descriptions[ ii ] = std::make_pair( representative[ ii ], cc_nelements[ ii ] );
82  }
83  return cc_descriptions;
84 }
85 
86 bool
88 {
89  for ( platform::Size ii = 1; ii <= visited.size(); ++ii ) {
90  if ( ! visited[ ii ] ) return false;
91  }
92  return true;
93 }
94 
96 find_connected_components( Graph const & g );
97 
98 void
100  Graph & g,
101  utility::vector1< platform::Size > const & node_groups
102 )
103 {
104  assert( node_groups.size() == g.num_nodes() );
105 
106  for( Graph::EdgeListIter edge_it = g.edge_list_begin(); edge_it != g.edge_list_end(); ){
107 
108  Graph::EdgeListIter next_edge = edge_it;
109  ++next_edge;
110 
111  if( node_groups[ (*edge_it)->get_first_node_ind() ] == node_groups[ (*edge_it)->get_second_node_ind() ] ){
112  //std::cout << "GRAPH_EDGE_DELETE resi " << (*edge_it)->get_first_node_ind() << " to " << (*edge_it)->get_second_node_ind() << std::endl;
113  g.delete_edge( *edge_it );
114 
115  }
116 
117  edge_it = next_edge;
118 
119  } //loop over edges
120 } //delete_all_group1_edges_except_to_group2
121 
122 }
123 }