Skip to content

Commit 94fa455

Browse files
authored
Consider distance-2 degree for LargestFirst in bipartite graph (#119)
1 parent 6d0899b commit 94fa455

4 files changed

Lines changed: 55 additions & 14 deletions

File tree

src/graph.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ function minimum_degree(bg::BipartiteGraph, ::Val{side}) where {side}
182182
return minimum(v -> degree(bg, Val(side), v), vertices(bg, Val(side)))
183183
end
184184

185+
function degree_dist2(bg::BipartiteGraph{T}, ::Val{side}, v::Integer) where {T,side}
186+
# not efficient, for testing purposes only
187+
other_side = 3 - side
188+
neighbors_dist2 = Set{T}()
189+
for u in neighbors(bg, Val(side), v)
190+
for w in neighbors(bg, Val(other_side), u)
191+
w != v && push!(neighbors_dist2, w)
192+
end
193+
end
194+
return length(neighbors_dist2)
195+
end
196+
185197
## Construct from matrices
186198

187199
"""

src/order.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ function vertices(g::Graph, ::LargestFirst)
6161
end
6262

6363
function vertices(bg::BipartiteGraph, ::Val{side}, ::LargestFirst) where {side}
64-
criterion(v) = degree(bg, Val(side), v)
64+
other_side = 3 - side
65+
n = nb_vertices(bg, Val(side))
66+
visited = falses(n) # necessary for distance-2 neighborhoods
67+
degrees_dist2 = zeros(Int, n)
68+
for v in vertices(bg, Val(side))
69+
fill!(visited, false)
70+
for u in neighbors(bg, Val(side), v)
71+
for w in neighbors(bg, Val(other_side), u)
72+
if w != v && !visited[w]
73+
degrees_dist2[v] += 1
74+
visited[w] = true # avoid double counting
75+
end
76+
end
77+
end
78+
end
79+
criterion(v) = degrees_dist2[v]
6580
return sort(vertices(bg, Val(side)); by=criterion, rev=true)
6681
end

test/graph.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
using LinearAlgebra
22
using SparseArrays
33
using SparseMatrixColorings:
4-
Graph, adjacency_graph, bipartite_graph, degree, nb_vertices, nb_edges, neighbors
4+
Graph,
5+
adjacency_graph,
6+
bipartite_graph,
7+
degree,
8+
degree_dist2,
9+
nb_vertices,
10+
nb_edges,
11+
neighbors
512
using Test
613

714
## Standard graph
@@ -81,6 +88,14 @@ end;
8188
@test neighbors(bg, Val(2), 6) == [1, 3, 4]
8289
@test neighbors(bg, Val(2), 7) == [1, 2, 4]
8390
@test neighbors(bg, Val(2), 8) == [1, 2, 3]
91+
@test degree_dist2(bg, Val(2), 1) == 3
92+
@test degree_dist2(bg, Val(2), 2) == 3
93+
@test degree_dist2(bg, Val(2), 3) == 3
94+
@test degree_dist2(bg, Val(2), 4) == 3
95+
@test degree_dist2(bg, Val(2), 5) == 6
96+
@test degree_dist2(bg, Val(2), 6) == 6
97+
@test degree_dist2(bg, Val(2), 7) == 6
98+
@test degree_dist2(bg, Val(2), 8) == 6
8499

85100
A = sparse([
86101
1 0 1 1

test/order.jl

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using SparseArrays
22
using SparseMatrixColorings:
3+
BipartiteGraph,
34
Graph,
45
adjacency_graph,
56
bipartite_graph,
67
LargestFirst,
78
NaturalOrder,
89
RandomOrder,
10+
degree_dist2,
911
vertices
1012
using StableRNGs
1113
using Test
@@ -53,22 +55,19 @@ end;
5355

5456
@test vertices(ag, LargestFirst()) == [2, 1, 3]
5557

56-
A = sparse([
57-
1 1 1
58-
1 0 0
59-
0 1 1
60-
0 0 0
61-
])
62-
bg = bipartite_graph(A)
63-
64-
@test vertices(bg, Val(1), LargestFirst()) == [1, 3, 2, 4]
65-
6658
A = sparse([
6759
1 1 0 0
68-
1 0 1 0
60+
0 1 1 1
61+
0 0 1 0
62+
0 0 0 0
6963
1 0 1 0
7064
])
7165
bg = bipartite_graph(A)
7266

73-
@test vertices(bg, Val(2), LargestFirst()) == [1, 3, 2, 4]
67+
for side in (1, 2)
68+
true_order = sort(
69+
vertices(bg, Val(side)); by=v -> degree_dist2(bg, Val(side), v), rev=true
70+
)
71+
@test vertices(bg, Val(side), LargestFirst()) == true_order
72+
end
7473
end;

0 commit comments

Comments
 (0)