Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. // for practicing at https://www.hackerrank.com/challenges/primsmstsub/problem?isFullScreen=false
  2.  
  3. void addPair(unsigned, unsigned);
  4. bool findLoop(unsigned startNode, unsigned numVertices);
  5. void removePair(unsigned a, unsigned b);
  6. bool pairExists(unsigned a, unsigned b);
  7.  
  8. vector <set<int>> Adj;      // Adj list(set) representation of MST
  9.  
  10. // Complete the prims function below.
  11.  
  12. int prims(int n, vector<vector<int>> edges, int start) {
  13.    
  14.     // save width as first element to keep sorted by width
  15.     multimap<int, pair<int,int>> edgesSortedByWeights;      // can use priority queue too
  16.     for(auto it=edges.begin(); it!=edges.end();it++)
  17.     {
  18.         edgesSortedByWeights.insert(pair<int, pair<int,int>>((*it)[2], pair<int,int>((*it)[0], (*it)[1])));
  19.     }
  20.     unsigned MSTWeight = 0;    // weight of the MST
  21.     Adj.resize(n+1);        // 1 to n
  22.    
  23.     int Ai, Bi;
  24.     auto it = edgesSortedByWeights.begin();
  25.     MSTWeight = it->first;
  26.     pair<unsigned, unsigned> AiBi = it->second;
  27.     Ai = AiBi.first;
  28.     Bi = AiBi.second;
  29.  
  30.     unsigned rootnode = Ai;     // let us use Ai of the smallest edge weight as first node
  31.     addPair(Ai, Bi);            // first edge, has to go in
  32.     it++;
  33.     for (; it != edgesSortedByWeights.end(); it++)
  34.     {
  35.         AiBi = it->second;
  36.         Ai = AiBi.first;
  37.         Bi = AiBi.second;
  38.         if(!pairExists(Ai,Bi))
  39.         {
  40.             addPair(Ai, Bi);
  41.             if (findLoop(rootnode, n))
  42.                 removePair(Ai, Bi);
  43.             else
  44.             {
  45.                 MSTWeight += it->first;
  46.             }
  47.         }
  48.     }
  49.     //cout << MSTWeight<<endl;
  50.     return MSTWeight;
  51. }
  52.  
  53. void addPair(unsigned a, unsigned b)
  54. {
  55.     Adj[a].insert(b);
  56.     Adj[b].insert(a);
  57. }
  58.  
  59. void removePair(unsigned a, unsigned b)
  60. {
  61.     Adj[a].erase(b);
  62.     Adj[b].erase(a);
  63. }
  64.  
  65. bool dfsCycle(unsigned node, unsigned parent, vector<bool>&visited)
  66. {
  67.     bool result = false;
  68.     if (!visited[node])
  69.     {
  70.         visited[node] = true;
  71.         for (auto it = Adj[node].begin(); it != Adj[node].end() && !result; it++)
  72.         {
  73.             if (!visited[*it])
  74.                 result = dfsCycle(*it, node, visited);
  75.             else
  76.             {
  77.                 result = *it != parent;
  78.             }
  79.         }
  80.     }
  81.     return result;
  82. }
  83.  
  84. bool findLoop(unsigned startNode, unsigned numVertices)
  85. {
  86.     vector<bool>visited(numVertices+1);
  87.     unsigned parent = 0;
  88.     return dfsCycle(startNode, parent, visited);
  89. }
  90.  
  91. bool pairExists(unsigned a, unsigned b)
  92. {
  93.     return (Adj[a].find(b) != Adj[a].end()) && (Adj[b].find(a) != Adj[b].end());
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement