Tamjow

aadsgraphs

Jun 24th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. // aads_graphs.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <algorithm>
  6. #include <iostream>
  7. #include <vector>
  8. #include <fstream>
  9. #include <sstream>
  10. #include <string>
  11. #include <list>
  12. #define nodes 5
  13. #define INF 99999
  14.  
  15. using namespace std;
  16. vector<vector<int>> readfromfile(string filename) {
  17.     vector<vector<int>> distances;
  18.     ifstream file(filename);
  19.     string line;
  20.     while (getline(file, line))
  21.     {
  22.         vector<int>   lineData;
  23.         stringstream  lineStream(line);
  24.  
  25.         int value;
  26.         // Read an integer at a time from the line
  27.         while (lineStream >> value)
  28.         {
  29.             // Add the integers from a line to a 1D array (vector)
  30.             lineData.push_back(value);
  31.         }
  32.         // When all the integers have been read, add the 1D array
  33.         // into a 2D array (as one line in the 2D array)
  34.         distances.push_back(lineData);
  35.     }
  36.     return distances;
  37. }
  38.  
  39. void fillgraphinit(int fill[nodes][nodes]) {
  40.     for (int row = 0; row < nodes; row++) {
  41.         for (int col = 0; col < nodes; col++) {
  42.             if (row == col)
  43.                 fill[row][col] = 0;
  44.             else
  45.                 fill[row][col] = INF;
  46.         }
  47.     }
  48.     //fills array with zero distances between the same nodes and infinity between different nodes
  49. }
  50.  
  51. void printarr(int arr[nodes][nodes]) {
  52.     for (int i = 0; i < nodes; ++i)
  53.     {
  54.         for (int j = 0; j < nodes; ++j)
  55.         {
  56.             cout << arr[i][j] << " ";
  57.         }
  58.         cout << endl;
  59.     }
  60. }
  61.  
  62. void printvec(vector<vector<int>> vec) {
  63.     for (int i = 0; i < vec.size(); i++)
  64.     {
  65.         for (int j = 0; j < vec[i].size(); j++)
  66.         {
  67.             cout << vec[i][j] << " ";
  68.         }
  69.         cout << endl;
  70.     }
  71. }
  72.  
  73.  
  74. void fwalg(int gr[nodes][nodes], int next[nodes][nodes], vector<vector<int>> vect) {
  75.     for (int row = 1; row < vect.size(); row++) {
  76.         gr[vect[row][0] - 1][vect[row][1] - 1] = vect[row][2];
  77.         next[vect[row][0] - 1][vect[row][1] - 1] = vect[row][1] - 1;
  78.     }
  79.     //go row by row and fill the graph array where the
  80.     //graph row index is the first number minus one of the value in column 0 of current row
  81.     //graph col index is the second number minus one of the value in column 1 of current row
  82.     //value to put into graph[row][col] is the distance between the nodes in previous columns in current row
  83.  
  84.     for (int k = 0; k < nodes; k++) {
  85.         for (int i = 0; i < nodes; i++) {
  86.             for (int j = 0; j < nodes; j++) {
  87.                 if (gr[i][j]>gr[i][k] + gr[k][j]) {
  88.                     gr[i][j] = gr[i][k] + gr[k][j];
  89.                     next[i][j] = next[i][k];
  90.                 }
  91.             }
  92.         }
  93.     }
  94. }
  95.  
  96. void printpath(int a, int b, int next[nodes][nodes]) {//a is the source node b is the target node
  97.     a = a - 1;
  98.     b = b - 1;
  99.     if (next[a][b] != 0) {
  100.         cout << a + 1 << "->";
  101.         while (a != b) {
  102.             a = next[a][b];
  103.             cout << a + 1 << "->";
  104.         }
  105.     }
  106.     cout << endl;
  107. }
  108.  
  109. void bfsutil(list<int> *edges, int currnode, bool visited[]) {
  110.     visited[currnode] = true;//set currently checked node as visited
  111.     cout << currnode << " ";
  112.    
  113.     list<int>::iterator i;
  114.     for (i = edges[currnode].begin(); i != edges[currnode].end(); ++i)
  115.         if (!visited[*i])// if the node isn't visited then rerun the util for next nodes
  116.             bfsutil(edges, *i, visited);
  117. }
  118. void bfs(vector< vector<int> > Edge, int currnode) {
  119.     int count = Edge[0][0];//count of nodes
  120.     list<int> *edges = new list<int>[count]; //list of edges
  121.     Edge.erase(Edge.begin());//delete first line from Edges vector since it has the count of nodes and edges instead of edge data
  122.     for (int i = 0; i < Edge.size(); i++)
  123.     {
  124.         edges[Edge[i][0]].push_back(Edge[i][1]);//fill list of edges with edges from file
  125.     }
  126.     bool *visited = new bool[count]; //list of visited nodes
  127.     for (int i = 0; i < count; i++)
  128.         visited[i] = false;//initialize array to all nodes not visited
  129.  
  130.     bfsutil(edges,currnode, visited);
  131.    
  132. }
  133.  
  134.  
  135. int main()
  136. {
  137.     int graph[nodes][nodes];
  138.     int next[nodes][nodes] = { 0 };
  139.     fillgraphinit(graph);
  140.  
  141.     vector<vector<int>> fileread = readfromfile("floyd1.txt");
  142.     //printvec(fileread);
  143.  
  144.     fwalg(graph, next, fileread);
  145.     //cout << endl;
  146.     //printarr(graph);
  147.     //cout << endl << endl;
  148.     //printarr(next);
  149.     printpath(5, 3, next);
  150.  
  151.     vector< vector<int> > bfsEdge;
  152.     bfsEdge = readfromfile("graph1.txt");
  153.     int count = bfsEdge[0][0];
  154.     //printvec(bfsEdge);
  155.     //cout << endl;
  156.     bfs(bfsEdge, 0);
  157.  
  158.     system("pause");
  159.     return 0;
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment