-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.go
40 lines (35 loc) · 875 Bytes
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package graph
import "fmt"
// Graph represents an undirected graph.
// The zero value in an empty graph ready to use
type Graph struct {
nbVertices int
nbEdges int
adj [][]int
}
// NewGraph returns an initialized graph with n vertices
func NewGraph(n int) *Graph {
g := &Graph{}
g.nbVertices = n
g.nbEdges = 0
g.adj = make([][]int, n)
return g
}
// AddEdge links vertices v and w in both direction
func (g *Graph) AddEdge(v int, w int) {
g.adj[v] = append(g.adj[v], w)
g.adj[w] = append(g.adj[w], v)
g.nbEdges++
}
// String returns a string representation of the graph's adjacency lists
func (g Graph) String() string {
s := fmt.Sprintln(g.nbVertices, "vertices,", g.nbEdges, "edges")
for v := 0; v < g.nbVertices; v++ {
s += fmt.Sprint(v, ": ")
for _, w := range g.adj[v] {
s += fmt.Sprint(w, ",")
}
s += fmt.Sprintln()
}
return s
}