-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify
non_backtracking_matrix
function's Return Type (#328)
* modified to use a sparse matrix in non_backtracking_matrix * modified to reserve a capacity of size 'nz' & used 'append!' instead of 'push!' * added tests for non_backtracking_matrix * modified to achieve zero allocation * reverted * Apply suggestions from code review --------- Co-authored-by: Guillaume Dalle <[email protected]>
- Loading branch information
Showing
3 changed files
with
71 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using SparseArrays | ||
|
||
@testset "nonbacktracking" begin | ||
# Case: simple undirected | ||
ug = path_graph(5) | ||
B, edgemap = non_backtracking_matrix(ug) | ||
# | 1->2 | 2->3 | 3->4 | 4->5 | 2->1 | 3->2 | 4->3 | 5->4 | ||
# ------------------------------------------------------------- | ||
# 1->2 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | ||
# 2->3 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | ||
# 3->4 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | ||
# 4->5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
# 2->1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
# 3->2 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | ||
# 4->3 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | ||
# 5->4 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | ||
B_ = [ | ||
0 1 0 0 0 0 0 0 | ||
0 0 1 0 0 0 0 0 | ||
0 0 0 1 0 0 0 0 | ||
0 0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 0 | ||
0 0 0 0 1 0 0 0 | ||
0 0 0 0 0 1 0 0 | ||
0 0 0 0 0 0 1 0 | ||
] | ||
egs = Edge.([(1, 2), (2, 3), (3, 4), (4, 5), (2, 1), (3, 2), (4, 3), (5, 4)]) | ||
@test typeof(B) <: SparseMatrixCSC | ||
@test B == B_ | ||
|
||
# Case: simple directed | ||
dg = SimpleDiGraph(5) | ||
add_edge!(dg, 1, 2) | ||
add_edge!(dg, 2, 3) | ||
add_edge!(dg, 1, 3) | ||
add_edge!(dg, 3, 4) | ||
add_edge!(dg, 3, 5) | ||
add_edge!(dg, 4, 3) | ||
B, edgemap = non_backtracking_matrix(dg) | ||
# | 1->2 | 1->3 | 2->3 | 3->4 | 3->5 | 4->3 | ||
# ----------------------------------------------- | ||
# 1->2 | 0 | 0 | 1 | 0 | 0 | 0 | ||
# 1->3 | 0 | 0 | 0 | 1 | 1 | 0 | ||
# 2->3 | 0 | 0 | 0 | 1 | 1 | 0 | ||
# 3->4 | 0 | 0 | 0 | 0 | 0 | 0 | ||
# 3->5 | 0 | 0 | 0 | 0 | 0 | 0 | ||
# 4->3 | 0 | 0 | 0 | 0 | 1 | 0 | ||
B_ = [ | ||
0 0 1 0 0 0 | ||
0 0 0 1 1 0 | ||
0 0 0 1 1 0 | ||
0 0 0 0 0 0 | ||
0 0 0 0 0 0 | ||
0 0 0 0 1 0 | ||
] | ||
egs = Edge.([(1, 2), (1, 3), (2, 3), (3, 4), (3, 5), (4, 3)]) | ||
@test typeof(B) <: SparseMatrixCSC | ||
@test B == B_ | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters