bool floyd_warshall_dispatch(const VertexListGraph& g, DistanceMatrix& d, const BinaryPredicate &compare, const BinaryFunction &combine, const Infinity& inf, const Zero& zero) { BOOST_USING_STD_MIN(); typename graph_traits<VertexListGraph>::vertex_iterator i, lasti, j, lastj, k, lastk; for (tie(k, lastk) = vertices(g); k != lastk; k++) for (tie(i, lasti) = vertices(g); i != lasti; i++) for (tie(j, lastj) = vertices(g); j != lastj; j++) { d[*i][*j] = min BOOST_PREVENT_MACRO_SUBSTITUTION (d[*i][*j], combine(d[*i][*k], d[*k][*j])); } for (tie(i, lasti) = vertices(g); i != lasti; i++) if (compare(d[*i][*i], zero)) return false; return true; }
void smallest_last_vertex_ordering(const VertexListGraph& G, Order order, Degree degree, Marker marker, BucketSorter& degree_buckets) { typedef typename boost::graph_traits<VertexListGraph> GraphTraits; typedef typename GraphTraits::vertex_descriptor Vertex; //typedef typename GraphTraits::size_type size_type; typedef std::size_t size_type; const size_type num = num_vertices(G); typename GraphTraits::vertex_iterator v, vend; for (boost::tie(v, vend) = vertices(G); v != vend; ++v) { put(marker, *v, num); put(degree, *v, out_degree(*v, G)); degree_buckets.push(*v); } size_type minimum_degree = 0; size_type current_order = num - 1; while ( 1 ) { typedef typename BucketSorter::stack MDStack; MDStack minimum_degree_stack = degree_buckets[minimum_degree]; while (minimum_degree_stack.empty()) minimum_degree_stack = degree_buckets[++minimum_degree]; Vertex node = minimum_degree_stack.top(); put(order, current_order, node); if ( current_order == 0 ) //find all vertices break; minimum_degree_stack.pop(); put(marker, node, 0); //node has been ordered. typename GraphTraits::adjacency_iterator v, vend; for (boost::tie(v,vend) = adjacent_vertices(node, G); v != vend; ++v) if ( get(marker,*v) > current_order ) { //*v is unordered vertex put(marker, *v, current_order); //mark the columns adjacent to node //delete *v from the bucket sorter degree_buckets.remove(*v); //It is possible minimum degree goes down //Here we keep tracking it. put(degree, *v, get(degree, *v) - 1); BOOST_USING_STD_MIN(); minimum_degree = min BOOST_PREVENT_MACRO_SUBSTITUTION(minimum_degree, get(degree, *v)); //reinsert *v in the bucket sorter with the new degree degree_buckets.push(*v); } current_order--; } //at this point, order[i] = v_i; }
void back_edge(const Edge& e, Graph& g) { typedef typename graph_traits<Graph>::vertex_descriptor vertex_t; typedef typename graph_traits<Graph>::vertices_size_type v_size_t; vertex_t s(source(e,g)); vertex_t t(target(e,g)); BOOST_USING_STD_MIN(); if ( t != get(parent, s) ) { v_size_t s_low_df_number = get(low, s); v_size_t t_df_number = get(df_number, t); v_size_t s_least_ancestor_df_number = get(least_ancestor, s); put(low, s, min BOOST_PREVENT_MACRO_SUBSTITUTION(s_low_df_number, t_df_number) ); put(least_ancestor, s, min BOOST_PREVENT_MACRO_SUBSTITUTION(s_least_ancestor_df_number, t_df_number ) ); } }
inline std::pair<typename property_traits<EccentricityMap>::value_type, typename property_traits<EccentricityMap>::value_type> all_eccentricities(const Graph& g, const DistanceMatrix& dist, EccentricityMap ecc) { function_requires< VertexListGraphConcept<Graph> >(); typedef typename graph_traits<Graph>::vertex_descriptor Vertex; typedef typename graph_traits<Graph>::vertex_iterator VertexIterator; function_requires< ReadablePropertyMapConcept<DistanceMatrix,Vertex> >(); typedef typename property_traits<DistanceMatrix>::value_type DistanceMap; function_requires< WritablePropertyMapConcept<EccentricityMap,Vertex> >(); typedef typename property_traits<EccentricityMap>::value_type Eccentricity; BOOST_USING_STD_MIN(); BOOST_USING_STD_MAX(); Eccentricity r = numeric_values<Eccentricity>::infinity(), d = numeric_values<Eccentricity>::zero(); VertexIterator i, end; tie(i, end) = vertices(g); for(tie(i, end) = vertices(g); i != end; ++i) { DistanceMap dm = get(dist, *i); Eccentricity e = eccentricity(g, dm); put(ecc, *i, e); // track the radius and diameter at the same time r = min BOOST_PREVENT_MACRO_SUBSTITUTION (r, e); d = max BOOST_PREVENT_MACRO_SUBSTITUTION (d, e); } return make_pair(r, d); }
template<class T, class Policies> inline interval<T, Policies> cos(const interval<T, Policies>& x) { if (interval_lib::detail::test_input(x)) return interval<T, Policies>::empty(); typename Policies::rounding rnd; typedef interval<T, Policies> I; typedef typename interval_lib::unprotect<I>::type R; // get lower bound within [0, pi] const R pi2 = interval_lib::pi_twice<R>(); R tmp = fmod((const R&)x, pi2); if (width(tmp) >= pi2.lower()) return I(static_cast<T>(-1), static_cast<T>(1), true); // we are covering a full period if (tmp.lower() >= interval_lib::constants::pi_upper<T>()) return -cos(tmp - interval_lib::pi<R>()); T l = tmp.lower(); T u = tmp.upper(); BOOST_USING_STD_MIN(); // separate into monotone subintervals if (u <= interval_lib::constants::pi_lower<T>()) return I(rnd.cos_down(u), rnd.cos_up(l), true); else if (u <= pi2.lower()) return I(static_cast<T>(-1), rnd.cos_up(min BOOST_PREVENT_MACRO_SUBSTITUTION(rnd.sub_down(pi2.lower(), u), l)), true); else return I(static_cast<T>(-1), static_cast<T>(1), true); }
inline void augment(Graph& g, typename graph_traits<Graph>::vertex_descriptor src, typename graph_traits<Graph>::vertex_descriptor sink, PredEdgeMap p, ResCapMap residual_capacity, RevEdgeMap reverse_edge) { typename graph_traits<Graph>::edge_descriptor e; typename graph_traits<Graph>::vertex_descriptor u; typedef typename property_traits<ResCapMap>::value_type FlowValue; // find minimum residual capacity along the augmenting path FlowValue delta = (std::numeric_limits<FlowValue>::max)(); e = p[sink]; do { BOOST_USING_STD_MIN(); delta = min BOOST_PREVENT_MACRO_SUBSTITUTION(delta, residual_capacity[e]); u = source(e, g); e = p[u]; } while (u != src); // push delta units of flow along the augmenting path e = p[sink]; do { residual_capacity[e] -= delta; residual_capacity[reverse_edge[e]] += delta; u = source(e, g); e = p[u]; } while (u != src); }
bool intersects(double x1, double y1, double x2, double y2, double a1, double b1, double a2, double b2, double epsilon = 0.000001 ) { if (x1 - x2 == 0) { std::swap(x1,a1); std::swap(y1,b1); std::swap(x2,a2); std::swap(y2,b2); } if (x1 - x2 == 0) { BOOST_USING_STD_MAX(); BOOST_USING_STD_MIN(); //two vertical line segments double min_y = min BOOST_PREVENT_MACRO_SUBSTITUTION(y1,y2); double max_y = max BOOST_PREVENT_MACRO_SUBSTITUTION(y1,y2); double min_b = min BOOST_PREVENT_MACRO_SUBSTITUTION(b1,b2); double max_b = max BOOST_PREVENT_MACRO_SUBSTITUTION(b1,b2); if ((max_y > max_b && max_b > min_y) || (max_b > max_y && max_y > min_b) ) return true; else return false; } double x_diff = x1 - x2; double y_diff = y1 - y2; double a_diff = a2 - a1; double b_diff = b2 - b1; double beta_denominator = b_diff - (y_diff/((double)x_diff)) * a_diff; if (beta_denominator == 0) { //parallel lines return false; } double beta = (b2 - y2 - (y_diff/((double)x_diff)) * (a2 - x2)) / beta_denominator; double alpha = (a2 - x2 - beta*(a_diff))/x_diff; double upper_bound = 1 - epsilon; double lower_bound = 0 + epsilon; return (beta < upper_bound && beta > lower_bound && alpha < upper_bound && alpha > lower_bound); }
T max_abs( T t1 , T t2 ) { BOOST_USING_STD_MIN(); BOOST_USING_STD_MAX(); if( t1>0 ) return max BOOST_PREVENT_MACRO_SUBSTITUTION ( t1 , t2 ); else return min BOOST_PREVENT_MACRO_SUBSTITUTION ( t1 , t2 ); }
template<class T, class Policies> inline interval<T, Policies> min BOOST_PREVENT_MACRO_SUBSTITUTION (const T& x, const interval<T, Policies>& y) { typedef interval<T, Policies> I; if (interval_lib::detail::test_input(x, y)) return I::empty(); BOOST_USING_STD_MIN(); return I(min BOOST_PREVENT_MACRO_SUBSTITUTION(x, y.lower()), min BOOST_PREVENT_MACRO_SUBSTITUTION(x, y.upper()), true); }
inline void union_successor_sets(const std::vector < std::size_t > &s1, const std::vector < std::size_t > &s2, std::vector < std::size_t > &s3) { BOOST_USING_STD_MIN(); for (std::size_t k = 0; k < s1.size(); ++k) s3[k] = min BOOST_PREVENT_MACRO_SUBSTITUTION(s1[k], s2[k]); }
bool floyd_warshall_all_pairs_shortest_paths( const VertexAndEdgeListGraph& g, DistanceMatrix& d, const WeightMap& w, const BinaryPredicate& compare, const BinaryFunction& combine, const Infinity& inf, const Zero& zero) { BOOST_USING_STD_MIN(); function_requires<VertexListGraphConcept<VertexAndEdgeListGraph> >(); function_requires<EdgeListGraphConcept<VertexAndEdgeListGraph> >(); function_requires<IncidenceGraphConcept<VertexAndEdgeListGraph> >(); typename graph_traits<VertexAndEdgeListGraph>::vertex_iterator firstv, lastv, firstv2, lastv2; typename graph_traits<VertexAndEdgeListGraph>::edge_iterator first, last; for(tie(firstv, lastv) = vertices(g); firstv != lastv; firstv++) for(tie(firstv2, lastv2) = vertices(g); firstv2 != lastv2; firstv2++) d[*firstv][*firstv2] = inf; for(tie(firstv, lastv) = vertices(g); firstv != lastv; firstv++) d[*firstv][*firstv] = 0; for(tie(first, last) = edges(g); first != last; first++) { if (d[source(*first, g)][target(*first, g)] != inf) d[source(*first, g)][target(*first, g)] = min BOOST_PREVENT_MACRO_SUBSTITUTION(get(w, *first), d[source(*first, g)][target(*first, g)]); else d[source(*first, g)][target(*first, g)] = get(w, *first); } bool is_undirected = is_same<typename graph_traits<VertexAndEdgeListGraph>::directed_category, undirected_tag>::value; if (is_undirected) { for(tie(first, last) = edges(g); first != last; first++) { if (d[target(*first, g)][source(*first, g)] != inf) d[target(*first, g)][source(*first, g)] = min BOOST_PREVENT_MACRO_SUBSTITUTION(get(w, *first), d[target(*first, g)][source(*first, g)]); else d[target(*first, g)][source(*first, g)] = get(w, *first); } } return detail::floyd_warshall_dispatch(g, d, compare, combine, inf, zero); }
void back_edge(const Edge& e, Graph& g) { BOOST_USING_STD_MIN(); if ( target(e, g) != get(pred, source(e, g)) ) { S.push(e); put(lowpt, source(e, g), min BOOST_PREVENT_MACRO_SUBSTITUTION(get(lowpt, source(e, g)), get(dtm, target(e, g)))); } }
template<class T, class Policies> inline interval<T, Policies> operator*(const interval<T, Policies>& x, const interval<T, Policies>& y) { BOOST_USING_STD_MIN(); BOOST_USING_STD_MAX(); typedef interval<T, Policies> I; if (interval_lib::detail::test_input(x, y)) return I::empty(); typename Policies::rounding rnd; const T& xl = x.lower(); const T& xu = x.upper(); const T& yl = y.lower(); const T& yu = y.upper(); if (interval_lib::user::is_neg(xl)) if (interval_lib::user::is_pos(xu)) if (interval_lib::user::is_neg(yl)) if (interval_lib::user::is_pos(yu)) // M * M return I(min BOOST_PREVENT_MACRO_SUBSTITUTION(rnd.mul_down(xl, yu), rnd.mul_down(xu, yl)), max BOOST_PREVENT_MACRO_SUBSTITUTION(rnd.mul_up (xl, yl), rnd.mul_up (xu, yu)), true); else // M * N return I(rnd.mul_down(xu, yl), rnd.mul_up(xl, yl), true); else if (interval_lib::user::is_pos(yu)) // M * P return I(rnd.mul_down(xl, yu), rnd.mul_up(xu, yu), true); else // M * Z return I(static_cast<T>(0), static_cast<T>(0), true); else if (interval_lib::user::is_neg(yl)) if (interval_lib::user::is_pos(yu)) // N * M return I(rnd.mul_down(xl, yu), rnd.mul_up(xl, yl), true); else // N * N return I(rnd.mul_down(xu, yu), rnd.mul_up(xl, yl), true); else if (interval_lib::user::is_pos(yu)) // N * P return I(rnd.mul_down(xl, yu), rnd.mul_up(xu, yl), true); else // N * Z return I(static_cast<T>(0), static_cast<T>(0), true); else if (interval_lib::user::is_pos(xu)) if (interval_lib::user::is_neg(yl)) if (interval_lib::user::is_pos(yu)) // P * M return I(rnd.mul_down(xu, yl), rnd.mul_up(xu, yu), true); else // P * N return I(rnd.mul_down(xu, yl), rnd.mul_up(xl, yu), true); else if (interval_lib::user::is_pos(yu)) // P * P return I(rnd.mul_down(xl, yl), rnd.mul_up(xu, yu), true); else // P * Z return I(static_cast<T>(0), static_cast<T>(0), true); else // Z * ? return I(static_cast<T>(0), static_cast<T>(0), true); }
template<class T, class Policies> inline interval<T, Policies> intersect(const interval<T, Policies>& x, const interval<T, Policies>& y) { BOOST_USING_STD_MIN(); BOOST_USING_STD_MAX(); if (interval_lib::detail::test_input(x, y)) return interval<T, Policies>::empty(); const T& l = max BOOST_PREVENT_MACRO_SUBSTITUTION(x.lower(), y.lower()); const T& u = min BOOST_PREVENT_MACRO_SUBSTITUTION(x.upper(), y.upper()); if (l <= u) return interval<T, Policies>(l, u, true); else return interval<T, Policies>::empty(); }
template<class T, class Policies> inline interval<T, Policies> hull(const T& x, const interval<T, Policies>& y) { BOOST_USING_STD_MIN(); BOOST_USING_STD_MAX(); bool bad_x = interval_lib::detail::test_input<T, Policies>(x); bool bad_y = interval_lib::detail::test_input(y); if (bad_x) if (bad_y) return interval<T, Policies>::empty(); else return y; else if (bad_y) return interval<T, Policies>(x, x, true); return interval<T, Policies>(min BOOST_PREVENT_MACRO_SUBSTITUTION(x, y.lower()), max BOOST_PREVENT_MACRO_SUBSTITUTION(x, y.upper()), true); }
void finish_vertex(const Vertex& u, Graph&) { typedef typename graph_traits<Graph>::vertices_size_type v_size_t; Vertex u_parent = get(parent, u); v_size_t u_parent_lowpoint = get(low, u_parent); v_size_t u_lowpoint = get(low, u); BOOST_USING_STD_MIN(); if (u_parent != u) { put(low, u_parent, min BOOST_PREVENT_MACRO_SUBSTITUTION(u_lowpoint, u_parent_lowpoint ) ); } }
void finish_vertex(const Vertex& u, Graph& g) { BOOST_USING_STD_MIN(); Vertex parent = get(pred, u); bool is_art_point = false; if ( get(dtm, parent) > get(dtm, u) ) { parent = get(pred, parent); is_art_point = true; } if ( parent == u ) { // at top if ( get(dtm, u) + 1 == get(dtm, get(pred, u)) ) is_art_point = false; } else { put(lowpt, parent, min BOOST_PREVENT_MACRO_SUBSTITUTION(get(lowpt, parent), get(lowpt, u))); if (get(lowpt, u) >= get(dtm, parent)) { if ( get(dtm, parent) > get(dtm, get(pred, parent)) ) { put(pred, u, get(pred, parent)); put(pred, parent, u); } while ( get(dtm, source(S.top(), g)) >= get(dtm, u) ) { put(comp, S.top(), c); S.pop(); } put(comp, S.top(), c); S.pop(); ++c; if ( S.empty() ) { put(pred, u, parent); put(pred, parent, u); } } } if ( is_art_point ) *out++ = u; }
inline std::pair<typename property_traits<EccentricityMap>::value_type, typename property_traits<EccentricityMap>::value_type> radius_and_diameter(const Graph& g, EccentricityMap ecc) { function_requires< VertexListGraphConcept<Graph> >(); typedef typename graph_traits<Graph>::vertex_descriptor Vertex; typedef typename graph_traits<Graph>::vertex_iterator VertexIterator; function_requires< ReadablePropertyMapConcept<EccentricityMap, Vertex> >(); typedef typename property_traits<EccentricityMap>::value_type Eccentricity; BOOST_USING_STD_MIN(); BOOST_USING_STD_MAX(); VertexIterator i, end; tie(i, end) = vertices(g); Eccentricity radius = get(ecc, *i); Eccentricity diameter = get(ecc, *i); for(i = boost::next(i); i != end; ++i) { Eccentricity cur = get(ecc, *i); radius = min BOOST_PREVENT_MACRO_SUBSTITUTION (radius, cur); diameter = max BOOST_PREVENT_MACRO_SUBSTITUTION (diameter, cur); } return std::make_pair(radius, diameter); }
slice::range<RandomAccessIterator> get_indices( const RandomAccessIterator& begin, const RandomAccessIterator& end) const { // This is based loosely on PySlice_GetIndicesEx(), but it has been // carefully crafted to ensure that these iterators never fall out of // the range of the container. slice::range<RandomAccessIterator> ret; typedef typename iterator_difference<RandomAccessIterator>::type difference_type; difference_type max_dist = abt_boost::detail::distance(begin, end); object slice_start = this->start(); object slice_stop = this->stop(); object slice_step = this->step(); // Extract the step. if (slice_step == object()) { ret.step = 1; } else { ret.step = extract<long>( slice_step); if (ret.step == 0) { PyErr_SetString( PyExc_IndexError, "step size cannot be zero."); throw_error_already_set(); } } // Setup the start iterator. if (slice_start == object()) { if (ret.step < 0) { ret.start = end; --ret.start; } else ret.start = begin; } else { difference_type i = extract<long>( slice_start); if (i >= max_dist && ret.step > 0) throw std::invalid_argument( "Zero-length slice"); if (i >= 0) { ret.start = begin; BOOST_USING_STD_MIN(); std::advance( ret.start, min BOOST_PREVENT_MACRO_SUBSTITUTION(i, max_dist-1)); } else { if (i < -max_dist && ret.step < 0) throw std::invalid_argument( "Zero-length slice"); ret.start = end; // Advance start (towards begin) not farther than begin. std::advance( ret.start, (-i < max_dist) ? i : -max_dist ); } } // Set up the stop iterator. This one is a little trickier since slices // define a [) range, and we are returning a [] range. if (slice_stop == object()) { if (ret.step < 0) { ret.stop = begin; } else { ret.stop = end; std::advance( ret.stop, -1); } } else { difference_type i = extract<long>(slice_stop); // First, branch on which direction we are going with this. if (ret.step < 0) { if (i+1 >= max_dist || i == -1) throw std::invalid_argument( "Zero-length slice"); if (i >= 0) { ret.stop = begin; std::advance( ret.stop, i+1); } else { // i is negative, but more negative than -1. ret.stop = end; std::advance( ret.stop, (-i < max_dist) ? i : -max_dist); } } else { // stepping forward if (i == 0 || -i >= max_dist) throw std::invalid_argument( "Zero-length slice"); if (i > 0) { ret.stop = begin; std::advance( ret.stop, (std::min)( i-1, max_dist-1)); } else { // i is negative, but not more negative than -max_dist ret.stop = end; std::advance( ret.stop, i-1); } } } // Now the fun part, handling the possibilites surrounding step. // At this point, step has been initialized, ret.stop, and ret.step // represent the widest possible range that could be traveled // (inclusive), and final_dist is the maximum distance covered by the // slice. typename iterator_difference<RandomAccessIterator>::type final_dist = abt_boost::detail::distance( ret.start, ret.stop); // First case, if both ret.start and ret.stop are equal, then step // is irrelevant and we can return here. if (final_dist == 0) return ret; // Second, if there is a sign mismatch, than the resulting range and // step size conflict: std::advance( ret.start, ret.step) goes away from // ret.stop. if ((final_dist > 0) != (ret.step > 0)) throw std::invalid_argument( "Zero-length slice."); // Finally, if the last step puts us past the end, we move ret.stop // towards ret.start in the amount of the remainder. // I don't remember all of the oolies surrounding negative modulii, // so I am handling each of these cases separately. if (final_dist < 0) { difference_type remainder = -final_dist % -ret.step; std::advance( ret.stop, remainder); } else { difference_type remainder = final_dist % ret.step; std::advance( ret.stop, -remainder); } return ret; }
ssca_iterator& operator++() { BOOST_USING_STD_MIN(); while (values.empty() && verticesRemaining > 0) { // If there are no values left, generate a new clique uniform_int<vertices_size_type> clique_size(1, maxCliqueSize); uniform_int<vertices_size_type> rand_vertex(0, totVertices-1); uniform_int<int> num_parallel_edges(1, maxParallelEdges); uniform_int<short> direction(0,1); uniform_01<RandomGenerator> prob(*gen); std::vector<vertices_size_type> cliqueVertices; cliqueVertices.clear(); vertices_size_type size = min BOOST_PREVENT_MACRO_SUBSTITUTION (clique_size(*gen), verticesRemaining); while (cliqueVertices.size() < size) { vertices_size_type v = rand_vertex(*gen); if (cliqueNum[v] == -1) { cliqueNum[v] = currentClique; cliqueVertices.push_back(v); verticesRemaining--; } } // Nick: This is inefficient when only a few vertices remain... // I should probably just select the remaining vertices // in order when only a certain fraction remain. typename std::vector<vertices_size_type>::iterator first, second; for (first = cliqueVertices.begin(); first != cliqueVertices.end(); ++first) for (second = first+1; second != cliqueVertices.end(); ++second) { Direction d; int edges; d = prob() < probUnidirectional ? (direction(*gen) == 0 ? FORWARD : BACKWARD) : BOTH; if (d & FORWARD) { edges = num_parallel_edges(*gen); for (int i = 0; i < edges; ++i) values.push(std::make_pair(*first, *second)); } if (d & BACKWARD) { edges = num_parallel_edges(*gen); for (int i = 0; i < edges; ++i) values.push(std::make_pair(*second, *first)); } } if (verticesRemaining == 0) { // Generate interclique edges for (vertices_size_type i = 0; i < totVertices; ++i) { double p = probIntercliqueEdges; for (vertices_size_type d = 2; d < totVertices/2; d *= 2, p/= 2) { vertices_size_type j = (i+d) % totVertices; if (cliqueNum[j] != cliqueNum[i] && prob() < p) { int edges = num_parallel_edges(*gen); for (int i = 0; i < edges; ++i) values.push(std::make_pair(i, j)); } } } } currentClique++; } if (!values.empty()) { // If we're not done return a value current = values.front(); values.pop(); } return *this; }