-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ca0857
commit aa48432
Showing
4 changed files
with
119 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package graph | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
const errInvaliVertexNumber = "graph: invalid vertex number %v" | ||
|
||
// Graph represents an undirected graph. | ||
type Graph struct { | ||
bags [][]int | ||
edges int | ||
} | ||
|
||
// New returns a new undirected graph instance with v vertexs. | ||
func New(v int) *Graph { | ||
return &Graph{bags: make([][]int, v)} | ||
} | ||
|
||
// Vertexs returns the number of vertexs of the graph. | ||
func (g Graph) Vertexs() int { | ||
return len(g.bags) | ||
} | ||
|
||
// Edges returns the number of edges of the graph. | ||
func (g Graph) Edges() int { | ||
return g.edges | ||
} | ||
|
||
// addEdge adds an edge to the graph. | ||
func (g *Graph) addEdge(v, w int) { | ||
if v < 0 || v >= len(g.bags) { | ||
panic(fmt.Errorf(errInvaliVertexNumber, v)) | ||
} | ||
|
||
if w < 0 || w >= len(g.bags) { | ||
panic(fmt.Errorf(errInvaliVertexNumber, w)) | ||
} | ||
|
||
for _, b := range g.bags[v] { | ||
if b == w { | ||
panic(fmt.Errorf(errInvaliVertexNumber, w)) | ||
} | ||
} | ||
|
||
g.bags[v] = append(g.bags[v], w) | ||
|
||
for _, b := range g.bags[w] { | ||
if b == v { | ||
panic(fmt.Errorf(errInvaliVertexNumber, v)) | ||
} | ||
} | ||
|
||
g.bags[w] = append(g.bags[w], v) | ||
|
||
g.edges++ | ||
} | ||
|
||
// GetAdjacentVertexs returns all adjacent vertexs of v in the graph. | ||
func (g *Graph) GetAdjacentVertexs(v int) []int { | ||
if v < 0 || v >= len(g.bags) { | ||
panic(fmt.Errorf(errInvaliVertexNumber, v)) | ||
} | ||
|
||
return g.bags[v] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package graph | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type GraphSuite struct { | ||
suite.Suite | ||
|
||
g *Graph | ||
} | ||
|
||
func (s *GraphSuite) SetupTest() { | ||
s.g = New(10) | ||
} | ||
|
||
func (s GraphSuite) TestNew() { | ||
s.Equal(10, len(s.g.bags)) | ||
} | ||
|
||
func (s GraphSuite) TestVertexs() { | ||
s.Equal(10, s.g.Vertexs()) | ||
} | ||
|
||
func (s GraphSuite) TestEdges() { | ||
s.Equal(0, s.g.Edges()) | ||
s.g.addEdge(0, 1) | ||
s.Equal(1, s.g.Edges()) | ||
} | ||
|
||
func (s GraphSuite) TestEdgesFailed() { | ||
s.Panics(func() { s.g.addEdge(11, 1) }) | ||
s.Panics(func() { s.g.addEdge(1, 11) }) | ||
} | ||
|
||
func (s GraphSuite) TestGetAdjacentVertexs() { | ||
s.g.addEdge(0, 1) | ||
s.g.addEdge(0, 2) | ||
|
||
s.Equal([]int{1, 2}, s.g.GetAdjacentVertexs(0)) | ||
} | ||
|
||
func TestGraph(t *testing.T) { | ||
suite.Run(t, new(GraphSuite)) | ||
} |