| 1 |
package build |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
func (g *Virtual) Subgraph(s VertexSet) *Virtual { |
| 6 |
n := g.Order() |
| 7 |
s = s.And(Range(0, n)) |
| 8 |
m := s.size() |
| 9 |
switch { |
| 10 |
case m == 0: |
| 11 |
return null |
| 12 |
case m == 1: |
| 13 |
return singleton() |
| 14 |
case m == n: |
| 15 |
return g |
| 16 |
} |
| 17 |
|
| 18 |
cost := func(v, w int) int64 { |
| 19 |
return g.cost(s.get(v), s.get(w)) |
| 20 |
} |
| 21 |
res := generic(m, cost, func(v, w int) bool { |
| 22 |
return g.edge(s.get(v), s.get(w)) |
| 23 |
}) |
| 24 |
res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { |
| 25 |
if a >= m { |
| 26 |
return |
| 27 |
} |
| 28 |
v0 := s.get(v) |
| 29 |
s0 := s.And(Range(s.get(a), n)) |
| 30 |
for _, in := range s0.set { |
| 31 |
if more := false; g.visit(v0, in.a, func(w0 int, c int64) (skip bool) { |
| 32 |
switch { |
| 33 |
case w0 >= in.b: |
| 34 |
more, skip = true, true |
| 35 |
return |
| 36 |
case do(s.rank(w0), c): |
| 37 |
return true |
| 38 |
default: |
| 39 |
return |
| 40 |
} |
| 41 |
}) && !more { |
| 42 |
return true |
| 43 |
} |
| 44 |
} |
| 45 |
return |
| 46 |
} |
| 47 |
return res |
| 48 |
} |