@@ -42,20 +42,21 @@ import (
4242// functions that describe the edges of a graph.
4343type Virtual struct {
4444 order int
45- // The edge and cost functions define a weighted graph without self-loops.
45+ // The ` edge` and ` cost` functions define a weighted graph without self-loops.
4646 //
4747 // • edge(v, w) returns true whenever (v, w) belongs to the graph;
4848 // the value is disregarded when v == w.
4949 //
50- // • cost(v, w) returns the cost of (v, w); the value is disregarded
51- // when edge(v, w) is false.
50+ // • cost(v, w) returns the cost of (v, w);
51+ // the value is disregarded when edge(v, w) is false.
52+ //
5253 edge func (v , w int ) bool
5354 cost func (v , w int ) int64
5455
55- // These functions can be used to improve performance.
56- // They MUST BE CONSISTENT with edge and cost.
57- // The generic() factory method contains a basic implementation .
58- // The Consistent test function should be used to check compliance.
56+ // The `degree` and `visit` functions can be used to improve performance.
57+ // They MUST BE CONSISTENT with edge and cost. If not implemented,
58+ // the ` generic` or `generic0` implementation is used instead .
59+ // The ` Consistent` test function should be used to check compliance.
5960 //
6061 // • degree(v) returns the outdegree of vertex v.
6162 //
@@ -64,12 +65,13 @@ type Virtual struct {
6465 // If a call to do returns true, visit MUST ABORT the iteration
6566 // and return true; if successful it should return false.
6667 // Precondition: a ≥ 0.
68+ //
6769 degree func (v int ) int
6870 visit func (v int , a int , do func (w int , c int64 ) (skip bool )) (aborted bool )
6971}
7072
7173// FilterFunc is a function that tells if there is a directed edge from v to w.
72- // The nil value represents an edge functions that always returns true.
74+ // The nil value represents an edge function that always returns true.
7375type FilterFunc func (v , w int ) bool
7476
7577// CostFunc is a function that computes the cost of an edge from v to w.
@@ -107,7 +109,7 @@ func max(m, n int) int {
107109 return m
108110}
109111
110- // null is the null graph; a graph of order 0 .
112+ // null is the null graph; a graph with no vertices .
111113var null = new (Virtual )
112114
113115// singleton returns a graph with one vertex.
@@ -126,7 +128,7 @@ func edge() *Virtual {
126128 g := & Virtual {
127129 order : 2 ,
128130 cost : zero ,
129- edge : func ( v , w int ) bool { return v != w } ,
131+ edge : alwaysEdge ,
130132 degree : degreeOne ,
131133 }
132134 g .visit = func (v int , a int , do func (w int , c int64 ) bool ) (aborted bool ) {
@@ -260,17 +262,16 @@ func Generic(n int, edge FilterFunc) *Virtual {
260262 return generic0 (n , edge )
261263}
262264
263- // Specific returns a cached copy of g with constant time performance
264- // for all basic operations. It uses space proportional to
265- // the size of the graph.
265+ // Specific returns a cached copy of g with constant time performance for
266+ // all basic operations. It uses space proportional to the size of the graph.
266267//
267268// This function does not accept multigraphs and graphs with self-loops.
268269func Specific (g graph.Iterator ) * Virtual {
269- stats := graph .Check (g )
270+ h := graph .Sort (g )
271+ stats := graph .Check (h )
270272 if stats .Multi != 0 || stats .Loops != 0 {
271273 panic ("Virtual doesn't support multiple edges or self-loops" )
272274 }
273- h := graph .Sort (g )
274275 res := & Virtual {
275276 order : h .Order (),
276277 edge : h .Edge ,
@@ -282,7 +283,7 @@ func Specific(g graph.Iterator) *Virtual {
282283 return res
283284 }
284285 res .cost = func (v , w int ) (cost int64 ) {
285- if ! res . edge (v , w ) {
286+ if ! h . Edge (v , w ) {
286287 return 0
287288 }
288289 h .VisitFrom (v , w , func (w int , c int64 ) (skip bool ) {
@@ -353,10 +354,7 @@ func (g *Virtual) Complement() *Virtual {
353354 return singleton ()
354355 }
355356 res := generic0 (n , func (v , w int ) (edge bool ) {
356- if v != w {
357- return ! g .edge (v , w )
358- }
359- return
357+ return v != w && ! g .edge (v , w )
360358 })
361359 res .degree = func (v int ) int { return n - 1 - g .degree (v ) }
362360 res .visit = func (v int , a int , do func (w int , c int64 ) bool ) (aborted bool ) {
0 commit comments