Esempio n. 1
0
TEST_F(TestPathFinder, StringsWithMoreThanOneDiffCharAreNotAdjacent)
{
    EXPECT_FALSE(are_adjacent("abda", "abcb"));
    EXPECT_FALSE(are_adjacent("abca", "abdb"));
    EXPECT_FALSE(are_adjacent("abdaa", "abcba"));
    EXPECT_FALSE(are_adjacent("abcaa", "abdba"));
}
Esempio n. 2
0
TEST_F(TestPathFinder, StringsWithOneDiffCharAreAdjacent)
{
    EXPECT_TRUE(are_adjacent("abd", "abc"));
    EXPECT_TRUE(are_adjacent("abc", "abd"));
    EXPECT_TRUE(are_adjacent("abdaa", "abcaa"));
    EXPECT_TRUE(are_adjacent("abcaa", "abdaa"));
}
Esempio n. 3
0
/**
 * Remove the edge connecting n to m. Will fail if are_adjacent() returns false.
 *
 * @param[in] n index of the first vertex.
 * @param[in] m index of the second vertex.
 */
void base::remove_edge(const vertices_size_type &n, const vertices_size_type &m)
{
	if (!are_adjacent(n,m)) {
		pagmo_throw(value_error,"cannot remove edge, vertices are not connected");
	}
	boost::remove_edge(boost::vertex(n,m_graph),boost::vertex(m,m_graph),m_graph);
}
Esempio n. 4
0
/**
 * Add an edge connecting n to m. Will fail if are_adjacent() returns true.
 *
 * @param[in] n index of the first vertex.
 * @param[in] m index of the second vertex.
 */
void base::add_edge(const vertices_size_type &n, const vertices_size_type &m)
{
	if (are_adjacent(n,m)) {
		pagmo_throw(value_error,"cannot add edge, vertices are already connected");
	}
	const std::pair<e_descriptor,bool> result = boost::add_edge(boost::vertex(n,m_graph),boost::vertex(m,m_graph),m_graph);
	pagmo_assert(result.second);
	// Assign weight 1 to the edge.
	boost::property_map<graph_type,boost::edge_weight_t>::type w = boost::get(boost::edge_weight,m_graph);
	w[result.first] = 1;
}
void Vertex_visibility_graph_2<Traits>::handle(Tree_iterator p, 
                                        Tree_iterator q, 
                                        const Polygon& polygon,
                                        Vertex_map& vertex_map)
{
#ifdef CGAL_VISIBILITY_GRAPH_DEBUG
      std::cout << "Handling edge from " << (*p).x() << " " << (*p).y() 
                << " to " << (*q).x() << " " << (*q).y() << std::endl;
#endif
   Vertex_map_iterator p_it = vertex_map.find(*p);
   Vertex_map_iterator q_it = vertex_map.find(*q);
   CGAL_assertion (p_it != vertex_map.end());
   CGAL_assertion (q_it != vertex_map.end());
#ifdef CGAL_VISIBILITY_GRAPH_DEBUG
   std::cout << "p currently sees : ";
   if ((*p_it).second.second != polygon.end())
      std::cout << *((*p_it).second.second) << endl;
   else
      std::cout << " NADA" << endl;
#endif

   // if p and q are adjacent
   if (are_adjacent(polygon, (*p_it).second.first, (*q_it).second.first))
   {
#ifdef CGAL_VISIBILITY_GRAPH_DEBUG
      cout << "are adjacent" << endl;
#endif
      insert_edge(Point_pair(*p,*q));
      update_visibility(p_it, q_it, polygon, 1);
   }
   else 
   {
      bool interior_at_p = diagonal_in_interior(polygon, (*p_it).second.first,
                                                (*q_it).second.first);
      bool interior_at_q = diagonal_in_interior(polygon, (*q_it).second.first,
                                                (*p_it).second.first);
      // line of site is through the interior of the polygon
      if (interior_at_p && interior_at_q)
      {
#ifdef CGAL_VISIBILITY_GRAPH_DEBUG
         cout << "both interior" << endl;
#endif
         // if p sees something and q is visible only through collinear
         // points then update p's visibility if one of the points adjacent
         // to q is above the line unless p's current visibility point 
         // obscures the view.
         if ((*p_it).second.second != polygon.end() &&
             are_strictly_ordered_along_line_2((*p_it).first,
                                             *(*p_it).second.second,
                                                (*q_it).first))
         {
            update_collinear_visibility(p_it, q_it, polygon);
         }
         // p current sees nothing or q is visible to p
         else if ((*p_it).second.second == polygon.end() ||
                  point_is_visible(polygon, (*q_it).second.first, p_it))
         {
            insert_edge(Point_pair(*p,*q));
            update_visibility(p_it, q_it, polygon, 0);
         }
      }
      else if (!interior_at_p && !interior_at_q) // both points exterior
      {
#ifdef CGAL_VISIBILITY_GRAPH_DEBUG
            cout << "both exterior" << endl;
#endif
         // p currently sees nothing or q is visible to p
         if ((*p_it).second.second == polygon.end() ||
             point_is_visible(polygon, (*q_it).second.first, p_it))
         {
            (*p_it).second.second = (*q_it).second.first;
         }
      }
   }
#ifdef CGAL_VISIBILITY_GRAPH_DEBUG
   std::cout << "p now sees : ";
   if ((*p_it).second.second != polygon.end())
      std::cout << *((*p_it).second.second) << endl;
   else
      std::cout << " NADA" << endl;
#endif
}
Esempio n. 6
0
void clustered_ba::connect(const vertices_size_type &idx)
{
	pagmo_assert(get_number_of_vertices() > 0);
	const vertices_size_type prev_size = get_number_of_vertices() - 1;
	if (prev_size < m_m0) {
		// If we had not built the initial m0 nodes, do it.
		// We want to connect the newcomer island with high probability, and make sure that
		// at least one connection exists (otherwise the island stays isolated).
		// NOTE: is it worth to make it a user-tunable parameter?
                const double prob = 0.0;
		// Flag indicating if at least 1 connection was added.
		bool connection_added = false;
		// Main loop.
		for (std::pair<v_iterator,v_iterator> vertices = get_vertices(); vertices.first != vertices.second; ++vertices.first) {
			// Do not consider the new vertex itself.
			if (*vertices.first != idx) {
				if (m_drng() < prob) {
					connection_added = true;
					// Add the connections
					add_edge(*vertices.first,idx);
					add_edge(idx,*vertices.first);
				}
			}
		}
		// If no connections were established and this is not the first island being inserted,
		// establish at least one connection with a random island other than n.
		if ((!connection_added) && (prev_size != 0)) {
			// Get a random vertex index between 0 and n_vertices - 1. Keep on repeating the procedure if by
			// chance we end up on idx again.
			boost::uniform_int<vertices_size_type> uni_int(0,get_number_of_vertices() - 1);
			vertices_size_type rnd;
                        do {
				rnd = uni_int(m_urng);
			} while (rnd == idx);
			// Add connections to the random vertex.
			add_edge(rnd,idx);
			add_edge(idx,rnd);
		}
	} else {
                // Now we need to add j edges, choosing the nodes with a probability
		// proportional to their number of connections. We keep track of the
		// connection established in order to avoid connecting twice to the same
		// node.
                // j is a random integer in the range 1 to m.
                boost::uniform_int<edges_size_type> uni_int2(1,m_m);
		std::size_t i = 0;
                std::size_t j = uni_int2(m_urng);
		std::pair<v_iterator,v_iterator> vertices;
		std::pair<a_iterator,a_iterator> adj_vertices;
                while (i < j) {
                        // Let's find the current total number of edges.
                        const edges_size_type n_edges = get_number_of_edges();
                        pagmo_assert(n_edges > 0);
                        boost::uniform_int<edges_size_type> uni_int(0,n_edges - 1 - i);
                        // Here we choose a random number between 0 and n_edges - 1 - i.
                        const edges_size_type rn = uni_int(m_urng);
                        edges_size_type n = 0;
			// Iterate over all vertices and accumulate the number of edges for each of them. Stop when the accumulated number of edges is greater
			// than rn. This is equivalent to giving a chance of connection to vertex v directly proportional to the number of edges departing from v.
			// You can think of this process as selecting a random edge among all the existing edges and connecting to the vertex from which the
			// selected edge departs.
			vertices = get_vertices();
			for (; vertices.first != vertices.second; ++vertices.first) {
				// Do not consider it_n.
				if (*vertices.first != idx) {
					adj_vertices = get_adjacent_vertices(*vertices.first);
					n += boost::numeric_cast<edges_size_type>(std::distance(adj_vertices.first,adj_vertices.second));
					if (n > rn) {
						break;
					}
				}
			}
			pagmo_assert(vertices.first != vertices.second);
			// If the candidate was not already connected, then add it.
			if (!are_adjacent(idx,*vertices.first)) {
                                // Connect to nodes that are already adjacent to idx with probability p.
                                // This step increases clustering in the network.
                                adj_vertices = get_adjacent_vertices(idx);
                                for(;adj_vertices.first != adj_vertices.second; ++adj_vertices.first) {
                                    if(m_drng() < m_p && *adj_vertices.first != *vertices.first && !are_adjacent(*adj_vertices.first,*vertices.first)) {
                                        add_edge(*adj_vertices.first, *vertices.first);
                                        add_edge(*vertices.first, *adj_vertices.first);
                                    }
                                }
                                // Connect to idx
				add_edge(*vertices.first,idx);
				add_edge(idx,*vertices.first);
				++i;
			}
		}
	}
}
Esempio n. 7
0
TEST_F(TestPathFinder, EqualStringsAreNotAdjacent)
{
    EXPECT_FALSE(are_adjacent("abc", "abc"));
}
Esempio n. 8
0
TEST_F(TestPathFinder, StringsWithDiffLenAreNotAdjacent)
{
    EXPECT_FALSE(are_adjacent("ab", "abc"));
}
Esempio n. 9
0
TEST_F(TestPathFinder, EmptyStringsAndNonEmptyStringAreNotAdjacent)
{
    EXPECT_FALSE(are_adjacent("a", ""));
    EXPECT_FALSE(are_adjacent("", "b"));
}