# Teaching Kids Programming – Minimum Genetic Mutation via Recursive Depth First Search Algorithm

Teaching Kids Programming – Minimum Genetic Mutation via Recursive Depth First Search Algorithm | ninjasquad

Teaching Kids Programming: Videos on Data Structures and Algorithms

A gene string can be represented by an 8-character long string, with choices from ‘A’, ‘C’, ‘G’, and ‘T’. Suppose we need to investigate a mutation from a gene string startGene to a gene string endGene where one mutation is defined as one single character changed in the gene string. For example, “AACCGGTT” –> “AACCGGTA” is one mutation. There is also a gene bank bank that records all the valid gene mutations. A gene must be in bank to make it a valid gene string. Given the two gene strings startGene and endGene and the gene bank bank, return the minimum number of mutations needed to mutate from startGene to endGene. If there is no such a mutation, return -1.

Note that the starting point is assumed to be valid, so it might not be included in the bank.

Example 1:
Input: startGene = “AACCGGTT”, endGene = “AACCGGTA”, bank = [“AACCGGTA”]
Output: 1

Example 2:
Input: startGene = “AACCGGTT”, endGene = “AAACGGTA”, bank = [“AACCGGTA”,”AACCGCTA”,”AAACGGTA”]
Output: 2

Constraints:
0 <= bank.length <= 10
startGene.length == endGene.length == bank[i].length == 8
startGene, endGene, and bank[i] consist of only the characters [‘A’, ‘C’, ‘G’, ‘T’].

### Minimum Genetic Mutation via Depth First Search Algorithm

Given the search space is bounded – we can only apply mutations (edges) as long as they are in the given bank. We can exhaust the paths using Depth First Search Algorithm. The following defines a recursive Depth First Search function.

 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ``` ```class Solution:     def minMutation(self, start: str, end: str, bank: List[str]) -> int:         bank = set(bank)         self.ans = inf           def dfs(s, e, d, seen = set()):             if s == e:                 self.ans = min(self.ans, d)                 return True             if s in seen:                 return False             seen.add(s)             for n in "ACGT":                 for i in range(len(s)):                     x = s[:i] + n + s[i + 1:]                     if x != s and x in bank and dfs(x, e, d + 1, seen):                         pass             seen.remove(s)             return False           dfs(start, end, 0, set())         return self.ans if self.ans != inf else -1```
```class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
bank = set(bank)
self.ans = inf

def dfs(s, e, d, seen = set()):
if s == e:
self.ans = min(self.ans, d)
return True
if s in seen:
return False
for n in "ACGT":
for i in range(len(s)):
x = s[:i] + n + s[i + 1:]
if x != s and x in bank and dfs(x, e, d + 1, seen):
pass
seen.remove(s)
return False

dfs(start, end, 0, set())
return self.ans if self.ans != inf else -1```

We recursively walk to the next mutation as long as it is valid (in the bank), and it has not been seen before. The space complexity is O(B) and the time complexity is O(B) as well.

GD Star Rating