Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstdlib>
- #include <fstream>
- #include <cassert>
- #include <sstream>
- #include <queue>
- using namespace std;
- class Graph{
- private:
- vector<vector<int» links;
- const int nodes;
- public:
- Graph(int nodes):
- nodes(nodes),
- links(vector<vector<int»(nodes))
- {}
- int get_size() const {
- return nodes;
- }
- void add_link(int from, int to){
- assert(from >= 0);
- assert(from < nodes);
- assert(to >= 0);
- assert(to < nodes);
- bool is_duplicate = false;
- for(int i = 0; i < links[from].size(); i++) {
- if (links[from][i] == to){
- is_duplicate = true;
- }
- }
- if(!is_duplicate){
- links[from].push_back(to);
- }
- }
- vector<int> get_neighbours(int node) const {
- assert(node >= 0);
- assert(node < nodes);
- return links[node];
- }
- static Graph read_undirected_graph(string file_name){
- ifstream file(file_name);
- int graph_size;
- file » graph_size;
- Graph output(graph_size);
- int current_link;
- for(int i = 0; i < graph_size; i++){
- for(int j = 0; j < graph_size; j++){
- file » current_link;
- if(current_link == 1){
- output.add_link(i,j);
- }
- }
- }
- return output;
- }
- string to_string() const{
- stringstream output;
- for(int i = 0; i < links.size(); i++){
- output « i « ": ";
- for(int j = 0; j < links[i].size(); j++){
- output « links[i][j] « " ";
- }
- output « endl;
- }
- return output.str();
- }
- };
- class Path{
- private:
- vector<int> path;
- public:
- Path(int node){
- add(node);
- }
- void add(int node){
- path.push_back(node);
- }
- int get_last() const {
- assert(path.size() > 0);
- return path[path.size() - 1];
- }
- int get_size() const {
- return path.size()-1;
- }
- string to_string(){
- stringstream output;
- for(int i = 0; i < path.size(); i++){
- output « path[i] « " ";
- }
- return output.str();
- }
- };
- vector<Path> get_paths(const Graph& graph,int start_node,int length){
- assert(start_node >= 0);
- assert(start_node < graph.get_size());
- assert(length > 0);
- vector<Path> output;
- Path start_path(start_node);
- queue<Path> task_queue;
- task_queue.push(start_path);
- while(!task_queue.empty()) {
- const Path current_path = task_queue.front();
- task_queue.pop();
- if (current_path.get_size() == length) {
- output.push_back(current_path);
- }
- else {
- vector<int> neighbours = graph.get_neighbours(current_path.get_last());
- for(int i = 0; i < neighbours.size(); i++){
- Path new_path = current_path;
- new_path.add(neighbours[i]);
- task_queue.push(new_path);
- }
- }
- }
- return output;
- }
- void read_parameteres(int& length, int& start_node){
- cout « "Enter the length of path" « endl;
- cin » length;
- cout « "Enter the start node" « endl;
- cin » start_node;
- }
- int main() {
- int length;
- int start_node;
- read_parameteres(length, start_node);
- Graph graph = Graph::read_undirected_graph("data.txt");
- cout « graph.to_string();
- vector<Path> paths = get_paths(graph,start_node,length);
- for(int i = 0; i < paths.size(); i++){
- cout « paths[i].to_string() « endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement