// Prim algorithm calculate minimum spanning tree on the input undirected graph
// Note that MinHeap in this implmenetation is based on "container/heap"
Prim algorithm calculate minimum spanning tree on the input undirected graph
Note that MinHeap in this implmenetation is based on "container/heap"
// Prim algorithm calculate minimum spanning tree on the input undirected graph // Note that MinHeap in this implmenetation is based on " container / heap "
func Prim(g weightedgraph.WeightedGraph) (MinSpanningTree, error) {
	mst := MinSpanningTree{[]graph.EdgeID{}, g}
	queringHeap := &queryHeap{minHeap{}, map[graph.NodeID]*heapNode{}}
	g.IterateAllNodes(func(u graph.NodeID) {
		item := &heapNode{self: u, parent: graph.InvalidNodeID, key: maxKey}
		queringHeap.minHeap = append(queringHeap.minHeap, item)
		queringHeap.nodeToItem[u] = item
	})
	queringHeap.minHeap[0].key = 0 
	heap.Init(&queringHeap.minHeap)
	for queringHeap.Len() > 0 {
		uItem := queringHeap.popMin() 
		if uItem.parent != graph.InvalidNodeID {
			mst.edges = append(mst.edges, graph.EdgeID{From: uItem.parent, To: uItem.self})
		}
		g.IterateAdjacencyNodes(uItem.self, func(v graph.NodeID) {
			item, ok := queringHeap.find(v)
			uvWeight, _ := g.Weight(uItem.self, v)
			if ok && uvWeight < item.key {
				queringHeap.update(item, uItem.self, uvWeight)
			}
		})
	}
	return mst, nil
}
func Prim ( g weightedgraph . WeightedGraph ) ( MinSpanningTree , error ) { mst := MinSpanningTree { [ ] graph . EdgeID { } , g } queringHeap := & queryHeap { minHeap { } , map [ graph . NodeID ] * heapNode { } } g . IterateAllNodes ( func ( u graph . NodeID ) { item := & heapNode { self : u , parent : graph . InvalidNodeID , key : maxKey } queringHeap . minHeap = append ( queringHeap . minHeap , item ) queringHeap . nodeToItem [ u ] = item } ) queringHeap . minHeap [ 0 ] . key = 0 heap . Init ( & queringHeap . minHeap ) for queringHeap . Len ( ) > 0 { uItem := queringHeap . popMin ( ) if uItem . parent != graph . InvalidNodeID { mst . edges = append ( mst . edges , graph . EdgeID { From : uItem . parent , To : uItem . self } ) } g . IterateAdjacencyNodes ( uItem . self , func ( v graph . NodeID ) { item , ok := queringHeap . find ( v ) uvWeight , _ := g . Weight ( uItem . self , v ) if ok && uvWeight < item . key { queringHeap . update ( item , uItem . self , uvWeight ) } } ) } return mst , nil }
Prim algorithm calculate minimum spanning tree on the input undirected graph
Note that MinHeap in this implmenetation is based on "container/heap"
Prim algorithm calculate minimum spanning tree on the input undirected graph Note that MinHeap in this implmenetation is based on " container / heap "
// initialize for queryHeap // random starting node // iterate all nodes by decreasing key //pop min key node