把注释中文改成英文
时间: 2023-07-01 11:21:24 浏览: 50
Sure, here are the algorithms with comments in English:
### Depth-First Search Algorithm
```python
# Define a graph class
class Graph:
def __init__(self, vertices):
self.vertices = vertices
self.adjacency_list = [[] for _ in range(vertices)]
self.visited = [False] * vertices
def add_edge(self, u, v):
self.adjacency_list[u].append(v)
def dfs(self, start, end):
# Mark the start node as visited
self.visited[start] = True
# Traverse the neighbors of the start node
for i in self.adjacency_list[start]:
# If the neighbor has not been visited, recursively visit it
if not self.visited[i]:
self.dfs(i, end)
# If the end node has been visited, there exists a path
if self.visited[end]:
return True
# If the end node has not been visited, there does not exist a path
return False
```
### Breadth-First Search Algorithm
```python
# Define a graph class
class Graph:
def __init__(self, vertices):
self.vertices = vertices
self.adjacency_list = [[] for _ in range(vertices)]
def add_edge(self, u, v):
self.adjacency_list[u].append(v)
def bfs(self, start, end):
# Create a queue and add the start node to the queue
queue = [start]
# Create a visited list to keep track of visited nodes
visited = [False] * self.vertices
visited[start] = True
while queue:
# Dequeue a node from the queue
node = queue.pop(0)
# Traverse the neighbors of the node
for i in self.adjacency_list[node]:
# If the neighbor has not been visited, add it to the queue and mark it as visited
if not visited[i]:
queue.append(i)
visited[i] = True
# If the end node is found, there exists a path
if i == end:
return True
# If the end node is not found after traversing the entire graph, there does not exist a path
return False
```
I hope this helps!