Advertisement
Guest User

knapsack problem kids & sport camps

a guest
Jul 12th, 2019
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. // https://en.wikipedia.org/wiki/Maximum_flow_problem#Maximum_cardinality_bipartite_matching
  2.  
  3. // array of array mapping sport to camps
  4. // compute another array mapping sport to (sport_slots - requests)
  5. // sort desc
  6. // negative values mean some kids will be left aside
  7. // fill result and decrement second array until each value is negative or zero
  8.  
  9. void Main()
  10. {
  11.     var kids = new []
  12.     {
  13.         new Kid("Ben", "Baseball"),
  14.         new Kid("John", "Baseball"),
  15.         new Kid("Tom", "Baseball"),
  16.         new Kid("Chris", "Soccer"),
  17.         new Kid("Josh", "Soccer"),
  18.         new Kid("Alan", "Basketball"),
  19.         new Kid("Timmy", "Basketball"),
  20.         new Kid("Greg", "Tennis"),
  21.     };
  22.    
  23.     var camps = new []
  24.     {
  25.         new Camp("Cool Camp", new [] { "Baseball", "Soccer" }),
  26.         new Camp("Fun Camp", new [] { "Baseball" }),
  27.         new Camp("Super Camp", new [] { "Basketball" }),
  28.         new Camp("Sporty Camp", new [] { "Basketball" }),
  29.         new Camp("Nerd Camp", new [] { "Programming" }),
  30.         new Camp("Super Nerd Camp", new [] { "Programming" })
  31.     };
  32.    
  33.     var sportOpenSlots = camps
  34.         .SelectMany(sc => sc.Sports, (Camp, Sport) => new { Camp, Sport } )
  35.         //.Dump("sportOpenSlots")
  36.         ;
  37.    
  38.     var requirements = kids
  39.         .GroupBy(x => x.Sport)
  40.         .Select(group => new { Sport = group.Key, SpotRequired = group.Count() })
  41.         //.Dump("requirements")
  42.         ;
  43.    
  44.     var allocatableSports =
  45.         (
  46.             from sos in sportOpenSlots
  47.             join r in requirements
  48.             on sos.Sport equals r.Sport into unused
  49.             from u in unused.DefaultIfEmpty()
  50.             where u != null
  51.             select new { Sport = sos.Sport, Camp = sos.Camp, Required = u.SpotRequired }
  52.         )
  53.         .OrderByDescending(x => x.Camp.OpenSpots - x.Required)
  54.         ;
  55.     //allocatableSports.Dump("allocatableSports");
  56.    
  57.     var SportInCamps = (from a in allocatableSports select a.Sport).Distinct();
  58.     //SportInCamps.Dump("SportInCamps");
  59.    
  60.     var kidsWithAllocatableSports =
  61.         from sic in SportInCamps
  62.         join k in kids
  63.         on sic equals k.Sport into matched
  64.         from m in matched.DefaultIfEmpty()
  65.         where m != null
  66.         select m;
  67.     //kidsWithAllocatableSports.Dump("kidsWithAllocatableSports");
  68.    
  69.     foreach (var currentSport in allocatableSports)
  70.     {
  71.         var kid = kids.Where(k => k.Camp == null & k.Sport == currentSport.Sport).FirstOrDefault();
  72.         while (kid != null)
  73.         {
  74.             kid.Camp = currentSport.Camp.Name;
  75.             currentSport.Camp.OpenSpots -= 1;
  76.             if (currentSport.Camp.OpenSpots == 0) break;
  77.             kid = kids.Where(k => k.Camp == null & k.Sport == currentSport.Sport).FirstOrDefault();
  78.         }
  79.     }
  80.    
  81.     //kids.Dump();
  82. }
  83.  
  84. public class Kid
  85. {
  86.     public string Name { get; set; }
  87.     public string Sport { get; set; }
  88.     public string Camp { get; set; }
  89.    
  90.     public Kid(string name, string sport, string camp = null)
  91.     {
  92.         this.Name = name;
  93.         this.Sport = sport;
  94.         this.Camp = camp;
  95.     }
  96. }
  97.  
  98. public class Camp
  99. {
  100.     public string Name { get; set; }
  101.     public string[] Sports { get; set; }
  102.     public int OpenSpots { get; set; }
  103.    
  104.     public Camp(string name, string[] sports)
  105.     {
  106.         this.Name = name;
  107.         this.Sports = sports;
  108.         this.OpenSpots = 3;
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement