Removed ancient BGL-Python code

[SVN r40812]
This commit is contained in:
Douglas Gregor 2007-11-05 21:25:10 +00:00
parent eb60f8c235
commit c09b24159b
53 changed files with 0 additions and 4990 deletions

View File

@ -1,61 +0,0 @@
# Copyright 2005 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
subproject libs/graph/build/python ;
SEARCH on python.jam = $(BOOST_BUILD_PATH) ;
include python.jam ;
extension bgl
: # Sources
../../src/python/module.cpp
# Graph types
../../src/python/graph.cpp
../../src/python/digraph.cpp
# Graph I/O
../../src/python/graphviz.cpp
# Graph algorithms
# Core Algorithm Patterns
../../src/python/breadth_first_search.cpp
../../src/python/depth_first_search.cpp
# Shortest Paths Algorithms
../../src/python/dijkstra_shortest_paths.cpp
../../src/python/bellman_ford_shortest_paths.cpp
../../src/python/dag_shortest_paths.cpp
# Minimum Spanning Tree Algorithms
../../src/python/kruskal_min_spanning_tree.cpp
../../src/python/prim_minimum_spanning_tree.cpp
# Connected Components Algorithms
../../src/python/connected_components.cpp
../../src/python/strong_components.cpp
../../src/python/biconnected_components.cpp
../../src/python/incremental_components.cpp
# ...
# Other algorithms
../../src/python/topological_sort.cpp
../../src/python/transitive_closure.cpp
# ../../src/python/transpose_graph.cpp Need copy_graph to work, first
../../src/python/isomorphism.cpp
../../src/python/betweenness_centrality.cpp
../../src/python/sequential_vertex_coloring.cpp
# Sparse Matrix Ordering
../../src/python/cuthill_mckee_ordering.cpp
../../src/python/king_ordering.cpp
# ../../src/python/minimum_degree_ordering.cpp
../../src/python/circle_layout.cpp
../../src/python/fruchterman_reingold.cpp
../../src/python/kamada_kawai_spring_layout.cpp
../../src/python/page_rank.cpp
# Pickling support
../../src/python/pickle.cpp
<dll>../../../python/build/boost_python
<lib>../bgl-viz
: # Default build
<debug-symbols>off
;

View File

@ -1,13 +0,0 @@
graph G {
A -- B
A -- F
A -- G
B -- C
B -- D
B -- E
C -- D
E -- F
G -- H
G -- I
H -- I
}

View File

@ -1,21 +0,0 @@
# Copyright 2005 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 bgl
g = bgl.Graph("biconnected_components.dot", bgl.file_kind.graphviz)
art_points = bgl.biconnected_components(g, g.get_edge_int_map("label"));
g.write_graphviz("biconnected_components_out.dot")
print "Articulation points: ",
node_id = g.get_vertex_string_map("node_id")
for v in art_points:
print node_id[v],
print " ",
print ""

View File

@ -1,15 +0,0 @@
from bgl import *
class bfs_print_discover_visitor(Graph.BFSVisitor):
def bfs_print_discover_visitor(self, dtime_map):
Graph.BFSVisitor.__init__(self)
def discover_vertex(self, u, g):
print "Discovered vertex ",
print u
g = Graph((("r", "s"), ("r", "v"), ("s", "w"), ("w", "r"), ("w", "t"), ("w", "x"), ("x", "t"), ("t", "u"), ("x", "y"), ("u", "y")), "label")
breadth_first_search(g, s, visitor=bfs_print_discover_visitor())

View File

@ -1,10 +0,0 @@
graph G {
A -- F
B -- C
B -- D
C -- D
E -- F
G -- H
G -- I
H -- I
}

View File

@ -1,10 +0,0 @@
graph G {
A -- C [weight="1.0"]
B -- D [weight="1.0"]
B -- E [weight="2.0"]
C -- B [weight="7.0"]
C -- D [weight="3.0"]
D -- E [weight="1.0"]
E -- A [weight="1.0"]
E -- B [weight="1.0"]
}

View File

@ -1,706 +0,0 @@
import bgl
import wx
import wx.lib.ogl as ogl
# import wx.lib.masked
import os
import random
import sys
wildcard = "GraphViz (*.dot)|*.dot|" \
"All files (*.*)|*.*"
def path_to_title(path):
title = path
dot_idx = title.rfind('.')
if dot_idx != -1: title = title[:dot_idx]
slash_idx = title.rfind('/')
if slash_idx != -1: title = title[slash_idx+1:]
slash_idx = title.rfind('\\')
if slash_idx != -1: title = title[slash_idx+1:]
return title
class GraphCanvas(ogl.ShapeCanvas):
def __init__(self, parent):
ogl.ShapeCanvas.__init__(self, parent)
maxWidth = 400
maxHeight = 400
self.SetScrollbars(20, 20, maxWidth/20, maxHeight/20)
def set_graph(self, graph, position_map, property_maps):
self.diagram = ogl.Diagram()
self.SetDiagram(self.diagram)
self.diagram.SetCanvas(self)
self.graph = graph
self.position_map = position_map
self.property_maps = property_maps
self.vertex_position_rect = self.compute_rect()
self.vertex_to_shape = graph.get_vertex_object_map("__stored_shape")
self.edge_to_shape = graph.get_edge_object_map("__stored_shape")
self.shape_to_vertex = {}
for v in graph.vertices:
self.add_vertex(v)
for e in graph.edges:
self.add_edge(e)
self.Refresh()
def compute_rect(self):
pos = self.position_map
if self.graph.num_vertices() == 0:
return (-50, -50, 50, 50)
else:
left, top, right, bottom = 0, 0, 0, 0
for v in self.graph.vertices:
if pos[v].x < left: left = pos[v].x
if pos[v].y < top: top = pos[v].y
if pos[v].x > right: right = pos[v].x
if pos[v].y > bottom: bottom = pos[v].y
return (left, top, right, bottom)
def update_layout(self):
self.vertex_position_rect = self.compute_rect()
dc = wx.ClientDC(self)
self.PrepareDC(dc)
for v in self.graph.vertices:
shape = self.vertex_to_shape[v]
x, y = self.vertex_position_to_shape_position(v)
shape.Move(dc, x, y, False)
self.Refresh()
def vertex_position_to_shape_position(self, vertex):
(width, height) = self.GetVirtualSize()
width = width - 20
height = height - 20
(pos_width, pos_height) = (self.vertex_position_rect[2]
- self.vertex_position_rect[0],
self.vertex_position_rect[3]
- self.vertex_position_rect[1])
return ((self.position_map[vertex].x - self.vertex_position_rect[0])
/ pos_width * width + 10,
(self.position_map[vertex].y - self.vertex_position_rect[1])
/ pos_height * height + 10);
def translate_color(self, color):
if color=="black": return wx.BLACK
elif color=="blue": return wx.BLUE
elif color=="red": return wx.RED
elif color=="green": return wx.GREEN
else: return wx.BLACK
def add_vertex(self, vertex):
shape = self.CreateVertex(vertex)
shape.SetDraggable(True, True)
shape.SetCanvas(self)
x, y = self.vertex_position_to_shape_position(vertex)
shape.SetX(x)
shape.SetY(y)
shape.SetPen(self.VertexPen(vertex))
shape.SetBrush(self.VertexBrush(vertex))
s = self.VertexLabel(vertex)
if s != "": shape.AddText(s)
self.diagram.AddShape(shape)
shape.Show(True)
self.vertex_to_shape[vertex] = shape
self.shape_to_vertex[shape] = vertex
evthandler = VertexEventHandler(self)
evthandler.SetShape(shape)
evthandler.SetPreviousHandler(shape.GetEventHandler())
shape.SetEventHandler(evthandler)
return shape;
def CreateVertex(self, vertex):
return ogl.CircleShape(20)
def VertexPen(self, vertex):
thickness = 1
color = wx.BLACK
if "vertex.border.thickness" in self.property_maps:
thickness = self.property_maps["vertex.border.thickness"][vertex]
if "vertex.border.color" in self.property_maps:
color_text = self.property_maps["vertex.border.color"][vertex]
color = translate_color(color_text)
return wx.Pen(color, thickness)
def VertexBrush(self, vertex):
return wx.GREEN_BRUSH
def VertexLabel(self, vertex):
if "vertex.label" in self.property_maps:
return self.property_maps["vertex.label"][vertex]
else:
return ""
def VertexShape(self, vertex):
return self.vertex_to_shape[vertex]
def add_edge(self, edge):
(u, v) = (self.graph.source(edge), self.graph.target(edge))
line = ogl.LineShape()
line.SetCanvas(self)
line.SetPen(wx.BLACK_PEN)
line.SetBrush(wx.BLACK_BRUSH)
if self.graph.is_directed(): line.AddArrow(ogl.ARROW_ARROW)
if "edge.label" in self.property_maps:
label = str(self.property_maps["edge.label"][edge])
line.AddText(label)
line.MakeLineControlPoints(2)
self.vertex_to_shape[u].AddLine(line, self.vertex_to_shape[v])
self.diagram.AddShape(line)
line.Show(True)
self.edge_to_shape[edge] = line
return line
def EdgeShape(self, edge):
return self.edge_to_shape[edge]
def set_vertex_colors_from_components(self, component_map):
brushes = {}
for v in self.graph.vertices:
shape = self.vertex_to_shape[v]
comp = component_map[v]
if not comp in brushes:
brushes[comp] = wx.Brush(wx.Color(random.randint(0, 200),
random.randint(0, 200),
random.randint(0, 200)))
shape.SetBrush(brushes[comp])
self.Refresh()
def set_edge_colors_from_components(self, component_map):
pens = {}
for e in self.graph.edges:
shape = self.edge_to_shape[e]
comp = component_map[e]
if not comp in pens:
pens[comp] = wx.Pen(wx.Color(random.randint(0, 200),
random.randint(0, 200),
random.randint(0, 200)),
1)
shape.SetPen(pens[comp])
self.Refresh()
def default_property_maps(self, graph, node_id = "node_id"):
maps = {}
if graph.has_vertex_map("label"):
maps["vertex.label"] = graph.get_vertex_string_map("label")
elif graph.has_vertex_map(node_id):
maps["vertex.label"] = graph.get_vertex_string_map(node_id)
if graph.has_edge_map("label"):
maps["edge.label"] = graph.get_edge_string_map("label")
elif graph.has_edge_map("weight"):
maps["edge.label"] = graph.get_edge_double_map("weight")
return maps
class VertexEventHandler(ogl.ShapeEvtHandler):
def __init__(self, graphwin):
ogl.ShapeEvtHandler.__init__(self)
self.graphwin = graphwin
def OnEndDragLeft(self, x, y, keys=0, attachment=0):
shape = self.GetShape()
if shape.Selected():
vertex = self.graphwin.shape_to_vertex[shape]
self.graphwin.position_map[vertex].x = x
self.graphwin.position_map[vertex].y = y
ogl.ShapeEvtHandler.OnEndDragLeft(self, x, y, keys, attachment)
class ErdosRenyiDialog(wx.Dialog):
def __init__(
self, parent, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition,
style=wx.DEFAULT_DIALOG_STYLE
):
wx.Dialog.__init__(self, parent, ID, title, pos, size, style,
"Erdos-Renyi Generator")
sizer = wx.BoxSizer(wx.VERTICAL)
box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(wx.StaticText(self, -1, "Number of vertices (n)"), 0,
wx.ALIGN_CENTRE|wx.ALL, 5)
num_vertices_spin = wx.SpinCtrl(self, -1, "", (30, 50))
num_vertices_spin.SetRange(0,10000000)
num_vertices_spin.SetValue(10)
box.Add(num_vertices_spin, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
self.num_vertices_spin = num_vertices_spin
sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(wx.StaticText(self, -1, "Edge probability (%)"), 0,
wx.ALIGN_CENTRE|wx.ALL, 5)
# This is better than what we're currently doing with a SpinCtrl, but
# it makes the program unstable (?)
# probability_ctrl = wx.lib.masked.numctrl.NumCtrl(self, value = 0.2,
# integerWidth = 1,
# fractionWidth = 3,
# allowNegative = False,
# min = 0.0, max = 1.0)
probability_ctrl = wx.SpinCtrl(self, -1, "", (30, 50))
probability_ctrl.SetRange(0,100)
probability_ctrl.SetValue(20)
self.probability_ctrl = probability_ctrl
box.Add(probability_ctrl, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(wx.StaticText(self, -1, "Random seed"), 0,
wx.ALIGN_CENTRE|wx.ALL, 5)
seed_ctrl = wx.SpinCtrl(self, -1, "", (30, 50))
seed_ctrl.SetRange(1, sys.maxint)
seed_ctrl.SetValue(random.randint(1, sys.maxint))
self.seed_ctrl = seed_ctrl
box.Add(seed_ctrl, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5)
box = wx.BoxSizer(wx.HORIZONTAL)
btn = wx.Button(self, wx.ID_OK, " Generate! ")
btn.SetDefault()
box.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
btn = wx.Button(self, wx.ID_CANCEL, " Cancel ")
box.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(box, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
self.SetSizer(sizer)
self.SetAutoLayout(True)
sizer.Fit(self)
def GetNumVertices(self):
return self.num_vertices_spin.GetValue()
def GetProbability(self):
return float(self.probability_ctrl.GetValue())
def GetRandomSeed(self):
return int(self.seed_ctrl.GetValue())
class PLODDialog(wx.Dialog):
def __init__(
self, parent, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition,
style=wx.DEFAULT_DIALOG_STYLE
):
wx.Dialog.__init__(self, parent, ID, title, pos, size, style)
sizer = wx.BoxSizer(wx.VERTICAL)
box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(wx.StaticText(self, -1, "Number of vertices (n)"), 0,
wx.ALIGN_CENTRE|wx.ALL, 5)
num_vertices_spin = wx.SpinCtrl(self, -1, "", (30, 50))
num_vertices_spin.SetRange(0,10000000)
num_vertices_spin.SetValue(10)
box.Add(num_vertices_spin, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
self.num_vertices_spin = num_vertices_spin
sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
# Alpha
box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(wx.StaticText(self, -1, "Alpha"), 0,
wx.ALIGN_CENTRE|wx.ALL, 5)
alpha_ctrl = wx.TextCtrl(self, -1, "2.75", (30, 50))
self.alpha_ctrl = alpha_ctrl
box.Add(alpha_ctrl, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
# Beta
box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(wx.StaticText(self, -1, "Beta"), 0,
wx.ALIGN_CENTRE|wx.ALL, 5)
beta_ctrl = wx.SpinCtrl(self, -1, "", (30, 50))
beta_ctrl.SetRange(1, sys.maxint)
beta_ctrl.SetValue(300)
self.beta_ctrl = beta_ctrl
box.Add(beta_ctrl, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(wx.StaticText(self, -1, "Random seed"), 0,
wx.ALIGN_CENTRE|wx.ALL, 5)
seed_ctrl = wx.SpinCtrl(self, -1, "", (30, 50))
seed_ctrl.SetRange(1, sys.maxint)
seed_ctrl.SetValue(random.randint(1, sys.maxint))
self.seed_ctrl = seed_ctrl
box.Add(seed_ctrl, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5)
box = wx.BoxSizer(wx.HORIZONTAL)
btn = wx.Button(self, wx.ID_OK, " Generate! ")
btn.SetDefault()
box.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
btn = wx.Button(self, wx.ID_CANCEL, " Cancel ")
box.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(box, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
self.SetSizer(sizer)
self.SetAutoLayout(True)
sizer.Fit(self)
def GetNumVertices(self):
return self.num_vertices_spin.GetValue()
def GetAlpha(self):
return float(self.alpha_ctrl.GetValue())
def GetBeta(self):
return float(self.beta_ctrl.GetValue())
def GetRandomSeed(self):
return int(self.seed_ctrl.GetValue())
class SmallWorldDialog(wx.Dialog):
def __init__(
self, parent, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition,
style=wx.DEFAULT_DIALOG_STYLE
):
wx.Dialog.__init__(self, parent, ID, title, pos, size, style)
sizer = wx.BoxSizer(wx.VERTICAL)
box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(wx.StaticText(self, -1, "Number of vertices (n)"), 0,
wx.ALIGN_CENTRE|wx.ALL, 5)
num_vertices_spin = wx.SpinCtrl(self, -1, "", (30, 50))
num_vertices_spin.SetRange(0,10000000)
num_vertices_spin.SetValue(10)
box.Add(num_vertices_spin, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
self.num_vertices_spin = num_vertices_spin
sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
# Number of neighbors
box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(wx.StaticText(self, -1, "Number of neighbors (k)"), 0,
wx.ALIGN_CENTRE|wx.ALL, 5)
num_neighbors_spin = wx.SpinCtrl(self, -1, "", (30, 50))
num_neighbors_spin.SetRange(0,10000000)
num_neighbors_spin.SetValue(4)
box.Add(num_neighbors_spin, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
self.num_neighbors_spin = num_neighbors_spin
sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(wx.StaticText(self, -1, "Rewiring probability (%)"), 0,
wx.ALIGN_CENTRE|wx.ALL, 5)
# This is better than what we're currently doing with a SpinCtrl, but
# it makes the program unstable (?)
# probability_ctrl = wx.lib.masked.numctrl.NumCtrl(self, value = 0.2,
# integerWidth = 1,
# fractionWidth = 3,
# allowNegative = False,
# min = 0.0, max = 1.0)
probability_ctrl = wx.SpinCtrl(self, -1, "", (30, 50))
probability_ctrl.SetRange(0,100)
probability_ctrl.SetValue(20)
self.probability_ctrl = probability_ctrl
box.Add(probability_ctrl, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(wx.StaticText(self, -1, "Random seed"), 0,
wx.ALIGN_CENTRE|wx.ALL, 5)
seed_ctrl = wx.SpinCtrl(self, -1, "", (30, 50))
seed_ctrl.SetRange(1, sys.maxint)
seed_ctrl.SetValue(random.randint(1, sys.maxint))
self.seed_ctrl = seed_ctrl
box.Add(seed_ctrl, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5)
box = wx.BoxSizer(wx.HORIZONTAL)
btn = wx.Button(self, wx.ID_OK, " Generate! ")
btn.SetDefault()
box.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
btn = wx.Button(self, wx.ID_CANCEL, " Cancel ")
box.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(box, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
self.SetSizer(sizer)
self.SetAutoLayout(True)
sizer.Fit(self)
def GetNumVertices(self):
return self.num_vertices_spin.GetValue()
def GetNumNeighbors(self):
return self.num_neighbors_spin.GetValue()
def GetProbability(self):
return float(self.probability_ctrl.GetValue())
def GetRandomSeed(self):
return int(self.seed_ctrl.GetValue())
class GraphEditorWindow(wx.Frame):
def __init__(self, parent, title='BGL Graph Viewer'):
wx.Frame.__init__(self, parent, -1, title);
self.canvas = GraphCanvas(self)
self.CreateMenuBar()
self.erdos_renyi_dlg = None
self.plod_dlg = None
self.small_world_dlg = None
self.NewGraph(None)
def CreateMenuBar(self):
menuBar = wx.MenuBar()
# File menu
fileMenu = wx.Menu()
# New graph menu
newGraphMenu = wx.Menu()
newGraphMenu.Append(111, "Empty graph")
newGraphMenu.Append(112, "Erdos-Renyi graph...")
newGraphMenu.Append(113, "Power Law Out Degree graph...")
newGraphMenu.Append(114, "Small-world graph...")
fileMenu.AppendMenu(110, "&New graph", newGraphMenu)
fileMenu.Append(120, "&Open graph")
fileMenu.Append(130, "&Save graph")
menuBar.Append(fileMenu, "&File")
# Algorithms menu
algorithmsMenu = wx.Menu()
# - Connected components menu
ccMenu = wx.Menu();
ccMenu.Append(201, "Connected Components")
ccMenu.Append(202, "Strongly-Connected Components")
ccMenu.Append(203, "Biconnected Components")
algorithmsMenu.AppendMenu(200, "Connected Components", ccMenu)
# - Minimum Spanning Tree menu
mstMenu = wx.Menu();
mstMenu.Append(212, "Kruskal")
algorithmsMenu.AppendMenu(210, "Minimum Spanning Tree", mstMenu)
# Other algorithms...
algorithmsMenu.Append(221, "Sequential vertex coloring")
menuBar.Append(algorithmsMenu, "&Algorithms")
# Layout menu
layoutMenu = wx.Menu()
layoutMenu.Append(301, "&Circle layout")
layoutMenu.Append(302, "&Fruchterman-Reingold layout")
layoutMenu.Append(303, "&Kamada-Kawai layout")
menuBar.Append(layoutMenu, "&Layout")
# File menu events
self.Bind(wx.EVT_MENU, self.NewGraph, id=111)
self.Bind(wx.EVT_MENU, self.ErdosRenyiGraph, id=112)
self.Bind(wx.EVT_MENU, self.PLODGraph, id=113)
self.Bind(wx.EVT_MENU, self.SmallWorldGraph, id=114)
self.Bind(wx.EVT_MENU, self.OpenGraph, id=120)
self.Bind(wx.EVT_MENU, self.SaveGraph, id=130)
# Algorithms menu events
self.Bind(wx.EVT_MENU, self.ConnectedComponents, id=201)
self.Bind(wx.EVT_MENU, self.StrongComponents, id=202)
self.Bind(wx.EVT_MENU, self.BiconnectedComponents, id=203)
self.Bind(wx.EVT_MENU, self.KruskalMST, id=212)
self.Bind(wx.EVT_MENU, self.SequentialVertexColoring, id=221)
# Layout menu events
self.Bind(wx.EVT_MENU, self.CircleLayout, id=301)
self.Bind(wx.EVT_MENU, self.FruchtermanReingoldLayout, id=302)
self.Bind(wx.EVT_MENU, self.KamadaKawaiLayout, id=303)
self.SetMenuBar(menuBar)
def NewGraph(self, event):
graph = bgl.Graph()
position_map = graph.get_vertex_point2d_map("position")
self.canvas.set_graph(graph, position_map, {})
self.SetTitle("Graph")
def ErdosRenyiGraph(self, event):
if not self.erdos_renyi_dlg:
self.erdos_renyi_dlg = ErdosRenyiDialog(self, -1,
"Erdos-Renyi Generator")
dlg = self.erdos_renyi_dlg
if dlg.ShowModal() == wx.ID_OK:
graph = bgl.Graph(bgl.ErdosRenyi(dlg.GetNumVertices(),
dlg.GetProbability() / 100),
dlg.GetRandomSeed())
position_map = graph.get_vertex_point2d_map("position")
bgl.circle_graph_layout(graph, position_map, 50)
self.canvas.set_graph(graph, position_map, {})
self.SetTitle("Erdos-Renyi Graph ("
+ str(dlg.GetNumVertices()) + ", "
+ str(dlg.GetProbability() / 100) + ")")
def PLODGraph(self, event):
if not self.plod_dlg:
self.plod_dlg = PLODDialog(self, -1,
"Power Law Out Degree Generator")
dlg = self.plod_dlg
if dlg.ShowModal() == wx.ID_OK:
graph = bgl.Graph(bgl.PowerLawOutDegree(dlg.GetNumVertices(),
dlg.GetAlpha(),
dlg.GetBeta()),
dlg.GetRandomSeed())
position_map = graph.get_vertex_point2d_map("position")
bgl.circle_graph_layout(graph, position_map, 50)
self.canvas.set_graph(graph, position_map, {})
self.SetTitle("Power Law Out Degree Graph ("
+ str(dlg.GetNumVertices()) + ", "
+ str(dlg.GetAlpha()) + ", "
+ str(dlg.GetBeta()) + ")")
def SmallWorldGraph(self, event):
if not self.small_world_dlg:
self.small_world_dlg = SmallWorldDialog(self, -1,
"Small-World Generator")
dlg = self.small_world_dlg
if dlg.ShowModal() == wx.ID_OK:
graph = bgl.Graph(bgl.SmallWorld(dlg.GetNumVertices(),
dlg.GetNumNeighbors(),
dlg.GetProbability() / 100),
dlg.GetRandomSeed())
position_map = graph.get_vertex_point2d_map("position")
bgl.circle_graph_layout(graph, position_map, 50)
self.canvas.set_graph(graph, position_map, {})
self.SetTitle("Small-World Graph ("
+ str(dlg.GetNumVertices()) + ", "
+ str(dlg.GetNumNeighbors()) + ", "
+ str(dlg.GetProbability() / 100) + ")")
def OpenGraph(self, event):
dlg = wx.FileDialog(
self, message="Choose a file", defaultDir=os.getcwd(),
defaultFile="", wildcard=wildcard, style=wx.OPEN | wx.CHANGE_DIR)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
try:
graph = bgl.Graph(path, bgl.file_kind.graphviz)
except bgl.directed_graph_error:
graph = bgl.Digraph(path, bgl.file_kind.graphviz)
needs_layout = not graph.has_vertex_map("position")
position_map = graph.get_vertex_point2d_map("position")
if needs_layout:
bgl.circle_graph_layout(graph, position_map, 50)
self.canvas.set_graph(graph, position_map,
self.canvas.default_property_maps(graph))
self.SetTitle(path_to_title(path))
dlg.Destroy()
def SaveGraph(self, event):
dlg = wx.FileDialog(
self, message="Choose a file", defaultDir=os.getcwd(),
defaultFile="", wildcard=wildcard, style=wx.SAVE | wx.CHANGE_DIR)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
# TBD: This fails because we can't serialize Python
# objects. Need to be able to mark some property maps as
# internal (i.e., don't serialize them).
self.canvas.graph.write_graphviz(path)
dlg.Destroy()
def ConnectedComponents(self, event):
graph = self.canvas.graph
component_map = graph.get_vertex_int_map("component")
bgl.connected_components(graph, component_map)
self.canvas.set_vertex_colors_from_components(component_map)
def StrongComponents(self, event):
graph = self.canvas.graph
component_map = graph.get_vertex_int_map("component")
bgl.strong_components(graph, component_map)
self.canvas.set_vertex_colors_from_components(component_map)
def BiconnectedComponents(self, event):
graph = self.canvas.graph
component_map = graph.get_edge_int_map("component")
art_points = bgl.biconnected_components(graph, component_map)
for v in art_points:
self.canvas.VertexShape(v).SetBrush(wx.RED_BRUSH)
self.canvas.set_edge_colors_from_components(component_map)
def KruskalMST(self, event):
graph = self.canvas.graph
weight_map = graph.get_edge_double_map("weight")
mst_edges = bgl.kruskal_minimum_spanning_tree(graph, weight_map)
for e in mst_edges:
shape = self.canvas.EdgeShape(e)
shape.SetPen(wx.Pen(shape.GetPen().GetColour(), 3))
self.canvas.Refresh()
def SequentialVertexColoring(self, event):
graph = self.canvas.graph
color_map = graph.get_vertex_int_map("color")
bgl.sequential_vertex_coloring(graph, color_map)
self.canvas.set_vertex_colors_from_components(color_map)
def CircleLayout(self, event):
bgl.circle_graph_layout(self.canvas.graph, self.canvas.position_map, 50)
self.canvas.update_layout()
def FruchtermanReingoldLayout(self, event):
bgl.fruchterman_reingold_force_directed_layout(self.canvas.graph,
self.canvas.position_map,
width=100, height=100,
progressive=True)
self.canvas.update_layout()
def KamadaKawaiLayout(self, event):
bgl.kamada_kawai_spring_layout(self.canvas.graph,
self.canvas.position_map, side_length=90)
self.canvas.update_layout()
class GraphDrawApp(wx.App):
def OnInit(self):
# This creates some pens and brushes that the OGL library uses.
# It should be called after the app object has been created, but
# before OGL is used.
ogl.OGLInitialize()
self.editor = GraphEditorWindow(None)
self.editor.Show(True)
return True
app = GraphDrawApp(0)
app.MainLoop()

View File

@ -1,686 +0,0 @@
// Copyright 2005 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
#include "basic_graph.hpp"
#include <boost/graph/iteration_macros.hpp>
namespace boost {
inline std::ostream& operator<<(std::ostream& out, default_color_type c)
{
switch (c) {
case white_color: return out << "white";
case gray_color: return out << "gray";
case green_color: return out << "green";
case red_color: return out << "red";
case black_color: return out << "black";
}
return out;
}
inline std::istream& operator>>(std::istream& in, default_color_type& c)
{
std::string text;
if (in >> text) {
if (text == "white") c = white_color;
else if (text == "gray") c = gray_color;
else if (text == "green") c = green_color;
else if (text == "red") c = red_color;
else if (text == "black") c = black_color;
else {
in.setstate(std::ios_base::failbit);
return in;
}
}
return in;
}
namespace graph { namespace python {
template<typename DirectedS>
struct build_string_property_maps
{
build_string_property_maps(basic_graph<DirectedS>* g) : g(g) { }
std::auto_ptr<boost::dynamic_property_map>
operator()(const std::string& name, const boost::any& key,
const boost::any& value)
{
typedef typename basic_graph<DirectedS>::VertexIndexMap VertexIndexMap;
typedef typename basic_graph<DirectedS>::EdgeIndexMap EdgeIndexMap;
std::auto_ptr<boost::dynamic_property_map> result(0);
if (key.type() == typeid(typename basic_graph<DirectedS>::Vertex)) {
typedef vector_property_map<std::string, VertexIndexMap>
property_map_type;
typedef python_dynamic_adaptor<property_map_type> adaptor_type;
result.reset
(new adaptor_type(property_map_type(g->num_vertices(),
g->get_vertex_index_map())));
} else if (key.type() == typeid(typename basic_graph<DirectedS>::Edge)) {
typedef vector_property_map<std::string, EdgeIndexMap> property_map_type;
typedef python_dynamic_adaptor<property_map_type> adaptor_type;
result.reset
(new adaptor_type(property_map_type(g->num_edges(),
g->get_edge_index_map())));
}
return result;
}
private:
basic_graph<DirectedS>* g;
};
// ----------------------------------------------------------
// Constructors
// ----------------------------------------------------------
template<typename DirectedS>
basic_graph<DirectedS>::basic_graph()
: inherited(), dp(build_string_property_maps<DirectedS>(this))
{ }
template<typename DirectedS>
basic_graph<DirectedS>::basic_graph(boost::python::object l,
const std::string& name_map)
: inherited()
{
using boost::python::object;
std::map<object, vertex_descriptor> verts;
int len = ::boost::python::extract<int>(l.attr("__len__")());
vector_property_map<object, VertexIndexMap> name;
if (!name_map.empty()) name = get_vertex_map<object>(name_map);
for (int i = 0; i < len; ++i) {
vertex_descriptor u, v;
object up = l[i][0];
object vp = l[i][1];
typename std::map<object, vertex_descriptor>::iterator pos;
pos = verts.find(up);
if (pos == verts.end()) {
u = verts[up] = add_vertex();
if (!name_map.empty()) name[u] = up;
}
else u = pos->second;
pos = verts.find(vp);
if (pos == verts.end()) {
v = verts[vp] = add_vertex();
if (!name_map.empty()) name[v] = vp;
}
else v = pos->second;
add_edge(u, v);
}
}
template<typename DirectedS>
basic_graph<DirectedS>::basic_graph(const std::string& filename,
graph_file_kind kind)
: inherited(), dp(build_string_property_maps<DirectedS>(this))
{
switch (kind) {
case gfk_adjlist:
break;
case gfk_graphviz:
read_graphviz(filename);
break;
}
}
template<typename DirectedS>
basic_graph<DirectedS>::basic_graph(erdos_renyi er, int seed)
: stored_minstd_rand(seed),
inherited(er_iterator(this->gen, er.n, er.p), er_iterator(), er.n)
{
renumber_vertices();
renumber_edges();
}
template<typename DirectedS>
basic_graph<DirectedS>::basic_graph(power_law_out_degree plod, int seed)
: stored_minstd_rand(seed),
inherited(sf_iterator(this->gen, plod.n, plod.alpha, plod.beta),
sf_iterator(),
plod.n)
{
renumber_vertices();
renumber_edges();
}
template<typename DirectedS>
basic_graph<DirectedS>::basic_graph(small_world sw, int seed)
: stored_minstd_rand(seed),
inherited(sw_iterator(this->gen, sw.n, sw.k, sw.p), sw_iterator(), sw.n)
{
renumber_vertices();
renumber_edges();
}
// ----------------------------------------------------------
// Incidence basic_graph<DirectedS> concept
// ----------------------------------------------------------
template<typename DirectedS>
typename basic_graph<DirectedS>::Vertex
basic_graph<DirectedS>::source(Edge e) const
{ return Vertex(boost::source(e.base, base())); }
template<typename DirectedS>
typename basic_graph<DirectedS>::Vertex
basic_graph<DirectedS>::target(Edge e) const
{ return Vertex(boost::target(e.base, base())); }
template<typename DirectedS>
std::pair<typename basic_graph<DirectedS>::out_edge_iterator,
typename basic_graph<DirectedS>::out_edge_iterator>
basic_graph<DirectedS>::out_edges(Vertex u) const
{
std::pair<base_out_edge_iterator, base_out_edge_iterator> rng =
boost::out_edges(u.base, base());
return std::make_pair(out_edge_iterator(rng.first, typename Edge::create()),
out_edge_iterator(rng.second,typename Edge::create()));
}
template<typename DirectedS>
simple_python_iterator<typename basic_graph<DirectedS>::out_edge_iterator>
basic_graph<DirectedS>::py_out_edges(Vertex u) const
{
return simple_python_iterator<out_edge_iterator>(out_edges(u));
}
template<typename DirectedS>
std::size_t
basic_graph<DirectedS>::out_degree(Vertex u) const
{
return boost::out_degree(u.base, base());
}
// ----------------------------------------------------------
// Bidirectional basic_graph<DirectedS> concept
// ----------------------------------------------------------
template<typename DirectedS>
std::pair<typename basic_graph<DirectedS>::in_edge_iterator,
typename basic_graph<DirectedS>::in_edge_iterator>
basic_graph<DirectedS>::in_edges(Vertex u) const
{
std::pair<base_in_edge_iterator, base_in_edge_iterator> rng =
boost::in_edges(u.base, base());
return std::make_pair(in_edge_iterator(rng.first, typename Edge::create()),
in_edge_iterator(rng.second, typename Edge::create()));
}
template<typename DirectedS>
simple_python_iterator<typename basic_graph<DirectedS>::in_edge_iterator>
basic_graph<DirectedS>::py_in_edges(Vertex u) const
{
return simple_python_iterator<in_edge_iterator>(in_edges(u));
}
template<typename DirectedS>
std::size_t
basic_graph<DirectedS>::in_degree(Vertex u) const
{
return boost::in_degree(u.base, base());
}
// ----------------------------------------------------------
// Adjacency basic_graph<DirectedS> concept
// ----------------------------------------------------------
template<typename DirectedS>
std::pair<typename basic_graph<DirectedS>::adjacency_iterator,
typename basic_graph<DirectedS>::adjacency_iterator>
basic_graph<DirectedS>::adjacent_vertices(Vertex u) const
{
std::pair<base_adjacency_iterator, base_adjacency_iterator> rng =
boost::adjacent_vertices(u.base, base());
return std::make_pair(adjacency_iterator(rng.first, typename Vertex::create()),
adjacency_iterator(rng.second, typename Vertex::create()));
}
template<typename DirectedS>
simple_python_iterator<typename basic_graph<DirectedS>::adjacency_iterator>
basic_graph<DirectedS>::py_adjacent_vertices(Vertex u) const
{
return
simple_python_iterator<adjacency_iterator>(adjacent_vertices(u));
}
// ----------------------------------------------------------
// Vertex properties
// ----------------------------------------------------------
template<typename DirectedS>
template<typename T>
vector_property_map<T, typename basic_graph<DirectedS>::VertexIndexMap>
basic_graph<DirectedS>::get_vertex_map(const std::string& name)
{
typedef vector_property_map<T, VertexIndexMap> result_type;
typedef python_dynamic_adaptor<result_type> adaptor_type;
dynamic_properties::iterator i = dp.lower_bound(name);
while (i != dp.end() && i->first == name) {
if (i->second->key() == typeid(Vertex)) {
if (i->second->value() == typeid(T)) {
return dynamic_cast<adaptor_type&>(*i->second).base();
} else {
// Convert the property map element-by-element to the
// requested type.
result_type result(num_vertices(), get_vertex_index_map());
for(vertex_iterator v = vertices().first; v != vertices().second;
++v) {
put(result, *v, lexical_cast<T>(i->second->get_string(*v)));
}
// Replace the existing property map with the converted one
adaptor_type* adaptor = new adaptor_type(result);
delete i->second;
i->second = adaptor;
// Return the new property map.
return result;
}
}
++i;
}
typedef vector_property_map<T, VertexIndexMap> property_map_type;
typedef python_dynamic_adaptor<property_map_type> adaptor_type;
property_map_type result(num_vertices(), get_vertex_index_map());
dp.insert(name,
std::auto_ptr<dynamic_property_map>(new adaptor_type(result)));
return result;
}
template<typename DirectedS>
bool
basic_graph<DirectedS>::has_vertex_map(const std::string& name) const
{
dynamic_properties::const_iterator i = dp.lower_bound(name);
while (i != dp.end() && i->first == name) {
if (i->second->key() == typeid(Vertex))
return true;
}
return false;
}
// ----------------------------------------------------------
// Edge properties
// ----------------------------------------------------------
template<typename DirectedS>
template<typename T>
vector_property_map<T, typename basic_graph<DirectedS>::EdgeIndexMap>
basic_graph<DirectedS>::get_edge_map(const std::string& name)
{
typedef vector_property_map<T, EdgeIndexMap> result_type;
typedef python_dynamic_adaptor<result_type> adaptor_type;
dynamic_properties::iterator i = dp.lower_bound(name);
while (i != dp.end() && i->first == name) {
if (i->second->key() == typeid(Edge)) {
if (i->second->value() == typeid(T)) {
return dynamic_cast<adaptor_type&>(*i->second).base();
} else {
// Convert the property map element-by-element to the
// requested type.
result_type result(num_vertices(), get_edge_index_map());
for(edge_iterator e = edges().first; e != edges().second; ++e) {
put(result, *e, lexical_cast<T>(i->second->get_string(*e)));
}
// Replace the existing property map with the converted one
adaptor_type* adaptor = new adaptor_type(result);
delete i->second;
i->second = adaptor;
// Return the new property map.
return result;
}
}
++i;
}
typedef vector_property_map<T, EdgeIndexMap> property_map_type;
typedef python_dynamic_adaptor<property_map_type> adaptor_type;
property_map_type result(num_edges(), get_edge_index_map());
dp.insert(name,
std::auto_ptr<dynamic_property_map>(new adaptor_type(result)));
return result;
}
template<typename DirectedS>
bool
basic_graph<DirectedS>::has_edge_map(const std::string& name) const
{
dynamic_properties::const_iterator i = dp.lower_bound(name);
while (i != dp.end() && i->first == name) {
if (i->second->key() == typeid(Edge))
return true;
}
return false;
}
// ----------------------------------------------------------
// Mutable graph
// ----------------------------------------------------------
template<typename DirectedS>
typename basic_graph<DirectedS>::Vertex basic_graph<DirectedS>::add_vertex()
{
using boost::add_vertex;
base_vertex_descriptor v = add_vertex(base());
put(vertex_index, base(), v, index_to_vertex.size());
index_to_vertex.push_back(v);
return Vertex(v);
}
template<typename DirectedS>
void basic_graph<DirectedS>::clear_vertex(Vertex vertex)
{
// Remove all incoming and outgoing edges
while (out_degree(vertex) > 0) remove_edge(*out_edges(vertex).first);
while (in_degree(vertex) > 0) remove_edge(*in_edges(vertex).first);
}
template<typename DirectedS>
void basic_graph<DirectedS>::remove_vertex(Vertex vertex)
{
using boost::remove_vertex;
// Update property maps
for (dynamic_properties::iterator i = dp.begin(); i != dp.end(); ++i) {
if (i->second->key() == typeid(Vertex)) {
dynamic_cast<python_dynamic_property_map*>(&*i->second)->
copy_value(vertex, Vertex(index_to_vertex.back()));
}
}
// Update vertex indices
std::size_t index = get(vertex_index, base(), vertex.base);
index_to_vertex[index] = index_to_vertex.back();
put(vertex_index, base(), index_to_vertex[index], index);
index_to_vertex.pop_back();
// Remove the actual vertex
remove_vertex(vertex.base, base());
}
template<typename DirectedS>
std::size_t basic_graph<DirectedS>::num_vertices() const
{
using boost::num_vertices;
return num_vertices(base());
}
template<typename DirectedS>
typename basic_graph<DirectedS>::vertex_iterator
basic_graph<DirectedS>::vertices_begin() const
{
return make_transform_iterator(boost::vertices(base()).first,
typename Vertex::create());
}
template<typename DirectedS>
typename basic_graph<DirectedS>::vertex_iterator
basic_graph<DirectedS>::vertices_end() const
{
return make_transform_iterator(boost::vertices(base()).second,
typename Vertex::create());
}
template<typename DirectedS>
std::pair<typename basic_graph<DirectedS>::vertex_iterator,
typename basic_graph<DirectedS>::vertex_iterator>
basic_graph<DirectedS>::vertices() const
{
return std::make_pair(vertices_begin(), vertices_end());
}
template<typename DirectedS>
simple_python_iterator<typename basic_graph<DirectedS>::vertex_iterator>
basic_graph<DirectedS>::py_vertices() const
{
return simple_python_iterator<vertex_iterator>(vertices());
}
template<typename DirectedS>
typename basic_graph<DirectedS>::Edge
basic_graph<DirectedS>::add_edge(Vertex u, Vertex v)
{
using boost::add_edge;
base_edge_descriptor e = add_edge(u.base, v.base, base()).first;
put(edge_index, base(), e, index_to_edge.size());
index_to_edge.push_back(e);
return Edge(e);
}
template<typename DirectedS>
void basic_graph<DirectedS>::remove_edge(Edge edge)
{
using boost::remove_edge;
// Update property maps
for (dynamic_properties::iterator i = dp.begin(); i != dp.end(); ++i) {
if (i->second->key() == typeid(Edge)) {
dynamic_cast<python_dynamic_property_map*>(&*i->second)->
copy_value(edge, Edge(index_to_edge.back()));
}
}
// Update edge indices
std::size_t index = get(edge_index, base(), edge.base);
index_to_edge[index] = index_to_edge.back();
put(edge_index, base(), index_to_edge[index], index);
index_to_edge.pop_back();
// Remove the actual edge
remove_edge(edge.base, base());
}
template<typename DirectedS>
std::size_t basic_graph<DirectedS>::num_edges() const
{
using boost::num_edges;
return num_edges(base());
}
template<typename DirectedS>
typename basic_graph<DirectedS>::edge_iterator
basic_graph<DirectedS>::edges_begin() const
{
return make_transform_iterator(boost::edges(base()).first,
typename Edge::create());
}
template<typename DirectedS>
typename basic_graph<DirectedS>::edge_iterator
basic_graph<DirectedS>::edges_end() const
{
return make_transform_iterator(boost::edges(base()).second,
typename Edge::create());
}
template<typename DirectedS>
std::pair<typename basic_graph<DirectedS>::edge_iterator,
typename basic_graph<DirectedS>::edge_iterator>
basic_graph<DirectedS>::edges() const
{
return std::make_pair(edges_begin(), edges_end());
}
template<typename DirectedS>
simple_python_iterator<typename basic_graph<DirectedS>::edge_iterator>
basic_graph<DirectedS>::py_edges() const
{
return simple_python_iterator<edge_iterator>(edges());
}
template<typename DirectedS>
void basic_graph<DirectedS>::renumber_vertices()
{
using boost::vertices;
BGL_FORALL_VERTICES_T(v, base(), inherited) {
put(vertex_index, base(), v, index_to_vertex.size());
index_to_vertex.push_back(v);
}
}
template<typename DirectedS>
void basic_graph<DirectedS>::renumber_edges()
{
using boost::edges;
BGL_FORALL_EDGES_T(e, base(), inherited) {
put(edge_index, base(), e, index_to_edge.size());
index_to_edge.push_back(e);
}
}
template<typename Graph> void export_in_graph();
template<typename DirectedS>
void export_basic_graph(const char* name)
{
using boost::python::arg;
using boost::python::class_;
using boost::python::init;
using boost::python::object;
using boost::python::range;
using boost::python::scope;
using boost::python::self;
using boost::python::tuple;
typedef basic_graph<DirectedS> Graph;
typedef typename Graph::Vertex Vertex;
typedef typename Graph::Edge Edge;
typedef typename Graph::VertexIndexMap VertexIndexMap;
typedef typename Graph::EdgeIndexMap EdgeIndexMap;
{
scope s(
class_<basic_graph<DirectedS> >(name)
// Constructors
.def(init<object>())
.def(init<object, std::string>())
.def(init<std::string, graph_file_kind>())
.def(init<erdos_renyi, std::size_t>(
(arg("generator"), arg("seed") = 1)))
.def(init<power_law_out_degree, std::size_t>(
(arg("generator"), arg("seed") = 1)))
.def(init<small_world, std::size_t>(
(arg("generator"), arg("seed") = 1)))
.def("is_directed", &Graph::is_directed)
// Vertex List Graph concept
.def("num_vertices", &Graph::num_vertices)
.add_property("vertices", &Graph::py_vertices)
// Edge List Graph concept
.def("num_edges", &Graph::num_edges)
.add_property("edges", &Graph::py_edges)
// Mutable Graph concept
.def("add_vertex", &Graph::add_vertex)
.def("clear_vertex", &Graph::clear_vertex)
.def("remove_vertex", &Graph::remove_vertex)
.def("add_edge", &Graph::add_edge)
.def("remove_edge", &Graph::remove_edge)
// Incidence Graph concept
.def("source", &Graph::source)
.def("target", &Graph::target)
.def("out_edges", &Graph::py_out_edges)
.def("out_degree", &Graph::out_degree)
.def("in_edges", &Graph::py_in_edges)
.def("in_degree", &Graph::in_degree)
.def("adjacent_vertices", &Graph::py_adjacent_vertices)
// Vertex property maps
.def("has_vertex_map", &Graph::has_vertex_map)
.def("get_vertex_index_map", &Graph::get_vertex_index_map)
.def("get_vertex_color_map", &Graph::get_vertex_color_map)
.def("get_vertex_double_map", &Graph::get_vertex_double_map)
.def("get_vertex_int_map", &Graph::get_vertex_int_map)
.def("get_vertex_string_map", &Graph::get_vertex_string_map)
.def("get_vertex_object_map", &Graph::get_vertex_object_map)
.def("get_vertex_point2d_map", &Graph::get_vertex_point2d_map)
// Edge property maps
.def("has_edge_map", &Graph::has_edge_map)
.def("get_edge_index_map", &Graph::get_edge_index_map)
.def("get_edge_color_map", &Graph::get_edge_color_map)
.def("get_edge_double_map", &Graph::get_edge_double_map)
.def("get_edge_int_map", &Graph::get_edge_int_map)
.def("get_edge_string_map", &Graph::get_edge_string_map)
.def("get_edge_object_map", &Graph::get_edge_object_map)
// Graph I/O
.def("read_graphviz", &Graph::read_graphviz)
.def("write_graphviz", &Graph::write_graphviz)
.def("write_graphviz", &Graph::write_graphviz_def)
// Pickling
.def_pickle(graph_pickle_suite<DirectedS>())
);
export_in_graph<Graph>();
}
if (!type_already_registered<Vertex>())
class_<Vertex>("Vertex")
.def(self == self)
.def(self != self);
if (!type_already_registered<Edge>())
class_<Edge>("Edge")
.def(self == self)
.def(self != self);
// Iterators
simple_python_iterator<typename Graph::vertex_iterator>
::declare("vertex_iterator");
simple_python_iterator<typename Graph::edge_iterator>
::declare("edge_iterator");
simple_python_iterator<typename Graph::out_edge_iterator>
::declare("out_edge_iterator");
simple_python_iterator<typename Graph::in_edge_iterator>
::declare("in_edge_iterator");
simple_python_iterator<typename Graph::adjacency_iterator>
::declare("adjacency_iterator");
// Vertex property maps
declare_readable_property_map<VertexIndexMap>
::declare("vertex_index_map");
declare_property_map<vector_property_map<Vertex, VertexIndexMap> >
::declare("vertex_vertex_map");
declare_property_map<vector_property_map<double, VertexIndexMap> >
::declare("vertex_double_map");
declare_property_map<vector_property_map<int, VertexIndexMap> >
::declare("vertex_int_map");
declare_property_map<vector_property_map<std::string, VertexIndexMap> >
::declare("vertex_string_map");
declare_property_map<vector_property_map<object, VertexIndexMap> >
::declare("vertex_object_map");
declare_property_map<vector_property_map<default_color_type,
VertexIndexMap> >
::declare("vertex_color_map");
declare_property_map<vector_property_map<point2d, VertexIndexMap> >
::declare("vertex_point2d_map");
// Edge property maps
declare_readable_property_map<EdgeIndexMap>
::declare("edge_index_map");
declare_property_map<vector_property_map<Edge, EdgeIndexMap> >
::declare("edge_edge_map");
declare_property_map<vector_property_map<double, EdgeIndexMap> >
::declare("edge_double_map");
declare_property_map<vector_property_map<int, EdgeIndexMap> >
::declare("edge_int_map");
declare_property_map<vector_property_map<std::string, EdgeIndexMap> >
::declare("edge_string_map");
declare_property_map<vector_property_map<object, EdgeIndexMap> >
::declare("edge_object_map");
declare_property_map<vector_property_map<default_color_type, EdgeIndexMap> >
::declare("edge_color_map");
}
} } } // end namespace boost::graph::python

View File

@ -1,596 +0,0 @@
// Copyright 2004-5 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
#ifndef BOOST_GRAPH_BASIC_GRAPH_HPP
#define BOOST_GRAPH_BASIC_GRAPH_HPP
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/vector_property_map.hpp>
#include <boost/dynamic_property_map.hpp>
#include <boost/random/linear_congruential.hpp>
#include <boost/graph/erdos_renyi_generator.hpp>
#include <boost/graph/plod_generator.hpp>
#include <boost/graph/small_world_generator.hpp>
#include <boost/python.hpp>
#include "point2d.hpp"
#include "generators.hpp"
namespace boost { namespace graph { namespace python {
template<typename T> bool type_already_registered()
{
using boost::python::objects::registered_class_object;
using boost::python::type_id;
return registered_class_object(type_id<T>()).get() != 0;
}
template<typename Iterator>
class simple_python_iterator
{
public:
typedef typename std::iterator_traits<Iterator>::difference_type
difference_type;
simple_python_iterator(std::pair<Iterator, Iterator> p)
: orig_first(p.first), first(p.first), last(p.second), n(-1) { }
typename std::iterator_traits<Iterator>::value_type next()
{
using boost::python::objects::stop_iteration_error;
if (first == last) stop_iteration_error();
return *first++;
}
difference_type len()
{
if (n == -1) n = std::distance(first, last);
return n;
}
static void declare(const char* name)
{
using boost::python::class_;
using boost::python::no_init;
using boost::python::objects::identity_function;
if (!type_already_registered<simple_python_iterator>())
class_<simple_python_iterator<Iterator> >(name, no_init)
.def("__iter__", identity_function())
.def("__len__", &simple_python_iterator<Iterator>::len)
.def("next", &simple_python_iterator<Iterator>::next)
;
}
private:
Iterator orig_first;
Iterator first;
Iterator last;
difference_type n;
};
template<typename PropertyMap>
struct declare_readable_property_map
{
typedef typename property_traits<PropertyMap>::key_type key_type;
typedef typename property_traits<PropertyMap>::value_type value_type;
static value_type getitem(const PropertyMap& pm, const key_type& key)
{ return get(pm, key); }
static void declare(const char* name)
{
using boost::python::class_;
using boost::python::no_init;
if (!type_already_registered<PropertyMap>())
class_<PropertyMap>(name, no_init)
.def("__getitem__", &getitem)
;
}
};
template<typename PropertyMap>
struct declare_property_map
{
typedef typename property_traits<PropertyMap>::key_type key_type;
typedef typename property_traits<PropertyMap>::value_type value_type;
static value_type getitem(const PropertyMap& pm, const key_type& key)
{ return get(pm, key); }
static void
setitem(const PropertyMap& pm, const key_type& key,const value_type& value)
{ return put(pm, key, value); }
static void declare(const char* name)
{
using boost::python::class_;
using boost::python::no_init;
if (!type_already_registered<PropertyMap>())
class_<PropertyMap>(name, no_init)
.def("__getitem__", &getitem)
.def("__setitem__", &setitem)
;
}
};
template<typename T, typename DirectedS>
struct basic_descriptor
{
basic_descriptor() {}
basic_descriptor(T base) : base(base) { }
operator T() const { return base; }
struct create
{
typedef basic_descriptor result_type;
basic_descriptor operator()(T base) const { return base; }
};
T base;
};
template<typename T, typename DirectedS>
inline bool
operator==(const basic_descriptor<T, DirectedS>& u,
const basic_descriptor<T, DirectedS>& v)
{ return u.base == v.base; }
template<typename T, typename DirectedS>
inline bool
operator!=(const basic_descriptor<T, DirectedS>& u,
const basic_descriptor<T, DirectedS>& v)
{ return u.base != v.base; }
template<typename Key, typename IndexMap>
struct basic_index_map
{
typedef Key key_type;
typedef typename property_traits<IndexMap>::value_type value_type;
typedef typename property_traits<IndexMap>::reference reference;
typedef typename property_traits<IndexMap>::category category;
basic_index_map(const IndexMap& id = IndexMap())
: id(id) { }
IndexMap id;
};
template<typename Key, typename IndexMap>
inline typename basic_index_map<Key, IndexMap>::value_type
get(const basic_index_map<Key, IndexMap>& pm,
typename basic_index_map<Key, IndexMap>::key_type const& key)
{ return get(pm.id, key.base); }
enum graph_file_kind { gfk_adjlist, gfk_graphviz };
struct stored_minstd_rand
{
stored_minstd_rand(int seed = 1) : gen(seed) { }
minstd_rand gen;
};
template<typename DirectedS>
class basic_graph
: public stored_minstd_rand,
public adjacency_list<listS, listS, DirectedS,
property<vertex_index_t, std::size_t>,
property<edge_index_t, std::size_t> >
{
typedef adjacency_list<listS, listS, DirectedS,
property<vertex_index_t, std::size_t>,
property<edge_index_t, std::size_t> > inherited;
typedef graph_traits<inherited> traits;
typedef typename traits::vertex_iterator base_vertex_iterator;
typedef typename traits::edge_iterator base_edge_iterator;
typedef typename traits::out_edge_iterator base_out_edge_iterator;
typedef typename traits::in_edge_iterator base_in_edge_iterator;
typedef typename traits::adjacency_iterator base_adjacency_iterator;
typedef typename property_map<inherited, vertex_index_t>::const_type
base_vertex_index_map;
typedef typename property_map<inherited, edge_index_t>::const_type
base_edge_index_map;
typedef typename traits::vertex_descriptor base_vertex_descriptor;
typedef typename traits::edge_descriptor base_edge_descriptor;
typedef erdos_renyi_iterator<minstd_rand, basic_graph<DirectedS> >
er_iterator;
typedef plod_iterator<minstd_rand, basic_graph<DirectedS> >
sf_iterator;
typedef small_world_iterator<minstd_rand, basic_graph<DirectedS> >
sw_iterator;
public:
typedef basic_descriptor<base_vertex_descriptor, DirectedS>
Vertex;
typedef Vertex vertex_descriptor;
typedef basic_descriptor<base_edge_descriptor, DirectedS>
Edge;
typedef Edge edge_descriptor;
typedef basic_index_map<Vertex, base_vertex_index_map>
VertexIndexMap;
typedef basic_index_map<Edge, base_edge_index_map> EdgeIndexMap;
typedef std::size_t vertices_size_type;
typedef std::size_t edges_size_type;
typedef std::size_t degree_size_type;
typedef typename traits::directed_category directed_category;
typedef typename traits::edge_parallel_category edge_parallel_category;
typedef typename traits::traversal_category traversal_category;
typedef transform_iterator<typename Vertex::create, base_vertex_iterator>
vertex_iterator;
typedef transform_iterator<typename Edge::create, base_edge_iterator>
edge_iterator;
typedef transform_iterator<typename Edge::create, base_out_edge_iterator>
out_edge_iterator;
typedef transform_iterator<typename Edge::create, base_in_edge_iterator>
in_edge_iterator;
typedef transform_iterator<typename Vertex::create, base_adjacency_iterator>
adjacency_iterator;
basic_graph();
basic_graph(boost::python::object,
const std::string& name_map = std::string());
basic_graph(const std::string& filename, graph_file_kind kind);
basic_graph(erdos_renyi, int seed);
basic_graph(power_law_out_degree, int seed);
basic_graph(small_world, int seed);
bool is_directed() const
{ return is_convertible<directed_category, directed_tag>::value; }
Vertex add_vertex();
void clear_vertex(Vertex vertex);
void remove_vertex(Vertex vertex);
std::size_t num_vertices() const;
std::pair<vertex_iterator, vertex_iterator> vertices() const;
simple_python_iterator<vertex_iterator> py_vertices() const;
vertex_iterator vertices_begin() const;
vertex_iterator vertices_end() const;
Edge add_edge(Vertex u, Vertex v);
void remove_edge(Edge edge);
std::size_t num_edges() const;
std::pair<edge_iterator, edge_iterator> edges() const;
simple_python_iterator<edge_iterator> py_edges() const;
edge_iterator edges_begin() const;
edge_iterator edges_end() const;
// Incidence Graph concept
Vertex source(Edge e) const;
Vertex target(Edge e) const;
std::pair<out_edge_iterator, out_edge_iterator> out_edges(Vertex u) const;
simple_python_iterator<out_edge_iterator> py_out_edges(Vertex u) const;
std::size_t out_degree(Vertex u) const;
// Bidirectional Graph concept
std::pair<in_edge_iterator, in_edge_iterator> in_edges(Vertex u) const;
simple_python_iterator<in_edge_iterator> py_in_edges(Vertex u) const;
std::size_t in_degree(Vertex u) const;
// Adjacency Graph concept
std::pair<adjacency_iterator, adjacency_iterator>
adjacent_vertices(Vertex u) const;
simple_python_iterator<adjacency_iterator>
py_adjacent_vertices(Vertex u) const;
// Vertex property maps
VertexIndexMap get_vertex_index_map() const
{ return get(vertex_index, base()); }
template<typename T>
vector_property_map<T, VertexIndexMap>
get_vertex_map(const std::string& name);
bool has_vertex_map(const std::string& name) const;
// Edge property maps
EdgeIndexMap get_edge_index_map() const
{ return get(edge_index, base()); }
template<typename T>
vector_property_map<T, EdgeIndexMap>
get_edge_map(const std::string& name);
bool has_edge_map(const std::string& name) const;
// Graph I/O
void read_graphviz(const std::string& filename,
const std::string& node_id = std::string("node_id"));
void write_graphviz(const std::string& filename,
const std::string& node_id = std::string("node_id"));
void write_graphviz_def(const std::string& filename)
{ write_graphviz(filename); }
// Simple functions for Visual C++ 7.1 :(
vector_property_map<default_color_type, VertexIndexMap>
get_vertex_color_map(const std::string& name)
{ return get_vertex_map<default_color_type>(name); }
vector_property_map<double, VertexIndexMap>
get_vertex_double_map(const std::string& name)
{ return get_vertex_map<double>(name); }
vector_property_map<int, VertexIndexMap>
get_vertex_int_map(const std::string& name)
{ return get_vertex_map<int>(name); }
vector_property_map<std::string, VertexIndexMap>
get_vertex_string_map(const std::string& name)
{ return get_vertex_map<std::string>(name); }
vector_property_map<boost::python::object, VertexIndexMap>
get_vertex_object_map(const std::string& name)
{ return get_vertex_map<boost::python::object>(name); }
vector_property_map<point2d, VertexIndexMap>
get_vertex_point2d_map(const std::string& name)
{ return get_vertex_map<point2d>(name); }
vector_property_map<default_color_type, EdgeIndexMap>
get_edge_color_map(const std::string& name)
{ return get_edge_map<default_color_type>(name); }
vector_property_map<double, EdgeIndexMap>
get_edge_double_map(const std::string& name)
{ return get_edge_map<double>(name); }
vector_property_map<int, EdgeIndexMap>
get_edge_int_map(const std::string& name)
{ return get_edge_map<int>(name); }
vector_property_map<std::string, EdgeIndexMap>
get_edge_string_map(const std::string& name)
{ return get_edge_map<std::string>(name); }
vector_property_map<boost::python::object, EdgeIndexMap>
get_edge_object_map(const std::string& name)
{ return get_edge_map<boost::python::object>(name); }
inherited& base() { return *this; }
const inherited& base() const { return *this; }
dynamic_properties& get_dynamic_properties() { return dp; }
const dynamic_properties& get_dynamic_properties() const { return dp; }
protected:
void renumber_vertices();
void renumber_edges();
private:
std::vector<vertex_descriptor> index_to_vertex;
std::vector<edge_descriptor> index_to_edge;
dynamic_properties dp;
};
// Vertex List Graph concept
template<typename DirectedS>
inline std::pair<typename basic_graph<DirectedS>::vertex_iterator,
typename basic_graph<DirectedS>::vertex_iterator>
vertices(const basic_graph<DirectedS>& g)
{ return g.vertices(); }
template<typename DirectedS>
inline std::size_t num_vertices(const basic_graph<DirectedS>& g)
{ return g.num_vertices(); }
// Edge List Graph concept
template<typename DirectedS>
inline std::pair<typename basic_graph<DirectedS>::edge_iterator,
typename basic_graph<DirectedS>::edge_iterator>
edges(const basic_graph<DirectedS>& g)
{ return g.edges(); }
template<typename DirectedS>
inline std::size_t num_edges(const basic_graph<DirectedS>& g)
{ return g.num_edges(); }
// Incidence Graph concept
template<typename DirectedS>
inline typename basic_graph<DirectedS>::vertex_descriptor
source(typename basic_graph<DirectedS>::edge_descriptor e,
const basic_graph<DirectedS>& g)
{ return g.source(e); }
template<typename DirectedS>
inline typename basic_graph<DirectedS>::vertex_descriptor
target(typename basic_graph<DirectedS>::edge_descriptor e,
const basic_graph<DirectedS>& g)
{ return g.target(e); }
template<typename DirectedS>
inline std::pair<typename basic_graph<DirectedS>::out_edge_iterator,
typename basic_graph<DirectedS>::out_edge_iterator>
out_edges(typename basic_graph<DirectedS>::vertex_descriptor u,
const basic_graph<DirectedS>& g)
{ return g.out_edges(u); }
template<typename DirectedS>
inline std::size_t
out_degree(typename basic_graph<DirectedS>::vertex_descriptor u,
const basic_graph<DirectedS>& g)
{ return g.out_degree(u); }
// Bidirectional Graph concept
template<typename DirectedS>
inline std::pair<typename basic_graph<DirectedS>::in_edge_iterator,
typename basic_graph<DirectedS>::in_edge_iterator>
in_edges(typename basic_graph<DirectedS>::vertex_descriptor u,
const basic_graph<DirectedS>& g)
{ return g.in_edges(u); }
template<typename DirectedS>
inline std::size_t
in_degree(typename basic_graph<DirectedS>::vertex_descriptor u,
const basic_graph<DirectedS>& g)
{ return g.in_degree(u); }
// Adjacency Graph concept
template<typename DirectedS>
inline std::pair<typename basic_graph<DirectedS>::adjacency_iterator,
typename basic_graph<DirectedS>::adjacency_iterator>
adjacent_vertices(typename basic_graph<DirectedS>::vertex_descriptor u,
const basic_graph<DirectedS>& g)
{ return g.adjacent_vertices(u); }
// Mutable basic_graph<DirectedS> concept
template<typename DirectedS>
inline typename basic_graph<DirectedS>::vertex_descriptor
add_vertex(basic_graph<DirectedS>& g)
{ return g.add_vertex(); }
template<typename DirectedS>
inline std::pair<typename basic_graph<DirectedS>::edge_descriptor, bool>
add_edge(typename basic_graph<DirectedS>::vertex_descriptor u,
typename basic_graph<DirectedS>::vertex_descriptor v,
basic_graph<DirectedS>& g)
{ return std::make_pair(g.add_edge(u, v), true); }
template<typename DirectedS>
void export_basic_graph(const char* name);
template<typename DirectedS>
typename basic_graph<DirectedS>::VertexIndexMap
get(vertex_index_t, const basic_graph<DirectedS>& g)
{ return g.get_vertex_index_map(); }
template<typename DirectedS>
typename basic_graph<DirectedS>::EdgeIndexMap
get(edge_index_t, const basic_graph<DirectedS>& g)
{ return g.get_edge_index_map(); }
template<typename DirectedS>
struct graph_pickle_suite : boost::python::pickle_suite
{
typedef basic_graph<DirectedS> Graph;
typedef typename Graph::vertex_descriptor Vertex;
typedef typename Graph::edge_descriptor Edge;
static
boost::python::tuple
getstate(boost::python::object g_obj);
static
void
setstate(boost::python::object g_obj, boost::python::tuple state);
};
class python_dynamic_property_map
{
public:
virtual ~python_dynamic_property_map() {}
virtual void copy_value(const any& to, const any& from) = 0;
virtual boost::python::object get_python(const any& key) = 0;
};
template<typename PropertyMap,
typename ValueType = typename property_traits<PropertyMap>::value_type>
class python_dynamic_adaptor
: public boost::detail::dynamic_property_map_adaptor<PropertyMap>,
public python_dynamic_property_map
{
typedef boost::detail::dynamic_property_map_adaptor<PropertyMap> inherited;
public:
typedef typename property_traits<PropertyMap>::key_type key_type;
explicit python_dynamic_adaptor(const PropertyMap& property_map)
: inherited(property_map) { }
virtual void copy_value(const any& to, const any& from)
{
boost::put(this->base(), any_cast<key_type>(to),
boost::get(this->base(), any_cast<key_type>(from)));
}
virtual boost::python::object get_python(const any& key)
{
#if defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ == 95)
return boost::get(this->base(), any_cast<key_type>(key));
#else
using boost::get;
return boost::python::object(get(this->base(), any_cast<key_type>(key)));
#endif
}
};
template<typename PropertyMap>
class python_dynamic_adaptor<PropertyMap, boost::python::object>
: public boost::detail::dynamic_property_map_adaptor<PropertyMap>,
public python_dynamic_property_map
{
typedef boost::detail::dynamic_property_map_adaptor<PropertyMap> inherited;
public:
typedef typename property_traits<PropertyMap>::key_type key_type;
explicit python_dynamic_adaptor(const PropertyMap& property_map)
: inherited(property_map) { }
virtual void copy_value(const any& to, const any& from)
{
boost::put(this->base(), any_cast<key_type>(to),
boost::get(this->base(), any_cast<key_type>(from)));
}
virtual std::string get_string(const any& key)
{
using boost::python::extract;
using boost::python::str;
return std::string(
extract<const char*>(str(boost::get(this->base(),
any_cast<key_type>(key)))));
}
virtual boost::python::object get_python(const any& key)
{
#if defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ == 95)
return boost::get(this->base(), any_cast<key_type>(key));
#else
using boost::get;
return get(this->base(), any_cast<key_type>(key));
#endif
}
};
} } } // end namespace boost::graph::python
#if 0
// Triggers bugs in GCC
namespace boost {
template<typename DirectedS>
struct property_map<graph::python::basic_graph<DirectedS>, vertex_index_t>
{
typedef typename graph::python::basic_graph<DirectedS>::VertexIndexMap
type;
typedef type const_type;
};
template<typename DirectedS>
struct property_map<graph::python::basic_graph<DirectedS>, edge_index_t>
{
typedef typename graph::python::basic_graph<DirectedS>::EdgeIndexMap
type;
typedef type const_type;
};
}
#endif
#endif // BOOST_GRAPH_BASIC_GRAPH_HPP

View File

@ -1,15 +0,0 @@
// Copyright 2005 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
BGL_PYTHON_EVENT(initialize_vertex, vertex_descriptor)
BGL_PYTHON_EVENT(examine_edge, edge_descriptor)
BGL_PYTHON_EVENT(edge_relaxed, edge_descriptor)
BGL_PYTHON_EVENT(edge_not_relaxed, edge_descriptor)
BGL_PYTHON_EVENT(edge_minimized, edge_descriptor)
BGL_PYTHON_EVENT(edge_not_minimized, edge_descriptor)

View File

@ -1,111 +0,0 @@
// Copyright 2005 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
#include <boost/graph/bellman_ford_shortest_paths.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/python.hpp>
namespace boost { namespace graph { namespace python {
#define BGL_PYTHON_VISITOR bellman_ford_visitor
#define BGL_PYTHON_EVENTS_HEADER "bellman_ford_events.hpp"
#include "visitor.hpp"
#undef BGL_PYTHON_EVENTS_HEADER
#undef BGL_PYTHON_VISITOR
template<typename Graph>
void
bellman_ford_shortest_paths
(Graph& g, typename Graph::Vertex s,
const vector_property_map<typename Graph::Vertex,
typename Graph::VertexIndexMap>* in_predecessor,
const vector_property_map<double, typename Graph::VertexIndexMap>* in_distance,
const vector_property_map<double, typename Graph::EdgeIndexMap>* in_weight,
const bellman_ford_visitor<Graph>& visitor)
{
typedef vector_property_map<typename Graph::Vertex,
typename Graph::VertexIndexMap>
PredecessorMap;
typedef vector_property_map<double, typename Graph::VertexIndexMap>
DistanceMap;
typedef vector_property_map<double, typename Graph::EdgeIndexMap>
WeightMap;
PredecessorMap predecessor =
in_predecessor? *in_predecessor
: PredecessorMap(g.num_vertices(), g.get_vertex_index_map());
DistanceMap distance =
in_distance? *in_distance
: DistanceMap(g.num_vertices(), g.get_vertex_index_map());
WeightMap weight = in_weight? *in_weight
: g.template get_edge_map<double>("weight");
typedef typename bellman_ford_visitor<Graph>::default_arg default_visitor;
bool has_default_visitor = dynamic_cast<const default_visitor*>(&visitor);
if (!has_default_visitor) {
boost::bellman_ford_shortest_paths
(g,
root_vertex(s).
vertex_index_map(g.get_vertex_index_map()).
visitor(typename bellman_ford_visitor<Graph>::ref(visitor)).
predecessor_map(predecessor).
distance_map(distance).
weight_map(weight));
} else {
boost::bellman_ford_shortest_paths
(g,
root_vertex(s).
vertex_index_map(g.get_vertex_index_map()).
predecessor_map(predecessor).
distance_map(distance).
weight_map(weight));
}
}
template<typename Graph>
void export_bellman_ford_shortest_paths_in_graph()
{
bellman_ford_visitor<Graph>::declare("BellmanFordVisitor",
"DefaultBellmanFordVisitor");
}
void export_bellman_ford_shortest_paths()
{
using boost::python::arg;
using boost::python::def;
def("bellman_ford_shortest_paths", &bellman_ford_shortest_paths<Graph>,
(arg("graph"), arg("root_vertex"),
arg("predecessor_map") =
(vector_property_map<Graph::Vertex, Graph::VertexIndexMap>*)0,
arg("distance_map") =
(vector_property_map<double, Graph::VertexIndexMap>*)0,
arg("weight_map") =
(vector_property_map<double, Graph::EdgeIndexMap>*)0,
arg("visitor") = bellman_ford_visitor<Graph>::default_arg()));
def("bellman_ford_shortest_paths", &bellman_ford_shortest_paths<Digraph>,
(arg("graph"), arg("root_vertex"),
arg("predecessor_map") =
(vector_property_map<Digraph::Vertex, Digraph::VertexIndexMap>*)0,
arg("distance_map") =
(vector_property_map<double, Digraph::VertexIndexMap>*)0,
arg("weight_map") =
(vector_property_map<double, Digraph::EdgeIndexMap>*)0,
arg("visitor") = bellman_ford_visitor<Digraph>::default_arg()));
}
template void export_bellman_ford_shortest_paths_in_graph<Graph>();
template void export_bellman_ford_shortest_paths_in_graph<Digraph>();
} } } // end namespace boost::graph::python

View File

@ -1,196 +0,0 @@
// Copyright 2005 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
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/graph/betweenness_centrality.hpp>
#include <boost/graph/bc_clustering.hpp>
namespace boost { namespace graph { namespace python {
template<typename Graph>
void
brandes_betweenness_centrality
(Graph& g,
const vector_property_map<double, typename Graph::VertexIndexMap>* in_vertex_centrality,
const vector_property_map<double, typename Graph::EdgeIndexMap>* in_edge_centrality,
const vector_property_map<double, typename Graph::EdgeIndexMap>* weight)
{
typedef vector_property_map<double, typename Graph::VertexIndexMap>
VertexCentralityMap;
typedef vector_property_map<double, typename Graph::EdgeIndexMap>
EdgeCentralityMap;
VertexCentralityMap vertex_centrality =
in_vertex_centrality? *in_vertex_centrality
: g.template get_vertex_map<double>("centrality");
EdgeCentralityMap edge_centrality =
in_edge_centrality? *in_edge_centrality
: g.template get_edge_map<double>("centrality");
if (weight) {
boost::brandes_betweenness_centrality
(g,
weight_map(*weight).
centrality_map(vertex_centrality).
edge_centrality_map(edge_centrality).
vertex_index_map(g.get_vertex_index_map()));
} else {
boost::brandes_betweenness_centrality
(g,
centrality_map(vertex_centrality).
edge_centrality_map(edge_centrality).
vertex_index_map(g.get_vertex_index_map()));
}
}
template<typename Graph>
void
relative_betweenness_centrality
(Graph& g,
const vector_property_map<double, typename Graph::VertexIndexMap>* in_centrality)
{
typedef vector_property_map<double, typename Graph::VertexIndexMap>
CentralityMap;
CentralityMap centrality =
in_centrality? *in_centrality
: g.template get_vertex_map<double>("centrality");
relative_betweenness_centrality(g, centrality);
}
template<typename Graph>
double
central_point_dominance
(Graph& g,
const vector_property_map<double, typename Graph::VertexIndexMap>* in_centrality)
{
typedef vector_property_map<double, typename Graph::VertexIndexMap>
CentralityMap;
CentralityMap centrality =
in_centrality? *in_centrality
: g.template get_vertex_map<double>("centrality");
return boost::central_point_dominance(g, centrality);
}
struct bc_clustering_done_python
{
explicit bc_clustering_done_python(boost::python::object done)
: done(done) { }
template<typename Graph>
bool
operator()(double max_centrality,
typename graph_traits<Graph>::edge_descriptor e,
const Graph& g)
{
using boost::python::extract;
return extract<bool>(done(max_centrality, e, ref(g)));
}
private:
boost::python::object done;
};
template<typename Graph>
void
betweenness_centrality_clustering
(Graph& g, boost::python::object done,
const vector_property_map<double, typename Graph::EdgeIndexMap>* in_edge_centrality)
// const vector_property_map<double, typename Graph::EdgeIndexMap>* weight)
{
typedef vector_property_map<double, typename Graph::EdgeIndexMap>
EdgeCentralityMap;
EdgeCentralityMap edge_centrality =
in_edge_centrality? *in_edge_centrality
: g.template get_edge_map<double>("centrality");
#if 0
if (weight) {
boost::betweenness_centrality_clustering
(g,
weight_map(*weight).
centrality_map(vertex_centrality).
edge_centrality_map(edge_centrality).
vertex_index_map(g.get_vertex_index_map()));
} else {
#endif
boost::betweenness_centrality_clustering(g,
bc_clustering_done_python(done),
edge_centrality,
g.get_vertex_index_map());
// }
}
void export_betweenness_centrality()
{
using boost::python::arg;
using boost::python::def;
// Graph
def("brandes_betweenness_centrality",
&brandes_betweenness_centrality<Graph>,
(arg("graph"),
arg("vertex_centrality_map") =
(vector_property_map<double, Graph::VertexIndexMap>*)0,
arg("edge_centrality_map") =
(vector_property_map<double, Graph::EdgeIndexMap>*)0,
arg("weight_map") =
(vector_property_map<double, Graph::EdgeIndexMap>*)0));
def("relative_betweenness_centrality",
&relative_betweenness_centrality<Graph>,
(arg("graph"),
arg("vertex_centrality_map") =
(vector_property_map<double, Graph::VertexIndexMap>*)0));
def("central_point_dominance",
&central_point_dominance<Graph>,
(arg("graph"),
arg("vertex_centrality_map") =
(vector_property_map<double, Graph::VertexIndexMap>*)0));
def("betweenness_centrality_clustering",
&betweenness_centrality_clustering<Graph>,
(arg("graph"),
arg("done"),
arg("edge_centrality_map") =
(vector_property_map<double, Graph::EdgeIndexMap>*)0));
// Digraph
def("brandes_betweenness_centrality",
&brandes_betweenness_centrality<Digraph>,
(arg("graph"),
arg("vertex_centrality_map") =
(vector_property_map<double, Digraph::VertexIndexMap>*)0,
arg("edge_centrality_map") =
(vector_property_map<double, Digraph::EdgeIndexMap>*)0,
arg("weight_map") =
(vector_property_map<double, Digraph::EdgeIndexMap>*)0));
def("relative_betweenness_centrality",
&relative_betweenness_centrality<Digraph>,
(arg("graph"),
arg("vertex_centrality_map") =
(vector_property_map<double, Digraph::VertexIndexMap>*)0));
def("central_point_dominance",
&central_point_dominance<Digraph>,
(arg("graph"),
arg("vertex_centrality_map") =
(vector_property_map<double, Digraph::VertexIndexMap>*)0));
def("betweenness_centrality_clustering",
&betweenness_centrality_clustering<Digraph>,
(arg("graph"),
arg("done"),
arg("edge_centrality_map") =
(vector_property_map<double, Digraph::EdgeIndexMap>*)0));
}
} } } // end namespace boost::graph::python

View File

@ -1,18 +0,0 @@
// Copyright 2005 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
BGL_PYTHON_EVENT(initialize_vertex, vertex_descriptor)
BGL_PYTHON_EVENT(discover_vertex, vertex_descriptor)
BGL_PYTHON_EVENT(examine_vertex, vertex_descriptor)
BGL_PYTHON_EVENT(examine_edge, edge_descriptor)
BGL_PYTHON_EVENT(tree_edge, edge_descriptor)
BGL_PYTHON_EVENT(non_tree_edge, edge_descriptor)
BGL_PYTHON_EVENT(gray_target, edge_descriptor)
BGL_PYTHON_EVENT(black_target, edge_descriptor)
BGL_PYTHON_EVENT(finish_vertex, vertex_descriptor)

View File

@ -1,71 +0,0 @@
// Copyright 2005 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
#include <boost/graph/biconnected_components.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/python.hpp>
#include <list>
#include <iterator>
namespace boost { namespace graph { namespace python {
template<typename Graph>
boost::python::list
biconnected_components
(Graph& g,
const vector_property_map<int, typename Graph::EdgeIndexMap>* in_component)
{
typedef vector_property_map<int, typename Graph::EdgeIndexMap> ComponentMap;
ComponentMap component =
in_component? *in_component : g.template get_edge_map<int>("bicomponent");
std::list<typename Graph::Vertex> art_points;
boost::biconnected_components(g, component, std::back_inserter(art_points),
boost::vertex_index_map(g.get_vertex_index_map()));
boost::python::list result;
for (typename std::list<typename Graph::Vertex>::iterator i
= art_points.begin(); i != art_points.end(); ++i)
result.append(*i);
return result;
}
template<typename Graph>
boost::python::list
articulation_points(const Graph& g)
{
std::list<typename Graph::Vertex> art_points;
boost::python::list result;
boost::articulation_points(g, std::back_inserter(art_points),
boost::vertex_index_map(g.get_vertex_index_map()));
for (typename std::list<typename Graph::Vertex>::iterator i
= art_points.begin(); i != art_points.end(); ++i)
result.append(*i);
return result;
}
void export_biconnected_components()
{
using boost::python::arg;
using boost::python::def;
def("biconnected_components", &biconnected_components<Graph>,
(arg("graph"),
arg("component_map") =
(vector_property_map<int, Graph::EdgeIndexMap>*)0));
def("articulation_points", &articulation_points<Graph>, (arg("graph")));
def("biconnected_components", &biconnected_components<Digraph>,
(arg("graph"),
arg("component_map") =
(vector_property_map<int, Digraph::EdgeIndexMap>*)0));
def("articulation_points", &articulation_points<Digraph>, (arg("graph")));
}
} } } // end namespace boost::graph::python

View File

@ -1,147 +0,0 @@
// Copyright 2005 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
#include <boost/graph/breadth_first_search.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include "queue.hpp"
namespace boost { namespace graph { namespace python {
#define BGL_PYTHON_VISITOR bfs_visitor
#define BGL_PYTHON_EVENTS_HEADER "bfs_events.hpp"
#include "visitor.hpp"
#undef BGL_PYTHON_EVENTS_HEADER
#undef BGL_PYTHON_VISITOR
template<typename Graph>
void
breadth_first_search
(const Graph& g,
typename Graph::Vertex s,
python_queue<typename Graph::Vertex>& Q,
const bfs_visitor<Graph>& visitor,
const vector_property_map<default_color_type,
typename Graph::VertexIndexMap>* in_color)
{
typedef vector_property_map<default_color_type,
typename Graph::VertexIndexMap> ColorMap;
ColorMap color =
in_color? *in_color : ColorMap(g.num_vertices(), g.get_vertex_index_map());
typedef typename python_queue<typename Graph::Vertex>::default_queue
default_queue_type;
bool has_default_buffer = dynamic_cast<default_queue_type*>(&Q);
bool has_default_visitor =
dynamic_cast<typename bfs_visitor<Graph>::default_arg const*>(&visitor);
if (has_default_buffer) {
if (has_default_visitor) {
boost::breadth_first_search(g, s, color_map(color));
} else {
boost::breadth_first_search
(g, s,
color_map(color).
visitor(typename bfs_visitor<Graph>::ref(visitor)));
}
} else {
if (has_default_visitor) {
boost::breadth_first_search(g, s,
buffer(Q).
color_map(color));
} else {
boost::breadth_first_search
(g, s, Q, typename bfs_visitor<Graph>::ref(visitor), color);
}
}
}
template<typename Graph>
void
breadth_first_visit
(const Graph& g,
typename Graph::Vertex s,
python_queue<typename Graph::Vertex>& Q,
const bfs_visitor<Graph>& visitor,
const vector_property_map<default_color_type,
typename Graph::VertexIndexMap>& color)
{
typedef typename python_queue<typename Graph::Vertex>::default_queue
default_queue_type;
bool has_default_buffer = dynamic_cast<default_queue_type*>(&Q);
bool has_default_visitor =
dynamic_cast<typename bfs_visitor<Graph>::default_arg const*>(&visitor);
if (has_default_buffer) {
if (has_default_visitor) {
boost::breadth_first_visit(g, s, color_map(color));
} else {
boost::breadth_first_visit
(g, s,
color_map(color).
visitor(typename bfs_visitor<Graph>::ref(visitor)));
}
} else {
if (has_default_visitor) {
boost::breadth_first_visit(g, s,
buffer(Q).
color_map(color));
} else {
boost::breadth_first_visit
(g, s, Q, typename bfs_visitor<Graph>::ref(visitor), color);
}
}
}
void export_breadth_first_search()
{
using boost::python::arg;
using boost::python::def;
def("breadth_first_search", &breadth_first_search<Graph>,
(arg("graph"), "root_vertex",
arg("buffer") = python_queue<Graph::Vertex>::default_queue(),
arg("visitor") = bfs_visitor<Graph>::default_arg(),
arg("color_map") =
(vector_property_map<default_color_type, Graph::VertexIndexMap>*)0));
def("breadth_first_visit", &breadth_first_visit<Graph>,
(arg("graph"), "root_vertex",
arg("buffer") = python_queue<Graph::Vertex>::default_queue(),
arg("visitor") = bfs_visitor<Graph>::default_arg(),
arg("color_map")));
def("breadth_first_search", &breadth_first_search<Digraph>,
(arg("graph"), "root_vertex",
arg("buffer") = python_queue<Digraph::Vertex>::default_queue(),
arg("visitor") = bfs_visitor<Digraph>::default_arg(),
arg("color_map") =
(vector_property_map<default_color_type, Digraph::VertexIndexMap>*)0));
def("breadth_first_visit", &breadth_first_visit<Digraph>,
(arg("graph"), "root_vertex",
arg("buffer") = python_queue<Digraph::Vertex>::default_queue(),
arg("visitor") = bfs_visitor<Digraph>::default_arg(),
arg("color_map")));
}
template<typename Graph>
void export_breadth_first_search_in_graph()
{
bfs_visitor<Graph>::declare("BFSVisitor", "DefaultBFSVisitor");
python_queue<typename Graph::Vertex>::declare("VertexQueue",
"DefaultVertexQueue");
}
template void export_breadth_first_search_in_graph<Graph>();
template void export_breadth_first_search_in_graph<Digraph>();
} } } // end namespace boost::graph::python

View File

@ -1,52 +0,0 @@
// Copyright 2005 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
#include <boost/graph/circle_layout.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include "point2d.hpp"
namespace boost { namespace graph { namespace python {
template<typename Graph>
void
circle_graph_layout
(Graph& g,
const vector_property_map<point2d, typename Graph::VertexIndexMap>* in_pos,
double radius)
{
typedef vector_property_map<point2d, typename Graph::VertexIndexMap>
PositionMap;
PositionMap pos =
in_pos? *in_pos : g.template get_vertex_map<point2d>("position");
circle_graph_layout(g, pos, radius);
}
void export_circle_graph_layout()
{
using boost::python::arg;
using boost::python::def;
def("circle_graph_layout",
&circle_graph_layout<Graph>,
(arg("graph"),
arg("position") =
(vector_property_map<point2d, Graph::VertexIndexMap>*)0,
arg("radius") = 250.0));
def("circle_graph_layout",
&circle_graph_layout<Digraph>,
(arg("graph"),
arg("position") =
(vector_property_map<point2d, Digraph::VertexIndexMap>*)0,
arg("radius") = 250.0));
}
} } } // end namespace boost::graph::python

View File

@ -1,51 +0,0 @@
// Copyright 2005 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
#include <boost/graph/connected_components.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/python.hpp>
namespace boost { namespace graph { namespace python {
template<typename Graph>
int
connected_components
(Graph& g,
const vector_property_map<int, typename Graph::VertexIndexMap>* in_component,
const vector_property_map<default_color_type,
typename Graph::VertexIndexMap>* in_color)
{
typedef vector_property_map<int, typename Graph::VertexIndexMap> ComponentMap;
typedef vector_property_map<default_color_type,
typename Graph::VertexIndexMap> ColorMap;
ComponentMap component =
in_component? *in_component : g.template get_vertex_map<int>("component");
ColorMap color =
in_color? *in_color : ColorMap(g.num_vertices(), g.get_vertex_index_map());
return boost::connected_components(g, component, color_map(color));
}
void export_connected_components()
{
using boost::python::arg;
using boost::python::def;
def("connected_components", &connected_components<Graph>,
(arg("graph"),
arg("component_map") =
(vector_property_map<int, Graph::VertexIndexMap>*)0,
arg("color_map") =
(vector_property_map<default_color_type, Graph::VertexIndexMap>*)0));
}
} } } // end namespace boost::graph::python

View File

@ -1,39 +0,0 @@
// Copyright 2005 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
#include <boost/graph/cuthill_mckee_ordering.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/python.hpp>
#include <list>
#include <iterator>
namespace boost { namespace graph { namespace python {
template<typename Graph>
boost::python::list
cuthill_mckee_ordering(const Graph& g)
{
std::list<typename Graph::Vertex> ordering;
boost::python::list result;
boost::cuthill_mckee_ordering(g, std::back_inserter(ordering),
g.get_vertex_index_map());
for (typename std::list<typename Graph::Vertex>::iterator i
= ordering.begin(); i != ordering.end(); ++i)
result.append(*i);
return result;
}
void export_cuthill_mckee_ordering()
{
using boost::python::arg;
using boost::python::def;
def("cuthill_mckee_ordering", &cuthill_mckee_ordering<Graph>, arg("graph"));
}
} } } // end namespace boost::graph::python

View File

@ -1,103 +0,0 @@
// Copyright 2005 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
#include <boost/graph/dag_shortest_paths.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/python.hpp>
#include "dijkstra_visitor.hpp"
namespace boost { namespace graph { namespace python {
template<typename Graph>
void
dag_shortest_paths
(Graph& g, typename Graph::Vertex s,
const vector_property_map<typename Graph::Vertex,
typename Graph::VertexIndexMap>* in_predecessor,
const vector_property_map<double, typename Graph::VertexIndexMap>* in_distance,
const vector_property_map<double, typename Graph::EdgeIndexMap>* in_weight,
const dijkstra_visitor<Graph>& visitor)
{
typedef vector_property_map<typename Graph::Vertex,
typename Graph::VertexIndexMap>
PredecessorMap;
typedef vector_property_map<double, typename Graph::VertexIndexMap>
DistanceMap;
typedef vector_property_map<double, typename Graph::EdgeIndexMap>
WeightMap;
PredecessorMap predecessor =
in_predecessor? *in_predecessor
: PredecessorMap(g.num_vertices(), g.get_vertex_index_map());
DistanceMap distance =
in_distance? *in_distance
: DistanceMap(g.num_vertices(), g.get_vertex_index_map());
WeightMap weight = in_weight? *in_weight
: g.template get_edge_map<double>("weight");
typedef typename dijkstra_visitor<Graph>::default_arg default_visitor;
bool has_default_visitor = dynamic_cast<const default_visitor*>(&visitor);
if (!has_default_visitor) {
boost::dag_shortest_paths
(g, s,
vertex_index_map(g.get_vertex_index_map()).
visitor(typename dijkstra_visitor<Graph>::ref(visitor)).
predecessor_map(predecessor).
distance_map(distance).
weight_map(weight));
} else {
boost::dag_shortest_paths
(g, s,
vertex_index_map(g.get_vertex_index_map()).
predecessor_map(predecessor).
distance_map(distance).
weight_map(weight));
}
}
template<typename Graph>
void export_dag_shortest_paths_in_graph()
{
dijkstra_visitor<Graph>::declare("DijkstraVisitor",
"DefaultDijkstraVisitor");
}
void export_dag_shortest_paths()
{
using boost::python::arg;
using boost::python::def;
def("dag_shortest_paths", &dag_shortest_paths<Graph>,
(arg("graph"), arg("root_vertex"),
arg("predecessor_map") =
(vector_property_map<Graph::Vertex, Graph::VertexIndexMap>*)0,
arg("distance_map") =
(vector_property_map<double, Graph::VertexIndexMap>*)0,
arg("weight_map") =
(vector_property_map<double, Graph::EdgeIndexMap>*)0,
arg("visitor") = dijkstra_visitor<Graph>::default_arg()));
def("dag_shortest_paths", &dag_shortest_paths<Digraph>,
(arg("graph"), arg("root_vertex"),
arg("predecessor_map") =
(vector_property_map<Digraph::Vertex, Digraph::VertexIndexMap>*)0,
arg("distance_map") =
(vector_property_map<double, Digraph::VertexIndexMap>*)0,
arg("weight_map") =
(vector_property_map<double, Digraph::EdgeIndexMap>*)0,
arg("visitor") = dijkstra_visitor<Digraph>::default_arg()));
}
template void export_dag_shortest_paths_in_graph<Graph>();
template void export_dag_shortest_paths_in_graph<Digraph>();
} } } // end namespace boost::graph::python

View File

@ -1,163 +0,0 @@
// Copyright 2005 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
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/undirected_dfs.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include "queue.hpp"
namespace boost { namespace graph { namespace python {
#define BGL_PYTHON_VISITOR dfs_visitor
#define BGL_PYTHON_EVENTS_HEADER "dfs_events.hpp"
#include "visitor.hpp"
#undef BGL_PYTHON_EVENTS_HEADER
#undef BGL_PYTHON_VISITOR
template<typename Graph>
void
depth_first_search
(const Graph& g,
typename Graph::Vertex s,
const dfs_visitor<Graph>& visitor,
const vector_property_map<default_color_type,
typename Graph::VertexIndexMap>* in_color)
{
typedef vector_property_map<default_color_type,
typename Graph::VertexIndexMap> ColorMap;
ColorMap color =
in_color? *in_color : ColorMap(g.num_vertices(), g.get_vertex_index_map());
bool has_default_visitor =
dynamic_cast<typename dfs_visitor<Graph>::default_arg const*>(&visitor);
if (s == graph_traits<Graph>::null_vertex() && g.num_vertices() > 0)
s = *g.vertices().first;
if (has_default_visitor) {
boost::depth_first_search(g, boost::dfs_visitor<>(), color, s);
} else {
boost::depth_first_search(g, typename dfs_visitor<Graph>::ref(visitor),
color, s);
}
}
template<typename Graph>
void
depth_first_visit
(const Graph& g,
typename Graph::Vertex s,
const dfs_visitor<Graph>& visitor,
const vector_property_map<default_color_type,
typename Graph::VertexIndexMap>* in_color)
{
typedef vector_property_map<default_color_type,
typename Graph::VertexIndexMap> ColorMap;
ColorMap color =
in_color? *in_color : ColorMap(g.num_vertices(), g.get_vertex_index_map());
bool has_default_visitor =
dynamic_cast<typename dfs_visitor<Graph>::default_arg const*>(&visitor);
if (has_default_visitor) {
boost::depth_first_visit(g, s, boost::dfs_visitor<>(), color);
} else {
boost::depth_first_visit(g, s, typename dfs_visitor<Graph>::ref(visitor),
color);
}
}
template<typename Graph>
void
undirected_dfs
(const Graph& g,
const dfs_visitor<Graph>& visitor,
const vector_property_map<default_color_type,
typename Graph::VertexIndexMap>* in_color,
const vector_property_map<default_color_type,
typename Graph::EdgeIndexMap>* in_edge_color)
{
typedef vector_property_map<default_color_type,
typename Graph::VertexIndexMap> ColorMap;
typedef vector_property_map<default_color_type,
typename Graph::EdgeIndexMap> EdgeColorMap;
ColorMap color =
in_color? *in_color : ColorMap(g.num_vertices(), g.get_vertex_index_map());
EdgeColorMap edge_color =
in_edge_color? *in_edge_color
: EdgeColorMap(g.num_edges(), g.get_edge_index_map());
bool has_default_visitor =
dynamic_cast<typename dfs_visitor<Graph>::default_arg const*>(&visitor);
if (has_default_visitor) {
boost::undirected_dfs(g, boost::dfs_visitor<>(), color, edge_color);
} else {
boost::undirected_dfs(g, typename dfs_visitor<Graph>::ref(visitor),
color, edge_color);
}
}
void export_depth_first_search()
{
using boost::python::arg;
using boost::python::def;
def("depth_first_search", &depth_first_search<Graph>,
(arg("graph"),
arg("root_vertex") = graph_traits<Graph>::null_vertex(),
arg("visitor") = dfs_visitor<Graph>::default_arg(),
arg("color_map") =
(vector_property_map<default_color_type, Graph::VertexIndexMap>*)0));
def("depth_first_visit", &depth_first_visit<Graph>,
(arg("graph"),
arg("root_vertex"),
arg("visitor") = dfs_visitor<Graph>::default_arg(),
arg("color_map") =
(vector_property_map<default_color_type, Graph::VertexIndexMap>*)0));
def("undirected_dfs", &undirected_dfs<Graph>,
(arg("graph"),
arg("visitor") = dfs_visitor<Graph>::default_arg(),
arg("color_map") =
(vector_property_map<default_color_type, Graph::VertexIndexMap>*)0,
arg("edge_color_map") =
(vector_property_map<default_color_type, Graph::EdgeIndexMap>*)0));
def("depth_first_search", &depth_first_search<Digraph>,
(arg("graph"),
arg("root_vertex") = graph_traits<Digraph>::null_vertex(),
arg("visitor") = dfs_visitor<Digraph>::default_arg(),
arg("color_map") =
(vector_property_map<default_color_type, Digraph::VertexIndexMap>*)0));
def("depth_first_visit", &depth_first_visit<Digraph>,
(arg("graph"),
arg("root_vertex"),
arg("visitor") = dfs_visitor<Digraph>::default_arg(),
arg("color_map") =
(vector_property_map<default_color_type, Digraph::VertexIndexMap>*)0));
}
template<typename Graph>
void export_depth_first_search_in_graph()
{
dfs_visitor<Graph>::declare("DFSVisitor", "DefaultDFSVisitor");
}
template void export_depth_first_search_in_graph<Graph>();
template void export_depth_first_search_in_graph<Digraph>();
} } } // end namespace boost::graph::python

View File

@ -1,18 +0,0 @@
// Copyright 2005 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
BGL_PYTHON_EVENT(initialize_vertex, vertex_descriptor)
BGL_PYTHON_EVENT(start_vertex, vertex_descriptor)
BGL_PYTHON_EVENT(discover_vertex, vertex_descriptor)
BGL_PYTHON_EVENT(examine_vertex, vertex_descriptor)
BGL_PYTHON_EVENT(examine_edge, edge_descriptor)
BGL_PYTHON_EVENT(tree_edge, edge_descriptor)
BGL_PYTHON_EVENT(back_edge, edge_descriptor)
BGL_PYTHON_EVENT(forward_or_cross_edge, edge_descriptor)
BGL_PYTHON_EVENT(finish_vertex, vertex_descriptor)

View File

@ -1,18 +0,0 @@
// Copyright 2005 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
#include "digraph.hpp"
#include "basic_graph.cpp"
namespace boost { namespace graph { namespace python {
void export_Digraph()
{
export_basic_graph<bidirectionalS>("Digraph");
}
} } } // end namespace boost::graph::python

View File

@ -1,18 +0,0 @@
// Copyright 2005 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
#ifndef BOOST_GRAPH_PYTHON_DIGRAPH_HPP
#define BOOST_GRAPH_PYTHON_DIGRAPH_HPP
#include "basic_graph.hpp"
namespace boost { namespace graph { namespace python {
typedef basic_graph<bidirectionalS> Digraph;
} } } // end namespace boost::graph::python
#endif // BOOST_GRAPH_PYTHON_DIGRAPH_HPP

View File

@ -1,15 +0,0 @@
// Copyright 2005 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
BGL_PYTHON_EVENT(initialize_vertex, vertex_descriptor)
BGL_PYTHON_EVENT(examine_vertex, vertex_descriptor)
BGL_PYTHON_EVENT(examine_edge, edge_descriptor)
BGL_PYTHON_EVENT(discover_vertex, vertex_descriptor)
BGL_PYTHON_EVENT(edge_relaxed, edge_descriptor)
BGL_PYTHON_EVENT(edge_not_relaxed, edge_descriptor)
BGL_PYTHON_EVENT(finish_vertex, vertex_descriptor)

View File

@ -1,103 +0,0 @@
// Copyright 2005 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
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/python.hpp>
#include "dijkstra_visitor.hpp"
namespace boost { namespace graph { namespace python {
template<typename Graph>
void
dijkstra_shortest_paths
(Graph& g, typename Graph::Vertex s,
const vector_property_map<typename Graph::Vertex,
typename Graph::VertexIndexMap>* in_predecessor,
const vector_property_map<double, typename Graph::VertexIndexMap>* in_distance,
const vector_property_map<double, typename Graph::EdgeIndexMap>* in_weight,
const dijkstra_visitor<Graph>& visitor)
{
typedef vector_property_map<typename Graph::Vertex,
typename Graph::VertexIndexMap>
PredecessorMap;
typedef vector_property_map<double, typename Graph::VertexIndexMap>
DistanceMap;
typedef vector_property_map<double, typename Graph::EdgeIndexMap>
WeightMap;
PredecessorMap predecessor =
in_predecessor? *in_predecessor
: PredecessorMap(g.num_vertices(), g.get_vertex_index_map());
DistanceMap distance =
in_distance? *in_distance
: DistanceMap(g.num_vertices(), g.get_vertex_index_map());
WeightMap weight = in_weight? *in_weight
: g.template get_edge_map<double>("weight");
typedef typename dijkstra_visitor<Graph>::default_arg default_visitor;
bool has_default_visitor = dynamic_cast<const default_visitor*>(&visitor);
if (!has_default_visitor) {
boost::dijkstra_shortest_paths
(g, s,
vertex_index_map(g.get_vertex_index_map()).
visitor(typename dijkstra_visitor<Graph>::ref(visitor)).
predecessor_map(predecessor).
distance_map(distance).
weight_map(weight));
} else {
boost::dijkstra_shortest_paths
(g, s,
vertex_index_map(g.get_vertex_index_map()).
predecessor_map(predecessor).
distance_map(distance).
weight_map(weight));
}
}
template<typename Graph>
void export_dijkstra_shortest_paths_in_graph()
{
dijkstra_visitor<Graph>::declare("DijkstraVisitor",
"DefaultDijkstraVisitor");
}
void export_dijkstra_shortest_paths()
{
using boost::python::arg;
using boost::python::def;
def("dijkstra_shortest_paths", &dijkstra_shortest_paths<Graph>,
(arg("graph"), arg("root_vertex"),
arg("predecessor_map") =
(vector_property_map<Graph::Vertex, Graph::VertexIndexMap>*)0,
arg("distance_map") =
(vector_property_map<double, Graph::VertexIndexMap>*)0,
arg("weight_map") =
(vector_property_map<double, Graph::EdgeIndexMap>*)0,
arg("visitor") = dijkstra_visitor<Graph>::default_arg()));
def("dijkstra_shortest_paths", &dijkstra_shortest_paths<Digraph>,
(arg("graph"), arg("root_vertex"),
arg("predecessor_map") =
(vector_property_map<Digraph::Vertex, Digraph::VertexIndexMap>*)0,
arg("distance_map") =
(vector_property_map<double, Digraph::VertexIndexMap>*)0,
arg("weight_map") =
(vector_property_map<double, Digraph::EdgeIndexMap>*)0,
arg("visitor") = dijkstra_visitor<Digraph>::default_arg()));
}
template void export_dijkstra_shortest_paths_in_graph<Graph>();
template void export_dijkstra_shortest_paths_in_graph<Digraph>();
} } } // end namespace boost::graph::python

View File

@ -1,20 +0,0 @@
// Copyright 2005 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
#ifndef BOOST_GRAPH_PYTHON_DIJKSTRA_VISITOR_HPP
#define BOOST_GRAPH_PYTHON_DIJKSTRA_VISITOR_HPP
namespace boost { namespace graph { namespace python {
#define BGL_PYTHON_VISITOR dijkstra_visitor
#define BGL_PYTHON_EVENTS_HEADER "dijkstra_events.hpp"
#include "visitor.hpp"
#undef BGL_PYTHON_EVENTS_HEADER
#undef BGL_PYTHON_VISITOR
} } } // end namespace boost::graph::python
#endif BOOST_GRAPH_PYTHON_DIJKSTRA_VISITOR_HPP

View File

@ -1,136 +0,0 @@
// Copyright 2005 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
#include <boost/graph/fruchterman_reingold.hpp>
#include <boost/graph/random_layout.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include "point2d.hpp"
#include <boost/random/linear_congruential.hpp>
#include <ctime>
namespace boost { namespace graph { namespace python {
template<typename F, typename Result = double>
struct python_or_functor
{
explicit python_or_functor(boost::python::object callable, const F& f = F())
: callable(callable), f(f) { }
// For cooling
Result operator()()
{
using boost::python::object;
using boost::python::extract;
if (callable != object()) return extract<Result>(callable());
else return f();
}
// For the attractive_force
template<typename Graph>
Result
operator()(typename graph_traits<Graph>::edge_descriptor e,
double k, double dist, const Graph& g) const
{
using boost::python::object;
using boost::python::extract;
if (callable != object()) return extract<Result>(callable(e, k, dist, g));
else return f(e, k, dist, g);
}
// For the repulsive_force
template<typename Graph>
Result
operator()(typename graph_traits<Graph>::vertex_descriptor u,
typename graph_traits<Graph>::vertex_descriptor v,
double k, double dist, const Graph& g) const
{
using boost::python::object;
using boost::python::extract;
if (callable != object())
return extract<Result>(callable(u, v, k, dist, g));
else return f(u, v, k, dist, g);
}
private:
boost::python::object callable;
F f;
};
template<typename Graph>
void
fruchterman_reingold_force_directed_layout
(Graph& g,
const vector_property_map<point2d, typename Graph::VertexIndexMap>* in_pos,
double width, double height,
boost::python::object attractive_force,
boost::python::object repulsive_force,
// TBD: force pairs?
boost::python::object cooling,
bool progressive)
{
using boost::python::object;
typedef vector_property_map<point2d, typename Graph::VertexIndexMap>
PositionMap;
PositionMap pos =
in_pos? *in_pos : g.template get_vertex_map<point2d>("position");
if (!progressive) {
minstd_rand gen(std::time(0));
random_graph_layout(g, pos, -width/2, width/2, -height/2, height/2, gen);
}
python_or_functor<linear_cooling<double> > cool(cooling, 100);
if (attractive_force != object() || repulsive_force != object()) {
python_or_functor<square_distance_attractive_force> fa(attractive_force);
python_or_functor<square_distance_repulsive_force> fr(repulsive_force);
boost::fruchterman_reingold_force_directed_layout
(g, pos, width, height,
boost::vertex_index_map(g.get_vertex_index_map()).
attractive_force(fa).repulsive_force(fr).
cooling(cool));
} else {
if (cooling != object()) {
boost::fruchterman_reingold_force_directed_layout
(g, pos, width, height,
boost::vertex_index_map(g.get_vertex_index_map()).
cooling(cool));
} else {
boost::fruchterman_reingold_force_directed_layout
(g, pos, width, height,
vertex_index_map(g.get_vertex_index_map()));
}
}
}
void export_fruchterman_reingold_force_directed_layout()
{
using boost::python::arg;
using boost::python::def;
using boost::python::object;
def("fruchterman_reingold_force_directed_layout",
&fruchterman_reingold_force_directed_layout<Graph>,
(arg("graph"),
arg("position") =
(vector_property_map<point2d, Graph::VertexIndexMap>*)0,
arg("width") = 500.0,
arg("height") = 500.0,
arg("attractive_force") = object(),
arg("repulsive_force") = object(),
arg("cooling") = object(),
arg("progressive") = false));
}
} } } // end namespace boost::graph::python

View File

@ -1,45 +0,0 @@
// Copyright 2004-5 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
#ifndef BOOST_GRAPH_PYTHON_GENERATORS_HPP
#define BOOST_GRAPH_PYTHON_GENERATORS_HPP
#include <cstdlib>
namespace boost { namespace graph { namespace python {
struct erdos_renyi
{
erdos_renyi(std::size_t n, double p) : n(n), p(p) { }
std::size_t n;
double p;
};
struct power_law_out_degree
{
power_law_out_degree(std::size_t n, double alpha, double beta)
: n(n), alpha(alpha), beta(beta) { }
std::size_t n;
double alpha;
double beta;
};
struct small_world
{
small_world(std::size_t n, std::size_t k, double p) : n(n), k(k), p(p) { }
std::size_t n;
std::size_t k;
double p;
};
} } } // end namespace boost::graph::python
#endif // BOOST_GRAPH_PYTHON_GENERATORS_HPP

View File

@ -1,19 +0,0 @@
// Copyright 2005 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
#include "graph.hpp"
#include "basic_graph.cpp"
#include <iostream>
namespace boost { namespace graph { namespace python {
void export_Graph()
{
export_basic_graph<undirectedS>("Graph");
}
} } } // end namespace boost::graph::python

View File

@ -1,36 +0,0 @@
// Copyright 2005 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
#ifndef BOOST_GRAPH_PYTHON_GRAPH_HPP
#define BOOST_GRAPH_PYTHON_GRAPH_HPP
#include "basic_graph.hpp"
namespace boost { namespace graph { namespace python {
typedef basic_graph<undirectedS> Graph;
#if 0
class Graph : public basic_graph<undirectedS>
{
typedef basic_graph<undirectedS> inherited;
public:
Graph() : inherited() { }
Graph(const std::string& filename, graph_file_kind kind)
: inherited(filename, kind) { }
vertex_iterator vertices_begin() const;
vertex_iterator vertices_end() const;
edge_iterator edges_begin() const;
edge_iterator edges_end() const;
};
#endif
} } } // end namespace boost::graph::python
#endif // BOOST_GRAPH_PYTHON_GRAPH_HPP

View File

@ -1,114 +0,0 @@
// Copyright 2005 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
#include "basic_graph.hpp"
#include <boost/graph/graphviz.hpp>
#include <boost/python.hpp>
#include <fstream>
#include <string>
namespace boost { namespace graph { namespace python {
template<typename DirectedS>
void
basic_graph<DirectedS>::read_graphviz(const std::string& filename,
const std::string& node_id)
{
std::ifstream in(filename.c_str());
boost::read_graphviz(in, *this, dp, node_id);
}
template<typename DirectedS>
void
basic_graph<DirectedS>::write_graphviz(const std::string& filename,
const std::string& node_id)
{
std::ofstream out(filename.c_str());
if (has_vertex_map(node_id))
boost::write_graphviz(out, *this, dp, node_id,
get_vertex_map<std::string>(node_id));
else
boost::write_graphviz(out, *this, dp, node_id, get_vertex_index_map());
}
template<typename E>
class translate_exception
{
explicit translate_exception(boost::python::object type) : type(type) { }
public:
template<typename Base>
static void declare(const char* name)
{
using boost::python::class_;
using boost::python::bases;
declare(class_<E, bases<Base> >(name));
}
static void declare(boost::python::object type)
{
using boost::python::register_exception_translator;
register_exception_translator<E>(translate_exception(type));
}
void operator()(const E& e) const
{
using boost::python::object;
PyErr_SetObject(type.ptr(), object(e).ptr());
}
private:
boost::python::object type;
};
void export_graphviz()
{
using boost::python::class_;
using boost::python::bases;
using boost::python::init;
using boost::python::no_init;
using boost::python::object;
object ge_type =
class_<graph_exception, noncopyable >("graph_exception", no_init);
translate_exception<graph_exception>::declare(ge_type);
object bpe_type =
class_<bad_parallel_edge, bases<graph_exception> >("bad_parallel_edge",
no_init)
.def(init<std::string, std::string>());
translate_exception<bad_parallel_edge>::declare(bpe_type);
translate_exception<directed_graph_error>
::declare<graph_exception>("directed_graph_error");
translate_exception<undirected_graph_error>
::declare<graph_exception>("undirected_graph_error");
}
// Explicit instantiations
template
void
basic_graph<undirectedS>::read_graphviz(const std::string& filename,
const std::string& node_id);
template
void
basic_graph<undirectedS>::write_graphviz(const std::string& filename,
const std::string& node_id);
template
void
basic_graph<bidirectionalS>::read_graphviz(const std::string& filename,
const std::string& node_id);
template
void
basic_graph<bidirectionalS>::write_graphviz(const std::string& filename,
const std::string& node_id);
} } } // end namespace boost::graph::python

View File

@ -1,68 +0,0 @@
// Copyright 2005 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
#include <boost/graph/incremental_components.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/python.hpp>
namespace boost { namespace graph { namespace python {
template<typename Graph>
class IncrementalComponents
{
public:
typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
typedef vector_property_map<vertex_descriptor,
typename Graph::VertexIndexMap> ParentMap;
typedef vector_property_map<std::size_t,
typename Graph::VertexIndexMap> RankMap;
IncrementalComponents(const Graph& g)
: ds(RankMap(num_vertices(g), g.get_vertex_index_map()),
ParentMap(num_vertices(g), g.get_vertex_index_map()))
{
initialize_incremental_components(g, ds);
incremental_components(g, ds);
}
void make_set(vertex_descriptor u) { ds.make_set(u); }
void union_set(vertex_descriptor u, vertex_descriptor v)
{ ds.union_set(u, v); }
bool same_component(vertex_descriptor u, vertex_descriptor v)
{ return boost::same_component(u, v, ds); }
private:
disjoint_sets<RankMap, ParentMap> ds;
};
template<typename Graph>
std::auto_ptr<IncrementalComponents<Graph> >
incremental_components(Graph& g)
{
typedef std::auto_ptr<IncrementalComponents<Graph> > result_type;
return result_type(new IncrementalComponents<Graph>(g));
}
void export_incremental_components()
{
using boost::python::arg;
using boost::python::def;
using boost::python::class_;
using boost::python::no_init;
class_<IncrementalComponents<Graph> >("IncrementalComponents", no_init)
.def("make_set", &IncrementalComponents<Graph>::make_set)
.def("union_set", &IncrementalComponents<Graph>::union_set)
.def("same_component", &IncrementalComponents<Graph>::same_component)
;
def("incremental_components", &incremental_components<Graph>);
}
} } } // end namespace boost::graph::python

View File

@ -1,80 +0,0 @@
// Copyright 2005 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
#include <boost/graph/isomorphism.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/python.hpp>
namespace boost { namespace graph { namespace python {
template<typename Vertex>
struct py_vertex_invariant
{
explicit py_vertex_invariant(boost::python::object invariant)
: invariant(invariant) { }
int operator()(const Vertex& v1, const Vertex& v2)
{ return boost::python::extract<int>(invariant(v1, v2)); }
private:
boost::python::object invariant;
};
template<typename Graph>
bool
isomorphism
(Graph& g1,
Graph& g2,
const vector_property_map<typename Graph::Vertex,
typename Graph::VertexIndexMap>* in_iso,
boost::python::object invariant)
{
typedef typename Graph::Vertex Vertex;
typedef vector_property_map<Vertex, typename Graph::VertexIndexMap> IsoMap;
IsoMap iso =
in_iso? *in_iso
: IsoMap(num_vertices(g1), g1.get_vertex_index_map());
if (invariant != boost::python::object())
return boost::isomorphism
(g1, g2,
isomorphism_map(iso).
vertex_invariant(py_vertex_invariant<Vertex>(invariant)).
vertex_index1_map(g1.get_vertex_index_map()).
vertex_index2_map(g2.get_vertex_index_map()));
else
return boost::isomorphism
(g1, g2,
isomorphism_map(iso).
vertex_index1_map(g1.get_vertex_index_map()).
vertex_index2_map(g2.get_vertex_index_map()));
}
void export_isomorphism()
{
using boost::python::arg;
using boost::python::def;
using boost::python::object;
def("isomorphism", &isomorphism<Graph>,
(arg("g1"), arg("g2"),
arg("isomorphism_map") =
(vector_property_map<Graph::Vertex, Graph::VertexIndexMap>*)0,
arg("vertex_invariant") = object()));
def("isomorphism", &isomorphism<Digraph>,
(arg("g1"), arg("g2"),
arg("isomorphism_map") =
(vector_property_map<Digraph::Vertex, Digraph::VertexIndexMap>*)0,
arg("vertex_invariant") = object()));
}
} } } // end namespace boost::graph::python

View File

@ -1,80 +0,0 @@
// Copyright 2005 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
#include <boost/graph/kamada_kawai_spring_layout.hpp>
#include <boost/graph/circle_layout.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include "point2d.hpp"
#include <boost/graph/iteration_macros.hpp>
namespace boost { namespace graph { namespace python {
template<typename Graph>
void
kamada_kawai_spring_layout
(Graph& g,
const vector_property_map<point2d, typename Graph::VertexIndexMap>* in_pos,
const vector_property_map<double, typename Graph::EdgeIndexMap>* in_weight,
double side_length,
boost::python::object done,
double spring_constant,
bool progressive)
{
using boost::python::object;
typedef vector_property_map<point2d, typename Graph::VertexIndexMap>
PositionMap;
typedef vector_property_map<double, typename Graph::EdgeIndexMap>
WeightMap;
PositionMap pos =
in_pos? *in_pos : g.template get_vertex_map<point2d>("position");
WeightMap weight =
in_weight? *in_weight : WeightMap(num_edges(g), g.get_edge_index_map());
// If we weren't give a weight map, assume unweighted edges
if (!in_weight) BGL_FORALL_EDGES_T(e, g, Graph) put(weight, e, 1.0);
if (!progressive) circle_graph_layout(g, pos, side_length/2);
if (done != object()) {
boost::kamada_kawai_spring_layout(g, pos, weight,
boost::side_length(side_length), done,
spring_constant,
g.get_vertex_index_map());
} else {
boost::kamada_kawai_spring_layout(g, pos, weight,
boost::side_length(side_length),
layout_tolerance<double>(),
spring_constant,
g.get_vertex_index_map());
}
}
void export_kamada_kawai_spring_layout()
{
using boost::python::arg;
using boost::python::def;
using boost::python::object;
def("kamada_kawai_spring_layout",
&kamada_kawai_spring_layout<Graph>,
(arg("graph"),
arg("position") =
(vector_property_map<point2d, Graph::VertexIndexMap>*)0,
arg("weight") =
(vector_property_map<double, Graph::EdgeIndexMap>*)0,
arg("side_length") = 500.0,
arg("done") = object(),
arg("spring_constant") = 1.0,
arg("progressive") = false));
}
} } } // end namespace boost::graph::python

View File

@ -1,39 +0,0 @@
// Copyright 2005 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
#include <boost/graph/king_ordering.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/python.hpp>
#include <list>
#include <iterator>
namespace boost { namespace graph { namespace python {
template<typename Graph>
boost::python::list
king_ordering(const Graph& g)
{
std::list<typename Graph::Vertex> ordering;
boost::python::list result;
boost::king_ordering(g, std::back_inserter(ordering),
g.get_vertex_index_map());
for (typename std::list<typename Graph::Vertex>::iterator i
= ordering.begin(); i != ordering.end(); ++i)
result.append(*i);
return result;
}
void export_king_ordering()
{
using boost::python::arg;
using boost::python::def;
def("king_ordering", &king_ordering<Graph>, arg("graph"));
}
} } } // end namespace boost::graph::python

View File

@ -1,53 +0,0 @@
// Copyright 2005 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
#include <boost/graph/kruskal_min_spanning_tree.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/python.hpp>
#include <list>
#include <iterator>
namespace boost { namespace graph { namespace python {
template<typename Graph>
boost::python::list
kruskal_minimum_spanning_tree
(Graph& g,
const vector_property_map<double, typename Graph::EdgeIndexMap>* in_weight)
{
typedef vector_property_map<double, typename Graph::EdgeIndexMap> WeightMap;
WeightMap weight =
in_weight? *in_weight : g.template get_edge_map<double>("weight");
std::list<typename Graph::Edge> mst_edges;
boost::kruskal_minimum_spanning_tree
(g,
std::back_inserter(mst_edges),
vertex_index_map(g.get_vertex_index_map()).
weight_map(weight));
boost::python::list result;
for (typename std::list<typename Graph::Edge>::iterator i
= mst_edges.begin(); i != mst_edges.end(); ++i)
result.append(*i);
return result;
}
void export_kruskal_minimum_spanning_tree()
{
using boost::python::arg;
using boost::python::def;
def("kruskal_minimum_spanning_tree", &kruskal_minimum_spanning_tree<Graph>,
(arg("graph"),
arg("weight_map") =
(vector_property_map<double, Graph::EdgeIndexMap>*)0));
}
} } } // end namespace boost::graph::python

View File

@ -1,145 +0,0 @@
// Copyright 2005 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
#include "basic_graph.cpp"
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/python.hpp>
#include "point2d.hpp"
#include "generators.hpp"
namespace boost { namespace graph { namespace python {
extern void export_Graph();
extern void export_Digraph();
extern void export_graphviz();
extern void export_breadth_first_search();
extern void export_depth_first_search();
extern void export_dijkstra_shortest_paths();
extern void export_bellman_ford_shortest_paths();
extern void export_dag_shortest_paths();
extern void export_prim_minimum_spanning_tree();
template<typename Graph> void export_breadth_first_search_in_graph();
template<typename Graph> void export_depth_first_search_in_graph();
template<typename Graph> void export_dijkstra_shortest_paths_in_graph();
template<typename Graph> void export_dag_shortest_paths_in_graph();
template<typename Graph> void export_bellman_ford_shortest_paths_in_graph();
template<typename Graph> void export_prim_minimum_spanning_tree_in_graph();
extern void export_connected_components();
extern void export_strong_components();
extern void export_biconnected_components();
extern void export_incremental_components();
extern void export_topological_sort();
extern void export_cuthill_mckee_ordering();
extern void export_king_ordering();
//extern void export_minimum_degree_ordering();
extern void export_sequential_vertex_coloring();
extern void export_betweenness_centrality();
extern void export_page_rank();
extern void export_circle_graph_layout();
extern void export_fruchterman_reingold_force_directed_layout();
extern void export_kamada_kawai_spring_layout();
extern void export_kruskal_minimum_spanning_tree();
extern void export_transitive_closure();
//extern void export_transpose_graph();
extern void export_isomorphism();
template<typename Graph>
void export_in_graph()
{
export_breadth_first_search_in_graph<Graph>();
export_depth_first_search_in_graph<Graph>();
export_dijkstra_shortest_paths_in_graph<Graph>();
export_bellman_ford_shortest_paths_in_graph<Graph>();
export_dag_shortest_paths_in_graph<Graph>();
export_prim_minimum_spanning_tree_in_graph<Graph>();
}
BOOST_PYTHON_MODULE(bgl)
{
using boost::python::class_;
using boost::python::enum_;
using boost::python::no_init;
using boost::python::init;
using boost::python::arg;
enum_<graph_file_kind>("file_kind")
.value("adjlist", gfk_adjlist)
.value("graphviz", gfk_graphviz)
;
enum_<default_color_type>("Color")
.value("white", color_traits<default_color_type>::white())
.value("gray", color_traits<default_color_type>::gray())
.value("black", color_traits<default_color_type>::black())
;
class_<point2d>("Point2D")
.def_readwrite("x", &point2d::x)
.def_readwrite("y", &point2d::y)
;
class_<erdos_renyi>("ErdosRenyi", no_init)
.def(init<std::size_t, double>(
(arg("n"), arg("probability") = 1)))
;
class_<power_law_out_degree>("PowerLawOutDegree", no_init)
.def(init<std::size_t, double, double>(
(arg("n"), arg("alpha"), arg("beta"))))
;
class_<small_world>("SmallWorld", no_init)
.def(init<std::size_t, std::size_t, double>
((arg("n"), arg("k"), arg("probability"))))
;
export_Graph();
export_Digraph();
export_graphviz();
// Core Algorithm Patterns
export_breadth_first_search();
export_depth_first_search();
// Shortest Paths Algorithms
export_dijkstra_shortest_paths();
export_bellman_ford_shortest_paths();
export_dag_shortest_paths();
// Minimum Spanning Tree Algorithms
export_kruskal_minimum_spanning_tree();
export_prim_minimum_spanning_tree();
// Connected Components Algorithms
export_connected_components();
export_strong_components();
export_biconnected_components();
export_incremental_components();
// Sparse Matrix Ordering
export_cuthill_mckee_ordering();
export_king_ordering();
// export_minimum_degree_ordering();
// Other algorithms
export_topological_sort();
export_transitive_closure();
export_sequential_vertex_coloring();
export_betweenness_centrality();
export_page_rank();
// Layout Algorithms
export_circle_graph_layout();
export_fruchterman_reingold_force_directed_layout();
export_kamada_kawai_spring_layout();
// export_transpose_graph();
export_isomorphism();
}
template void export_in_graph<Graph>();
template void export_in_graph<Digraph>();
} } } // end namespace boost::graph::python

View File

@ -1,81 +0,0 @@
// Copyright 2005 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
#include "digraph.hpp"
#include <boost/graph/page_rank.hpp>
namespace boost { namespace graph { namespace python {
template<typename Graph>
void
page_rank_iterations
(Graph& g,
const vector_property_map<double, typename Graph::VertexIndexMap>* in_rank,
int iterations)
{
typedef vector_property_map<double, typename Graph::VertexIndexMap>
RankMap;
RankMap rank =
in_rank? *in_rank : g.template get_vertex_map<double>("pagerank");
boost::graph::page_rank(g, rank, graph::n_iterations(20));
}
struct page_rank_wrap_done
{
page_rank_wrap_done(boost::python::object done) : done(done) { }
template<typename RankMap, typename Graph>
bool
operator()(const RankMap& rank, const Graph& g) const
{
using boost::python::extract;
return extract<bool>(done(rank, g));
}
private:
boost::python::object done;
};
template<typename Graph>
void
page_rank_done
(Graph& g,
const vector_property_map<double, typename Graph::VertexIndexMap>* in_rank,
boost::python::object done)
{
typedef vector_property_map<double, typename Graph::VertexIndexMap>
RankMap;
RankMap rank =
in_rank? *in_rank : g.template get_vertex_map<double>("pagerank");
boost::graph::page_rank(g, rank, page_rank_wrap_done(done));
}
void export_page_rank()
{
using boost::python::arg;
using boost::python::def;
def("page_rank", &page_rank_iterations<Digraph>,
(arg("graph"),
arg("rank_map") =
(vector_property_map<double, Digraph::VertexIndexMap>*)0,
arg("iterations") = 20));
def("page_rank", &page_rank_done<Digraph>,
(arg("graph"),
arg("rank_map") =
(vector_property_map<double, Digraph::VertexIndexMap>*)0,
arg("done")));
}
} } } // end namespace boost::graph::python

View File

@ -1,154 +0,0 @@
// Copyright 2005 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
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/graph/iteration_macros.hpp>
namespace boost { namespace graph { namespace python {
template<typename DirectedS>
boost::python::tuple
graph_pickle_suite<DirectedS>::getstate(boost::python::object g_obj)
{
using boost::python::tuple;
using boost::python::make_tuple;
using boost::python::list;
using boost::python::extract;
using boost::python::dict;
using boost::python::object;
const Graph& g = extract<const Graph&>(g_obj)();
typename Graph::VertexIndexMap vertex_index_map = g.get_vertex_index_map();
typename Graph::EdgeIndexMap edge_index_map = g.get_edge_index_map();
dict vertex_properties;
dict edge_properties;
// Collect edges
std::vector<tuple> the_edges(g.num_edges());
BGL_FORALL_EDGES_T(e, g, Graph)
the_edges[get(edge_index_map, e)] =
make_tuple(get(vertex_index_map, source(e, g)),
get(vertex_index_map, target(e, g)));
list edges_list;
for (std::vector<tuple>::iterator i = the_edges.begin();
i != the_edges.end(); ++i)
edges_list.append(*i);
// Collect vertex and edge properties
const dynamic_properties& dp = g.get_dynamic_properties();
for (dynamic_properties::const_iterator pm = dp.begin();
pm != dp.end(); ++pm) {
python_dynamic_property_map* pmap =
dynamic_cast<python_dynamic_property_map*>(pm->second);
if (pm->second->key() == typeid(Vertex)) {
std::vector<object> values(g.num_vertices());
BGL_FORALL_VERTICES_T(v, g, Graph)
values[get(vertex_index_map, v)] = pmap->get_python(v);
list values_list;
for (std::vector<object>::iterator i = values.begin();
i != values.end(); ++i)
values_list.append(*i);
vertex_properties[pm->first] = tuple(values_list);
} else if (pm->second->key() == typeid(Edge)) {
std::vector<object> values(g.num_edges());
BGL_FORALL_EDGES_T(e, g, Graph)
values[get(edge_index_map, e)] = pmap->get_python(e);
list values_list;
for (std::vector<object>::iterator i = values.begin();
i != values.end(); ++i)
values_list.append(*i);
edge_properties[pm->first] = tuple(values_list);
} else {
assert(false);
}
}
return make_tuple(g_obj.attr("__dict__"),
g.num_vertices(),
edges_list,
vertex_properties,
edge_properties);
}
template<typename DirectedS>
void
graph_pickle_suite<DirectedS>::setstate(boost::python::object g_obj,
boost::python::tuple state)
{
using boost::python::tuple;
using boost::python::make_tuple;
using boost::python::list;
using boost::python::extract;
using boost::python::dict;
using boost::python::object;
Graph& g = extract<Graph&>(g_obj)();
// restore the graph's __dict__
dict d = extract<dict>(g_obj.attr("__dict__"))();
d.update(state[0]);
// Get the number of vertices
typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
vertices_size_type n = extract<vertices_size_type>(state[1]);
std::vector<Vertex> vertices;
vertices.reserve(n);
while (vertices.size() < n) vertices.push_back(g.add_vertex());
// Get the edges
typedef typename graph_traits<Graph>::edges_size_type edges_size_type;
std::vector<Edge> the_edges;
list edges_list = extract<list>(state[2]);
edges_size_type m = extract<int>(edges_list.attr("__len__")());
the_edges.reserve(m);
for (unsigned i = 0; i < m; ++i) {
tuple e = extract<tuple>(edges_list[i]);
the_edges.push_back
(g.add_edge(vertices[extract<vertices_size_type>(e[0])],
vertices[extract<vertices_size_type>(e[1])]));
}
// Get the vertex properties
typedef typename Graph::VertexIndexMap VertexIndexMap;
dict vertex_properties = extract<dict>(state[3]);
list vertex_map_names = vertex_properties.keys();
while (vertex_map_names != list()) {
object name_obj = vertex_map_names.pop(0);
const char* name = extract<const char*>(name_obj);
vector_property_map<object, VertexIndexMap> pmap =
g.get_vertex_object_map(name);
tuple values = extract<tuple>(vertex_properties[name_obj]);
for (vertices_size_type i = 0; i < g.num_vertices(); ++i)
put(pmap, vertices[i], values[i]);
}
// Get the edge properties
typedef typename Graph::EdgeIndexMap EdgeIndexMap;
dict edge_properties = extract<dict>(state[4]);
list edge_map_names = edge_properties.keys();
while (edge_map_names != list()) {
object name_obj = edge_map_names.pop(0);
const char* name = extract<const char*>(name_obj);
vector_property_map<object, EdgeIndexMap> pmap =
g.get_edge_object_map(name);
tuple values = extract<tuple>(edge_properties[name_obj]);
for (edges_size_type i = 0; i < g.num_edges(); ++i)
put(pmap, the_edges[i], values[i]);
}
}
template struct graph_pickle_suite<undirectedS>;
template struct graph_pickle_suite<bidirectionalS>;
} } } // end namespace boost::graph::python

View File

@ -1,28 +0,0 @@
// Copyright 2005 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
#ifndef BOOST_GRAPH_PYTHON_POINT2D_HPP
#define BOOST_GRAPH_PYTHON_POINT2D_HPP
#include <iostream>
namespace boost { namespace graph { namespace python {
struct point2d
{
double x;
double y;
};
inline std::ostream& operator<<(std::ostream& out, point2d p)
{ return out << p.x << ' ' << p.y; }
inline std::istream& operator>>(std::istream& in, point2d& p)
{ return in >> p.x >> p.y; }
} } } // end namespace boost::graph::python
#endif // BOOST_GRAPH_PYTHON_POINT2D_HPP

View File

@ -1,105 +0,0 @@
// Copyright 2005 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
#include <boost/graph/prim_minimum_spanning_tree.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/python.hpp>
#include "dijkstra_visitor.hpp"
namespace boost { namespace graph { namespace python {
template<typename Graph>
void
prim_minimum_spanning_tree
(Graph& g, typename Graph::Vertex s,
const vector_property_map<typename Graph::Vertex,
typename Graph::VertexIndexMap>* in_predecessor,
const vector_property_map<double, typename Graph::VertexIndexMap>* in_distance,
const vector_property_map<double, typename Graph::EdgeIndexMap>* in_weight,
const dijkstra_visitor<Graph>& visitor)
{
typedef vector_property_map<typename Graph::Vertex,
typename Graph::VertexIndexMap>
PredecessorMap;
typedef vector_property_map<double, typename Graph::VertexIndexMap>
DistanceMap;
typedef vector_property_map<double, typename Graph::EdgeIndexMap>
WeightMap;
PredecessorMap predecessor =
in_predecessor? *in_predecessor
: PredecessorMap(g.num_vertices(), g.get_vertex_index_map());
DistanceMap distance =
in_distance? *in_distance
: DistanceMap(g.num_vertices(), g.get_vertex_index_map());
WeightMap weight = in_weight? *in_weight
: g.template get_edge_map<double>("weight");
typedef typename dijkstra_visitor<Graph>::default_arg default_visitor;
bool has_default_visitor = dynamic_cast<const default_visitor*>(&visitor);
if (!has_default_visitor) {
boost::prim_minimum_spanning_tree
(g,
predecessor,
root_vertex(s).
vertex_index_map(g.get_vertex_index_map()).
visitor(typename dijkstra_visitor<Graph>::ref(visitor)).
distance_map(distance).
weight_map(weight));
} else {
boost::prim_minimum_spanning_tree
(g,
predecessor,
root_vertex(s).
vertex_index_map(g.get_vertex_index_map()).
distance_map(distance).
weight_map(weight));
}
}
template<typename Graph>
void export_prim_minimum_spanning_tree_in_graph()
{
dijkstra_visitor<Graph>::declare("DijkstraVisitor",
"DefaultDijkstraVisitor");
}
void export_prim_minimum_spanning_tree()
{
using boost::python::arg;
using boost::python::def;
def("prim_minimum_spanning_tree", &prim_minimum_spanning_tree<Graph>,
(arg("graph"), arg("root_vertex"),
arg("predecessor_map") =
(vector_property_map<Graph::Vertex, Graph::VertexIndexMap>*)0,
arg("distance_map") =
(vector_property_map<double, Graph::VertexIndexMap>*)0,
arg("weight_map") =
(vector_property_map<double, Graph::EdgeIndexMap>*)0,
arg("visitor") = dijkstra_visitor<Graph>::default_arg()));
def("prim_minimum_spanning_tree", &prim_minimum_spanning_tree<Digraph>,
(arg("graph"), arg("root_vertex"),
arg("predecessor_map") =
(vector_property_map<Digraph::Vertex, Digraph::VertexIndexMap>*)0,
arg("distance_map") =
(vector_property_map<double, Digraph::VertexIndexMap>*)0,
arg("weight_map") =
(vector_property_map<double, Digraph::EdgeIndexMap>*)0,
arg("visitor") = dijkstra_visitor<Digraph>::default_arg()));
}
template void export_prim_minimum_spanning_tree_in_graph<Graph>();
template void export_prim_minimum_spanning_tree_in_graph<Digraph>();
} } } // end namespace boost::graph::python

View File

@ -1,71 +0,0 @@
// Copyright 2005 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
#ifndef BOOST_GRAPH_PYTHON_QUEUE_HPP
#define BOOST_GRAPH_PYTHON_QUEUE_HPP
#include <boost/python.hpp>
namespace boost { namespace graph { namespace python {
template<typename T>
class python_queue
{
class wrap
: public python_queue<T>, public boost::python::wrapper<python_queue<T> >
{
public:
bool empty() const { return this->get_override("empty")(); }
T top() const { return this->get_override("top")(); }
void pop() { this->get_override("pop")(); }
void push(const T& x) { this->get_override("push")(x); }
};
public:
class default_queue : public python_queue<T>
{
bool empty() const { return true; }
T top() const { return T(); }
void pop() {}
void push(const T&) {}
};
virtual ~python_queue() {}
virtual bool empty() const = 0;
virtual T top() const = 0;
virtual void pop() = 0;
virtual void push(const T&) = 0;
static void declare(const char* name, const char* default_name)
{
using boost::python::objects::registered_class_object;
using boost::python::type_id;
using boost::python::class_;
using boost::python::bases;
using boost::python::no_init;
using boost::python::pure_virtual;
if (registered_class_object(type_id<wrap>()).get() == 0) {
class_<wrap, boost::noncopyable>(name)
.def("empty", pure_virtual(&python_queue<T>::empty))
.def("top", pure_virtual(&python_queue<T>::top))
.def("pop", pure_virtual(&python_queue<T>::pop))
.def("push", pure_virtual(&python_queue<T>::push))
;
}
if (registered_class_object(type_id<default_queue>()).get() == 0)
{
class_<default_queue, bases<python_queue> >(default_name, no_init);
}
}
};
} } } // end namespace boost::graph::python
#endif // BOOST_GRAPH_PYTHON_QUEUE_HPP

View File

@ -1,47 +0,0 @@
// Copyright 2005 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
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/graph/sequential_vertex_coloring.hpp>
namespace boost { namespace graph { namespace python {
template<typename Graph>
int
sequential_vertex_coloring
(Graph& g,
const vector_property_map<int, typename Graph::VertexIndexMap>* in_color)
{
typedef vector_property_map<int, typename Graph::VertexIndexMap> ColorMap;
ColorMap color =
in_color? *in_color
: ColorMap(num_vertices(g), g.get_vertex_index_map());
return boost::sequential_vertex_coloring(g, color);
}
void export_sequential_vertex_coloring()
{
using boost::python::arg;
using boost::python::def;
def("sequential_vertex_coloring",
&sequential_vertex_coloring<Graph>,
(arg("graph"),
arg("color_map") =
(vector_property_map<int, Graph::VertexIndexMap>*)0));
def("sequential_vertex_coloring",
&sequential_vertex_coloring<Digraph>,
(arg("graph"),
arg("color_map") =
(vector_property_map<int, Digraph::VertexIndexMap>*)0));
}
} } } // end namespace boost::graph::python

View File

@ -1,42 +0,0 @@
// Copyright 2005 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
#include <boost/graph/strong_components.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/python.hpp>
namespace boost { namespace graph { namespace python {
template<typename Graph>
int
strong_components
(Graph& g,
const vector_property_map<int, typename Graph::VertexIndexMap>* in_component)
{
typedef vector_property_map<int, typename Graph::VertexIndexMap> ComponentMap;
ComponentMap component =
in_component? *in_component : g.template get_vertex_map<int>("component");
return boost::strong_components (g, component,
vertex_index_map(g.get_vertex_index_map()));
}
void export_strong_components()
{
using boost::python::arg;
using boost::python::def;
def("strong_components", &strong_components<Digraph>,
(arg("graph"),
arg("component_map") =
(vector_property_map<int, Digraph::VertexIndexMap>*)0));
}
} } } // end namespace boost::graph::python

View File

@ -1,55 +0,0 @@
// Copyright 2005 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
#include <boost/graph/topological_sort.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/python.hpp>
#include <list>
#include <iterator>
namespace boost { namespace graph { namespace python {
template<typename Graph>
boost::python::list
topological_sort
(const Graph& g,
const vector_property_map<default_color_type,
typename Graph::VertexIndexMap>* in_color)
{
typedef vector_property_map<default_color_type,
typename Graph::VertexIndexMap> ColorMap;
ColorMap color =
in_color? *in_color : ColorMap(g.num_vertices(), g.get_vertex_index_map());
std::list<typename Graph::Vertex> topo_order;
boost::python::list result;
boost::topological_sort(g, std::back_inserter(topo_order), color_map(color));
for (typename std::list<typename Graph::Vertex>::iterator i
= topo_order.begin(); i != topo_order.end(); ++i)
result.append(*i);
return result;
}
void export_topological_sort()
{
using boost::python::arg;
using boost::python::def;
def("topological_sort", &topological_sort<Graph>,
(arg("graph"),
arg("color_map") =
(vector_property_map<default_color_type, Graph::VertexIndexMap>*)0));
def("topological_sort", &topological_sort<Digraph>,
(arg("graph"),
arg("color_map") =
(vector_property_map<default_color_type, Digraph::VertexIndexMap>*)0));
}
} } } // end namespace boost::graph::python

View File

@ -1,45 +0,0 @@
// Copyright 2005 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
#include <boost/graph/transitive_closure.hpp>
#include "graph.hpp"
#include "digraph.hpp"
#include <boost/python.hpp>
#include <list>
#include <iterator>
namespace boost { namespace graph { namespace python {
template<typename Graph>
std::auto_ptr<Graph>
transitive_closure
(const Graph& g,
const vector_property_map<typename Graph::Vertex,
typename Graph::VertexIndexMap>* g_to_tc_map)
{
std::auto_ptr<Graph> tc(new Graph);
if (g_to_tc_map)
boost::transitive_closure(g, *tc, *g_to_tc_map, g.get_vertex_index_map());
else
boost::transitive_closure(g, *tc,
vertex_index_map(g.get_vertex_index_map()));
return tc;
}
void export_transitive_closure()
{
using boost::python::arg;
using boost::python::def;
def("transitive_closure", &transitive_closure<Digraph>,
(arg("graph"),
arg("orig_to_copy") =
(vector_property_map<Digraph::Vertex, Digraph::VertexIndexMap>*)0));
}
} } } // end namespace boost::graph::python

View File

@ -1,85 +0,0 @@
// Copyright 2005 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
template<typename Graph>
class BGL_PYTHON_VISITOR
{
class wrap
: public BGL_PYTHON_VISITOR<Graph>,
public boost::python::wrapper<BGL_PYTHON_VISITOR<Graph> >
{
public:
typedef typename BGL_PYTHON_VISITOR<Graph>::vertex_descriptor vertex_descriptor;
typedef typename BGL_PYTHON_VISITOR<Graph>::edge_descriptor edge_descriptor;
#define BGL_PYTHON_EVENT(Name,Descriptor) \
void Name(Descriptor x, const Graph& g) const \
{ \
if (boost::python::override f = this->get_override(#Name)) \
f(x, boost::cref(g)); \
else BGL_PYTHON_VISITOR<Graph>::Name(x, g); \
} \
\
void default_##Name(Descriptor x, const Graph& g) const \
{ this->BGL_PYTHON_VISITOR<Graph>::Name(x, g); }
# include BGL_PYTHON_EVENTS_HEADER
#undef BGL_PYTHON_EVENT
};
public:
class default_arg : public BGL_PYTHON_VISITOR<Graph> { };
struct ref
{
typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
ref(const BGL_PYTHON_VISITOR<Graph>& v) : v(v) { }
#define BGL_PYTHON_EVENT(Name, Descriptor) \
void Name(Descriptor x, const Graph& g) const { v.Name(x, g); }
# include BGL_PYTHON_EVENTS_HEADER
#undef BGL_PYTHON_EVENT
private:
const BGL_PYTHON_VISITOR<Graph>& v;
};
typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
virtual ~BGL_PYTHON_VISITOR() {}
#define BGL_PYTHON_EVENT(Name, Descriptor) \
virtual void Name(Descriptor x, const Graph& g) const {}
# include BGL_PYTHON_EVENTS_HEADER
#undef BGL_PYTHON_EVENT
static void declare(const char* name, const char* default_name)
{
using boost::python::class_;
using boost::python::bases;
using boost::python::no_init;
using boost::python::objects::registered_class_object;
using boost::python::type_id;
if (registered_class_object(type_id<wrap>()).get() != 0)
return;
#define BGL_PYTHON_EVENT(Name, Descriptor) \
.def(#Name, &BGL_PYTHON_VISITOR<Graph>::Name, &wrap::default_##Name)
class_<wrap, boost::noncopyable>(name)
# include BGL_PYTHON_EVENTS_HEADER
#undef BGL_PYTHON_EVENT
;
class_<default_arg, bases<BGL_PYTHON_VISITOR<Graph> > >(default_name,
no_init);
}
};

View File

@ -1,13 +0,0 @@
graph G {
A -- B
A -- F
A -- G
B -- C
B -- D
B -- E
C -- D
E -- F
G -- H
G -- I
H -- I
}

View File

@ -1,21 +0,0 @@
# Copyright 2005 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 bgl
g = bgl.Graph("biconnected_components.dot", bgl.file_kind.graphviz)
art_points = bgl.biconnected_components(g, g.get_edge_int_map("label"));
g.write_graphviz("biconnected_components_out.dot")
print "Articulation points: ",
node_id = g.get_vertex_string_map("node_id")
for v in art_points:
print node_id[v],
print " ",
print ""

View File

@ -1,14 +0,0 @@
# Copyright 2005 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 bgl
g = bgl.Graph("biconnected_components.dot", bgl.file_kind.graphviz)
bgl.circle_graph_layout(g, radius=200)
g.write_graphviz("circle_graph_layout_out.dot")

View File

@ -1,14 +0,0 @@
# Copyright 2005 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 bgl
g = bgl.Graph("biconnected_components.dot", bgl.file_kind.graphviz)
bgl.fruchterman_reingold_force_directed_layout(g, width=400, height=400)
g.write_graphviz("fruchterman_reingold_out.dot")

View File

@ -1,14 +0,0 @@
# Copyright 2005 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 bgl
g = bgl.Graph("biconnected_components.dot", bgl.file_kind.graphviz)
bgl.kamada_kawai_spring_layout(g, side_length=400)
g.write_graphviz("kamada_kawai_spring_layout_out.dot")

View File

@ -1,110 +0,0 @@
# Copyright 2005 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
# Note: this code requires the Boost Graph Library bindings for Python
# and the priodict module. The latter is part of the Python NMS
# library (http://pynms.sourceforge.net/).
import bgl
import priodict
class pqueue(bgl.Digraph.VertexQueue):
def __init__(self, distance):
bgl.Digraph.VertexQueue.__init__(self)
self.Q = priodict.priorityDictionary()
self.distance = distance
def empty(self):
return self.Q == {}
def top(self):
return self.Q.smallest();
def pop(self):
del self.Q[self.Q.smallest()]
def push(self, x):
self.Q[x] = self.distance[x]
def update(self, x, v):
self.distance[x] = v
self.Q[x] = v
class dijkstra_bfs_visitor(bgl.Digraph.BFSVisitor):
def __init__(self, Q, weight, distance, predecessor):
bgl.Digraph.BFSVisitor.__init__(self)
self.Q = Q
self.weight = weight
self.distance = distance
self.predecessor = predecessor
def tree_edge(self, e, g):
(u, v) = (g.source(e), g.target(e))
self.distance[v] = self.distance[u] + self.weight[e]
self.predecessor[v] = u
def gray_target(self, e, g):
(u, v) = (g.source(e), g.target(e))
if self.distance[u] + self.weight[e] < self.distance[v]:
self.Q.update(v, self.distance[u] + self.weight[e])
self.predecessor[v] = u;
g = bgl.Digraph()
# Create vertices in the graph
name = g.get_vertex_string_map("node_id")
A = g.add_vertex()
name[A] = "A"
B = g.add_vertex()
name[B] = "B"
C = g.add_vertex()
name[C] = "C"
D = g.add_vertex()
name[D] = "D"
E = g.add_vertex()
name[E] = "E"
# Create (weighted) edges in the graph
weight = g.get_edge_double_map("label")
weight[g.add_edge(A, C)] = 1
weight[g.add_edge(B, B)] = 2
weight[g.add_edge(B, D)] = 1
weight[g.add_edge(B, E)] = 2.5
weight[g.add_edge(C, B)] = 7
weight[g.add_edge(C, D)] = 3
weight[g.add_edge(D, E)] = 1
weight[g.add_edge(E, A)] = 1
weight[g.add_edge(E, B)] = 1
# Initialize property maps
predecessor = {}
distance = g.get_vertex_double_map("distance_from_A")
for v in g.vertices:
predecessor[v] = v
distance[v] = 1e100
# Run breadth-first search to compute shortest paths
distance[A] = 0
buf = pqueue(distance)
bgl.breadth_first_search(g, A, buf,
dijkstra_bfs_visitor(buf,weight,distance,predecessor),
color_map = g.get_vertex_color_map("color"))
class show_relaxed_edges(bgl.Digraph.DijkstraVisitor):
def edge_relaxed(self, e, g):
text = "Relaxed edge (" + name[g.source(e)] + ", " + name[g.target(e)] + ")"
print text
# Run Dijkstra's algorithm to compute shortest paths
distance2 = g.get_vertex_double_map("distance_from_A_also");
bgl.dijkstra_shortest_paths(g, A, distance_map = distance2,
visitor=show_relaxed_edges(),
weight_map = g.get_edge_double_map("label"));
# Emit graph
g.write_graphviz("dijkstra-example.dot")