基于韦尔奇·鲍威尔法对图着色 含c++代码
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                基于韦尔奇·鲍威尔法对图着色 含c++代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                期末考試前面的最后一次離散數學編程作業,十分感慨。
一學期的離散數學學習,讓我懂得了不能說是整個離散數學的體系吧,也可以說是一點也聽不懂了,現在正值6月13號這么一個神圣的日子,我不禁感嘆:我真的不想掛科。但是這門折磨的所謂的“專業課”也就到此為止了,或余實不合科研之路,只得目光短淺于兩三前后端之瑣事。故人各有志,非眾生皆可忍導師壓榨之苦、論文枯竭之苦,愚且鼠目寸光之輩哉,且浮于淺顯之處乎!遂鐘寫此篇,抒己淺志,且造福于眾生也。
代碼如下:(望君付師作業時稍加改動,不至連坐之刑也)
#include "iostream" #include "vector" #include "algorithm"using namespace std;class Node { private:int degree;int color = -1;int number;static int maxColor;vector<int> connected; public:explicit Node(int degree = 0, int number = 0) : degree(degree), number(number) {}~Node() = default;int getDegree() const;int getColor() const;int getNumber() const;void setDegree(const int& d);void setColor(const int& c);void setNumber(const int& n);void pushNode(const int& n);static void setMaxColor(const int& max);static int getMaxColor();bool find(Node* node);bool operator>=(const Node& node) const; };int Node::maxColor = -1;int Node::getDegree() const {return degree; }int Node::getNumber() const {return number; }int Node::getColor() const {return color; }void Node::setColor(const int &c) {color = c; }void Node::setNumber(const int &n) {number = n; }void Node::setDegree(const int &d) {degree = d; }void Node::pushNode(const int &n) {connected.push_back(n);degree++; }bool Node::find(Node *node) {for (auto &item: connected) {if (item == node->number) {return true;}}return false; }void Node::setMaxColor(const int &max) {if (maxColor < max) {maxColor = max;} }int Node::getMaxColor() {return maxColor; }bool Node::operator>=(const Node& node) const {return degree >= node.degree; }bool cmp(Node* a, Node* b) {return a->getDegree() > b->getDegree(); }class Graph{ private:vector<Node*> nodes; public:Graph() = default;~Graph() = default;void init(vector<vector<int>>& matrix);bool isColored();void showColor();void startColor(); };void Graph::init(vector<vector<int>> &matrix) {for (int i = 0; i < matrix.size(); i++) {auto node = new Node(0, i + 1);for (int j = 0; j < matrix[i].size(); j++) {if (i == j) {continue;} else if (matrix[i][j] == 1) {node->pushNode(j + 1);}}nodes.push_back(node);}sort(nodes.begin(), nodes.end(), cmp);nodes[0]->setColor(0);Node::setMaxColor(0); }bool Graph::isColored() {for (const auto &item: nodes) {if (item->getColor() == -1) {return false;}}return true; }void Graph::showColor() {cout << "--------------------------" << endl;for (int i = 0; i < nodes.size(); i++) {if (i != nodes.size() - 1) {cout << "v" << i + 1 << ":" << nodes[i]->getColor() << ",";} else {cout << "v" << i + 1 << ":" << nodes[i]->getColor();}}cout << endl;cout << "--------------------------" << endl; }void Graph::startColor() {for (int i = 1; i < nodes.size(); i++) {int check = 0;for (int j = 0; j < i; j++) {if (nodes[i]->find(nodes[j])) {continue;} else {nodes[i]->setColor(nodes[j]->getColor());check = 1;break;}}if (check == 0) {nodes[i]->setColor(Node::getMaxColor() + 1);Node::setMaxColor(nodes[i]->getColor());}} }int main() {vector<vector<int>> matrix;int n;cout << "輸入節點個數" << endl;cin >> n;cout << "輸入鄰接矩陣" << endl;cout << " ";for (int i = 0; i < n; i++) {cout << i + 1 << " ";}cout << endl;for (int i = 0; i < n; i++) {vector<int> temp;cout << i + 1 << " ";for (int j = 0; j < n; j++) {int value;cin >> value;temp.push_back(value);}matrix.push_back(temp);}Graph p;p.init(matrix);p.startColor();p.showColor();return 0; }愚尚且不及佬也,若有不慎之處,懇請指正于評論區。
總結
以上是生活随笔為你收集整理的基于韦尔奇·鲍威尔法对图着色 含c++代码的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Android之Activity界面劫持
- 下一篇: msyql字符串类型转换为整数类型
