You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 6103438
Browse filesBrowse the repository at this point in the historyBrowse files
This needs Python 3.9 or later. The dependencies, NumPy, Matplotlib and Spatial Maths, are installed with it.
54
+
55
+
## Examples
56
+
57
+
### A small example: routing through towns in Queensland
58
+
59
+

The data file and the complete example are in the notebook [`examples/roads.ipynb`](https://github.com/petercorke/pgraph-python/blob/main/examples/roads.ipynb).
83
+
84
+
### A bigger example: routing through central London
85
+
86
+

87
+
88
+
A directed graph of the road network of central London, taken from [OpenStreetMap](https://www.openstreetmap.org/copyright), and the shortest drive between Buckingham Palace and Tower Bridge, there (solid) and back (dashed). The two routes differ in length, 5,515 m and 5,948 m, because of the one-way streets. The notebook [`examples/london.ipynb`](https://github.com/petercorke/pgraph-python/blob/main/examples/london.ipynb) builds the graph from the map data, keeps the part of it that can be driven around in, and plans with `A*`. This graph has over 4,000 vertices.
Full details are given in the [online documentation](https://petercorke.github.io/pgraph-python).
95
+
74
96
### Properties and methods of the graph
97
+
75
98
Graphs belong to the class `UGraph` or `DGraph` for undirected or directed graphs respectively. The graph is essentially a container for the vertices.
76
99
77
100
-`g.add_vertex()` add a vertex
@@ -84,23 +107,39 @@ Graphs belong to the class `UGraph` or `DGraph` for undirected or directed graph
84
107
-`g.edges()` all edges in the graph
85
108
-`g.plot()` plots the vertices and edges
86
109
-`g.nc` the number of graph components, 1 if fully connected
87
-
-`g.component(v)` the component that vertex `v` belongs to
110
+
-`g.component(c)` the vertices in component number `c`, numbered from 0
88
111
-`g.samecomponent(v1, v2)` predicate for vertices belonging to the same component
89
112
90
113
***
91
-
-`g.path_BFS()` breadth-first search
92
-
-`g.path_Astar()` A* search
114
+
-`g.path_BFS()` breadth-first search, the path with the fewest edges
115
+
-`g.path_UCS()` uniform-cost search, the cheapest path
116
+
-`g.path_Astar()` A* search, the cheapest path using a heuristic
117
+
118
+
***
119
+
-`g.closest(point)` the vertex nearest to a point
120
+
-`g.near(point, radius)` the vertices within a distance of a point
121
+
-`g.within(box)` the vertices inside a box
122
+
-`g.subgraph(vertices)` the part of the graph that those vertices induce, as a new graph
123
+
-`g.coordinates()` the vertex coordinates as a matrix, a column per vertex
124
+
-`g.build_kdtree()` an optional spatial index (needs scipy) that makes `closest` and `near` faster on a large graph
125
+
-`g.metric` the distance metric: `"L2"` (the default), `"L1"`, `"SE2"`, `SE2Metric(weight)` that weights the angle of a pose, or a function of your own
93
126
94
127
***
95
128
-`g.adjacency()` adjacency matrix
96
129
-`g.laplacian()` Laplacian matrix
97
130
-`g.incidence()` incidence matrix
98
131
132
+
***
133
+
-`g.copy()` a deep copy; a graph can also be pickled
134
+
-`g.export()` write the graph as GraphViz DOT, Mermaid, GraphML, ELK, or an image (which needs GraphViz)
135
+
-`g.validate()` check that the graph's records agree with each other
136
+
99
137
### Properties and methods of a vertex
100
138
Vertices are instances of the class `Vertex`, for both undirected and directed graphs.
101
139
102
140
-`v.coord` the coordinate vector for embedded graph (optional)
103
141
-`v.name` the name of the vertex (optional)
142
+
-`v.label` the number of the component that the vertex belongs to
104
143
-`v.adjacent()` is a list of the adjacent vertices (`neighbours()` and `neighbors()` are the same)
105
144
106
145
Vertices can be named and referenced by name.
@@ -113,25 +152,29 @@ Each edge references a pair of vertices, the graph lists all of its edges in `g.
113
152
-`e.next(v)` vertex on edge `e` that is not `v`
114
153
-`e.v1`, `e.v2` the two vertices that define the edge `e`
115
154
116
-
## Modifying a graph
155
+
###Modifying a graph
117
156
118
157
-`g.remove_vertex(v)` or `v.remove()` remove vertex `v`
119
158
-`g.remove_edge(e)` or `e.remove()` remove edge `e`
120
159
121
-
## Subclassing pgraph classes
160
+
###Subclassing `pgraph` classes
122
161
123
162
Consider a user class `Foo` that we would like to connect using a graph _overlay_, i.e.
124
163
instances of `Foo` become vertices in a graph.
125
164
126
165
- Have it subclass `Vertex`
127
166
- Then place instances of `Foo` into the graph using `add_vertex` and create edges as required
128
167
129
-
```
168
+
```python
169
+
from pgraph import UGraph, Vertex
170
+
130
171
classFoo(Vertex):
131
-
# foo stuff goes here
132
-
133
-
f1 = Foo(...)
134
-
f2 = Foo(...)
172
+
# foo stuff goes here
173
+
defsay_hi(self):
174
+
print(f"hi from {self.name}")
175
+
176
+
f1 = Foo(name="f1")
177
+
f2 = Foo(name="f2")
135
178
136
179
g = UGraph() # create a new undirected graph
137
180
g.add_vertex(f1)
@@ -142,14 +185,52 @@ for f in f1.adjacent():
142
185
f.say_hi() # say hi to the neighbours
143
186
```
144
187
145
-
## MATLAB version
146
-
147
-
This is a re-engineered version of [PGraph.m](https://github.com/petercorke/spatialmath-matlab/blob/main/PGraph.m) which ships as part of the [Spatial Math Toolbox for MATLAB](https://github.com/petercorke/spatialmath-matlab). This class is used to support bundle adjustment, pose-graph SLAM and various planners such as PRM, RRT and Lattice.
148
-
149
-
The Python version was designed from the start to work with directed and undirected graphs, whereas directed graphs were a late addition to the MATLAB version. Semantics are similar but not identical. In particular the use of subclassing rather than references to
150
-
_user data_ is encouraged.
151
-
152
-
153
-
154
-
155
-
188
+
Or tell the graph which classes to make, with `UGraph(vertex_class=Foo, edge_class=Bar)`, and `g.add_vertex()` and `g.add_edge()` create instances of them, so they never need to be constructed by hand.
189
+
190
+
## Performance
191
+
192
+
Timings of the key operations on random geometric graphs, each vertex joined to its 4 nearest neighbours, of 100, 1,000 and 10,000 vertices, measured with [`examples/benchmark.py`](https://github.com/petercorke/pgraph-python/blob/main/examples/benchmark.py) on an Apple M4 with Python 3.12 and NumPy 2.5. The path times are the median of 10 random start and goal pairs. The last column is the exponent *b* in time ∝ N<sup>*b*</sup> between the smallest and the largest graph for the quantity shown, so 0 is constant, 1 is linear and 2 is quadratic. The adjacency, Laplacian and incidence matrices are dense, so they are only formed up to 1,000 vertices.
This is a re-engineered version of [PGraph.m](https://github.com/petercorke/spatialmath-matlab/blob/main/PGraph.m) which ships as part of the [Spatial Math Toolbox for MATLAB](https://github.com/petercorke/spatialmath-matlab). That class was used to support bundle adjustment, pose-graph SLAM and various planners such as PRM, RRT and Lattice.
235
+
236
+
The Python version was designed from the start to work with directed and undirected graphs, whereas directed graphs were a late addition to the MATLAB version. Semantics are similar but not identical. In particular the use of subclassing rather than references to _user data_ is encouraged.
0 commit comments