3 //kell hogy tree_edge invalid elekbol alljon, Szep
4 //lenne ha az elejen a konstrualas ilyet adna, de
5 //ugy fest nem igy lesz, ekkor invalidalni kell
8 *template <Graph, T, Heap=FibHeap>
12 *Prim(Graph G, Graph::EdgeMap<T> weight, NodeIt root=[G.first()])
19 * The followings functions should be used after run() was already run.
21 *T weight() : returns the minimum weight of a spanning tree of the
22 * component of the root.
24 *EdgeIt tree(NodeIt v) : returns the first edge in the path from v
25 * to the root. Returns an invalid iterator if v=s or v is
26 * not reachable from the root.
28 *bool conn() : true iff G is connected
30 *bool reach(NodeIt v) : true iff v is in the same component as the root
32 *NodeIt root() : returns the root
45 template <typename Graph, typename T,
46 typename Heap=FibHeap<typename Graph::NodeIt, T,
47 typename Graph::NodeMap<int> > >
49 typedef typename Graph::NodeIt NodeIt;
50 typedef typename Graph::EachNodeIt EachNodeIt;
51 typedef typename Graph::EdgeIt EdgeIt;
52 typedef typename Graph::OutEdgeIt OutEdgeIt;
53 typedef typename Graph::InEdgeIt InEdgeIt;
57 typename Graph::NodeMap<EdgeIt> tree_edge;
58 typename Graph::NodeMap<T> min_weight;
59 typename Graph::EdgeMap<T>& edge_weight;
60 typename Graph::NodeMap<bool> reached;
64 Prim(Graph& _G, typename Graph::EdgeMap<T>& _edge_weight,
66 G(_G), r(_r), tree_edge(G), min_weight(G),
67 edge_weight(_edge_weight), reached(G, false) { }
69 Prim(Graph& _G, typename Graph::EdgeMap<T>& _edge_weight) :
70 G(_G), tree_edge(G), min_weight(G), edge_weight(_edge_weight), reached(G, false)
72 EachNodeIt _r; //FIXME
80 typename Graph::NodeMap<bool> scanned(G, false);
81 typename Graph::NodeMap<int> heap_map(G,-1);
88 while ( !heap.empty() ) {
91 min_weight.set(v, heap.get(v));
96 for( G.getFirst(e,v); G.valid(e); G.next(e)) {
99 if ( !scanned.get(w) ) {
100 if ( !reached.get(w) ) {
102 heap.push(w, edge_weight.get(e));
104 } else if ( edge_weight.get(e) < heap.get(w) ) {
106 heap.decrease(w, edge_weight.get(e));
112 for( G.getFirst(f,v); G.valid(f); G.next(f)) {
115 if ( !scanned.get(w) ) {
116 if ( !reached.get(w) ) {
118 heap.push(w, edge_weight.get(f));
120 } else if ( edge_weight.get(f) < heap.get(w) ) {
122 heap.decrease(w, edge_weight.get(f));
133 for ( G.getFirst(u) ; G.valid(u) ; G.next(u) ) w+=min_weight.get(u);
138 EdgeIt tree(NodeIt v) {
139 return tree_edge.get(v);
146 for ( G.getFirst(u) ; G.valid(u) ; G.next(u) )
147 if ( !reached.get(u) ) {
155 bool reach(NodeIt v) {
156 return reached.get(v);