鱼C论坛

 找回密码
 立即注册
查看: 2730|回复: 1

[技术交流] 地图染色问题(C++)

[复制链接]
发表于 2014-4-17 13:07:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
graph.png
上面这张图就是这个程序的测试数据。代码利用回溯法。
graph.h
  1. #if !defined (GRAPH_H)
  2. #define GRAPH_H

  3. /************************************************************

  4. Class Graph represents a Graph [V,E] having vertices V and
  5. edges E.

  6. ************************************************************/
  7. class Graph {
  8.     private:
  9.         int    _n;  /// n is the number of vertices in the graph
  10.         int ** _A;  /// A stores the edges between two vertices
  11.         int  * color;
  12.     public:
  13.         Graph(int size = 2);
  14.         ~Graph();
  15.         bool isConnected(int, int);
  16.         void addEdge(int x, int y);
  17.         bool DFS(int);
  18.                 bool isSafe(int, int);
  19.         void GraphColoring();
  20. };

  21. #endif
复制代码
graph.cc
  1. #include <iostream>
  2. #include "graph.h"

  3. Graph::Graph(int size) {
  4.         int i, j;
  5.         if (size < 2) _n = 2;
  6.         else _n = size;
  7.         _A = new int*[_n];
  8.         for (i = 0; i < _n; ++i)
  9.                 _A[i] = new int[_n];
  10.         for (i = 0; i < _n; ++i)
  11.                 for (j = 0; j < _n; ++j)
  12.                         _A[i][j] = 0;
  13. }

  14. Graph::~Graph() {
  15.         for (int i = 0; i < _n; ++i)
  16.         delete [] _A[i];
  17.         delete [] _A;
  18. }

  19. /******************************************************
  20. Checks if two given vertices are connected by an edge
  21. @param u vertex
  22. @param v vertex
  23. @return true if connected false if not connected
  24. ******************************************************/
  25. bool Graph::isConnected(int x, int y) {
  26.         return (_A[x-1][y-1] == 1);
  27. }

  28. /*****************************************************
  29. adds an edge E to the graph G.
  30. @param u vertex
  31. @param v vertex
  32. *****************************************************/
  33. void Graph::addEdge(int x, int y) {
  34.         _A[x-1][y-1] = _A[y-1][x-1] = 1;
  35. }

  36. /*****************************************************
  37. performs Depth First Search
  38. @param x initial vertex
  39. *****************************************************/
  40. bool Graph::DFS(int v)
  41. {
  42.         if(v == _n+1) return true;
  43.         for (int c = 1; c <= 4; c++)
  44.         {
  45.                 if(isSafe(v, c))
  46.                 {
  47.                         color[v] = c;
  48.                         if(DFS(v+1)) return true;
  49.                         color[v] = 0;
  50.                 }
  51.         }
  52.         return false;
  53. }

  54. bool Graph::isSafe(int v, int c)
  55. {
  56.         for(int w = 1; w <= _n; ++w)
  57.                 if(isConnected(v, w) && c == color[w])
  58.                         return false;
  59.         return true;
  60. }

  61. void Graph::GraphColoring()
  62. {
  63.         color = new int[_n+1];
  64.         for (int i = 1; i <= _n; ++i)
  65.                 color[i] = 0;
  66.         DFS(1);
  67.         for (int w = 1; w <= _n; ++w)
  68.                 std::cout << color[w] << ' ';
  69.         std::cout << std::endl;
  70.         delete [] color;
  71. }
复制代码
main.cpp
  1. #include "graph.h"

  2. int main(){
  3.     /** Creates a graph with 8 vertices */
  4.     Graph g1(8);

  5.     /** Adds edges to the graph */
  6.     g1.addEdge(1, 6); g1.addEdge(1, 3);
  7.     g1.addEdge(1, 2); g1.addEdge(4, 7);
  8.     g1.addEdge(4, 6); g1.addEdge(4 ,5);
  9.     g1.addEdge(6, 5); g1.addEdge(7, 5);
  10.     g1.addEdge(7, 1); g1.addEdge(4, 3);
  11.     g1.addEdge(2, 5); g1.addEdge(1, 8);
  12.     g1.addEdge(2, 8); g1.addEdge(5, 8);
  13.     g1.addEdge(7, 8);

  14.     /** Explores all vertices findable from vertex 1 */
  15.     g1.GraphColoring();
  16. }
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-4-18 19:19:27 | 显示全部楼层
这个必须要看看那啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 15:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表