Reporting triangles in a graph
The goal is to return all triangles in a graph, that is, all triplets
The first algorithm to achieve this is a simple triple nested loop, with runtime of
The second algorithm will prove that this simple iteration is not the most efficient. The idea behind it is that first algorithm completely ignores number of edges and will always iterate through all vertices
Note that order of loops doesn’t matter, we can first iterate over vertices or over edges
Runtime of this algorithm is
The third algorithm relies on the fact that triangles with edge
Note that if the graph is represented via adjacency lists and vertices in all lists are sorted by some global order, it is possible to compute
Lower bound
Note that in the worst case, the graph contains all
One of the cases when all
Let us partition vertices of the graph into two categories:
Each triangle can than be classified as
- Light, if it contains no heavy vertices
- Heavy, if it contains at least one heavy vertex
And the following algorithm is defined:
Hamming distance
As seen in previous practice sessions, computing Hamming distance on small alphabets is most efficient using FFT polynomial multiplication.
However, if the alphabet is large, it is better to use another approach.
First option is to consider texts where each character appears at most
Note that it is possible to apply light-heavy classification to the characters in