1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| #include "graph.h" #include <iostream> #include <algorithm> #include <set> using namespace std;
void Edge::printEdge() { cout << _idFrom << "---" << _idTo << " " << _weight << endl; }
bool Edge::operator<(const Edge & rhs) { return this->_weight < rhs.getWeight(); }
Graph::Graph() { cout << "please input nodes' id:" << endl; int id, idFrom, idTo, weight; while(cin >> id, id != 0) { _nodes.push_back(id); } cout << "please input edges:" << endl; while(cin >> idFrom, idFrom != 0) { cin >> idTo >> weight; _edges.push_back(Edge(idFrom, idTo, weight)); } }
void Graph::printGraph() { for(auto e : _edges) { e.printEdge(); } }
void Graph::printKruscal() { for(auto e : _kruscalEdges) { e.printEdge(); } }
void Graph::printPrim() { for(auto e : _primEdges) { e.printEdge(); } }
bool isNotInOneTree(int id1, int id2, vector<vector<int>> & forest) { int i1 = 0, i2 = 0; for(unsigned int i = 0; i < forest.size(); ++i) { if(find(forest[i].begin(), forest[i].end(), id1) != forest[i].end()) i1 = i; if(find(forest[i].begin(), forest[i].end(), id2) != forest[i].end()) i2 = i; } if(i1 != i2) { for(unsigned int i = 0; i < forest[i2].size(); ++i) { forest[i1].push_back(forest[i2][i]); } forest[i2].clear(); return true; } return false; }
void Graph::mstKruscal() { sort(_edges.begin(), _edges.end()); auto it = _edges.begin(); vector<vector<int>> forest(_nodes.size()); for(unsigned int i = 0; i < _nodes.size(); ++i) { forest[i].push_back(_nodes[i]); } while(it != _edges.end()) { if(isNotInOneTree(it->getIdFrom(), it->getIdTo(), forest)) _kruscalEdges.push_back(*it); ++it; } }
bool isInSet(int id, vector<int> sets) { if(find(sets.begin(), sets.end(), id) != sets.end()) return true; else return false; }
typedef struct Handle { Handle(int id) :_id(id) {}
bool operator()(Edge e) { return e.getIdFrom() == _id; } int _id; }Handle_t;
void findCanReach(int *prim, int *notInPrim, int primCnt, vector<Edge>& canReach, Graph *G) { int i, j = 0; while(j < primCnt) { auto it = G->_edges.begin(); while(it != G->_edges.end()) { it = find_if(it, G->_edges.end(), Handle_t(prim[j])); if(it == G->_edges.end()) break; int exCnt = 0; for(i = 0; i < G->_nodes.size(); ++i) { if(it->getIdTo() == prim[i] || it->getIdFrom() == prim[i]) { ++exCnt; } } if(exCnt <= 1) { canReach.push_back(*it); } ++it; } ++j; } }
void Graph::mstPrim() { unsigned int i; int primCnt = 1; vector<Edge>::iterator it; int *prim = (int *)calloc(_nodes.size(), sizeof(int)); int *notInPrim = (int *)calloc(_nodes.size(), sizeof(int));; vector<Edge> canReach; prim[0] = _nodes[0]; for(i = 1; i < _nodes.size(); ++i) { notInPrim[i] = _nodes[i]; } while(primCnt < _nodes.size()) { findCanReach(prim, notInPrim, primCnt, canReach, this); sort(canReach.begin(), canReach.end()); _primEdges.push_back(canReach.front()); prim[primCnt] = canReach.front().getIdTo(); ++primCnt; canReach.clear(); } free(prim); free(notInPrim); }
Graph::~Graph() { cout << "program done" << endl; }
|