示例#1
0
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
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
}
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
}