/**
 * opal_carto_base_get_nodes_distance - returns the distance of
 * all the nodes from the reference node.
 * 
 * @param graph
 * @param reference_node
 * @param node_type the type of the nodes in the returned array
 * @param dist_array
 * 
 * @return int number of nodes in the returned array.
 */
int opal_carto_base_get_nodes_distance_fn(opal_carto_graph_t *graph, opal_carto_base_node_t *reference_node, 
                                       const char *node_type, opal_value_array_t *dist_array)
{
    opal_value_array_t *distance_array;
    vertex_distance_from_t *vertex_distance;
    opal_carto_base_node_t *node;
    uint32_t i, graph_order;
    int distance_array_size;
    opal_carto_node_distance_t node_distance;


    distance_array = OBJ_NEW(opal_value_array_t);
    opal_value_array_init(distance_array, sizeof(vertex_distance_from_t));
    opal_value_array_reserve(distance_array,50);
    /* use dijkstra algorithm to receive the distance of all the nodes from the referenced node */
    graph_order = opal_graph_dijkstra(graph, reference_node->vertex, distance_array);
    /* for all the nodes in the dijkstra array */
    for (i = 0, distance_array_size = 0; i < graph_order; i++) {
        vertex_distance = opal_value_array_get_item(distance_array, i);
        node = vertex_distance->vertex->vertex_data;
        /* check if the node is in the correct type */
        if (NULL == node_type || 0 == strcmp(node->node_type, node_type)) {
            /* assigne the result distance array */
            node_distance.node = vertex_distance->vertex->vertex_data;
            node_distance.node_distance = vertex_distance->weight;
            opal_value_array_append_item(dist_array, (void *)&node_distance);
        }
    }
    /* return the result distance array */
    return distance_array_size;
}
Exemple #2
0
uint32_t opal_graph_spf(opal_graph_t *graph, opal_graph_vertex_t *vertex1, opal_graph_vertex_t *vertex2)
{
    opal_value_array_t *distance_array;
    uint32_t items_in_distance_array, spf = DISTANCE_INFINITY;
    vertex_distance_from_t *vertex_distance;
    uint32_t i;

    /**
     * Verify that the first vertex belongs to the graph.
     */
    if (graph != vertex1->in_graph) {
        OPAL_OUTPUT((0,"opal_graph_spf 1 Vertex1 %p not in the graph %p\n",(void *)vertex1,(void *)graph));
        return DISTANCE_INFINITY;
    }
    /**
     * Verify that the second vertex belongs to the graph.
     */
    if (graph != vertex2->in_graph) {
        OPAL_OUTPUT((0,"opal_graph_spf 2 Vertex2 %p not in the graph %p\n",(void *)vertex2,(void *)graph));
        return DISTANCE_INFINITY;
    }
    /**
     * Run Dijkstra algorithm on the graph from the start vertex.
     */
    distance_array = OBJ_NEW(opal_value_array_t);
    opal_value_array_init(distance_array, sizeof(vertex_distance_from_t));
    opal_value_array_reserve(distance_array,50);
    items_in_distance_array = opal_graph_dijkstra(graph, vertex1, distance_array);
    /**
     * find the end vertex in the distance array that Dijkstra
     * algorithm returned.
     */
    for (i = 0; i < items_in_distance_array; i++) {
        vertex_distance = opal_value_array_get_item(distance_array, i);
        if (vertex_distance->vertex == vertex2) {
            spf = vertex_distance->weight;
            break;
        }
    }
    OBJ_RELEASE(distance_array);
    /* return the distance (weight) to the end vertex */
    return spf;
}