Advertisement
Adam_mz_

Untitled

Jan 18th, 2022
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////// graph.h
  2. #ifndef GRAPH_H
  3. #define GRAPH_H
  4.  
  5. #include <map>
  6. #include <vector>
  7. #include <algorithm>
  8.  
  9. bool compare(const std::pair<int, int> &a, const std::pair<int, int> &b)
  10. {
  11.     return a.second < b.second;
  12. }
  13.  
  14.  
  15. template<typename T>
  16. class Graph
  17. {
  18. public:
  19.  
  20.     class Node
  21.     {
  22.     public:
  23.  
  24.         T data;
  25.  
  26.         Node(const T &data = T()) : data(data)
  27.         {}
  28.  
  29.  
  30.     };
  31.  
  32.     ~Graph()
  33.     {
  34.         for (Node *node : _nodes)
  35.             delete node;
  36.     }
  37.  
  38.     Node *addNode(const T &data = T())
  39.     {
  40.         Node *node = new Node(data);
  41.  
  42.         _nodes.push_back(node);
  43.  
  44.         return node;
  45.     }
  46.  
  47.     void addEdge(Node *nodeSource, Node *nodeDest)
  48.     {
  49.         bool existsNodeSource = std::find(_nodes.begin(), _nodes.end(), nodeSource) != _nodes.end();
  50.         bool existsNodeDest = std::find(_nodes.begin(), _nodes.end(), nodeDest) != _nodes.end();
  51.  
  52.         if (existsNodeSource && existsNodeDest)
  53.             _adjList.insert({nodeSource, nodeDest});
  54.     }
  55.  
  56.     bool doesFollow(Node *nodeX, Node *nodeY)
  57.     {
  58.         auto res = _adjList.equal_range(nodeX);
  59.         for (auto it = res.first; it != res.second; ++it)
  60.         {
  61.             Node *check = it->second;
  62.             if (check->data == nodeY->data)
  63.                 return true;
  64.         }
  65.         return false;
  66.     }
  67.  
  68.     std::vector<T> followers(Node *nodeX)
  69.     {
  70.         std::vector<T> followers;
  71.         auto res = _adjList.equal_range(nodeX);
  72.         for (auto it = res.first; it != res.second; ++it)
  73.         {
  74.             Node *check = it->second;
  75.             followers.push_back(check->data);
  76.         }
  77.  
  78.         return followers;
  79.     }
  80.  
  81.     T findTheStalker()
  82.     {
  83.      return   findBySubs(true);
  84.     }
  85.  
  86.     T findTheMostPopular()
  87.     {
  88.  
  89.         return findBySubs(false);
  90.     }
  91.  
  92.     T findBySubs(bool byFollowers)
  93.     {
  94.         std::map<Node *, size_t> subscriptions;
  95.         for (auto it = _adjList.begin(); it != _adjList.end(); ++it)
  96.         {
  97.             Node *check;
  98.             if (byFollowers)
  99.                 check = it->first;
  100.             else
  101.                 check = it->second;
  102.             subscriptions[check] += 1;
  103.         }
  104.  
  105.  
  106.         int max = -1;
  107.         Node *max_user;
  108.         for (auto it = subscriptions.begin(); it != subscriptions.end(); ++it)
  109.         {
  110.             if ((int) it->second > max)
  111.             {
  112.                 max = it->second;
  113.                 max_user = it->first;
  114.             }
  115.         }
  116.  
  117.         return max_user->data;
  118.     }
  119.  
  120. private:
  121.  
  122.     std::multimap<Node *, Node *> _adjList;
  123.  
  124.     std::vector<Node *> _nodes;
  125.  
  126. };
  127.  
  128. #endif
  129.  
  130.  
  131.  
  132.  
  133. /////////////////////////////////////////////////////////////////// main.cpp
  134.  
  135. #include <iostream>
  136. #include "graph.h"
  137.  
  138.  
  139. int main()
  140. {
  141.     Graph<std::string> graph;
  142.  
  143.     Graph<std::string>::Node *yulian = graph.addNode("Yulian");
  144.     Graph<std::string>::Node *anna = graph.addNode("Anna");
  145.     Graph<std::string>::Node *michael = graph.addNode("Michael");
  146.     Graph<std::string>::Node *john = graph.addNode("John");
  147.     Graph<std::string>::Node *angela = graph.addNode("Angela");
  148.  
  149.  
  150.     graph.addEdge(michael, anna);
  151.     graph.addEdge(michael, yulian);
  152.     graph.addEdge(yulian, michael);
  153.     graph.addEdge(yulian, anna);
  154.     graph.addEdge(john, michael);
  155.     graph.addEdge(john, yulian);
  156.     graph.addEdge(john, anna);
  157.     graph.addEdge(john, angela);
  158.     graph.addEdge(angela, anna);
  159.  
  160.  
  161.     std::cout << graph.doesFollow(yulian, anna) << '\n';
  162.     std::cout << graph.doesFollow(yulian, angela) << '\n';
  163.     std::cout << graph.doesFollow(angela, michael) << '\n';
  164.     std::cout << graph.doesFollow(john, michael) << '\n';
  165.     std::cout << graph.doesFollow(michael, michael) << '\n';
  166.  
  167.     std::vector<std::string> followers = graph.followers(anna);
  168.     for (const std::string &follower:followers)
  169.         std::cout << anna->data << ": " << follower << '\n';
  170.  
  171.  
  172.     followers = graph.followers(john);
  173.     for (const std::string &follower:followers)
  174.         std::cout << john->data << ": " << follower << '\n';
  175.  
  176.     std::cout << "Most subscriptions: " << graph.findTheStalker() << '\n';
  177.     std::cout << "Most subscribers: " << graph.findTheMostPopular() << '\n';
  178.  
  179.     return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement