Skip to content

Commit 65d0a80

Browse files
authored
[p1-greedy] Properly initialize degree maps (fixes #9)
1 parent 81cd180 commit 65d0a80

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

internal/phase1/greedy.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,24 @@ func execGreedy(g *graph.DGraph, params graph.Params) {
3636
indeg: graph.NodeIntMap{},
3737
}
3838

39-
nodeCount := len(g.Nodes)
39+
var (
40+
nodeCount = len(g.Nodes)
41+
sources []*graph.Node
42+
sinks []*graph.Node
43+
)
4044

41-
sources := slices.Collect(g.Sources())
42-
sinks := slices.Collect(g.Sinks())
45+
for _, n := range g.Nodes {
46+
p.indeg[n] = n.Indeg()
47+
if n.Indeg() == 0 {
48+
sources = append(sources, n)
49+
}
50+
p.outdeg[n] = n.Outdeg()
51+
if n.Outdeg() == 0 {
52+
sinks = append(sinks, n)
53+
}
54+
}
55+
sources = slices.Clip(sources)
56+
sinks = slices.Clip(sinks)
4357

4458
// here ELK accounts for edge priority: particular edges that the user doesn't want to reverse
4559
// can be assigned a non-zero priority; this will artificially increase the node's in-/out-degrees.

internal/testfiles/adjacency_lists.go

+24
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,30 @@ var issues1and4 = [][]string{
1515
{"N2", "Nd"},
1616
}
1717

18+
var issue9 = [][]string{
19+
{"N1", "N4"},
20+
{"N1", "N8"},
21+
{"N2", "N5"},
22+
{"N2", "N8"},
23+
{"N3", "N8"},
24+
{"N6", "N3"},
25+
{"N8", "N1"},
26+
{"N8", "N2"},
27+
{"N8", "N7"},
28+
{"N8", "N15"},
29+
{"N8", "N16"},
30+
{"N9", "N10"},
31+
{"N10", "N11"},
32+
{"N12", "N13"},
33+
{"N13", "N14"},
34+
{"N15", "N1"},
35+
{"N15", "N9"},
36+
{"N15", "N10"},
37+
{"N16", "N2"},
38+
{"N16", "N12"},
39+
{"N16", "N13"},
40+
}
41+
1842
var simpleVirtualNodes = [][]string{
1943
{"N1", "N2"},
2044
{"N2", "N3"},

internal/testfiles/bugfix_test.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ import (
1414
)
1515

1616
func TestCrashers(t *testing.T) {
17-
t.Run("phase4 SinkColoring", func(t *testing.T) {
18-
t.Run("program hangs", func(t *testing.T) {
19-
src := graph.EdgeSlice(issues1and4)
20-
assert.NotPanics(t, func() { _ = autog.Layout(src, autog.WithPositioning(autog.PositioningSinkColoring)) })
17+
t.Run("phase1", func(t *testing.T) {
18+
t.Run("greedy cycle breaker fails to break cycles", func(t *testing.T) {
19+
assert.NotPanics(t, func() {
20+
_ = autog.Layout(
21+
graph.EdgeSlice(issue9),
22+
autog.WithNonDeterministicGreedyCycleBreaker(),
23+
)
24+
})
2125
})
2226
})
2327

@@ -47,6 +51,13 @@ func TestCrashers(t *testing.T) {
4751
})
4852
})
4953

54+
t.Run("phase4 SinkColoring", func(t *testing.T) {
55+
t.Run("program hangs", func(t *testing.T) {
56+
src := graph.EdgeSlice(issues1and4)
57+
assert.NotPanics(t, func() { _ = autog.Layout(src, autog.WithPositioning(autog.PositioningSinkColoring)) })
58+
})
59+
})
60+
5061
t.Run("phase4 NetworkSimplex", func(t *testing.T) {
5162
g := &ig.DGraph{}
5263
graph.EdgeSlice(DotAbstract).Populate(g)

0 commit comments

Comments
 (0)