Message Boards Message Boards

Visualize the inclusion–exclusion principle

GROUPS:

The inclusion–exclusion principle is sometimes difficult to grasp on a larger number of Sets and especially using Venn Diagrams. In an attempt to visualize it I found a Grid structure to be suitable (on my kids :) ). It's now easy to see that the Union of Sets A,B,C is found by adding the number of items in de Sets A,B,C and subtracting the "Even" Intersections and Adding the "Odd" intersection. The intersection A,B,C,D has one item (the brown square). This item is found in all other intersections. Anyway had fun with playing with the Wolfram language.

enter image description here

enter image description here

sets = {"A", "B", "C", "D", "E"}
namedcolors = {Red, Green, Blue, Black, Gray, Cyan, Magenta, Yellow, 
  Brown, Orange, Pink, Purple}
shapes :=
 {
   Graphics[{ #, Polygon[{{0, 0}, {1, 1}, {0, 1}, {1, 0}, {0, 0}}]}, 
    ImageSize -> 15],
   Graphics[{#, Polygon[{{0, 0}, {1, 0}, {0.5, 1}, {0, 0}}]}, 
    ImageSize -> 15],
   Graphics[{ #, 
     Polygon[{{0, 1}, {0.5, 0}, {1.5, 0}, {1, 1}, {0, 1}}]}, 
    ImageSize -> 15],
   Graphics[{#, Polygon[{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}]}, 
    ImageSize -> 15],
   Graphics[{#, 
     Polygon[{{0, 0.5}, {0.5, 0}, {1, 0.5}, {0.5, 1}, {0, 0.5}}]}, 
    ImageSize -> 15],
   Graphics[{ #, Disk[]}, ImageSize -> 15]} &
items = RandomSample[shapes[#] & /@ namedcolors // Flatten, 72];
findsubsets[x_] := 
 If[SubsetQ[#, x] == True, #, Nothing] & /@ Subsets[sets][[2 ;;]]
setlist = Map[{#, findsubsets[#]} &, Subsets[sets][[2 ;;]]];
colorrules = 
  Thread[Rule[setlist[[All, 1]], 
    items[[;; Length[setlist[[All, 1]]]]]]];
allign[subset_, set_] := 
 Flatten[ReplaceAll[
   SequenceAlignment[subset, set], {{}, items : {__}} :> 
    Table[Graphics[{White, 
       Polygon[{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}]}, 
      ImageSize -> 15], {x, 1, Length[items]}]], 1]
colorsetlist = 
  Map[Flatten[{StringRiffle[#[[1]], "\[Intersection]"], 
      allign[#[[2]] /. colorrules, colorrules[[All, 2]]]}] &, 
   setlist];
header = Flatten[{StringRiffle[sets, "\[Union]"], 
   colorrules[[All, 2]]}]
Grid[Prepend[colorsetlist, header], Spacings -> {0, 0}, Frame -> All]
POSTED BY: l van Veen
Answer
3 months ago

Group Abstract Group Abstract