MCPcopy
hub / github.com/arnauddri/algorithms / AddEdge

Method AddEdge

data-structures/graph/graph.go:107–135  ·  view source on GitHub ↗
(from, to VertexId, weight int)

Source from the content-addressed store, hash-verified

105}
106
107func (g *graph) AddEdge(from, to VertexId, weight int) error {
108 if from == to {
109 return errors.New("Cannot add self loop")
110 }
111
112 if !g.CheckVertex(from) || !g.CheckVertex(to) {
113 return errors.New("Vertices don't exist")
114 }
115
116 i, _ := g.edges[from][to]
117 j, _ := g.edges[to][from]
118
119 if i > 0 || j > 0 {
120 return errors.New("Edge already defined")
121 }
122
123 g.TouchVertex(from)
124 g.TouchVertex(to)
125
126 g.edges[from][to] = weight
127
128 if !g.isDirected {
129 g.edges[to][from] = weight
130 }
131
132 g.edgesCount++
133
134 return nil
135}
136
137func (g *graph) RemoveEdge(from, to VertexId) error {
138 i, _ := g.edges[from][to]

Callers 10

ReverseMethod · 0.80
TestUndirectedGraphFunction · 0.80
TestDirectedGraphFunction · 0.80
TestTopologicalSortFunction · 0.80
TestDijkstraFunction · 0.80
TestBfsShortestPathFunction · 0.80
TestBfsFunction · 0.80
TestTopologicalSortFunction · 0.80
TestUndirectedDfsFunction · 0.80
TestDirectedDfsFunction · 0.80

Calls 2

CheckVertexMethod · 0.95
TouchVertexMethod · 0.95

Tested by 9

TestUndirectedGraphFunction · 0.64
TestDirectedGraphFunction · 0.64
TestTopologicalSortFunction · 0.64
TestDijkstraFunction · 0.64
TestBfsShortestPathFunction · 0.64
TestBfsFunction · 0.64
TestTopologicalSortFunction · 0.64
TestUndirectedDfsFunction · 0.64
TestDirectedDfsFunction · 0.64