public static void main(String[] args)
    {

        String words[] = {"ABCDE", "CF", "DG"};
        DAG dag = new DAG();
        for (int i = 0; i < words.length - 1; i++) {
            char[] word1 = words[i].toCharArray();
            for (int j = 0; j < word1.length - 1; j++) {
                if (word1[j] != word1[j + 1]) {
                    dag.addEdge(word1[j], word1[j + 1]);
                }
            }
            char[] word2 = words[i + 1].toCharArray();
            int k = word1.length < word2.length ? word1.length : word2.length;
            for (int j = 0; j < k; j++) {
                if (word1[j] != word2[j]) {
                    dag.addEdge(word1[j], word2[j]);
                    break;
                }
            }
        }

        char[] lastWord = words[words.length - 1].toCharArray();
        for (int j = 0; j < lastWord.length - 1; j++) {
            if (lastWord[j] != lastWord[j + 1]) {
                dag.addEdge(lastWord[j], lastWord[j + 1]);
            }
        }

        for (Character c : dag.topologicalSort()) {
            System.out.print(c + " ");
        }
        System.out.println();

    }