Пример #1
0
unsigned int graph_connected_components(graph_t g) {
    unsigned int classes_count = 0, n, m, left, right;
    union_find_t uf;
    edge_t *edges;

    n = graph_vertices_count(g);
    m = graph_edges_count(g);
    uf = union_find_create(n);

    edges = graph_edges(g);
    for (unsigned int i = 0; i < m; i++) {
        left = vertex_label(edge_left_vertex(edges[i]));
        right = vertex_label(edge_right_vertex(edges[i]));
        left = union_find_find(uf, left);
        right = union_find_find(uf, right);
        if (!union_find_connected(uf, left, right)) {
            union_find_union(uf, left, right);
        }
        edges[i] = edge_destroy(edges[i]);
    }
    free(edges);
    classes_count = union_find_count(uf);
    uf = union_find_destroy(uf);

    return (classes_count);
}
int main() {
	unsigned int maxSize = 10;
	union_find_t uf = union_find_create(maxSize);
	bool exit = false;
	char *option = NULL;
	unsigned int u,v;
	printf("%u cantidad de nodos.\n",union_find_count(uf));
	do {
		option = print_menu();
		switch(*option) {
			case FIND:
				printf("\nPor favor ingrese el nodo a buscar: ");
				scanf("%u",&u);
				printf("\nEl representante es %u.\n",union_find_find(uf, u));
				break;
			case UNION:
				printf("\nPor favor ingrese los conjuntos a unir: ");
				scanf("%u %u",&u,&v);
				union_find_union(uf, u, v);
				break;
			case CONNECT:
				printf("\nPor favor ingrese los nodos: ");
				scanf("%u %u",&u,&v);
				if(union_find_connected(uf, u, v))
					printf("TRUE\n");
				else
					printf("FALSE\n");
				break;
			case EXIT:
				exit = true;
				break;
			
		}
		free(option);
        option = NULL;
	} while(!exit);
	uf = union_find_destroy(uf);

}
Пример #3
0
graph_t kruskal(graph_t graph) {
/* Computes a MST of the given graph.
 *
 * This function returns a copy of the input graph in which
 * only the edges of the MST are marked as primary. The
 * remaining edges are marked as secondary. 
 *
 * The input graph does not change. 
 * 
*/
    graph_t mst;
    union_find_t uf;
    pqueue_t pq;
    edge_t *edges;
    edge_t e;
    unsigned int left, right, n, m;

    /* Inicialización */
    n = graph_vertices_count(graph);
    m = graph_edges_count(graph);
    mst = graph_empty(n);
    uf = union_find_create(n);
    pq = pqueue_empty(m);

    /* Llenar `pq` */
    edges = graph_edges(graph);
    for (unsigned int j = 0; j < m; j++) {
        pqueue_enqueue(pq, edges[j]);
    }
    /* Ahora las aristas están en `pq` */
    free(edges);
    edges = NULL;


    /* Principal */
    while (!pqueue_is_empty(pq) && union_find_count(uf) > 1) {
        e = edge_copy(pqueue_fst(pq));
        left = vertex_label(edge_left_vertex(e));
        right = vertex_label(edge_right_vertex(e));
        left = union_find_find(uf, left);
        right = union_find_find(uf, right);
        if (!union_find_connected(uf, left, right)) {
            e = edge_set_primary(e, true);
            union_find_union(uf, left, right);
        } else {
            e = edge_set_primary(e, false);
        }
        mst = graph_add_edge(mst, e);
        pqueue_dequeue(pq);
    }

    /* Agregar aristas restantes como secundarias */
    while (!pqueue_is_empty(pq)) {
        e = edge_copy(pqueue_fst(pq));
        e = edge_set_primary(e, false);
        mst = graph_add_edge(mst, e);
        pqueue_dequeue(pq);
    }

    /* Destroy */
    uf = union_find_destroy(uf);
    pq = pqueue_destroy(pq);

    return (mst);
}