/** * \function igraph_is_matching * Checks whether the given matching is valid for the given graph. * * This function checks a matching vector and verifies whether its length * matches the number of vertices in the given graph, its values are between * -1 (inclusive) and the number of vertices (exclusive), and whether there * exists a corresponding edge in the graph for every matched vertex pair. * For bipartite graphs, it also verifies whether the matched vertices are * in different parts of the graph. * * \param graph The input graph. It can be directed but the edge directions * will be ignored. * \param types If the graph is bipartite and you are interested in bipartite * matchings only, pass the vertex types here. If the graph is * non-bipartite, simply pass \c NULL. * \param matching The matching itself. It must be a vector where element i * contains the ID of the vertex that vertex i is matched to, * or -1 if vertex i is unmatched. * \param result Pointer to a boolean variable, the result will be returned * here. * * \sa \ref igraph_is_maximal_matching() if you are also interested in whether * the matching is maximal (i.e. non-extendable). * * Time complexity: O(|V|+|E|) where |V| is the number of vertices and * |E| is the number of edges. * * \example examples/simple/igraph_maximum_bipartite_matching.c */ int igraph_is_matching(const igraph_t* graph, const igraph_vector_bool_t* types, const igraph_vector_long_t* matching, igraph_bool_t* result) { long int i, j, no_of_nodes = igraph_vcount(graph); igraph_bool_t conn; /* Checking match vector length */ if (igraph_vector_long_size(matching) != no_of_nodes) { *result = 0; return IGRAPH_SUCCESS; } for (i = 0; i < no_of_nodes; i++) { j = VECTOR(*matching)[i]; /* Checking range of each element in the match vector */ if (j < -1 || j >= no_of_nodes) { *result = 0; return IGRAPH_SUCCESS; } /* When i is unmatched, we're done */ if (j == -1) continue; /* Matches must be mutual */ if (VECTOR(*matching)[j] != i) { *result = 0; return IGRAPH_SUCCESS; } /* Matched vertices must be connected */ IGRAPH_CHECK(igraph_are_connected(graph, (igraph_integer_t) i, (igraph_integer_t) j, &conn)); if (!conn) { /* Try the other direction -- for directed graphs */ IGRAPH_CHECK(igraph_are_connected(graph, (igraph_integer_t) j, (igraph_integer_t) i, &conn)); if (!conn) { *result = 0; return IGRAPH_SUCCESS; } } } if (types != 0) { /* Matched vertices must be of different types */ for (i = 0; i < no_of_nodes; i++) { j = VECTOR(*matching)[i]; if (j == -1) continue; if (VECTOR(*types)[i] == VECTOR(*types)[j]) { *result = 0; return IGRAPH_SUCCESS; } } } *result = 1; return IGRAPH_SUCCESS; }
bool Graph::areConnected(long int u, long int v) const { igraph_bool_t result; assert(m_pGraph); IGRAPH_TRY(igraph_are_connected(m_pGraph, u, v, &result)); return result; }
static void SF2ER (igraph_t *graph, int N, int param, double alpha, bool isDirected) { int i, j, k, m; igraph_bool_t areconn; vector<int> p (N, 0); //quantidade de arestas PA int etot = 0, acc; igraph_empty(graph, N, isDirected); for (i = 0; i < 2*param+1; i++) for (j = i+1; j < 2*param+1; j++) { igraph_add_edge (graph, i, j); p[j]++; if (!isDirected) p[i]++; etot += 1+(!isDirected); } for (; i < N; i++) { for (m = param; m > 0; m--) { if (Statistic::unif() < alpha) { //ER do { j = rand()%(N-1); if (j >= i) j++; igraph_are_connected(graph, i, j, &areconn); //printf ("er %d\n", (int)areconn); } while (areconn == 1); igraph_add_edge (graph, i, j); } else { //SF do { k = rand()%(etot-p[i]); //printf ("%d %d %d\n", k, etot, p[i]); acc = 0; for (j = 0; j < N; j++) { if (i == j) continue; if (acc+p[j] >= k) break; acc+=p[j]; } igraph_are_connected(graph, i, j, &areconn); //printf ("sf %d %d %d\n",k, (int)areconn, etot); } while (areconn == 1); igraph_add_edge (graph, i, j); p[j]++; if (!isDirected) p[i]++; etot += 1+(!isDirected); } } } }
bool Graph::areConnected(int i, int j) { igraph_bool_t res; igraph_are_connected(graph, i, j,&res); return bool(res); }