Here's the problem statement (as given on GeeksForGeeks website):
Given a sorted dictionary of an alien language having N words and k starting alphabets of standard dictionary, find the order of characters in the alien language.
Explanation: We're given a sorted dictionary having N words. Here a dictionary is an ordered list. A word is a sequence of allowed characters. There are 'K' allowed characters. These characters are first K alphabets of roman script. For example, K=5 implies that the allowed characters are {a, b, c, d, e}. But the ordering of the allowed characters is different in the alien language from standard roman script. For example, in the alien language the order of characters could be [b, d, e, a, c]. The words in the given dictionary are sorted lexicographically. Using the dictionary, we've to figure out the ordering of characters in the alien language.
Example test case: N = 5, K = 4, dict = ["baa","abcd","abca","cab","cad"]
Solution: {b, d, a, c}
Now the solution requires building a DAG. We iterate over the dictionary and compare every word to its next word to get information about the ordering of alphabets in the alien dictionary, i.e., pair-wise comparisons. In an optimal approach, (N-1) comparisons will be required to create the DAG (N being number of words in the dictionary). Every time we compare 2 word, we can infer something about the ordering of alphabets. This information is used to build the order of alphabets in the alien dictionary.
My first intuition was that every string in the dictionary can be compared to every string succeeding it thus totalling comparisons in order of N^2, i.e. [(n-1)+(n-2)+...+2+1] comparisons. But it turns out that N^2 comparisons aren't required. If we use only (N-1) comparisons as mentioned in previous paragraph, the information is sufficient to deduce the order of alphabets. How can I prove that the extra comparisons made in the N^2 approach will always be redundant giving no useful information?