Esempio n. 1
0
func (l *LazyPrimMst) visit(g weighted.EdgeWeightedGraph, v int) {
	l.marked[v] = true

	for _, e := range g.AdjacentTo(v) {
		if !l.marked[e.OtherVertex(v)] {
			l.pq.Insert(&e)
		}
	}

}
func (sp *DijkstraShortestPaths) visit(g weighted.EdgeWeightedGraph, v int) {
	for _, e := range g.AdjacentTo(v) {
		var w = e.To() // to because directed graph
		totalWeighToVertex := sp.distTo[v] + e.Weight()
		if sp.distTo[w] > totalWeighToVertex { //is new total weigh to the vertex is lower
			sp.distTo[w] = totalWeighToVertex
			sp.edgeTo[w] = e
			if sp.pq.Contains(w) { //has lower weight than previous vertice
				sp.pq.Change(w, sp.distTo[w])
			} else { //there is no weight for the vertice
				vertex := weighted.NewVertex(w, sp.distTo[w])
				sp.pq.Insert(&vertex)
			}
		}
	}
}
Esempio n. 3
0
func (l *PrimMst) visit(g weighted.EdgeWeightedGraph, v int) {
	l.marked[v] = true

	//finds not visited lowest weight vertice
	for _, e := range g.AdjacentTo(v) {
		w := e.OtherVertex(v) //get vertex connected with the v vertex
		if l.marked[w] {      //vertice was already visited
			continue
		}
		if e.Weight() < l.distTo[w] {
			l.edgeTo[w] = e
			l.distTo[w] = e.Weight()
			if l.pq.Contains(w) { //has lower weight than previous vertice
				l.pq.Change(w, l.distTo[w])
			} else { //there is no weight for the vertice
				vertex := weighted.NewVertex(w, l.distTo[w])
				l.pq.Insert(&vertex)
			}
		}
	}
}