Optimal way for grouping events
Represent the events as a bipartite graphs: the vertices are the users and subjects and the edges are events (so there's an edge from user $x$ to subject $y$ if there's an event involving that user and subject).  What you're looking for is essentially a minimal vertex cover. A vertex cover is a set $C$ of vertices such that every edge includes at least one vertex from $C$. A minimal vertex cover is one that includes the smallest possible number of vertices.

Example. If the events are "John likes pizza", "John likes candy" and "Jane likes candy", the graph has vertices John, Jane, pizza and candy and the edges are John&ndash;pizza, John&ndash;candy and Jane&ndash;candy. A minimal vertex cover would be John and candy (in this case, there are several other minimal vertex covers).

For general graphs, finding minimal vertex covers is NP-hard but, for bipartite graphs, there is a polynomial-time algorithm because of K&ouml;nig's theorem, which says that finding minimal vertex covers of bipartite graphs is equivalent to finding maximum matchings (i.e., a biggest-possible set of edges such that no pair of edges in the set shares an endpoint), which can be done by the Hopcroft&ndash;Karp algorithm.  The algorithm for finding a minimum vertex cover from a maximum matching is described in the "Proof" section of the Wikipedia K&ouml;nig's theorem page.

Let $C$ be the minimum vertex cover that you just computed, and let the vertices in it be $c_1, \dots, c_k$ (so each $c_i$ is either a user or a subject).  The groups are $g_1, \dots, g_k$, where $g_1$ is all the events that involve the user or subject $c_1$and, for $i&gt;1$, $g_i$ is the set of all events that involve the user or subject $c_i$ that haven't already been included in one of $g_1, \dots, g_{i-1}$.