/**
 * Connect two nodes by adding an edge to the graph.
 * 
 * @param graph the graph that the nodes belongs to.
 * @param start the start node
 * @param end the end node
 * @param weight the weight of the connection
 * 
 * @return int success or error (if one of the nodes does not
 *         belong to the graph.
 */
int opal_carto_base_connect_nodes_fn(opal_carto_graph_t *graph, opal_carto_base_node_t *start, opal_carto_base_node_t *end, uint32_t weight)
{
    opal_graph_edge_t *edge;

    /* construct anew edge */
    edge = OBJ_NEW(opal_graph_edge_t);
    /* assigne the start and the end nodes vertices to the new edge */
    edge->start = start->vertex;
    edge->end = end->vertex;
    /* assign the weight to the edge */
    edge->weight = weight;
    /* add the edge to the graph */
    return opal_graph_add_edge((opal_graph_t *)graph, edge);
}
Esempio n. 2
0
/**
 * This graph API duplicates a graph. Note that this API does
 * not copy the graph but builds a new graph while coping just
 * the vertex data.
 *
 * @param dest The new created graph.
 * @param src The graph we want to duplicate.
 */
void opal_graph_duplicate(opal_graph_t **dest, opal_graph_t *src)
{
    opal_adjacency_list_t *aj_list;
    opal_list_item_t *aj_list_item, *edg_item;
    opal_graph_vertex_t *vertex;
    opal_graph_edge_t *edge, *new_edge;

    /* construct a new graph */
    *dest = OBJ_NEW(opal_graph_t);
    /* Run on all the vertices of the src graph */
    for (aj_list_item = opal_list_get_first(src->adjacency_list);
         aj_list_item != opal_list_get_end(src->adjacency_list);
         aj_list_item  = opal_list_get_next(aj_list_item)) {
        aj_list = (opal_adjacency_list_t *) aj_list_item;
        /* for each vertex in the src graph, construct a new vertex */
        vertex = OBJ_NEW(opal_graph_vertex_t);
        /* associate the new vertex to a vertex from the original graph */
        vertex->sibling = aj_list->vertex;
        /* associate the original vertex to the new constructed vertex */
        aj_list->vertex->sibling = vertex;
        /* allocate space to vertex data in the new vertex */
        if (NULL != aj_list->vertex->alloc_vertex_data) {
            vertex->vertex_data = aj_list->vertex->alloc_vertex_data();
            vertex->alloc_vertex_data = aj_list->vertex->alloc_vertex_data;
        }
        /* copy the vertex data from the original vertex  to the new vertex */
        if (NULL != aj_list->vertex->copy_vertex_data) {
            aj_list->vertex->copy_vertex_data(&(vertex->vertex_data), aj_list->vertex->vertex_data);
            vertex->copy_vertex_data = aj_list->vertex->copy_vertex_data;
        }
        /* copy all the fields of the original vertex to the new vertex. */
        vertex->free_vertex_data = aj_list->vertex->free_vertex_data;
        vertex->print_vertex = aj_list->vertex->print_vertex;
        vertex->compare_vertex = aj_list->vertex->compare_vertex;
        vertex->in_graph = *dest;
        /* add the new vertex to the new graph */
        opal_graph_add_vertex(*dest, vertex);
    }
    /**
     * Now, copy all the edges from the source graph
     */
    /* Run on all the adjscency lists in the graph */
    for (aj_list_item = opal_list_get_first(src->adjacency_list);
         aj_list_item != opal_list_get_end(src->adjacency_list);
         aj_list_item  = opal_list_get_next(aj_list_item)) {
        aj_list = (opal_adjacency_list_t *) aj_list_item;
        /* for all the edges in the adjscency list */
        for (edg_item = opal_list_get_first(aj_list->edges);
             edg_item != opal_list_get_end(aj_list->edges);
             edg_item = opal_list_get_next(edg_item)) {
            edge = (opal_graph_edge_t *)edg_item;
            /* construct new edge for the new graph */
            new_edge = OBJ_NEW(opal_graph_edge_t);
            /* copy the edge weight from the original edge */
            new_edge->weight = edge->weight;
            /* connect the new edge according to start and end associations to the vertices of the src graph */
            new_edge->start = edge->start->sibling;
            new_edge->end = edge->end->sibling;
            /* add the new edge to the new graph */
            opal_graph_add_edge(*dest, new_edge);
        }
    }
}