-
Notifications
You must be signed in to change notification settings - Fork 3
/
adjacency_list_directed.go
54 lines (42 loc) · 1.1 KB
/
adjacency_list_directed.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package graph
import "errors"
//AdjacencyListDirected Directed graphs
type AdjacencyListDirected struct {
AdjacencyList
}
//AddDirectedEdge Add the x->y vertice with default value.
func (g *AdjacencyListDirected) AddDirectedEdge(x, y *Node) error {
g.lazyInit()
if _, ok := g.a[x]; ok == false {
return errors.New("node X doesn't exists")
}
if _, ok := g.a[y]; ok == false {
return errors.New("node Y doesn't exists")
}
g.a[x][y] = nil
return nil
}
//RemoveDirectedEdge Remove the x->y vertice, if exists.
func (g *AdjacencyList) RemoveDirectedEdge(x, y *Node) error {
g.lazyInit()
if _, ok := g.a[x]; ok == false {
return errors.New("node X doesn't exists")
}
if _, ok := g.a[y]; ok == false {
return errors.New("node Y doesn't exists")
}
delete(g.a[x], y)
return nil
}
//SetDirectedEdgeValue ...
func (g *AdjacencyList) SetDirectedEdgeValue(x, y *Node, v interface{}) error {
g.lazyInit()
if _, ok := g.a[x]; ok == false {
return errors.New("node X doesn't exists")
}
if _, ok := g.a[y]; ok == false {
return errors.New("node Y doesn't exists")
}
g.a[x][y] = v
return nil
}