| 105 | } |
| 106 | |
| 107 | func (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 | |
| 137 | func (g *graph) RemoveEdge(from, to VertexId) error { |
| 138 | i, _ := g.edges[from][to] |