Given a graph G=(V, E), a subset S that belongs to V, and the subset S' containing every vertex of G not belonging to S, I want to count the total number of edges between the nodes of S and S'.
An algorithm that could solve this problem with better complexity than O(n^2).
Assuming by "lower than O(n^2)" you mean something like O(|E|), then you can do that by using hashing structures. Put all nodes of S in a hashset, iterate over all edges of G and for each edge check whether both endpoints are in the hashset. Building the hashset is O(n), and assuming a sensible hashing function, processing all edges is O(|E|). If |E| in Omega(n^2)
, you can't do better than O(n^2).
EDIT: two things:
|E| in Omega(n^2)
is wrong, depending on the representation you use for the graph. Let E' = {e = {s,v} in E | s in S}
be the set of edges incident with at least one edge in S. If you have incidence/adjacency lists, then you can improve the complexity to O(|E'|) by only iterating over the edges incident with a node in S, and |E'| may be smaller than |E| by a non-constant factor depending on S.