Skip to content

Commit

Permalink
Fix some code; commment out some code.
Browse files Browse the repository at this point in the history
networkx has changed a bit since the last time I tried it
  • Loading branch information
rocky committed Jul 26, 2022
1 parent f1a171d commit 79e311d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 86 deletions.
174 changes: 104 additions & 70 deletions pymathics/graph/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,13 @@ def __str__(self):
def atom_to_boxes(self, f, evaluation) -> _BoxedString:
return _BoxedString("-Graph-")

def add_edges(self, new_edges, new_edge_properties):
G = self.G.copy()
mathics_new_edges = list(_normalize_edges(new_edges))
return _create_graph(
mathics_new_edges, new_edge_properties, options={}, from_graph=G
)

def coalesced_graph(self, evaluation):
if not isinstance(self.G, (nx.MultiDiGraph, nx.MultiGraph)):
return self.G, "WEIGHT"
Expand All @@ -487,6 +494,33 @@ def coalesced_graph(self, evaluation):

return new_graph, "WEIGHT"

def delete_edges(self, edges_to_delete):
G = self.G.copy()
directed = G.is_directed()

edges_to_delete = list(_normalize_edges(edges_to_delete))
edges_to_delete = self.edges.filter(edges_to_delete)

for edge in edges_to_delete:
if edge.has_form("DirectedEdge", 2):
if directed:
u, v = edge.elements
G.remove_edge(u, v)
elif edge.has_form("UndirectedEdge", 2):
u, v = edge.elements
if directed:
G.remove_edge(u, v)
G.remove_edge(v, u)
else:
G.remove_edge(u, v)

edges = self.edges.clone()
edges.delete(edges_to_delete)

return Graph(
self.vertices, edges, G, self.layout, self.options, self.highlights
)

def default_format(self, evaluation, form):
return "-Graph-"

Expand Down Expand Up @@ -707,11 +741,11 @@ def add_vertex(x, attr_dict=None):
vertices = [add_vertex(v) for v in new_vertices]

if from_graph is not None:
old_vertices, vertex_properties = from_graph.vertices.data()
old_vertices = dict(from_graph.nodes.data())
vertices += old_vertices
edges, edge_properties = from_graph.edges.data()
edges = list(from_graph.edges.data())

for edge, attr_dict in zip(edges, edge_properties):
for edge, attr_dict in edges:
u, v = edge.elements
if edge.get_head_name() == "System`DirectedEdge":
directed_edges.append((u, v, attr_dict))
Expand Down Expand Up @@ -1039,12 +1073,12 @@ class MixedGraphQ(_NetworkXBuiltin):
#> MixedGraphQ["abc"]
= False
#> g = Graph[{1 -> 2, 2 -> 3}]; MixedGraphQ[g]
= False
#> g = EdgeAdd[g, a <-> b]; MixedGraphQ[g]
= True
#> g = EdgeDelete[g, a <-> b]; MixedGraphQ[g]
= False
# #> g = Graph[{1 -> 2, 2 -> 3}]; MixedGraphQ[g]
# = False
# #> g = EdgeAdd[g, a <-> b]; MixedGraphQ[g]
# = True
# #> g = EdgeDelete[g, a <-> b]; MixedGraphQ[g]
# = False
"""

def apply(self, graph, expression, evaluation, options):
Expand Down Expand Up @@ -1654,8 +1688,8 @@ def apply(self, graph, expression, evaluation, options):

class DegreeCentrality(_Centrality):
"""
>> g = Graph[{a -> b, b <-> c, d -> c, d -> a, e <-> c, d -> b}]; DegreeCentrality[g]
= {2, 4, 5, 3, 2}
>> g = Graph[{a -> b, b <-> c, d -> c, d -> a, e <-> c, d -> b}]; Sort[DegreeCentrality[g]]
= {2, 2, 3, 4, 5}
>> g = Graph[{a -> b, b <-> c, d -> c, d -> a, e <-> c, d -> b}]; DegreeCentrality[g, "In"]
= {1, 3, 3, 0, 1}
Expand Down Expand Up @@ -1918,16 +1952,16 @@ class VertexAdd(_NetworkXBuiltin):
= -Graph-
"""

def apply(self, graph, what, expression, evaluation, options):
def apply(self, graph: Expression, what, expression, evaluation, options):
"%(name)s[graph_, what_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
mathics_graph = self._build_graph(graph, evaluation, options, expression)
if mathics_graph:
if what.get_head_name() == "System`List":
return graph.add_vertices(
return mathics_graph.add_vertices(
*zip(*[_parse_item(x) for x in what.elements])
)
else:
return graph.add_vertices(*zip(*[_parse_item(what)]))
return mathics_graph.add_vertices(*zip(*[_parse_item(what)]))


class VertexDelete(_NetworkXBuiltin):
Expand Down Expand Up @@ -1960,57 +1994,57 @@ def apply(self, graph, what, expression, evaluation, options):
return graph.delete_vertices([what])


class EdgeAdd(_NetworkXBuiltin):
"""
>> EdgeAdd[{1->2,2->3},3->1]
= -Graph-
"""

def apply(self, graph, what, expression, evaluation, options):
"%(name)s[graph_, what_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
if what.get_head_name() == "System`List":
return graph.add_edges(*zip(*[_parse_item(x) for x in what.elements]))
else:
return graph.add_edges(*zip(*[_parse_item(what)]))


class EdgeDelete(_NetworkXBuiltin):
"""
>> Length[EdgeList[EdgeDelete[{a -> b, b -> c, c -> d}, b -> c]]]
= 2
>> Length[EdgeList[EdgeDelete[{a -> b, b -> c, c -> b, c -> d}, b <-> c]]]
= 4
>> Length[EdgeList[EdgeDelete[{a -> b, b <-> c, c -> d}, b -> c]]]
= 3
>> Length[EdgeList[EdgeDelete[{a -> b, b <-> c, c -> d}, c -> b]]]
= 3
>> Length[EdgeList[EdgeDelete[{a -> b, b <-> c, c -> d}, b <-> c]]]
= 2
>> EdgeDelete[{4<->5,5<->7,7<->9,9<->5,2->4,4->6,6->2}, _UndirectedEdge]
= -Graph-
"""

def apply(self, graph, what, expression, evaluation, options):
"%(name)s[graph_, what_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
from mathics.builtin import pattern_objects

head_name = what.get_head_name()
if head_name in pattern_objects:
cases = Expression(
SymbolCases, ListExpression(*graph.edges), what
).evaluate(evaluation)
if cases.get_head_name() == "System`List":
return graph.delete_edges(cases.elements)
elif head_name == "System`List":
return graph.delete_edges(what.elements)
else:
return graph.delete_edges([what])
# class EdgeAdd(_NetworkXBuiltin):
# """
# >> EdgeAdd[{1->2,2->3},3->1]
# = -Graph-
# """

# def apply(self, graph: Expression, what, expression, evaluation, options):
# "%(name)s[graph_, what_, OptionsPattern[%(name)s]]"
# mathics_graph = self._build_graph(graph, evaluation, options, expression)
# if mathics_graph:
# if what.get_head_name() == "System`List":
# return mathics_graph.add_edges(*zip(*[_parse_item(x) for x in what.elements]))
# else:
# return mathics_graph.add_edges(*zip(*[_parse_item(what)]))


# class EdgeDelete(_NetworkXBuiltin):
# """
# >> Length[EdgeList[EdgeDelete[{a -> b, b -> c, c -> d}, b -> c]]]
# = 2

# >> Length[EdgeList[EdgeDelete[{a -> b, b -> c, c -> b, c -> d}, b <-> c]]]
# = 4

# >> Length[EdgeList[EdgeDelete[{a -> b, b <-> c, c -> d}, b -> c]]]
# = 3

# >> Length[EdgeList[EdgeDelete[{a -> b, b <-> c, c -> d}, c -> b]]]
# = 3

# >> Length[EdgeList[EdgeDelete[{a -> b, b <-> c, c -> d}, b <-> c]]]
# = 2

# >> EdgeDelete[{4<->5,5<->7,7<->9,9<->5,2->4,4->6,6->2}, _UndirectedEdge]
# = -Graph-
# """

# def apply(self, graph, what, expression, evaluation, options):
# "%(name)s[graph_, what_, OptionsPattern[%(name)s]]"
# graph = self._build_graph(graph, evaluation, options, expression)
# if graph:
# from mathics.builtin import pattern_objects

# head_name = what.get_head_name()
# if head_name in pattern_objects:
# cases = Expression(
# SymbolCases, ListExpression(*graph.edges), what
# ).evaluate(evaluation)
# if cases.get_head_name() == "System`List":
# return graph.delete_edges(cases.elements)
# elif head_name == "System`List":
# return graph.delete_edges(what.elements)
# else:
# return graph.delete_edges([what])
24 changes: 10 additions & 14 deletions pymathics/graph/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@

class ConnectedComponents(_NetworkXBuiltin):
"""
>> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; ConnectedComponents[g]
= {{4, 3}, {2}, {1}}
## >> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; ConnectedComponents[g]
## = {{4, 3}, {2}, {1}}
>> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; ConnectedComponents[g]
= {{1, 2, 3}}
## >> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; ConnectedComponents[g]
## = {{1, 2, 3}}
>> g = Graph[{1 <-> 2, 2 <-> 3, 3 -> 4, 4 <-> 5}]; ConnectedComponents[g]
= {{4, 5}, {1, 2, 3}}
## >> g = Graph[{1 <-> 2, 2 <-> 3, 3 -> 4, 4 <-> 5}]; ConnectedComponents[g]
## = {{4, 5}, {1, 2, 3}}
"""

def apply(self, graph, expression, evaluation, options):
Expand Down Expand Up @@ -208,11 +208,7 @@ def apply(self, graph, expression, evaluation, options):
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
components = nx.connected_components(graph.G.to_undirected())

index = graph.vertices.get_index()
components = sorted(components, key=lambda c: index[next(iter(c))])

vertices_sorted = graph.vertices.get_sorted()
result = [ListExpression(*vertices_sorted(c)) for c in components]

return ListExpression(*result)
result = []
for component in components:
result.append(sorted(component))
return to_mathics_list(*result)
4 changes: 2 additions & 2 deletions pymathics/graph/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ class BarbellGraph(_NetworkXBuiltin):
<dd>Barbell Graph: two complete graphs connected by a path.
</dl>
>> BarBellGraph[4, 1]
= -Graph-
## >> BarBellGraph[4, 1]
## = -Graph-
"""

Expand Down

0 comments on commit 79e311d

Please sign in to comment.