结果不对呀,输出的结果应该都是顶点开始,以最后一个节点为结束才对吧?
时间: 2023-11-28 11:49:16 浏览: 30
是的,你说得对。我之前理解有误,非常抱歉。输出的结果应该是从起点到终点的路径,而不是只输出起点和终点。因此,我们需要对代码进行修改。以下是修改后的代码:
```
def dfs(u, target, visited, graph, path):
visited[u] = True
path.append(u)
if u == target:
print(path)
else:
for v in graph[u]:
if not visited[v]:
dfs(v, target, visited, graph, path)
path.pop()
visited[u] = False
def find_path_dfs(graph, start, target):
visited = {v: False for v in graph}
path = []
dfs(start, target, visited, graph, path)
```
修改后的代码会输出起点到终点的路径。
相关问题
已知一个带权有向图,其存储结构为邻接表结构 ,设计一算法,由键盘输入数据,建立邻接表,并将其输出。 用例说明: 第一行输入图的顶点数; 第二行输入顶点信息; 第三行以后,按照顺序,每个顶点对应一行,按行输入该顶点对应边的信息(位置 1 权值 1 位置 2 权值 2 ……. ,以 -1 结束)。 输出格式中,代表链表节点的信息是:(边)权值,参见下面用例。
以下是一个实现该算法的Python代码:
```python
class Node:
def __init__(self, val, weight):
self.val = val
self.weight = weight
self.next = None
class Graph:
def __init__(self, n):
self.n = n
self.adj = [None] * n
def add_edge(self, u, v, w):
node = Node(v, w)
node.next = self.adj[u]
self.adj[u] = node
def print_graph(self):
for i in range(self.n):
print(f"Vertex {i}: ", end="")
node = self.adj[i]
while node:
print(f"({node.val}, {node.weight}) -> ", end="")
node = node.next
print("None")
n = int(input("请输入图的顶点数:"))
graph = Graph(n)
print("请输入顶点信息:")
vertices = input().split()
print("请按行输入每个顶点对应的边信息(以-1结束):")
for i in range(n):
print(f"请输入顶点{i}的边信息:")
edges = input().split()
j = 1
while edges[j] != "-1":
v = int(edges[j - 1])
w = int(edges[j])
graph.add_edge(i, v, w)
j += 2
graph.print_graph()
```
这个算法的基本思路是,定义一个`Node`类表示链表节点,包含节点值、边权值和指向下一个节点的指针。然后定义一个`Graph`类表示图,包含图的顶点数和邻接表。在输入每个顶点对应的边信息时,将每条边添加到起点对应的链表中。最后输出每个顶点对应的链表即可。
编程要求 输入 多组数据,每组m+2数据行。第一行有两个数字n和m,代表有n个顶点和m条边。顶点编号为1到n。第二行到第m+1行每行有两个整数h和k,代表边依附的两个顶点。第m+2行有一个整数d,代表从d开始遍历。当n和m都等于0时,输入结束。 输出 每组数据输出一行,为深度优先搜索的遍历结果。每两个数字之间用空格隔开。
好的,这是一个深度优先搜索(DFS)遍历图的问题,以下是代码实现:
```python
def dfs(cur, visited, graph, res):
visited[cur] = True
res.append(cur)
for next_node in graph[cur]:
if not visited[next_node]:
dfs(next_node, visited, graph, res)
while True:
n, m = map(int, input().split())
if n == 0 and m == 0:
break
graph = [[] for _ in range(n+1)]
for i in range(m):
h, k = map(int, input().split())
graph[h].append(k)
graph[k].append(h)
d = int(input())
visited = [False] * (n+1)
res = []
dfs(d, visited, graph, res)
print(" ".join(str(x) for x in res))
```
以上代码中,dfs函数接收当前节点cur,已经访问过的节点列表visited,节点之间的关系图graph和遍历结果列表res作为参数。将当前节点cur标记为已经访问过,将其加入遍历结果列表res。然后,遍历当前节点cur所能到达的所有节点,如果这些节点中存在一个未被访问过的节点next_node,则递归调用dfs函数以遍历next_node节点。最后,将遍历结果列表res中的元素按顺序输出。
在主程序中,首先读入顶点个数n和边数m,如果n和m均为0,则退出循环。接下来,对于每条边,读入边依附的两个顶点h和k,并在关系图graph中将节点h和节点k相连接。最后,读入起始遍历节点d,将已经访问过的节点列表visited初始化为全False,将遍历结果列表res初始化为空列表,调用dfs函数以遍历从起点d开始能够到达的所有节点,并将遍历结果输出。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)