Union grouping in bipartite graphs? I'm trying to figure out a good (and fast) solution to the following problem:

I have two roles I'm working with, let's call them players and teams having many-to-many relationship (a player can be on multiple teams and a team can have multiple players). I'm working on creating a UI element on a form that allows a user to select multiple teams (checkboxes). As the user is selecting (or deselecting) teams, I'd like to display the teams grouped by the players.

So for examples:


If the selected teams have no common players, each team will have its own section. 
If the user selects two teams and they have the same players, there would be one section containing the names of the two teams and all the players.
If TEAM_A has players [1, 2, 4, 5] and TEAM_B has players [1, 3, 5, 6]. There would be the following sections: SECTION_X = [TEAM_A, TEAM_B, 1, 5], SECTION_Y = [TEAM_A, 2, 3], SECTION _Z = [TEAM_B, 3, 5]


I hope that's clear. Essentially, I want to find the teams that players have in common and group by that. I was thinking maybe there is a way to do this by navigating a bipartite graph? Not exactly sure how though and I might be overthinking it. I was hoping to do this by creating some type of data structure on the server and using it on the client. I would love to hear your suggestions and I appreciate any help you can give!
I'll present an algorithm by example. Initialize a list of lists containing a list containing all of the players.

[[1, 2, 3, 4, 5, 6]]


For the first team, TEAM_A, replace each list by two lists, where the first list contains TEAM_A and the players on TEAM_A and the second list contains the players not on TEAM_A. Duplicate all of the existing team entries in both lists (none here).

[[TEAM_A, 1, 2, 4, 5], [3, 6]]


Discard all lists with no players (none here). For the second team, TEAM_B, replace each list by two lists, where the first list contains TEAM_B and the players on TEAM_B and the second list contains the players not on TEAM_B. Duplicate all of the existing team entries in both lists.

[[TEAM_B, TEAM_A, 1, 5], [TEAM_A, 2, 4], [TEAM_B, 3, 6], []]


Discard all lists with no players.

[[TEAM_B, TEAM_A, 1, 5], [TEAM_A, 2, 4], [TEAM_B, 3, 6]]


Well implemented, this should run in time proportional to the product of the number of teams and the number of players.