Grafalgo
Library of useful data structures and algorithms
|
00001 #include "stdinc.h" 00002 #include "Dlist.h" 00003 #include "Partition.h" 00004 #include "LlheapSet.h" 00005 #include "Wgraph.h" 00006 00007 using namespace grafalgo; 00008 00009 Wgraph *gp; 00010 Partition *pp; 00011 00012 // Return true if the endpoints of e are in same tree. 00013 bool delf(edge e) { 00014 return (*pp).find((*gp).left((e+1)/2)) == 00015 (*pp).find((*gp).right((e+1)/2)); 00016 } 00017 00023 void rrobin(Wgraph& wg, list<edge>& mstree) { 00024 edge e; vertex u,v,cu,cv; 00025 Dlist q(wg.n()); List elist(2*wg.m()); 00026 lheap *h = new lheap[wg.n()+1]; 00027 Partition prtn(wg.n()); 00028 LlheapSet heapSet(2*wg.m(),delf); 00029 gp = &wg; pp = &prtn; 00030 for (e = 1; e <= wg.m(); e++) { 00031 heapSet.setkey(2*e,wg.weight(e)); 00032 heapSet.setkey(2*e-1,wg.weight(e)); 00033 } 00034 for (u = 1; u <= wg.n(); u++) { 00035 elist.clear(); 00036 for (e = wg.firstAt(u); e != 0; e = wg.nextAt(u,e)) { 00037 elist.addLast(2*e - (u == wg.left(e))); 00038 } 00039 if (!elist.empty()) { 00040 h[u] = heapSet.makeheap(elist); 00041 q.addLast(u); 00042 } 00043 } 00044 while (q.get(2) != 0) { 00045 vertex q1 = q.first(); 00046 h[q1] = heapSet.findmin(h[q1]); 00047 if (h[q1] == 0) { q.removeFirst(); continue; } 00048 e = (h[q1]+1)/2; mstree.push_back(e); 00049 u = wg.left(e); v = wg.right(e); 00050 cu = prtn.find(u); cv = prtn.find(v); 00051 q.remove(cu); q.remove(cv); 00052 h[prtn.link(cu,cv)] = heapSet.lmeld(h[cu],h[cv]); 00053 q.addLast(prtn.find(u)); 00054 } 00055 delete [] h; 00056 }