Guest User

Untitled

a guest
Dec 7th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <queue>
  5. using namespace std;
  6.  
  7. vector<int> bfs(vector<int> someGraph[], int source, int destination) {     //This function will return a vector, this vector is going to keep the path from source to destination
  8.     int prev[1000] = {0};                                                   //Keeps the previous node of the index
  9.     bool visited[1000] = {false};                                           //Keeps track if whether a node was visited during BFS
  10.     prev[source] = 0;                                                       //The parent(root)
  11.     queue<int> q;
  12.     q.push(source);
  13.     while (!q.empty()) {
  14.         int cur = q.front();
  15.         q.pop();
  16.         for (int j = 0; j < someGraph[cur].size(); j++) {
  17.             if (!visited[someGraph[cur][j]]) {
  18.                 visited[someGraph[cur][j]] = true;
  19.                 prev[someGraph[cur][j]] = cur;
  20.                 q.push(someGraph[cur][j]);
  21.             }
  22.         }
  23.     }
  24.     vector<int> path;                                                       //Vector to keep our shortest path
  25.     for (int i = destination; i != source; i = prev[i]) {                   //We trace back
  26.         path.push_back(i);
  27.     }
  28.     path.push_back(source);
  29.  
  30.     return path;
  31. }
  32.  
  33. void printPath(vector<int> path) {                                          //Printing the vector from the back, because when we found the path it was backwards
  34.     for (int i = path.size() - 1; i >=0; i--) {
  35.         cout << path[i] << " ";
  36.     }
  37.     cout << "\n";
  38. }
  39.  
  40. int main() {
  41.     ifstream fin("graphedges14.txt");
  42.     int n1, n2;
  43.     int edges = 0;
  44.     vector<int> graph[1000];                            //Declare the array of vectors of integer type (adjacency list)
  45.  
  46.     while (fin >> n1) {                                 //Read first node
  47.         fin >> n2;                                      //Read second node
  48.         graph[n1].push_back(n2);                        //In n1 we put n2
  49.         graph[n2].push_back(n1);                        //In n2 we put n1 (vice versa because it is indirected)
  50.         edges++;
  51.     }
  52.     cout << edges << "\n";
  53.  
  54.     int isolates[1000];
  55.     int numOfIsolates = 0;
  56.     int max = 0;
  57.     int maxNode;
  58.  
  59.     for (int i = 0; i < 1000; i++) {                    //For loop to find the isolates
  60.         if (graph[i].empty()) {                         //If the vector is empty it's an isolate
  61.             isolates[numOfIsolates++] = i;              //A counter for our isolates in an array form, the last number in the array is the number of isolates
  62.         }
  63.     }
  64.  
  65.     cout << numOfIsolates << "\n";
  66.  
  67.     for (int i = 0; i < numOfIsolates; i++) {
  68.         cout << isolates[i] << " ";
  69.     }
  70.  
  71.     for (int i = 0; i < 1000; i++) {                    //For loop to calculate the max node(which is the node with maximum connections)
  72.         if (graph[i].size() > max) {                    //If the number of edges of the node is bigger than the max, then the max node is going to be equal of this node, and max is going to be the number of edges of this node
  73.             maxNode = i;                                //Size function returns how many elements in the vector
  74.             max = graph[i].size();
  75.         }
  76.     }
  77.     cout << "\n";
  78.     cout << maxNode << " " << max << "\n";
  79.  
  80.     vector<int> maxDist;                                //The diameter
  81.  
  82.     for (int i = 0; i < 1000; i++) {
  83.         for (int j = 0; j < 1000; j++) {
  84.             if(!graph[i].empty() && !graph[j].empty()) {
  85.                 vector<int> dist = bfs(graph, i, j);    //Use the BFS function to find the longest shortest path
  86.                 if (dist > maxDist) {
  87.                     maxDist = dist;
  88.                 }
  89.             }
  90.         }
  91.     }
  92.  
  93.     printPath(maxDist);
  94.  
  95.     int a = 363; int b = 642;
  96.     int c = 738; int d = 685;
  97.     int e = 739; int f = 891;
  98.     vector<int> path = bfs(graph, a, b);
  99.     printPath(path);
  100.     path.clear();
  101.     path = bfs(graph, c, d);
  102.     printPath(path);
  103.     path.clear();
  104.     path = bfs(graph, e, f);
  105.     printPath(path);
  106.  
  107.     vector<int> newGraph[1000];
  108.     fin.clear();
  109.     fin.seekg(0);
  110.     edges = 0;
  111.     while (fin >> n1) {
  112.         if (n1 % 17 == 0 || n1 == 224 || n1 == 611 || n1 == 424 || n1 == 982 || n1 == 61) {
  113.             fin >> n2;
  114.             continue;
  115.         }
  116.         fin >> n2;
  117.         if(n2 % 17 == 0 || n2 == 224 || n2 == 611 || n2 == 424 || n2 == 982 || n2 == 61)
  118.             continue;
  119.  
  120.         newGraph[n1].push_back(n2);
  121.         newGraph[n2].push_back(n1);
  122.         edges++;
  123.     }
  124.  
  125.     cout << "\n" << edges << "\n";
  126.     numOfIsolates = 0;
  127.     for (int i = 0; i < 1000; i++) {
  128.         if (newGraph[i].empty()) {
  129.             if (!(i % 17 == 0 || i == 224 || i == 611 || i == 424 || i == 982 || i == 61)) {
  130.                 isolates[numOfIsolates++] = i;
  131.             }
  132.         }
  133.     }
  134.     cout << numOfIsolates << "\n";
  135.     for (int i = 0; i < numOfIsolates; i++) {
  136.         cout << isolates[i] << " ";
  137.     }
  138.     max = 0;
  139.     for (int i = 0; i < 1000; i++) {
  140.         if (newGraph[i].size() > max) {
  141.             maxNode = i;
  142.             max = newGraph[i].size();
  143.         }
  144.     }
  145.     cout << "\n";
  146.     cout << maxNode << " " << max << "\n";
  147.  
  148.  
  149.         for (int i = 0; i < 1000; i++) {
  150.         for (int j = 0; j < 1000; j++) {
  151.             if(!graph[i].empty() && !graph[j].empty() && i % 17 !=0 && j % 17 !=0) {
  152.                 vector<int> dist = bfs(graph, i, j);    //Use the BFS function to find the longest shortest path
  153.                 if (dist > maxDist) {
  154.                     maxDist = dist;
  155.                 }
  156.             }
  157.         }
  158.     }
  159.  
  160.     printPath(maxDist);
  161.  
  162.     path = bfs(newGraph, a, b);
  163.     printPath(path);
  164.     path.clear();
  165.     path = bfs(newGraph, c, d);
  166.     printPath(path);
  167.     path.clear();
  168.     path = bfs(newGraph, e, f);
  169.     printPath(path);
  170.  
  171.     return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment