|
1 | 1 | """ |
2 | | -Basic Graph Measures |
| 2 | +Graph Measures and Metrics |
| 3 | +
|
| 4 | +Measures include basic measures, such as the number of vertices and edges, \ |
| 5 | +connectivity, degree measures, centrality, and so on. |
3 | 6 | """ |
4 | 7 |
|
| 8 | + |
5 | 9 | from typing import Optional |
6 | 10 |
|
7 | 11 | from mathics.core.atoms import Integer |
8 | 12 | from mathics.core.convert.expression import ListExpression |
9 | 13 | from mathics.core.expression import Expression |
10 | 14 | from mathics.core.symbols import Symbol |
| 15 | +from mathics.core.systemsymbols import SymbolLength |
11 | 16 |
|
12 | 17 | from pymathics.graph.base import _NetworkXBuiltin |
13 | 18 |
|
14 | 19 | # FIXME: add context |
15 | | -SymbolLength = Symbol("Length") |
16 | 20 | SymbolCases = Symbol("Cases") |
17 | 21 |
|
18 | 22 |
|
| 23 | +# FIXME put this in its own file/module basic |
| 24 | +# when pymathics doc can handle this. |
| 25 | +# """ |
| 26 | +# Basic Graph Measures |
| 27 | +# """ |
19 | 28 | class _PatternCount(_NetworkXBuiltin): |
20 | 29 | """ |
21 | 30 | Counts of vertices or edges, allowing rules to specify the graph. |
@@ -62,6 +71,7 @@ class EdgeCount(_PatternCount): |
62 | 71 | = 2 |
63 | 72 | """ |
64 | 73 |
|
| 74 | + no_doc = False |
65 | 75 | summary_text = "count edges in graph" |
66 | 76 |
|
67 | 77 | def _items(self, graph): |
@@ -92,7 +102,52 @@ class VertexCount(_PatternCount): |
92 | 102 | = 2 |
93 | 103 | """ |
94 | 104 |
|
| 105 | + no_doc = False |
95 | 106 | summary_text = "count vertices in graph" |
96 | 107 |
|
97 | 108 | def _items(self, graph): |
98 | 109 | return graph.G.nodes |
| 110 | + |
| 111 | + |
| 112 | +# Put this in its own file/module "degree.py" |
| 113 | +# when pymathics doc can handle. |
| 114 | +# """ |
| 115 | +# Graph Degree Measures |
| 116 | +# """ |
| 117 | + |
| 118 | + |
| 119 | +class VertexDegree(_NetworkXBuiltin): |
| 120 | + """ |
| 121 | + <url> |
| 122 | + :WMA link: |
| 123 | + https://reference.wolfram.com/language/ref/EdgeCount.html</url> |
| 124 | +
|
| 125 | + <dl> |
| 126 | + <dt>'VertexDegree[$g$]' |
| 127 | + <dd>returns a list of the degrees of each of the vertices in graph $g$. |
| 128 | +
|
| 129 | + <dt>'EdgeCount[$g$, $patt$]' |
| 130 | + <dd>returns the number of edges that match the pattern $patt$. |
| 131 | +
|
| 132 | + <dt>'EdgeCount[{$v$->$w}, ...}, ...]' |
| 133 | + <dd>uses rules $v$->$w$ to specify the graph $g$. |
| 134 | + </dl> |
| 135 | +
|
| 136 | + >> VertexDegree[{1 <-> 2, 2 <-> 3, 2 <-> 4}] |
| 137 | + = {1, 3, 1, 1} |
| 138 | + """ |
| 139 | + |
| 140 | + no_doc = False |
| 141 | + summary_text = "list graph vertex degrees" |
| 142 | + |
| 143 | + def eval(self, graph, evaluation, options): |
| 144 | + "%(name)s[graph_, OptionsPattern[%(name)s]]" |
| 145 | + |
| 146 | + def degrees(graph): |
| 147 | + degrees = dict(list(graph.G.degree(graph.vertices))) |
| 148 | + return ListExpression(*[Integer(degrees.get(v, 0)) for v in graph.vertices]) |
| 149 | + |
| 150 | + return self._evaluate_atom(graph, options, degrees) |
| 151 | + |
| 152 | + |
| 153 | +# TODO: VertexInDegree, VertexOutDegree |
0 commit comments