A simple students project implementing Dinic's Algorithm to compute the max flow/min cut of a network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.6 KiB

  1. #pragma once
  2. #include <chrono>
  3. #include <vector>
  4. #include <set>
  5. #include <stack>
  6. #include <string>
  7. #include <fstream>
  8. #include <iostream>
  9. #include "util/GraphParser.h"
  10. #include "util/Arc.h"
  11. #define NO_AUGMENTING_PATH_FOUND -1
  12. #define INVALID_VERTEX -1
  13. #define NOT_PROCESSED 0
  14. #define ON_STACK 1
  15. #define PROCESSED 2
  16. typedef std::vector<std::vector<Capacity>> CapacityMatrix;
  17. namespace data {
  18. class Graph {
  19. public:
  20. Graph(bool stdout_output, bool file_output, std::string output_filename, bool verbose_max_flow, bool min_cut, int verbosity);
  21. void parseFromString(const std::string &graph_string);
  22. void parseFromFile(const std::string &graph_file);
  23. int maxFlowDinic();
  24. void printMatrices() const;
  25. void hasUniqueMaxFlow();
  26. void hasUniqueMinCut();
  27. private:
  28. void initMatrices();
  29. void initOstream();
  30. void constructLevelGraph();
  31. int findAugmentingPaths();
  32. void buildPath(std::vector<Vertex> &current_path);
  33. void computeFlowForPath(const std::vector<Vertex> &current_path);
  34. void isResidualGraphCyclic(const CapacityMatrix &residual_graph, int *visited, const int previous, std::stack<int> *recursive_stack, bool &found_cycle);
  35. void printCycle(const CapacityMatrix residual_matrix, std::stack<int> *stack);
  36. void computeMinCut(std::vector<Vertex> *source_vertices = nullptr, std::vector<Vertex> *sink_vertices = nullptr, std::vector<Arc> *cut_edges = nullptr) const;
  37. void incrementArcCapacity(const int source, const int sink);
  38. void printInformation() const;
  39. void printMaxFlowInformation() const;
  40. void printMinCut() const;
  41. void printComputationStatistics(const std::chrono::steady_clock::time_point &start, const std::chrono::steady_clock::time_point &end) const;
  42. void disableOutput();
  43. void enableOutput();
  44. void resetNetwork();
  45. std::vector<Vertex> m_vertices;
  46. std::vector<Arc> m_arc_list;
  47. VertexID m_source_id;
  48. VertexID m_sink_id;
  49. std::vector<Vertex>::iterator m_source;
  50. std::vector<Vertex>::iterator m_sink;
  51. CapacityMatrix m_network;
  52. CapacityMatrix m_flow;
  53. CapacityMatrix m_capapcities;
  54. int m_num_vertices;
  55. int m_num_arcs;
  56. int m_max_flow = 0;
  57. bool m_stdout_output = true;
  58. bool m_file_output = false;
  59. std::string m_output_file_name;
  60. std::ostream *m_ofstream;
  61. bool m_verbose_max_flow = false;
  62. bool m_min_cut = false;
  63. int m_verbosity = 0;
  64. uint m_num_paths = 0;
  65. uint m_num_build_path_calls = 0;
  66. uint m_num_level_graphs_built = 0;
  67. std::vector<std::string> m_augmenting_paths;
  68. };
  69. }