Exemplo n.º 1
0
// RemoveEdge removes the edge between nodes identified by e.From and e.To and
// its adjacent faces from g.
func (g *Graph) RemoveEdge(e graph.Edge) {
	h := g.Halfedge(e.From(), e.To())
	if h == nil {
		// Nothing to do.
		return
	}

	// Remove any adjacent faces.
	if h.Face() != nil {
		g.RemoveFace(h.Face())
	}
	if h.Twin().Face() != nil {
		g.RemoveFace(h.Twin().Face())
	}

	// Detach both halfedges from their From nodes and update affected
	// halfedges.
	detach(h)
	detach(h.Twin())

	id := h.Edge().ID()
	delete(g.edges, id)
	if g.nextEdgeID != 0 && id == g.nextEdgeID {
		g.nextEdgeID--
	}
	g.freeEdges[id] = struct{}{}
}
Exemplo n.º 2
0
func (g *TileGraph) Cost(e graph.Edge) float64 {
	if edge := g.EdgeBetween(e.Head(), e.Tail()); edge != nil {
		return 1
	}

	return inf
}
Exemplo n.º 3
0
func (g *TileGraph) Weight(e graph.Edge) float64 {
	if edge := g.EdgeBetween(e.From(), e.To()); edge != nil {
		return 1
	}

	return inf
}
Exemplo n.º 4
0
func (g *Graph) Cost(e graph.Edge) float64 {
	if n, ok := g.neighbors[e.Head().ID()]; ok {
		if we, ok := n[e.Tail().ID()]; ok {
			return we.Cost
		}
	}
	return inf
}
Exemplo n.º 5
0
func (g *DirectedDenseGraph) SetEdgeWeight(e graph.Edge, weight float64) {
	fid := e.From().ID()
	tid := e.To().ID()
	if fid == tid {
		panic("concrete: set edge cost of illegal edge")
	}
	g.mat.Set(fid, tid, weight)
}
Exemplo n.º 6
0
func (g *DirectedGraph) Weight(e graph.Edge) float64 {
	if s, ok := g.successors[e.From().ID()]; ok {
		if we, ok := s[e.To().ID()]; ok {
			return we.Cost
		}
	}
	return inf
}
Exemplo n.º 7
0
func (g *DirectedGraph) Cost(e graph.Edge) float64 {
	if s, ok := g.successors[e.Head().ID()]; ok {
		if we, ok := s[e.Tail().ID()]; ok {
			return we.Cost
		}
	}
	return inf
}
Exemplo n.º 8
0
func (g *Graph) Weight(e graph.Edge) float64 {
	if n, ok := g.neighbors[e.From().ID()]; ok {
		if we, ok := n[e.To().ID()]; ok {
			return we.Cost
		}
	}
	return inf
}
Exemplo n.º 9
0
func (g *Graph) RemoveUndirectedEdge(e graph.Edge) {
	head, tail := e.Head(), e.Tail()
	if _, ok := g.nodeMap[head.ID()]; !ok {
		return
	} else if _, ok := g.nodeMap[tail.ID()]; !ok {
		return
	}

	delete(g.neighbors[head.ID()], tail.ID())
	delete(g.neighbors[tail.ID()], head.ID())
}
Exemplo n.º 10
0
func (g *UndirectedGraph) RemoveEdge(e graph.Edge) {
	from, to := e.From(), e.To()
	if _, ok := g.nodeMap[from.ID()]; !ok {
		return
	} else if _, ok := g.nodeMap[to.ID()]; !ok {
		return
	}

	delete(g.neighbors[from.ID()], to.ID())
	delete(g.neighbors[to.ID()], from.ID())
}
Exemplo n.º 11
0
// RemoveEdge removes e from the graph, leaving the terminal nodes. If the edge does not exist
// it is a no-op.
func (g *DirectedMatrix) RemoveEdge(e graph.Edge) {
	fid := e.From().ID()
	if !g.has(fid) {
		return
	}
	tid := e.To().ID()
	if !g.has(tid) {
		return
	}
	g.mat.Set(fid, tid, g.absent)
}
Exemplo n.º 12
0
func (g *DirectedGraph) RemoveDirectedEdge(e graph.Edge) {
	head, tail := e.Head(), e.Tail()
	if _, ok := g.nodeMap[head.ID()]; !ok {
		return
	} else if _, ok := g.nodeMap[tail.ID()]; !ok {
		return
	}

	delete(g.successors[head.ID()], tail.ID())
	delete(g.predecessors[tail.ID()], head.ID())
}
Exemplo n.º 13
0
// RemoveEdge removes e from the graph, leaving the terminal nodes. If the edge does not exist
// it is a no-op.
func (g *DirectedGraph) RemoveEdge(e graph.Edge) {
	from, to := e.From(), e.To()
	if _, ok := g.nodes[from.ID()]; !ok {
		return
	}
	if _, ok := g.nodes[to.ID()]; !ok {
		return
	}

	delete(g.from[from.ID()], to.ID())
	delete(g.to[to.ID()], from.ID())
}
Exemplo n.º 14
0
func (g *Graph) AddUndirectedEdge(e graph.Edge, cost float64) {
	head, tail := e.Head(), e.Tail()
	if !g.NodeExists(head) {
		g.AddNode(head)
	}

	if !g.NodeExists(tail) {
		g.AddNode(tail)
	}

	g.neighbors[head.ID()][tail.ID()] = WeightedEdge{Edge: e, Cost: cost}
	g.neighbors[tail.ID()][head.ID()] = WeightedEdge{Edge: e, Cost: cost}
}
Exemplo n.º 15
0
func (g *gnDirected) SetEdge(e graph.Edge) {
	switch {
	case e.From().ID() == e.To().ID():
		g.addSelfLoop = true
		return
	case g.DirectedBuilder.HasEdgeFromTo(e.From(), e.To()):
		g.addMultipleEdge = true
	}

	g.DirectedBuilder.SetEdge(e)
}
Exemplo n.º 16
0
// Weight returns the weight of the given edge.
func (g *Grid) Weight(e graph.Edge) float64 {
	if e := g.EdgeBetween(e.From(), e.To()); e != nil {
		if !g.AllowDiagonal {
			return 1
		}
		ux, uy := g.XY(e.From())
		vx, vy := g.XY(e.To())
		return math.Hypot(ux-vx, uy-vy)

	}
	return math.Inf(1)
}
Exemplo n.º 17
0
// SetEdge adds e, an edge from one node to another. If the nodes do not exist, they are added.
// It will panic if the IDs of the e.From and e.To are equal.
func (g *UndirectedGraph) SetEdge(e graph.Edge) {
	var (
		from = e.From()
		fid  = from.ID()
		to   = e.To()
		tid  = to.ID()
	)

	if fid == tid {
		panic("simple: adding self edge")
	}

	if !g.Has(from) {
		g.AddNode(from)
	}
	if !g.Has(to) {
		g.AddNode(to)
	}

	g.edges[fid][tid] = e
	g.edges[tid][fid] = e
}
Exemplo n.º 18
0
func (g *UndirectedDenseGraph) SetEdgeWeight(e graph.Edge) {
	fid := e.From().ID()
	tid := e.To().ID()
	if fid == tid {
		panic("concrete: set edge cost of illegal edge")
	}
	g.mat.SetSym(fid, tid, e.Weight())
}
Exemplo n.º 19
0
// SetEdge sets e, an edge from one node to another. If the ends of the edge are not in g
// or the edge is a self loop, SetEdge panics.
func (g *DirectedMatrix) SetEdge(e graph.Edge) {
	fid := e.From().ID()
	tid := e.To().ID()
	if fid == tid {
		panic("simple: set illegal edge")
	}
	g.mat.Set(fid, tid, e.Weight())
}
Exemplo n.º 20
0
func (g *DirectedGraph) SetEdge(e graph.Edge, cost float64) {
	var (
		from = e.From()
		fid  = from.ID()
		to   = e.To()
		tid  = to.ID()
	)

	if fid == tid {
		panic("concrete: adding self edge")
	}

	if !g.Has(from) {
		g.AddNode(from)
	}

	if !g.Has(to) {
		g.AddNode(to)
	}

	g.successors[fid][tid] = WeightedEdge{Edge: e, Cost: cost}
	g.predecessors[tid][fid] = WeightedEdge{Edge: e, Cost: cost}
}
Exemplo n.º 21
0
func (g *DirectedDenseGraph) RemoveEdge(e graph.Edge) {
	g.mat.Set(e.From().ID(), e.To().ID(), g.absent)
}
Exemplo n.º 22
0
func (g *gnUndirected) SetEdge(e graph.Edge) {
	switch {
	case e.From().ID() == e.To().ID():
		g.addSelfLoop = true
		return
	case e.From().ID() > e.To().ID():
		g.addBackwards = true
	case g.UndirectedBuilder.HasEdgeBetween(e.From(), e.To()):
		g.addMultipleEdge = true
	}

	g.UndirectedBuilder.SetEdge(e)
}
Exemplo n.º 23
0
// Sets the cost of an edge. If the cost is +Inf, it will remove the edge,
// if directed is true, it will only remove the edge one way. If it's false it will change the cost
// of the edge from succ to node as well.
func (g *DenseGraph) SetEdgeCost(e graph.Edge, cost float64, directed bool) {
	g.adjacencyMatrix[e.Head().ID()*g.numNodes+e.Tail().ID()] = cost
	if !directed {
		g.adjacencyMatrix[e.Tail().ID()*g.numNodes+e.Head().ID()] = cost
	}
}
Exemplo n.º 24
0
func (g *DenseGraph) Cost(e graph.Edge) float64 {
	return g.adjacencyMatrix[e.Head().ID()*g.numNodes+e.Tail().ID()]
}
Exemplo n.º 25
0
func (g *DirectedDenseGraph) Weight(e graph.Edge) float64 {
	return g.mat.At(e.From().ID(), e.To().ID())
}