Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.50 KB | None | 0 0
  1. #include "Graph.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <sstream>
  5.  
  6. typedef std::ifstream     ifstream;
  7. typedef std::ofstream     ofstream;
  8. typedef std::stringstream sstream;
  9. typedef std::exception    exception;
  10.  
  11. class graphm : public Graph {
  12.   private:
  13.     int numVertex, numEdge, *edge, *mark;
  14.  
  15.     int calcEdge(int v1, int v2) {
  16.       int e = (v1 > v2) ? v1 * (v1 - 1) / 2 + v2 : v2 * (v2 - 1) / 2 + v1;
  17.       return e;
  18.     }
  19.  
  20.   public:
  21.     graphm(int numVert) {
  22.       Init(numVert);
  23.     }
  24.  
  25.     ~graphm() {
  26.       delete[] edge;
  27.       delete[] mark;
  28.     }
  29.  
  30.     void Init(int n) {
  31.       int i, e;
  32.  
  33.       numVertex = n;
  34.       numEdge   = 0;
  35.  
  36.       // Use negative weight to represent no edge for Kruskal's algorithm
  37.       e    = n * (n - 1) / 2;
  38.       edge = new int[e];
  39.       for (i = 0; i < e; i++) {
  40.         edge[i] = -1;
  41.       }
  42.  
  43.       // Use -1 to represent UNVISITED
  44.       mark = new int[n];
  45.       for (i = 0; i < n; i++) {
  46.         mark[i] = -1;
  47.       }
  48.     }
  49.  
  50.     int n() {
  51.       return numVertex;
  52.     }
  53.  
  54.     int e() {
  55.       return numEdge;
  56.     }
  57.  
  58.     int first(int v) {
  59.       int i, e;
  60.  
  61.       for (i = 0; i < numVertex; i++) {
  62.         if (i == v) {
  63.           continue;
  64.         }
  65.  
  66.         e = calcEdge(v, i);
  67.  
  68.         if (edge[e] != -1) {
  69.           return i;
  70.         }
  71.       }
  72.  
  73.       return numVertex;
  74.     }
  75.  
  76.     int next(int v, int w) {
  77.       int i, e;
  78.  
  79.       for (i = w + 1; i < numVertex; i++) {
  80.         if (i == v) {
  81.           continue;
  82.         }
  83.  
  84.         e = calcEdge(v, i);
  85.  
  86.         if (edge[e] != -1) {
  87.           return i;
  88.         }
  89.       }
  90.  
  91.       return numVertex;
  92.     }
  93.  
  94.     void setEdge(int v1, int v2, int wt) {
  95.       if (!(wt > -1)) {
  96.         std::cerr << "Illegal weight value" << std::endl;
  97.         exit(1);
  98.       }
  99.  
  100.       if (v1 == v2) {
  101.         return;
  102.       }
  103.  
  104.       int e = calcEdge(v1, v2);
  105.  
  106.       if (edge[e] == -1) {
  107.         numEdge++;
  108.       }
  109.       edge[e] = wt;
  110.     }
  111.  
  112.     void delEdge(int v1, int v2) {
  113.       if (v1 == v2) {
  114.         return;
  115.       }
  116.  
  117.       int e = calcEdge(v1, v2);
  118.  
  119.       if (edge[e] != -1) {
  120.         numEdge--;
  121.       }
  122.       edge[e] = -1;
  123.     }
  124.  
  125.     bool isEdge(int i, int j) {
  126.       if (i == j) {
  127.         return false;
  128.       }
  129.       return edge[calcEdge(i, j)] != -1;
  130.     }
  131.  
  132.     int weight(int v1, int v2) {
  133.       if (v1 == v2) {
  134.         return -1;
  135.       }
  136.       return edge[calcEdge(v1, v2)];
  137.     }
  138.  
  139.     int getMark(int v) {
  140.       return mark[v];
  141.     }
  142.  
  143.     void setMark(int v, int val) {
  144.       mark[v] = val;
  145.     }
  146. };
  147.  
  148. /*
  149. *   File format:
  150. *     numVertex
  151. *     list of all weights in order (0,0) ... (n,n), separated by spaces
  152. *     list of all marks in order 0 ... n, separated by spaces
  153. *
  154. *   Example:
  155. *     5
  156. *     -1 3 -1 -1 -1 3 -1 -1 1 1 -1 -1 -1 -1 0 -1 1 -1 -1 -1 -1 1 0 -1 -1
  157. *     -1 -1 -1 -1 -1
  158. */
  159.  
  160. Graph *create_graph()   // creates empty graph (to be later initialized with Init method)
  161. {
  162.     Graph *x = new graphm(0);
  163.     return x;
  164. }
  165.  
  166. Graph* read_graph(string filename) {
  167.   try {
  168.     int      i, j, w;
  169.     ifstream ifs;
  170.     string   s;
  171.     graphm*  g;
  172.     sstream  ss1, ss2;
  173.  
  174.     ifs.open(filename);
  175.  
  176.     // Read in the number of vertices
  177.     getline(ifs, s);
  178.     g = new graphm(std::stoi(s));
  179.  
  180.     // Read in the list of weights and parse to set edges
  181.     getline(ifs, s);
  182.     ss1.str(s);
  183.     for (i = 0; i < g->n(); i++) {
  184.       for (j = 0; j < g->n(); j++) {
  185.         ss1 >> s;
  186.         w = std::stoi(s);
  187.         if (w == -1) {
  188.           continue;
  189.         }
  190.         g->setEdge(i, j, w);
  191.       }
  192.     }
  193.  
  194.     // Read in the list of marks and parse to set marks
  195.     getline(ifs, s);
  196.     ss2.str(s);
  197.     for (i = 0; i < g->n(); i++) {
  198.       ss2 >> s;
  199.       g->setMark(i, std::stoi(s));
  200.     }
  201.  
  202.     ifs.close();
  203.  
  204.     return (Graph*) g;
  205.   }
  206.   catch (exception e) {
  207.     return NULL;
  208.   }
  209. }
  210.  
  211.  
  212. bool write_graph(Graph* g, string filename) {
  213.   try {
  214.     int      i, j;
  215.     ofstream ofs;
  216.  
  217.     ofs.open(filename);
  218.  
  219.     // Write out the number of vertices
  220.     ofs << g->n() << std::endl;
  221.  
  222.     // Write out a list of all the weights
  223.     for (i = 0; i < g->n(); i++) {
  224.       for (j = 0; j < g->n(); j++) {
  225.         ofs << g->weight(i, j) << ' ';
  226.       }
  227.     }
  228.     ofs << std::endl;
  229.  
  230.     // Write out a list of all the marks
  231.     for (i = 0; i < g->n(); i++) {
  232.       ofs << g->getMark(i) << ' ';
  233.     }
  234.     ofs << std::endl;
  235.  
  236.     ofs.close();
  237.  
  238.     return true;
  239.   }
  240.   catch (exception e) {
  241.     return false;
  242.   }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement