Alien Dictionary

The task of determining the order of characters in an alien language can be modeled as finding the topological sort in a directed graph. In this graph, each node represents a character from the alien language, and a directed edge from character u to character v means that u comes before v in the language.

Here’s a step-by-step approach to solving this problem:

  1. Create a graph: Start by creating an adjacency list to represent the graph.
  2. Determine edges: Compare adjacent words in the list to determine the order between characters.
  3. Topological Sort: Perform topological sorting to determine the ordering of characters.
  4. Detect Cycles: If a cycle is detected, return an empty string as the order would be invalid.

The following code implements these steps:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from collections import defaultdict, Counter, deque

class Solution:
    def alienOrder(self, words: List[str]) -> str:
        # Step 1: Create a graph
        adj_list = defaultdict(set)
        in_degree = Counter({c: 0 for word in words for c in word})

        # Step 2: Determine edges by comparing adjacent words
        for i in range(len(words) - 1):
            word1, word2 = words[i], words[i + 1]
            for j in range(min(len(word1), len(word2))):
                if word1[j] != word2[j]:
                    if word2[j] not in adj_list[word1[j]]:
                        adj_list[word1[j]].add(word2[j])
                        in_degree[word2[j]] += 1
                    break
            else:
                if len(word1) > len(word2): return ""

        # Step 3: Topological Sort using BFS
        queue = deque([c for c in in_degree if in_degree[c] == 0])
        result = []
        while queue:
            node = queue.popleft()
            result.append(node)
            for neighbor in adj_list[node]:
                in_degree[neighbor] -= 1
                if in_degree[neighbor] == 0:
                    queue.append(neighbor)

        # Step 4: Detect Cycles
        if len(result) != len(in_degree):
            return ""

        return "".join(result)

Key Takeaways

  • The problem can be solved by constructing a graph and performing topological sorting.
  • If the graph contains a cycle, the ordering is invalid.
  • The time complexity of this solution is (O(N \cdot M)), where (N) is the number of words and (M) is the maximum length of a word. The space complexity is (O(C)), where (C) is the number of unique characters in the input.

title: Alien Dictionary excerpt: Graph, DFS, Adjacency List

Solution

I refactored the solution that I found online.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def alien_order(words)        
  graph = Graph.new
    
  # Add nodes to the graph. No neighbors added at this time.
  words.each do |word|
    word.each_char do |c|
      graph.add_node(c)
    end
  end
    
  # Create graph
  (words.size - 1).times do |i|
    # ['abc', 'ab'] is not valid, return ''
    if words[i].size != words[i+1].size
      if words[i].start_with?(words[i+1])
        return ''
      end
    end
    
    # Create the precedence rules
    words[i].each_char.with_index do |c, j|
      if words[i+1][j] && c != words[i+1][j]
        graph.add_edge(c, words[i+1][j])
        
        break
      end
    end
  end
    
  # DFS Toplogical Sort
  TopSort.new(graph).sort
end

class TopSort
  def initialize(graph)
    @adj = graph.adj
    @stack = []
    @visited = {}
  end
    
  def sort
    @adj.keys.each do |node|
      return '' if @visited[node].nil? && dfs(node)
    end
        
    @stack.join('')
  end
    
  def dfs(node)
    @visited[node] = false
        
    @adj[node].each do |neighbor|
      # We can explore neighbors only if it has not been explored  
      if @visited[neighbor].nil?
        # Explore only if the neighbor has neighbors to be explored
        if @adj[neighbor]
          return true if dfs(neighbor)
        end        
      else
        return true if !@visited[neighbor]
      end 
    end

    # adds the node to the beginning of the array
    # this happens when recursive calls begin to return
    @stack.unshift(node)
    @visited[node] = true
        
    false
  end
end

class Graph
  attr_accessor :adj
    
  def initialize
    @adj = Hash.new([])
  end
    
  def add_node(node)
    @adj[node] = []
  end
    
  def add_edge(source, dest)
    # Nothing to do, if the precedence rule is already defined
    return if @adj[source].include?(dest)
        
    @adj[source].push(dest)
  end
end

Few more lines to be cleaned up in the dfs method.