如何在Python中为BFS创建混合图?

发布于 2025-02-05 06:40:46 字数 4341 浏览 3 评论 0原文

这是我有史以来的第一个问题,所以如果有任何问题,我很抱歉。 任务是:创建一个可以通过使用边缘列表包含方向边缘和无定向边缘的图形(需要BFS搜索)。例如:混合图。在此图中,边缘列表将是此格式(edge_number:start_vertex end_vertex):

  • 0:0:0 1
  • 1:0 3
  • 2:1 2
  • 3:1 3
  • 4:2 1
  • 5:3 0
  • 6:3 2

这是与此处的代码输出:

print("First number is nodes, second number is edges: ")
N, M = map(int, input().split()) #N is nodes and M is edges
print(f"Now graph has {N} nodes and {M} edges")

print("\nNow input all neighbours: ")
graph = {i: set() for i in range(N)} #store as a dictionary
for i in range(M): #in the cycle add all of the M edges 
    v1, v2 = map(int, input().split()) #inputting start_vertex and end_vertex
    graph[v1].add(v2) #adding adjacency of two nodes 
    print(f"graph v1 is {graph[v1]}")
    graph[v2].add(v1)
    print(f"graph v2 is {graph[v2]}")
        
    print("\nNow you'll see all nodes: {their neighbours}: ------------HERE YOU SHOULD SEE POSSIBLE ROUTES LIKE NODE: {POSSIBLE_ROUTE_TO}")
    for key, value in graph.items():
        print("{0}: {1}".format(key,value))

和输出:

First number is nodes, second number is edges:
4 7
Now graph has 4 nodes and 7 edges

Now input all neighbours:
0 1
graph v1 is {1}
graph v2 is {0}
0 3
graph v1 is {1, 3}
graph v2 is {0}
1 2
graph v1 is {0, 2}
graph v2 is {1}
1 3
graph v1 is {0, 2, 3}
graph v2 is {0, 1}
2 1
graph v1 is {1}
graph v2 is {0, 2, 3}
3 0
graph v1 is {0, 1}
graph v2 is {1, 3}
3 2
graph v1 is {0, 1, 2}
graph v2 is {1, 3}

Now you'll see all nodes: {their neighbours}: ------------HERE YOU SHOULD SEE POSSIBLE ROUTES LIKE NODE: {POSSIBLE_ROUTE_TO}
0: {1, 3}
1: {0, 2, 3}
2: {1, 3}
3: {0, 1, 2}

关键点是,控制台说Node_3具有箭头(方向边)到Node_0,node_1和node_2。显然(在控制台i从图片中重复图)node_3应该只有一个方向的边缘(pic上的数字5)到node_2,而一个无方向的边缘(pic上的数字6)到node_0,但控制台说它具有另一种连接 - 带有node_1 (这就是问题)。

换句话说,我上面列出的代码找到了当前节点的所有邻居,但是它应该仅向我们显示可能的路由,即最后的控制台字符串应为“ 3:{0,2}”。 我该如何修复?我对集合的方法(例如set.update(x)或set.difference(x))有一些猜测,但老实说不知道。

非常感谢您的时间和可能的解决方案。

PS如果您想知道完整代码:

from collections import deque
print("First number is nodes, second number is edges: ")
N, M = map(int, input().split()) #N is nodes and M is edges
print(f"Now graph has {N} nodes and {M} edges")
    
print("\nNow input all neighbours: ")
graph = {i: set() for i in range(N)} #store as a dictionary
for i in range(M): #in the cycle add all of the M edges 
    v1, v2 = map(int, input().split()) #inputting start_vertex and end_vertex
    graph[v1].add(v2) #adding adjacency of two nodes 
    print(f"graph v1 is {graph[v1]}")
    graph[v2].add(v1)
    print(f"graph v2 is {graph[v2]}")
            
        print("\nNow you'll see all nodes: {their neighbours}: ------------HERE YOU SHOULD SEE POSSIBLE ROUTES LIKE NODE: {POSSIBLE_ROUTE_TO}")
        for key, value in graph.items():
            print("{0}: {1}".format(key,value))

for start_vertex in range(N):
    print(f"\n------------------------------------------START VERTEX IS {start_vertex} NOW----------------------------------------")

    distances = [None] * N 
    distances[start_vertex] = 0 #distance to ourself = 0
    queue = deque([start_vertex]) # init queue as a deque 
    parents = [None] * N

    while queue: #until queue is empty
        current_vertex = queue.popleft() #take 1st elem
        for neighbours_vertex in graph[current_vertex]: #going through its neighbours
            if distances[neighbours_vertex] is None: #if neigh hasn't been visited (distance = none)
                distances[neighbours_vertex] = distances[current_vertex] + 1 #now counting distance
                parents[neighbours_vertex] = current_vertex
                queue.append(neighbours_vertex) #add to queue for we'll check neighbours later
    print("Now you'll see the distances from start_vertex to all of them nodes in 0...N format: ", distances)   
    
    for end_vertex in range(N):
        print(f"END VERTEX IS {end_vertex} NOW")
        parent = parents[end_vertex]
        path = [end_vertex] 

        while not parent is None: 
            path.append(parent)
            parent = parents[parent]
        print(f"Optimal path from start_vertex {start_vertex} to end_vertex {end_vertex}", path[::-1])

It is my first question (here) ever, so I'm sorry in advance if anything is wrong.
The task is: create a graph that can contain both oriented edges and unoriented edges by using Edge List (it is need for a BFS search). For example: mixed graph. In this graph, Edge List would be this format (edge_number: start_vertex end_vertex):

  • 0: 0 1
  • 1: 0 3
  • 2: 1 2
  • 3: 1 3
  • 4: 2 1
  • 5: 3 0
  • 6: 3 2

Here is the code with output:

print("First number is nodes, second number is edges: ")
N, M = map(int, input().split()) #N is nodes and M is edges
print(f"Now graph has {N} nodes and {M} edges")

print("\nNow input all neighbours: ")
graph = {i: set() for i in range(N)} #store as a dictionary
for i in range(M): #in the cycle add all of the M edges 
    v1, v2 = map(int, input().split()) #inputting start_vertex and end_vertex
    graph[v1].add(v2) #adding adjacency of two nodes 
    print(f"graph v1 is {graph[v1]}")
    graph[v2].add(v1)
    print(f"graph v2 is {graph[v2]}")
        
    print("\nNow you'll see all nodes: {their neighbours}: ------------HERE YOU SHOULD SEE POSSIBLE ROUTES LIKE NODE: {POSSIBLE_ROUTE_TO}")
    for key, value in graph.items():
        print("{0}: {1}".format(key,value))

And the output:

First number is nodes, second number is edges:
4 7
Now graph has 4 nodes and 7 edges

Now input all neighbours:
0 1
graph v1 is {1}
graph v2 is {0}
0 3
graph v1 is {1, 3}
graph v2 is {0}
1 2
graph v1 is {0, 2}
graph v2 is {1}
1 3
graph v1 is {0, 2, 3}
graph v2 is {0, 1}
2 1
graph v1 is {1}
graph v2 is {0, 2, 3}
3 0
graph v1 is {0, 1}
graph v2 is {1, 3}
3 2
graph v1 is {0, 1, 2}
graph v2 is {1, 3}

Now you'll see all nodes: {their neighbours}: ------------HERE YOU SHOULD SEE POSSIBLE ROUTES LIKE NODE: {POSSIBLE_ROUTE_TO}
0: {1, 3}
1: {0, 2, 3}
2: {1, 3}
3: {0, 1, 2}

The key point is, console says that node_3 has arrows (oriented edges) to node_0, node_1 and node_2. Apparently (in console I repeated graph from picture) node_3 should have only one oriented edge (number 5 on pic) to node_2 and one unoriented edge (number 6 on pic) to node_0, but the console says it has one more connection - with node_1 (and that's the problem).

In other words, code I listed above finds all of the neighbours of the current node, but it should show us only possible routes, i.e. the last console string should be "3: {0, 2}".
How do I fix it? I have some guesses about sets' methods like set.update(x) or set.difference(x), but honestly have no idea.

Thank you very much for your time and possible solution.

P.S. If you wonder about full code:

from collections import deque
print("First number is nodes, second number is edges: ")
N, M = map(int, input().split()) #N is nodes and M is edges
print(f"Now graph has {N} nodes and {M} edges")
    
print("\nNow input all neighbours: ")
graph = {i: set() for i in range(N)} #store as a dictionary
for i in range(M): #in the cycle add all of the M edges 
    v1, v2 = map(int, input().split()) #inputting start_vertex and end_vertex
    graph[v1].add(v2) #adding adjacency of two nodes 
    print(f"graph v1 is {graph[v1]}")
    graph[v2].add(v1)
    print(f"graph v2 is {graph[v2]}")
            
        print("\nNow you'll see all nodes: {their neighbours}: ------------HERE YOU SHOULD SEE POSSIBLE ROUTES LIKE NODE: {POSSIBLE_ROUTE_TO}")
        for key, value in graph.items():
            print("{0}: {1}".format(key,value))

for start_vertex in range(N):
    print(f"\n------------------------------------------START VERTEX IS {start_vertex} NOW----------------------------------------")

    distances = [None] * N 
    distances[start_vertex] = 0 #distance to ourself = 0
    queue = deque([start_vertex]) # init queue as a deque 
    parents = [None] * N

    while queue: #until queue is empty
        current_vertex = queue.popleft() #take 1st elem
        for neighbours_vertex in graph[current_vertex]: #going through its neighbours
            if distances[neighbours_vertex] is None: #if neigh hasn't been visited (distance = none)
                distances[neighbours_vertex] = distances[current_vertex] + 1 #now counting distance
                parents[neighbours_vertex] = current_vertex
                queue.append(neighbours_vertex) #add to queue for we'll check neighbours later
    print("Now you'll see the distances from start_vertex to all of them nodes in 0...N format: ", distances)   
    
    for end_vertex in range(N):
        print(f"END VERTEX IS {end_vertex} NOW")
        parent = parents[end_vertex]
        path = [end_vertex] 

        while not parent is None: 
            path.append(parent)
            parent = parents[parent]
        print(f"Optimal path from start_vertex {start_vertex} to end_vertex {end_vertex}", path[::-1])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的巷 2025-02-12 06:40:46

指示您称之为目标的正确术语。

您不能在一个图中混合定向和无向链接。

您可以拥有一个无方向的图,每个链接都朝着两个方向传达。

您可以有一个有向图,每个链接都朝一个方向朝一个方向进行,如果您想指定可以从A到B和B,则需要节点之间的两个链接一个向每个方向沿着。

在您的输入代码中,

graph[v1].add(v2) 
graph[v2].add(v1)

您每次输入一对节点时,都会始终添加两个有针对性的边缘。您在想要一种单程连接的节点对之间没有区别,并且您想要一个链接朝一个方向延伸。最好的猜测:您应该删除第二行代码。

The correct term for what you call orientated is directed.

You cannot mix directed and undirected links in one graph.

You can have an undirected graph, where every link goes in both direction.

or

You can have a directed graph, where each link goes in one direction and if you want to specify that you can go from A to B and B to A then you need two links between the nodes one going in each direction.

In your input code you have

graph[v1].add(v2) 
graph[v2].add(v1)

So every time you input a pair of nodes, you ALWAYS add two directed edges going both ways. You are making no distinction between pairs of nodes where you want a one way connection and were you want a link going in just one direction. Best guess: you should remove the second line of code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文