def PerfectSquare_metric(bpmn_graph):
all_types_count = Counter([node[<NUM_LIT:1>]['<STR_LIT:type>'] for node in bpmn_graph.get_nodes() if node[<NUM_LIT:1>]['<STR_LIT:type>']])<EOL>sorted_counts = [count for _, count in all_types_count.most_common()]<EOL>potential_perfect_square = min(len(sorted_counts), int(sqrt(sum(sorted_counts))))<EOL>for i in range(potential_perfect_square, <NUM_LIT:0>, -<NUM_LIT:1>):<EOL><INDENT>if sum(sorted_counts[:potential_perfect_square]) >= potential_perfect_square * potential_perfect_square:<EOL><INDENT>return potential_perfect_square<EOL><DEDENT>else:<EOL><INDENT>potential_perfect_square -= <NUM_LIT:1><EOL><DEDENT><DEDENT>return <NUM_LIT:0><EOL>
Returns the value of the Perfect Square metric
("Given a set of element types ranked
in decreasing order of the number of their instances,
the PSM is the (unique) largest number
such that the top p types occur(together)
at least p2 times.")
for the BPMNDiagramGraph instance.

:param bpmn_graph: an instance of BpmnDiagramGraph representing BPMN model.