Advertisement
Guest User

for Michael only

a guest
Dec 6th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4. #include <fstream>
  5. #include <cassert>
  6. #include <sstream>
  7. #include <queue>
  8.  
  9. using namespace std;
  10.  
  11. class Graph{
  12.  
  13. private:
  14. vector<vector<int» links;
  15. const int nodes;
  16.  
  17.  
  18. public:
  19. Graph(int nodes):
  20. nodes(nodes),
  21. links(vector<vector<int»(nodes))
  22. {}
  23. int get_size() const {
  24. return nodes;
  25. }
  26. void add_link(int from, int to){
  27. assert(from >= 0);
  28. assert(from < nodes);
  29. assert(to >= 0);
  30. assert(to < nodes);
  31.  
  32. bool is_duplicate = false;
  33. for(int i = 0; i < links[from].size(); i++) {
  34. if (links[from][i] == to){
  35. is_duplicate = true;
  36. }
  37. }
  38. if(!is_duplicate){
  39. links[from].push_back(to);
  40. }
  41. }
  42.  
  43. vector<int> get_neighbours(int node) const {
  44. assert(node >= 0);
  45. assert(node < nodes);
  46.  
  47. return links[node];
  48. }
  49. static Graph read_undirected_graph(string file_name){
  50. ifstream file(file_name);
  51. int graph_size;
  52. file » graph_size;
  53. Graph output(graph_size);
  54.  
  55. int current_link;
  56. for(int i = 0; i < graph_size; i++){
  57. for(int j = 0; j < graph_size; j++){
  58. file » current_link;
  59. if(current_link == 1){
  60. output.add_link(i,j);
  61. }
  62. }
  63. }
  64. return output;
  65. }
  66.  
  67. string to_string() const{
  68. stringstream output;
  69. for(int i = 0; i < links.size(); i++){
  70. output « i « ": ";
  71. for(int j = 0; j < links[i].size(); j++){
  72. output « links[i][j] « " ";
  73. }
  74. output « endl;
  75. }
  76. return output.str();
  77. }
  78.  
  79.  
  80.  
  81. };
  82.  
  83. class Path{
  84. private:
  85. vector<int> path;
  86. public:
  87. Path(int node){
  88. add(node);
  89. }
  90. void add(int node){
  91. path.push_back(node);
  92. }
  93. int get_last() const {
  94. assert(path.size() > 0);
  95. return path[path.size() - 1];
  96. }
  97. int get_size() const {
  98. return path.size()-1;
  99. }
  100. string to_string(){
  101. stringstream output;
  102. for(int i = 0; i < path.size(); i++){
  103. output « path[i] « " ";
  104. }
  105. return output.str();
  106. }
  107.  
  108. };
  109.  
  110. vector<Path> get_paths(const Graph& graph,int start_node,int length){
  111. assert(start_node >= 0);
  112. assert(start_node < graph.get_size());
  113. assert(length > 0);
  114.  
  115. vector<Path> output;
  116. Path start_path(start_node);
  117. queue<Path> task_queue;
  118. task_queue.push(start_path);
  119.  
  120. while(!task_queue.empty()) {
  121. const Path current_path = task_queue.front();
  122. task_queue.pop();
  123. if (current_path.get_size() == length) {
  124. output.push_back(current_path);
  125. }
  126. else {
  127. vector<int> neighbours = graph.get_neighbours(current_path.get_last());
  128. for(int i = 0; i < neighbours.size(); i++){
  129. Path new_path = current_path;
  130. new_path.add(neighbours[i]);
  131. task_queue.push(new_path);
  132. }
  133.  
  134. }
  135. }
  136.  
  137. return output;
  138. }
  139. void read_parameteres(int& length, int& start_node){
  140. cout « "Enter the length of path" « endl;
  141. cin » length;
  142. cout « "Enter the start node" « endl;
  143. cin » start_node;
  144.  
  145. }
  146.  
  147. int main() {
  148. int length;
  149. int start_node;
  150. read_parameteres(length, start_node);
  151. Graph graph = Graph::read_undirected_graph("data.txt");
  152. cout « graph.to_string();
  153. vector<Path> paths = get_paths(graph,start_node,length);
  154. for(int i = 0; i < paths.size(); i++){
  155. cout « paths[i].to_string() « endl;
  156. }
  157. return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement