Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ serde_json = "1.0"
smallvec = { version = "1.0", features = ["union"] }
rustworkx-core = { path = "rustworkx-core", version = "=0.17.1" }
flate2 = "1.0.35"
pest = "=2.7.15"
pest_derive = "=2.7.15"

[dependencies.pyo3]
version = "0.24"
Expand Down
21 changes: 21 additions & 0 deletions releasenotes/notes/from-dot-b9f092537c7bce94.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
features:
- |
Added feature for importing graphs from the GraphViz DOT format via the new
:func:`~rustworkx.from_dot` function. This function takes a DOT string and
constructs either a :class:`~rustworkx.PyGraph` or
:class:`~rustworkx.PyDiGraph` object, automatically detecting the graph type
from the DOT input. Node attributes, edge attributes, and graph-level
attributes are preserved.For example::

import rustworkx

dot_str = '''
digraph {
0 [label="a", color=red];
1 [label="b", color=blue];
0 -> 1 [weight=1];
}
'''
g = rustworkx.from_dot(dot_str)
assert len(g.nodes()) == 2
assert len(g.edges()) == 1
2 changes: 1 addition & 1 deletion rustworkx-core/src/max_weight_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn assign_label<E>(
assign_label(
endpoints[mate[&base]],
1,
mate.get(&base).map(|p| (p ^ 1)),
mate.get(&base).map(|p| p ^ 1),
num_nodes,
in_blossoms,
labels,
Expand Down
1 change: 1 addition & 0 deletions rustworkx/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ from .rustworkx import GraphMLKey as GraphMLKey
from .rustworkx import digraph_node_link_json as digraph_node_link_json
from .rustworkx import graph_node_link_json as graph_node_link_json
from .rustworkx import from_node_link_json_file as from_node_link_json_file
from .rustworkx import from_dot as from_dot
from .rustworkx import parse_node_link_json as parse_node_link_json
from .rustworkx import digraph_bellman_ford_shortest_paths as digraph_bellman_ford_shortest_paths
from .rustworkx import graph_bellman_ford_shortest_paths as graph_bellman_ford_shortest_paths
Expand Down
3 changes: 3 additions & 0 deletions rustworkx/rustworkx.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,9 @@ def from_node_link_json_file(
node_attrs: Callable[[dict[str, str]], _S] | None = ...,
edge_attrs: Callable[[dict[str, str]], _T] | None = ...,
) -> PyDiGraph[_S, _T] | PyGraph[_S, _T]: ...
def from_dot(
dot_str: str,
) -> PyDiGraph[_S, _T] | PyGraph[_S, _T]: ...

# Shortest Path

Expand Down
43 changes: 43 additions & 0 deletions src/dot_parser/dot.pest
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
COMMENT = _{ "#" ~ (!NEWLINE ~ ANY)* | "//" ~ (!NEWLINE ~ ANY)* | "/*" ~ (!"*/" ~ ANY)* ~ "*/" }
NEWLINE = _{ "\n" | "\r\n" }

graph_file = { SOI ~ strict? ~ graph_type ~ id? ~ "{" ~ stmt_list? ~ "}" ~ EOI }

strict = { "strict" }
graph_type = { "graph" | "digraph" }

id = _{ number | identifier | quoted_id | html_id }

number = @{ ASCII_DIGIT+ }
identifier = @{ (ASCII_ALPHANUMERIC | "_" | "." | "/" | "\\" | "-")+ }
quoted_id = @{ "\"" ~ (("\\\"" | (!"\"" ~ ANY))*) ~ "\"" }
html_id = @{ "<" ~ (!">" ~ ANY)* ~ ">" }

stmt_list = { (stmt ~ (";" | NEWLINE)*)* }

stmt = _{
edge_stmt
| node_stmt
| attr_stmt
| assignment
| subgraph
}

node_stmt = { node_id ~ attr_list* }
edge_stmt = { edge_point ~ (edge_op ~ edge_point)+ ~ attr_list* }

edge_op = { "->" | "--" }

edge_point = { node_id | subgraph }

attr_stmt = { ("graph" | "node" | "edge") ~ attr_list+ }

attr_list = { "[" ~ a_list? ~ "]" }
a_list = { (id ~ ("=" ~ id)? ~ ("," | ";")?)* }

assignment = { id ~ "=" ~ id }

subgraph = { "subgraph" ~ id? ~ "{" ~ stmt_list? ~ "}" }

node_id = { id ~ (":" ~ id)? ~ (":" ~ id)? }
Loading
Loading