Skip to content

Commit

Permalink
Optimizing matrix generation by using its symmetrical property
Browse files Browse the repository at this point in the history
  • Loading branch information
JustineFricou committed May 2, 2024
1 parent 429a2b6 commit 1e85200
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions geotrek/core/path_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def get_cs_graph(self):

nodes_list = list(self.nodes.items())
for i, (key1, value1) in enumerate(nodes_list[:-1]):
# The last row is left blank and j starts at i + 1 because only the
# upper triangle is filled and the main diagonal is all zeros (the
# i ends at len - 1 and j starts at i + 1 because the
# matrix is symmetric and the main diagonal is all zeros (the
# weight from a node to itself is 0)
for j, (key2, value2) in enumerate(nodes_list[i + 1:]):
if key2 in value1.keys():
Expand All @@ -99,12 +99,7 @@ def get_cs_graph(self):
edge_weight = self.get_edge_weight(edge_id)
if edge_weight is not None:
matrix[i][j + i + 1] = edge_weight
# TODO: add matrix[j + i + 1][i] = edge_weight
# instead of using triangles

# Fill the lower triangle of the matrix symmetrically to the upper one
lower_triangle = np.triu(matrix, 1).T
matrix += lower_triangle
matrix[j + i + 1][i] = edge_weight

return matrix

Expand Down

0 comments on commit 1e85200

Please sign in to comment.