Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
min_tree.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file min_tree.cpp
1 /* This software is distributed under the GNU Lesser General Public License */
2 //==========================================================================
3 //
4 // min_tree.cpp
5 //
6 //==========================================================================
7 // $Id: min_tree.cpp,v 1.4 2001/11/07 13:58:10 pick Exp $
8 
9 #include <GTL/graph.h>
10 #include <stack>
11 #include <queue>
12 #include <GTL/min_tree.h>
13 
14 #ifdef __GTL_MSVCC
15 # ifdef _DEBUG
16 # ifndef SEARCH_MEMORY_LEAKS_ENABLED
17 # error SEARCH NOT ENABLED
18 # endif
19 # define new DEBUG_NEW
20 # undef THIS_FILE
21  static char THIS_FILE[] = __FILE__;
22 # endif // _DEBUG
23 #endif // __GTL_MSVCC
24 
26 
28  is_set_distances = false;
29  weight = 0;
30 }
31 
33  if (g.is_directed()) return GTL_ERROR;
34  else if (g.number_of_nodes() < 2) return GTL_ERROR;
35  else if (!g.is_connected()) return GTL_ERROR;
36  else if (!is_set_distances) return GTL_ERROR;
37  else return GTL_OK;
38 }
39 
41  this->dist = dist;
42  is_set_distances = true;
43 }
44 
45 set<edge> min_tree::get_min_tree() {
46  return this->tree;
47 }
48 
50  int sum;
51  set<edge>::iterator tree_it;
52 
53  sum = 0;
54 
55  for (tree_it = tree.begin(); tree_it != tree.end(); tree_it++)
56  sum += dist[*tree_it];
57 
58  return sum;
59 }
60 
62  priority_queue <TSP_A_VALUE, vector<TSP_A_VALUE>, input_comp> node_distances;
63  node::adj_edges_iterator adj_it, adj_end;
64  set<node> tree_nodes;
65  set<node>::iterator tree_it;
66  edge curr;
67  node new_node;
68  graph::edge_iterator edge_it, edges_end;
69  unsigned int number_of_nodes;
70  int min_dist;
71 
72 
73  // making out the start edge
74 
75  edge_it = g.edges_begin();
76  edges_end = g.edges_end();
77 
78  curr = *edge_it;
79  min_dist = dist[*edge_it];
80 
81  for (; edge_it != edges_end; edge_it++) {
82  if (dist[*edge_it] < min_dist) {
83  curr = *edge_it;
84  min_dist = dist[*edge_it];
85  }
86  }
87 
88  tree.insert(curr);
89 
90  tree_nodes.insert(curr.source());
91  tree_nodes.insert(curr.target());
92 
93 
94  for (tree_it = tree_nodes.begin(); tree_it != tree_nodes.end(); tree_it++) {
95  adj_it = (*tree_it).adj_edges_begin();
96  adj_end = (*tree_it).adj_edges_end();
97 
98  for (; adj_it != adj_end; adj_it++) {
99  node_distances.push(TSP_A_VALUE(dist[*adj_it], adj_it));
100  }
101  }
102 
103  // create the min_tree
104 
105  number_of_nodes = g.number_of_nodes();
106 
107  while(tree.size() < number_of_nodes - 1) {
108  curr = *((node_distances.top()).second);
109 
110  node_distances.pop();
111 
112  if (tree_nodes.find(curr.source()) != tree_nodes.end() &&
113  tree_nodes.find(curr.target()) != tree_nodes.end()) {
114  } else {
115  tree.insert(curr);
116  weight += dist[curr];
117 
118  if (tree_nodes.find(curr.source()) != tree_nodes.end()) {
119  new_node = curr.target();
120  } else {
121  new_node = curr.source();
122  }
123 
124  tree_nodes.insert(new_node);
125 
126  adj_it = new_node.adj_edges_begin();
127  adj_end = new_node.adj_edges_end();
128 
129  for (; adj_it != adj_end; adj_it++) {
130  node_distances.push(TSP_A_VALUE(dist[*adj_it], adj_it));
131  }
132  }
133  }
134 
135  return GTL_OK;
136 }
137 
139  tree.erase (tree.begin(), tree.end());
140  weight = 0;
141 }
142 
144 
145 
146 
147 
148 
149 
150 
151 
152