Skip to content

Commit 83fde21

Browse files
committed
433-minimum-genetic-mutation.md Added A* (A-Star) Algorithm description.
1 parent 34715c6 commit 83fde21

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

en/1-1000/433-minimum-genetic-mutation.md

+23-7
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,38 @@ Note that the starting point is assumed to be valid, so it might not be included
3030
- `startGene`, `endGene`, and `bank[i]` consist of only the characters `['A', 'C', 'G', 'T']`.
3131

3232
## Intuition
33-
This issue can be solved by **Breadth-First Search**.
33+
We can think of this problem as a shortest path problem on a graph: there are `4^8` vertices (strings `'AAAAAAAA'` to `'TTTTTTTT'`), and there is an edge between two vertices if they differ in one character, and if *both* vertices are in `bank`.
3434

35-
### Breadth-First Search
35+
So this issue can be solved by **Breadth-First Search** on a undirected graph.
36+
37+
### Solution 1: Breadth-First Search
3638
![](../../images/binary_tree_BFS_1.gif)
3739

38-
* As shown in the figure above, **breadth-first search** can be thought of as visiting vertices in rounds and rounds. Actually, whenever you see a question is about
39-
getting `shortest` or `least` of something of a graph, `breadth-first search` would probably help.
40+
* As shown in the figure above, **Breadth-First Search** can be thought of as visiting vertices in rounds and rounds. Actually, whenever you see a question is about
41+
getting `minimum number` of something of a graph, `Breadth-First Search` would probably help.
4042

41-
* `breadth-first search` emphasizes first-in-first-out, so a **queue** is needed.
43+
* `Breadth-First Search` emphasizes first-in-first-out, so a **queue** is needed.
4244

43-
## Approach
45+
#### Approach
4446
1. `Breadth-First Search` a graph means traversing **from near to far**, from `circle 1` to `circle N`. Each `circle` is a round of iteration, but we can simplify it by using just 1 round.
4547
1. So through `Breadth-First Search`, when a word matches `endWord`, the game is over, and we can return the number of **circle** as a result.
4648

47-
## Complexity
49+
#### Complexity
4850
> **N** is the length of `bank`.
4951
5052
* Time: `O((8 * 4) * N)`.
5153
* Space: `O(N)`.
5254

55+
### Solution 2: A* (A-Star) Algorithm
56+
57+
**A-Star Algorithm** can be used to improve the performance of **Breadth-First Search Algorithm**.
58+
59+
Please view **A-Star Algorithm** at [752. Open the Lock](./752-open-the-lock.md).
60+
61+
Bellow is code using _A-Star Algorithm_ in Python.
62+
5363
## Python
64+
### Solution 1: Breadth-First Search
5465
```python
5566
class Solution:
5667
def minMutation(self, start_gene: str, end_gene: str, bank: List[str]) -> int:
@@ -90,6 +101,11 @@ class Solution:
90101
self.bank.remove(mutation)
91102
```
92103

104+
### Solution 2: A* (A-Star) Algorithm
105+
```python
106+
107+
```
108+
93109
## JavaScript
94110
```javascript
95111
// Welcome to create a PR to complete the code of this language, thanks!

zh/1-1000/433-minimum-genetic-mutation.md

+23-7
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,38 @@ Note that the starting point is assumed to be valid, so it might not be included
3030
- `startGene`, `endGene`, and `bank[i]` consist of only the characters `['A', 'C', 'G', 'T']`.
3131

3232
## Intuition
33-
This issue can be solved by **Breadth-First Search**.
33+
We can think of this problem as a shortest path problem on a graph: there are `4^8` vertices (strings `'AAAAAAAA'` to `'TTTTTTTT'`), and there is an edge between two vertices if they differ in one character, and if *both* vertices are in `bank`.
3434

35-
### Breadth-First Search
35+
So this issue can be solved by **Breadth-First Search** on a undirected graph.
36+
37+
### Solution 1: Breadth-First Search
3638
![](../../images/binary_tree_BFS_1.gif)
3739

38-
* As shown in the figure above, **breadth-first search** can be thought of as visiting vertices in rounds and rounds. Actually, whenever you see a question is about
39-
getting `shortest` or `least` of something of a graph, `breadth-first search` would probably help.
40+
* As shown in the figure above, **Breadth-First Search** can be thought of as visiting vertices in rounds and rounds. Actually, whenever you see a question is about
41+
getting `minimum number` of something of a graph, `Breadth-First Search` would probably help.
4042

41-
* `breadth-first search` emphasizes first-in-first-out, so a **queue** is needed.
43+
* `Breadth-First Search` emphasizes first-in-first-out, so a **queue** is needed.
4244

43-
## Approach
45+
#### Approach
4446
1. `Breadth-First Search` a graph means traversing **from near to far**, from `circle 1` to `circle N`. Each `circle` is a round of iteration, but we can simplify it by using just 1 round.
4547
1. So through `Breadth-First Search`, when a word matches `endWord`, the game is over, and we can return the number of **circle** as a result.
4648

47-
## Complexity
49+
#### Complexity
4850
> **N** is the length of `bank`.
4951
5052
* Time: `O((8 * 4) * N)`.
5153
* Space: `O(N)`.
5254

55+
### Solution 2: A* (A-Star) Algorithm
56+
57+
**A-Star Algorithm** can be used to improve the performance of **Breadth-First Search Algorithm**.
58+
59+
Please view **A-Star Algorithm** at [752. Open the Lock](./752-open-the-lock.md).
60+
61+
Bellow is code using _A-Star Algorithm_ in Python.
62+
5363
## Python
64+
### Solution 1: Breadth-First Search
5465
```python
5566
class Solution:
5667
def minMutation(self, start_gene: str, end_gene: str, bank: List[str]) -> int:
@@ -90,6 +101,11 @@ class Solution:
90101
self.bank.remove(mutation)
91102
```
92103

104+
### Solution 2: A* (A-Star) Algorithm
105+
```python
106+
# TODO
107+
```
108+
93109
## JavaScript
94110
```javascript
95111
// Welcome to create a PR to complete the code of this language, thanks!

0 commit comments

Comments
 (0)