Various utils
Flowpaths implements various helper functions on graphs. They can be access with the prefix flowpaths.utils.
For example, you can create drawing as this one
using the following code:
import flowpaths as fp
import networkx as nx
# Create a simple graph
graph = nx.DiGraph()
graph.graph["id"] = "simple_graph"
graph.add_edge("s", "a", flow=6)
graph.add_edge("s", "b", flow=7)
graph.add_edge("a", "b", flow=2)
graph.add_edge("a", "c", flow=5)
graph.add_edge("b", "c", flow=9)
graph.add_edge("c", "d", flow=6)
graph.add_edge("c", "t", flow=7)
graph.add_edge("d", "t", flow=6)
# Solve the minimum path error model
mpe_model = fp.kMinPathError(graph, flow_attr="flow", k=3, weight_type=float)
mpe_model.solve()
# Draw the solution
if mpe_model.is_solved():
solution = mpe_model.get_solution()
fp.utils.draw_solution(
G=graph,
filename="simple_graph.pdf",
flow_attr="flow",
paths=solution["paths"],
weights=solution["weights"],
draw_options={
"show_graph_edges": True,
"show_edge_weights": False,
"show_path_weights": False,
"show_path_weight_on_first_edge": True,
"pathwidth": 2,
})
This produces a file with extension .pdf
storing the PDF image of the graph.
check_flow_conservation
Check if the flow conservation property holds for the given graph.
Parameters
-
G
: nx.DiGraphThe input directed acyclic graph, as networkx DiGraph.
-
flow_attr
: strThe attribute name from where to get the flow values on the edges.
Returns
-
bool:
True if the flow conservation property holds, False otherwise.
Source code in flowpaths/utils/graphutils.py
draw_solution
draw_solution(
G: DiGraph,
filename: str,
flow_attr: str = None,
paths: list = [],
weights: list = [],
additional_starts: list = [],
additional_ends: list = [],
subpath_constraints: list = [],
draw_options: dict = {
"show_graph_edges": True,
"show_edge_weights": False,
"show_node_weights": False,
"show_path_weights": False,
"show_path_weight_on_first_edge": True,
"pathwidth": 3.0,
},
)
Draw the graph with the paths and their weights highlighted.
Parameters
-
G
: nx.DiGraphThe input directed acyclic graph, as networkx DiGraph.
-
filename
: strThe name of the file to save the drawing. The file type is inferred from the extension. Supported extensions are ‘.bmp’, ‘.canon’, ‘.cgimage’, ‘.cmap’, ‘.cmapx’, ‘.cmapx_np’, ‘.dot’, ‘.dot_json’, ‘.eps’, ‘.exr’, ‘.fig’, ‘.gd’, ‘.gd2’, ‘.gif’, ‘.gtk’, ‘.gv’, ‘.ico’, ‘.imap’, ‘.imap_np’, ‘.ismap’, ‘.jp2’, ‘.jpe’, ‘.jpeg’, ‘.jpg’, ‘.json’, ‘.json0’, ‘.pct’, ‘.pdf’, ‘.pic’, ‘.pict’, ‘.plain’, ‘.plain-ext’, ‘.png’, ‘.pov’, ‘.ps’, ‘.ps2’, ‘.psd’, ‘.sgi’, ‘.svg’, ‘.svgz’, ‘.tga’, ‘.tif’, ‘.tiff’, ‘.tk’, ‘.vml’, ‘.vmlz’, ‘.vrml’, ‘.wbmp’, ‘.webp’, ‘.x11’, ‘.xdot’, ‘.xdot1.2’, ‘.xdot1.4’, ‘.xdot_json’, ‘.xlib’
-
flow_attr
: strThe attribute name from where to get the flow values on the edges. Default is an empty string, in which case no edge weights are shown.
-
paths
: listThe list of paths to highlight, as lists of nodes. Default is an empty list, in which case no path is drawn. Default is an empty list.
-
weights
: listThe list of weights corresponding to the paths, of various colors. Default is an empty list, in which case no path is drawn.
-
additional_starts
: listA list of additional nodes to highlight in green as starting nodes. Default is an empty list.
-
additional_ends
: listA list of additional nodes to highlight in red as ending nodes. Default is an empty list.
-
subpath_constraints
: listA list of subpaths to highlight in the graph as dashed edges, of various colors. Each subpath is a list of edges. Default is an empty list. There is no association between the subpath colors and the path colors.
-
draw_options
: dictA dictionary with the following keys:
-
show_graph_edges
: boolWhether to show the edges of the graph. Default is
True
. -
show_edge_weights
: boolWhether to show the edge weights in the graph from the
flow_attr
. Default isFalse
. -
show_node_weights
: boolWhether to show the node weights in the graph from the
flow_attr
. Default isFalse
. -
show_path_weights
: boolWhether to show the path weights in the graph on every edge. Default is
False
. -
show_path_weight_on_first_edge
: boolWhether to show the path weight on the first edge of the path. Default is
True
. -
pathwidth
: floatThe width of the path to be drawn. Default is
3.0
.
-
Source code in flowpaths/utils/graphutils.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
|
fpid
get_subgraph_between_topological_nodes
get_subgraph_between_topological_nodes(
graph: DiGraph,
topo_order: list,
left: int,
right: int,
) -> nx.DiGraph
Create a subgraph with the nodes between left and right in the topological order, including the edges between them, but also the edges from these nodes that are incident to nodes outside this range.
Source code in flowpaths/utils/graphutils.py
max_bottleneck_path
Computes the maximum bottleneck path in a directed graph.
Parameters
-
G
: nx.DiGraphA directed graph where each edge has a flow attribute.
-
flow_attr
: strThe flow attribute from where to get the flow values.
Returns
-
tuple: A tuple containing:
- The value of the maximum bottleneck.
- The path corresponding to the maximum bottleneck (list of nodes). If no s-t flow exists in the network, returns (None, None).
Source code in flowpaths/utils/graphutils.py
max_occurrence
Check what is the maximum number of edges of seq that appear in some path in the list paths_in_DAG.
This assumes paths_in_DAG are paths in a directed acyclic graph.
Parameters
- seq (list): The sequence of edges to check.
- paths (list): The list of paths to check against, as lists of nodes.
Returns
- int: the largest number of seq edges that appear in some path in paths_in_DAG