Esempio n. 1
0
def test_dijkstra():
    import boost.graph as bgl
    graph = bgl.Graph()
    a = graph.add_vertex()
    b = graph.add_vertex()
    e = graph.add_edge(a, b)
    distances = graph.add_vertex_property('float')
    predecessors = graph.add_vertex_property('vertex')
    bgl.dijkstra_shortest_paths(
            graph,
            a,
            predecessor_map = predecessors,
            distance_map = distances
    )
Esempio n. 2
0
def get_shortest_distances(g, vertices=None):
    "The shortest distance between the given vertices and all others in the graph"
    import boost.graph as bgl
    if None == vertices: vertices = g.vertices
    predecessor_map = g.add_vertex_property('predecessor', 'vertex')
    distance_map = g.add_vertex_property('distance', 'float')
    index_map = g.add_vertex_property('index', 'index')
    result = dict((index_map[v], dict()) for v in g.vertices)
    for v1 in vertices:
        if v1 in result: continue  # avoid duplicating work
        bgl.dijkstra_shortest_paths(g, v1, predecessor_map, distance_map)
        for v2 in g.vertices:
            result[index_map[v1]][index_map[v2]] = distance_map[v2]
    del g.vertex_properties['predecessor']
    del g.vertex_properties['distance']
    return result
Esempio n. 3
0
def dijkstraBoost(boostGraph, start):
    from boost.graph import dijkstra_shortest_paths
    dijkstra_shortest_paths(boostGraph, start,
                            distance_map = boostGraph.vertex_properties['distance'], 
                            predecessor_map = boostGraph.vertex_properties['predecessor'], 
                            weight_map = boostGraph.edge_properties['weight'])
    
    # dictionary of final distances
    D = {}
    # dictionary of predecessors
    P = {}
    for v in boostGraph.vertices:
        D[v.partner] = v.distance
        for edge in v.partner.inEdges:
            if edge.source == v.predecessor.partner:
                P[v.partner] = edge
                break
    return (D, P)
Esempio n. 4
0
def dijkstraBoost(boostGraph, start):
    from boost.graph import dijkstra_shortest_paths
    dijkstra_shortest_paths(
        boostGraph,
        start,
        distance_map=boostGraph.vertex_properties['distance'],
        predecessor_map=boostGraph.vertex_properties['predecessor'],
        weight_map=boostGraph.edge_properties['weight'])

    # dictionary of final distances
    D = {}
    # dictionary of predecessors
    P = {}
    for v in boostGraph.vertices:
        D[v.partner] = v.distance
        for edge in v.partner.inEdges:
            if edge.source == v.predecessor.partner:
                P[v.partner] = edge
                break
    return (D, P)
Esempio n. 5
0
def get_shortest_distances( g, vertices = None ):
    "The shortest distance between the given vertices and all others in the graph"
    import boost.graph as bgl
    if None == vertices: vertices = g.vertices
    predecessor_map = g.add_vertex_property( 'predecessor', 'vertex' )
    distance_map = g.add_vertex_property( 'distance', 'float' )
    index_map = g.add_vertex_property( 'index', 'index' )
    result = dict( (index_map[ v ], dict()) for v in g.vertices )
    for v1 in vertices:
        if v1 in result: continue # avoid duplicating work
        bgl.dijkstra_shortest_paths(
                g,
                v1,
                predecessor_map,
                distance_map
        )
        for v2 in g.vertices:
            result[ index_map[ v1 ] ][ index_map[ v2 ] ] = distance_map[ v2 ]
    del g.vertex_properties[ 'predecessor' ]
    del g.vertex_properties[ 'distance' ]
    return result
Esempio n. 6
0
# Copyright (C) 2006 The Trustees of Indiana University.

# Use, modification and distribution is subject to the Boost Software
# License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
# http:#www.boost.org/LICENSE_1_0.txt)

#  Authors: Douglas Gregor
#           Andrew Lumsdaine
import boost.graph as bgl

# Load a graph from the GraphViz file 'mst.dot'
graph = bgl.Graph.read_graphviz('mst.dot')

# Compute the shortest paths from the first vertex ('A')
A = graph.vertices.next()
bgl.dijkstra_shortest_paths(
    graph,
    A,
    distance_map=graph.add_vertex_property('distance'),
    predecessor_map=graph.add_vertex_property('predecessor'),
    weight_map=graph.edge_properties['weight'])

# Print out the shortest paths tree
for v in graph.vertices:
    print v.node_id, 'distance from A = ', v.distance,
    print ', predecessor = ', v.predecessor.node_id
Esempio n. 7
0
# Copyright (C) 2006 The Trustees of Indiana University.

# Use, modification and distribution is subject to the Boost Software
# License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
# http:#www.boost.org/LICENSE_1_0.txt)

#  Authors: Douglas Gregor
#           Andrew Lumsdaine
import boost.graph as bgl

# Load a graph from the GraphViz file 'mst.dot'
graph = bgl.Graph.read_graphviz('mst.dot')

# Compute the shortest paths from the first vertex ('A')
A = graph.vertices.next()
bgl.dijkstra_shortest_paths(graph, A,
                            distance_map = graph.add_vertex_property('distance') , 
                            predecessor_map = graph.add_vertex_property('predecessor'), 
                            weight_map = graph.edge_properties['weight'])

# Print out the shortest paths tree
for v in graph.vertices:
    print v.node_id,'distance from A = ',v.distance,
    print ', predecessor = ', v.predecessor.node_id