Skip to content

Commit fe61a98

Browse files
authored
Merge pull request #3 from yourbasic/tip
Tip
2 parents dc64dd9 + 68a5c07 commit fe61a98

26 files changed

+191
-78
lines changed

README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
# Your basic graph
22

3-
Golang library of basic graph algorithms
3+
### Golang library of basic graph algorithms
44

55
![Topological ordering](top.png)
66

77
*Topological ordering, image by [David Eppstein][de], [CC0 1.0][cc010].*
88

9-
### Generic graph algorithms
9+
This library offers efficient and well-tested algorithms for
10+
11+
- breadth-first and depth-first search,
12+
- topological ordering,
13+
- strongly and weakly connected components,
14+
- bipartion,
15+
- shortest paths,
16+
- maximum flow,
17+
- Euler walks,
18+
- and minimum spanning trees.
1019

1120
The algorithms can be applied to any graph data structure implementing
12-
the two Iterator methods: Order, which returns the number of vertices,
13-
and Visit, which iterates over the neighbors of a vertex.
21+
the two `Iterator` methods: `Order`, which returns the number of vertices,
22+
and `Visit`, which iterates over the neighbors of a vertex.
1423

1524
All algorithms operate on directed graphs with a fixed number
1625
of vertices, labeled from 0 to n-1, and edges with integer cost.
@@ -21,21 +30,21 @@ is both directed and undirected.
2130

2231
### Graph data structures
2332

24-
The type Mutable represents a directed graph with a fixed number
33+
The type `Mutable` represents a directed graph with a fixed number
2534
of vertices and weighted edges that can be added or removed.
2635
The implementation uses hash maps to associate each vertex
2736
in the graph with its adjacent vertices. This gives constant
2837
time performance for all basic operations.
2938

30-
The type Immutable is a compact representation of an immutable graph.
39+
The type `Immutable` is a compact representation of an immutable graph.
3140
The implementation uses lists to associate each vertex in the graph
3241
with its adjacent vertices. This makes for fast and predictable
3342
iteration: the Visit method produces its elements by reading
34-
from a fixed sorted precomputed list. This type supports multigraphs.
43+
from a fixed sorted precomputed list.
3544

3645
### Virtual graphs
3746

38-
The subpackage graph/build offers a tool for building virtual graphs.
47+
The subpackage `graph/build` offers a tool for building virtual graphs.
3948
In a virtual graph no vertices or edges are stored in memory,
4049
they are instead computed as needed. New virtual graphs are constructed
4150
by composing and filtering a set of standard graphs, or by writing
@@ -61,7 +70,7 @@ There is an online reference for the package at
6170
* Version numbers adhere to [semantic versioning][sv].
6271

6372
The only accepted reason to modify the API of this package is to
64-
handle bug fixes that can't be resolved in any other reasonable way.
73+
handle issues that can't be resolved in any other reasonable way.
6574

6675
New features and performance enhancements are limited to basic
6776
algorithms and data structures, akin to the ones that you might find

build/build.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ type Virtual struct {
6868
visit func(v int, a int, do func(w int, c int64) (skip bool)) (aborted bool)
6969
}
7070

71-
// FilterFunc is a function that tells if there is an edge from v to w.
71+
// FilterFunc is a function that tells if there is a directed edge from v to w.
7272
// The nil value represents an edge functions that always returns true.
7373
type FilterFunc func(v, w int) bool
7474

7575
// CostFunc is a function that computes the cost of an edge from v to w.
7676
// The nil value represents a cost function that always returns 0.
7777
type CostFunc func(v, w int) int64
7878

79-
// Cost returns a CostFunc which always returns n.
79+
// Cost returns a CostFunc that always returns n.
8080
func Cost(n int64) CostFunc {
8181
return func(int, int) int64 { return n }
8282
}
@@ -432,15 +432,15 @@ func (g *Virtual) AddCostFunc(c CostFunc) *Virtual {
432432
return &res
433433
}
434434

435-
// Order returns the number of vertices in this graph.
435+
// Order returns the number of vertices in the graph.
436436
func (g *Virtual) Order() int {
437437
return g.order
438438
}
439439

440-
// Degree returns the number of neighbors of v.
440+
// Degree returns the number of outward directed edges from v.
441441
func (g *Virtual) Degree(v int) int {
442442
if v < 0 || v >= g.order {
443-
return 0
443+
panic("vertex out of range")
444444
}
445445
return g.degree(v)
446446
}

build/cartesian.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package build
22

33
import "strconv"
44

5-
// Cartesian returns the cartesian product of g1 and g2;
5+
// Cartesian returns the cartesian product of g1 and g2:
66
// a graph whose vertices correspond to ordered pairs (v1, v2),
77
// where v1 and v2 are vertices in g1 and g2, respectively.
88
// The vertices (v1, v2) and (w1, w2) are connected by an edge if

build/cycle.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package build
22

3-
// Cycle returns a virtual cycle graph with the edges
4-
// {0, 1}, {1, 2}, {2, 3},... , {n-1, 0}.
3+
// Cycle returns a virtual cycle graph with n vertices and
4+
// the edges {0, 1}, {1, 2}, {2, 3},... , {n-1, 0}.
55
func Cycle(n int) *Virtual {
66
switch {
77
case n < 0:

build/edgeset.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NoEdges() EdgeSet {
2424
}
2525
}
2626

27-
// Edge returns a set consisting of the single edge {v, w}, where v ≠ w.
27+
// Edge returns a set consisting of a single edge {v, w}, v ≠ w, of zero cost.
2828
func Edge(v, w int) EdgeSet {
2929
if v < 0 || w < 0 || v == w {
3030
return NoEdges()
@@ -35,7 +35,7 @@ func Edge(v, w int) EdgeSet {
3535
}
3636
}
3737

38-
// Contains tells if the set contains the edge from v to w.
38+
// Contains tells if the set contains the edge {v, w}.
3939
func (e EdgeSet) Contains(v, w int) bool {
4040
switch {
4141
case e.Keep != nil && !e.Keep(v, w):

build/examples_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
// Find a shortest path going back and forth between
1111
// two sets of points in the plane.
1212
func Example_euclid() {
13-
type point struct{ x, y int }
13+
type Point struct{ x, y int }
1414

1515
// Euclidean distance.
16-
euclid := func(p, q point) float64 {
16+
Euclid := func(p, q Point) float64 {
1717
xd := p.x - q.x
1818
yd := p.y - q.y
1919
return math.Sqrt(float64(xd*xd + yd*yd))
@@ -22,7 +22,7 @@ func Example_euclid() {
2222
// 0 3
2323
// 1 4
2424
// 2 5
25-
points := []point{
25+
points := []Point{
2626
{0, 0}, {0, 1}, {0, 2},
2727
{4, 0}, {4, 1}, {4, 2},
2828
}
@@ -32,7 +32,7 @@ func Example_euclid() {
3232
// and then apply a cost function to the edges of the graph.
3333
g := build.Kmn(3, 3).AddCostFunc(func(v, w int) int64 {
3434
// Distance to three decimal places.
35-
return int64(1000 * euclid(points[v], points[w]))
35+
return int64(1000 * Euclid(points[v], points[w]))
3636
})
3737

3838
// Find a shortest path from 0 to 2.

build/grid.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package build
22

33
import "strconv"
44

5-
// Grid returns a virtual graph whose vertices correspond to points in the plane
6-
// with integer coordinates, y-coordinates being in the range 0..m-1,
7-
// and x-coordinates in the range 0..n-1. Two vertices are connected
8-
// by an edge whenever the corresponding points are at distance 1.
5+
// Grid returns a virtual graph whose vertices correspond to integer
6+
// points in the plane: y-coordinates being in the range 0..m-1,
7+
// and x-coordinates in the range 0..n-1. Two vertices of a grid
8+
// are adjacent whenever the corresponding points are at distance 1.
99
//
1010
// Point (x, y) gets index nx + y, and index i corresponds to the point (i/n, i%n).
1111
func Grid(m, n int) *Virtual {

build/match.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package build
33
// Match connects g1 to g2 by matching vertices in g1 with vertices in g2.
44
// Only vertices belonging to the bridge are included,
55
// and the vertices are matched in numerical order.
6-
// The vertices of g2 are renumbered before the operation:
6+
// The vertices of g2 are renumbered before the matching:
77
// vertex v ∊ g2 becomes v + g1.Order() in the new graph.
88
func (g1 *Virtual) Match(g2 *Virtual, bridge EdgeSet) *Virtual {
99
n := g1.order + g2.order

build/tensor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package build
22

33
import "strconv"
44

5-
// Tensor returns the tensor product of g1 and g2;
5+
// Tensor returns the tensor product of g1 and g2:
66
// a graph whose vertices correspond to ordered pairs (v1, v2),
77
// where v1 and v2 are vertices in g1 and g2, respectively.
88
// The vertices (v1, v2) and (w1, w2) are connected by an edge whenever

build/vertexset.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package build
33
import "sort"
44

55
// VertexSet represents a set of vertices in a graph.
6-
// The zero value of a VertexSet is the universe,
7-
// which represents all vertices in a graph.
6+
// The zero value of a VertexSet is the universe;
7+
// the set containing all vertices.
88
type VertexSet struct {
99
// A set is an immutable sorted list of non-empty disjoint intervals.
1010
// The zero value VertexSet{nil} represents the universe.
@@ -90,7 +90,7 @@ func (s VertexSet) rank(n int) int {
9090
return in.index + n - in.a
9191
}
9292

93-
// Contains tells if v is a member of set s.
93+
// Contains tells if v is a member of the set.
9494
func (s VertexSet) Contains(v int) bool {
9595
switch {
9696
case s.set == nil:

0 commit comments

Comments
 (0)