コード例 #1
0
ファイル: lazyPrimMst.go プロジェクト: Paullius/go-graphs-
func NewLazyPrimMst(g weighted.EdgeWeightedGraph) LazyPrimMst {
	l := LazyPrimMst{
		marked: make([]bool, g.Vertices()),
		mst:    collections.Queue{},
		pq:     collections.PriorityQueue{}}

	l.visit(g, 0)

	for !l.pq.IsEmpty() {
		e := l.pq.DelMin().(*weighted.Edge)
		v := e.From()
		w := e.OtherVertex(v)
		if l.marked[v] && l.marked[w] {
			continue
		}

		l.mst.Push(e)
		if !l.marked[v] {
			l.visit(g, v)
		}
		if !l.marked[w] {
			l.visit(g, w)
		}
	}

	return l
}
コード例 #2
0
ファイル: lazyPrimMst.go プロジェクト: Paullius/go-graphs-
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)
		}
	}

}
コード例 #3
0
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)
			}
		}
	}
}
コード例 #4
0
ファイル: primMst.go プロジェクト: Paullius/go-graphs-
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)
			}
		}
	}
}
コード例 #5
0
ファイル: primMst.go プロジェクト: Paullius/go-graphs-
func NewPrimMst(g weighted.EdgeWeightedGraph) PrimMst {
	l := PrimMst{
		marked: make([]bool, g.Vertices()),
		edgeTo: make([]weighted.Edge, g.Vertices()),
		distTo: make([]float32, g.Vertices()),
		pq:     collections.PriorityQueue{}}

	for v := 0; v < g.Vertices(); v++ { //initialize all vertice distance weight to maximum
		l.distTo[v] = math.MaxFloat32
	}
	l.distTo[0] = 0.0
	vertex := weighted.NewVertex(0, 0.0)
	l.pq.Insert(&vertex) //create zero index vertex with zero weight

	for !l.pq.IsEmpty() {
		vertex := l.pq.DelMin().(collections.IndexItem) // get lowest weight vertice from queue
		l.visit(g, vertex.Index())
	}

	return l
}
コード例 #6
0
func NewDijkstraShortestPaths(g weighted.EdgeWeightedGraph, from int) DijkstraShortestPaths {

	l := DijkstraShortestPaths{
		edgeTo: make([]weighted.Edge, g.Vertices()),
		distTo: make([]float32, g.Vertices()),
		pq:     collections.PriorityQueue{},
		from:   from}

	for v := 0; v < g.Vertices(); v++ { //initialize all vertice distance weight to maximum
		l.distTo[v] = math.MaxFloat32
	}
	l.distTo[from] = 0.0
	vertex := weighted.NewVertex(from, 0.0)
	l.pq.Insert(&vertex) //create zero index vertex with zero weight

	for l.pq.Len() > 0 {
		indexItem := l.pq.DelMin().(collections.IndexItem) // get lowest weight vertice from queue
		l.visit(g, indexItem.Index())
	}

	return l
}