算法导论 ch23 最小生成树 问题的拓展

refer to

最小生成树问题的拓展,

http://wenku.baidu.com/view/b9082d4ac850ad02de8041ac.html

1) 次最优的最小生成树

2) 最小度限制生成树

次最优的最小生成树代码如下:

1. add method AdjMatrixGraph::maxEdge2Vertexs

/* * return the max edge list from source vertex s to the other vertexes. */ MSTEdgePTR* AdjMatrixGraph::maxEdge2Vertexs(int s) { COLOR *c = new COLOR[v]; int *d = new int[v]; int *p = new int[v]; MSTEdgePTR* m = new MSTEdgePTR[v]; for (int i = 0; i < v; i++) { c[i] = WHITE; d[i] = numeric_limits<int>::max(); p[i] = -1; } c[s] = GRAY; d[s] = 0; p[s] = -1; m[s] = NULL; queue<int> q; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); for (int j = 0; j < v; j++) { if ((j != u ) && (A[u * v + j] > 0 && (A[u * v + j] < std::numeric_limits<int>::max()))&& (c[j] == WHITE)) { c[j] = GRAY; d[j] = d[u] + 1; p[j] = u; // update the max edge of vertext j from source vertex if(m[u]){ MSTEdge* me = m[u]; if((me->weight < A[u * v + j]) && (A[u * v + j] < std::numeric_limits<int>::max())){ m[j] = new MSTEdge(u + 1, j + 1, A[u * v + j]); }else{ m[j] = m[u]; } }else{ m[j] = new MSTEdge(u + 1, j + 1, A[u * v + j]); } q.push(j); } } c[u] = BLACK; } return m; }

2. test suite

#include "../ch22/Edge.h" #include "../ch22/Graph.h" #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { // Edge e[] = { { 1, 2, 1 }, { 1, 3, 4 }, { 1, 4, 6 }, { 2, 3, 8 }, // { 3, 4, 7 }, { 3, 6, 4 }, { 3, 9, 2 }, { 4, 5, 9 }, { 4, 6, 14 }, { // 5, 6, 10 }, { 6, 7, 2 }, { 7, 8, 1 }, { 7, 9, 6 }, { 8, 9, // 7 } }; Edge e[] = { { 1, 2, 1 }, { 1, 3, 4 }, { 1, 4, 6 }, { 2, 3, 2 }, { 2, 4, 7 }, { 3, 4, 3 } }; Edges* edges = Edges::getInstance(); for (unsigned int i = 0; i < sizeof(e)/sizeof(Edge); i++) { edges->insert(&(e[i])); } //int numberOfVertexes = 9; int numberOfVertexes = 4; AdjMatrixGraph *g1 = new AdjMatrixGraph(numberOfVertexes, edges, false, true); MatrixGraphBuilder* mgb = MatrixGraphBuilder::getInstance(); g1->build(mgb); //g1->display(); g1->mst_prim(0); // now we get the edges in the MST // construct the MST graph AdjMatrixGraph *g2 = new AdjMatrixGraph(numberOfVertexes, edges, false, true); g2->build(mgb); //g2->display(); // construct the max distance matrix between vertexes MSTEdgePTR *maxd = new MSTEdgePTR[numberOfVertexes * numberOfVertexes]; for(int i = 0; i < numberOfVertexes; i++){ for(int j = 0; j < numberOfVertexes; j++){ maxd[i * numberOfVertexes + j] = NULL; } } // compute the max distance between vertexes using BFS. for(int i = 0; i < numberOfVertexes; i++){ MSTEdgePTR *m = g2->maxEdge2Vertexs(i); for(int j = 0; j < numberOfVertexes; j++){ maxd[i * numberOfVertexes + j] = m[j]; } } // show the matrix cout << endl; cout << "the max edge between edges in the MST is : " << endl; for(int i = 0; i < numberOfVertexes; i++){ for(int j = 0; j < numberOfVertexes; j++){ if (maxd[i * numberOfVertexes + j]){ cout << maxd[i * numberOfVertexes + j]->weight << " "; }else{ cout << "NULL "; } } cout << endl; } // now compute the second MST // for all the trees not in the MST, check the value of w[u, v] - max[u, v] Edges* mst = g2->getEdges(); // Edge* xx = mst->head; // cout << "mst edges" << endl; // while(xx){ // cout << (Edge*)xx << endl; // xx = xx->next; // } Edges* all = new Edges; for (unsigned int i = 0; i < sizeof(e)/sizeof(Edge); i++) { all->insert(&(e[i])); } Edge* pe = all->head; MSTEdgePTR cute = NULL; Edge* add = NULL; int delta = std::numeric_limits<int>::max(); while(pe){ // e is not in the mst edges if(!mst->contain(pe)){ int f = pe->from; int t = pe->to; int w = pe->weight; MSTEdgePTR maxdptr = maxd[(f-1) * numberOfVertexes + t-1]; if (maxdptr){ int d = w - maxdptr->weight; if(d < delta){ delta = d; cute = maxdptr; add = pe; } } } pe = pe->next; } cout << "the second MST shall cut edge " << cute->from << ", " << cute->to <<", add edge " << add << endl; }

3. run result

vertex 0 is selected with parent -1, and distance is 0 vertex 1 is selected with parent 0, and distance is 1 vertex 2 is selected with parent 1, and distance is 2 vertex 3 is selected with parent 2, and distance is 3 weight of MST is 6 the max edge between edges in the MST is : NULL 1 2 3 1 NULL 2 3 2 2 NULL 3 3 3 3 NULL the second MST shall cut edge 2, 3, add edge (1, 3)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值