Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. /* I need to find the longest depth in a multimap, I've tried everything I know and I'm lost at this point. I'd appreciate any help.
  2.  
  3.  
  4. I have a program that connects 2 people. The connection is saved to a multimap as key value pair. for e.g `add john tim`  adds tim as a connection to john, tim can add more people and his connections can add more people.
  5.  
  6. I need to find the depth of a person’s subnetwork, that is, the longest recruitment chain starting from this person. For e.g, given the following set of data
  7.  
  8.  
  9.     > add john mary
  10.     > add john tom
  11.     > add mary brad
  12.     > add tom Maria
  13.     > add mary Eli
  14.     > add brad Sofia
  15.  
  16. `Depth John` should return 4, because the longest chain `John` has is 4.
  17.  
  18. `Mary, Brad, sofia, Eli` (4)
  19.  
  20. `tom -> Maria` (2)
  21.  
  22. and `Depth sofia` will return 1, since she's on her own and doesn't extend to anyone.
  23.  
  24. Here's what I've attempted.
  25.  
  26.  * for each key, find how many exists in the map (using equal_range)
  27.  * then iterate through that and for each of them, count their depth
  28.  * save the depth as the max and compare it after each iteration and return the max
  29.  
  30. Here is the code. */
  31.  
  32.     void count_depth_recursive(std::multimap<std::string, std::string>networkMap, std::string id, int& depth)
  33.     {
  34.  
  35.         auto range = networkMap.equal_range(id);
  36.         for(auto it = range.first; it != range.second; ++it)
  37.         {
  38.             ++depth;
  39.             std::cout  << it->second << std::endl;
  40.             count_depth_recursive(networkMap, it->second, depth);
  41.         }
  42.  
  43.     }
  44.  
  45. Driver function.
  46.  
  47.     int count_depth(std::multimap<std::string, std::string>networkMap, std::string id)
  48.     {
  49.         int depth = 0;
  50.         int max = 0;
  51.  
  52.         if(networkMap.count(id) == 0)
  53.         {
  54.             return 1;
  55.         }
  56.  
  57.         auto find = networkMap.equal_range(id);
  58.         for(auto itr = find.first; itr != find.second; ++itr)
  59.         {
  60.             std::cout << "id is  " << itr->first <<" passing in: " << itr->second << std::endl;
  61.             count_depth_recursive(networkMap, itr->first, depth);
  62.             if(depth > max)
  63.             {
  64.                 max = depth;
  65.                 depth = 0;
  66.             }
  67.             std::cout << "**************************************************************" << std::endl;
  68.  
  69.         }
  70.  
  71.         return max;
  72.     }
  73. /*
  74. I keep getting 6 instead of 4.
  75.  I've been at it for ages now, I can't seem to figure out why it's not working. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement