I'm solving a problem from Italian Olympiad in Informatics, the statament says:
1 ≤ N ≤ 100000. N-1 ≤ M ≤ 2N-3. 1 ≤ I, J ≤ N.
There is a secret organization. If you want to enter the organization you must find two members of the organization who will guarantee for you. You got N guys, and M relationships (i,j) between them. A relationship means that guy[i] guaranted for guy[j] (or vice-versa).
A "triad" is a triple (i,j,k) of guys such the relationships (i,j) (j,k) (k,i) exist.
You want to count how many triads are in the input file, that is formed as follows:
First line, M (relationships) and N (guys)
From 2 to M+1 each line has 2 integers (i,j) representing a relationship between i and j (or viceversa)
Output: the number of triads.
Sample input:
13 8 4 2 8 3 1 2 8 5 6 8 4 8 7 2 6 7 2 8 7 4 8 1 5 6 3 2
Output:
5
Note: the order in which you consider a triad is irrelevant. In the sample we have the triad 2,4,7 that is the same as saying 4,7,2 ...
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
I have this heuristic solution due to the sparse nature of the graph.
Firstly, we compute the degrees of each vertex in the graph (in-degree + out-degree).
If degree[x] <= 1, we know that we can reject x, as x will never form a triad.
If degree[x] = 2, then we know that if x is in a triad, it can only be in one triad. So we can check whether x is in a triad and then delete it from the graph.
If we do this simplification, we will eventually end in a situation where degree[x] >= 3 for all x. I think the worst case is if we do not delete any edges at all during this process (basically a complete graph followed by a lot of isolated vertices).
Therefore, suppose the graph has K vertices left after we simplify the graph:
2N - 3 = approximately K^2. That means we will have around sqrt(N) vertices left.
If we use the K^3 algorithm now, the solution will be on the order of N*sqrt(N), which is about 32 million. I think this should run in time.
If not, you can add in more heuristics. For example, you can run SCC and analyse each component separately. Maybe the transpose graph is even easier to analyse (This is assuming that (i,j) and (j,i) are both allowed. In that case, you are able to get a complete (or almost complete) undirected graph, so the transpose graph has very little edges). You can analyse the number of triads in the transpose graph. Suppose that is A. Then the answer is (K choose 3) - A.
EDIT:
Oops, I realised that it does not have to be a complete graph. All it needs to be is a 3-regular graph. That means there are still N/3 vertices left! :O.
To solve this, I think we can keep removing vertices for degrees <= 10 for example, or until the remaining number of vertices is small enough. Then the preprocessing time will be around N * lg N * (10 choose 2) = 74 million, which should be small enough.
In any case, this algorithm should work on randomly generated graphs, so this should give you a lot of marks :D. Maybe you will fail one or two testcases if you are unlucky.
May I know why you say that there will always be a vertex with degree <= 2 in each subgraph? Also, may I know what legend is skipped?
Are you referring to this statement? "If you want to enter the organization you must find two members of the organization who will guarantee for you. "
However, is it not possible that there are more than 2 people who can guarantee you, and you can choose any two?
Just checking, if indegree of any person <= 2, is your solution to process the vertices one by one? Since indegree <= 2, the triads each vertex is in can be processed in at most 2 * D[x] time, where D[x] is the outdegree of vertex x. I think you would need some set or map to check for the edges efficiently, so the overall runtime is N lg N?
By the way, may I know how you solve the generalised cases?
Given a directed graph, compute the number of 3-cycles.
What is the optimal solution for such problems?
Also, what happens if it is a undirected graph and we want to find the number of triangles?
Thanks in advance!