title: Querying the shared nodes in a RDF graph
body: <p>I have a graph of RDF data, that is the result of a <code>SPARQL</code> query in <code>rdflib</code>, but this question is valid just on any endpoint too. The graph looks like the picture below. </p>
  
  <p>I want to find a way to query the nodes that are shared between two clusters. Those are basically the nodes that are:</p>
  
  <ul>
  <li>Subject to two objects</li>
  <li>Object to two subjects</li>
  <li>Object to a subject, and, then subject to another object</li>
  </ul>
  
  <p>I tried with <code>Graph.subjects()</code> and <code>Graph.objects()</code> on <code>rdflib</code> it seems to me that they are only iterable and I have to iterate the whole graph three times, for each of the above scenarios, and it would result in a lot of double counting. </p>
  
  <p>I was wondering if anyone has an idea on how to do this in a better way, perhaps within SPARQL to begin with.</p>
  
  <p><img src="https://i.stack.imgur.com/GB2mZ.png" alt="visualization"></p>