Guest User

graph.h

a guest
Apr 27th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #ifndef GRAPH_H
  2. #define GRAPH_H
  3.  
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <string>
  7. #include <fstream>
  8. #include <sstream>
  9.  
  10. class Graph
  11. {
  12. private:
  13.     int adj_matrix_size;
  14.     int* adj_matrix;
  15.     bool filled;
  16.  
  17. public:
  18.     Graph() { adj_matrix_size = 0; adj_matrix = 0; filled = false; };
  19.     ~Graph();
  20.  
  21.     bool create_graph(std::string);
  22.     int get_weight(int, int);
  23.     void print_adj_matrix();
  24.  
  25.     int dimension() { return adj_matrix_size; }
  26. };
  27.  
  28. Graph::~Graph()
  29. {
  30.     filled = false;
  31.  
  32.     for (int r = 0; r < adj_matrix_size; ++r)
  33.         for (int c = 0; c < adj_matrix_size; ++c)
  34.             adj_matrix[r * adj_matrix_size + c] = 0;
  35.  
  36.     if (adj_matrix)
  37.         delete adj_matrix;
  38. }
  39.  
  40. bool Graph::create_graph(std::string path)
  41. {
  42.     // open file with input file stream on given path
  43.     std::ifstream fs(path);
  44.     std::string line;
  45.  
  46.     // check for errors openign file
  47.     if (!fs.is_open())
  48.         return false;
  49.  
  50.     // clear any existing graph data
  51.     adj_matrix_size = 0;
  52.     filled = false;
  53.     if (adj_matrix)
  54.     {
  55.         delete adj_matrix;
  56.         adj_matrix = 0;
  57.     }
  58.  
  59.     // get matrix size from first line
  60.     std::getline(fs, line);
  61.     std::istringstream ss(line);
  62.  
  63.     ss >> adj_matrix_size;
  64.  
  65.     // create square matrix to store graph information
  66.     adj_matrix = new int[adj_matrix_size * adj_matrix_size];
  67.  
  68.     for (int r = 0; r < adj_matrix_size; ++r)
  69.     {
  70.         // get next line of adj matrix
  71.         if (!std::getline(fs, line))
  72.             return false;
  73.  
  74.         // clear previous flag, set iss to read over new string
  75.         ss.clear();
  76.         ss.str(line);
  77.  
  78.         for (int c = 0; c < adj_matrix_size; ++c)
  79.         {
  80.             // fill matrix token by token
  81.             if (!(ss >> adj_matrix[r * adj_matrix_size + c]))
  82.                 return false; // exit if reading token failed
  83.         }
  84.     }
  85.  
  86.     if (fs.bad())
  87.         return false;
  88.  
  89.     fs.close();
  90.     filled = true;
  91.     return true;
  92. }
  93.  
  94. int Graph::get_weight(int row, int column)
  95. {
  96.     if (row < 0 || adj_matrix_size <= row || column < 0 || adj_matrix_size <= column)
  97.     {
  98.         std::cout << "Invalid indexing, no weight exists at " << row << "," << column << std::endl;
  99.         exit(1);
  100.     }
  101.  
  102.     return adj_matrix[row * adj_matrix_size + column];
  103. }
  104.  
  105. void Graph::print_adj_matrix()
  106. {
  107.     for (int r = 0; r < adj_matrix_size; ++r)
  108.     {
  109.         for (int c = 0; c < adj_matrix_size; ++c)
  110.             std::cout << adj_matrix[r * adj_matrix_size + c] << " ";
  111.  
  112.         std::cout << std::endl;
  113.     }
  114. }
  115.  
  116. #endif
Advertisement
Add Comment
Please, Sign In to add comment