简体   繁体   English

当我运行我的程序时,为什么没有发生任何事情

[英]why is nothing happening when i run my program

I tried to run the Dijkstra algorithm in Python. 我试图在Python中运行Dijkstra算法。 however, when I execute this code under Eclipse, there is nothing shown. 但是,当我在Eclipse下执行此代码时,没有显示任何内容。 I see that pydev understood all my imports and there is no error message.Can anyone point me in the right direction? 我看到pydev了解我的所有导入,并且没有错误信息。任何人都指出我正确的方向?

class Graph(object):
    def __init__(self):
        self.nodes = set()
        self.edges = {}
        self.distances = {}

    def add_node(self, value):
        self.nodes.add(value) 

    def add_edge(self, from_node, to_node, distance):
        self._add_edge(from_node, to_node, distance)
        self._add_edge(to_node, from_node, distance) 

    def _add_edge(self, from_node, to_node, distance):
        self.edges.setdefault(from_node, [])
        self.edges[from_node].append(to_node)
        self.distances[(from_node, to_node)] = distance 
def dijkstra(graph, initial_node):
    visited = {initial_node: 0}
    current_node = initial_node
    path = {}

    nodes = set(graph.nodes)

    while nodes:
        min_node = None
        for node in nodes:
            if node in visited:
                if min_node is None:
                    min_node = node
                elif visited[node] < visited[min_node]:
                    min_node = node  
        if min_node is None:
            break 

        nodes.remove(min_node)
        cur_wt = visited[min_node]

        for edge in graph.edges[min_node]:
            wt = cur_wt + graph.distances[(min_node, edge)]
            if edge not in visited or wt < visited[edge]:
                visited[edge] = wt
                path[edge] = min_node 

    return visited, path

def shortest_path(graph, initial_node, goal_node):
    distances, paths = dijkstra(graph, initial_node)
    route = [goal_node]

    while goal_node != initial_node:
        route.append(paths[goal_node])
        goal_node = paths[goal_node]

    route.reverse()
    return route

if __name__ == '__main__':
    g = Graph()
    g.nodes = set(range(1, 7))
    g.add_edge(1, 2, 7)
    g.add_edge(1, 3, 9)
    g.add_edge(1, 6, 14)
    g.add_edge(2, 3, 10)
    g.add_edge(2, 4, 15)
    g.add_edge(3, 4, 11)
    g.add_edge(3, 6, 2)
    g.add_edge(4, 5, 6)
    g.add_edge(5, 6, 9)
    assert shortest_path(g, 1, 5) == [1, 3, 6, 5]
    assert shortest_path(g, 5, 1) == [5, 6, 3, 1]
    assert shortest_path(g, 2, 5) == [2, 3, 6, 5]
    assert shortest_path(g, 1, 4) == [1, 3, 4]

You are not printing anything. 你没有打印任何东西。 If assert doesn't fail, it won't show anything. 如果assert没有失败,它将不会显示任何内容。

Instead of assert use print to know if shortest_path returns those arrays. 而不是assert使用print来知道shortest_path返回那些数组。 (You can actually know it does because assert doesn't raise any errors). (你实际上可以知道它的确如此,因为assert不会引发任何错误)。

Assuming that g is a graph object, you may try printing Nodes, edges and distances by following: 假设g是图形对象,您可以尝试通过以下方式打印节点,边和距离:

print g.nodes
print g.edges
print g.distances

It will help you to understand the graph structure and try using print instead of assert. 它将帮助您理解图形结构并尝试使用print而不是assert。 In your case, these three lines will print the following: 在您的情况下,这三行将打印以下内容:

set([1, 2, 3, 4, 5, 6])
{1: [2, 3, 6], 2: [1, 3, 4], 3: [1, 2, 4, 6], 4: [2, 3, 5], 5: [4, 6], 6:[1, 3, 5]}
{(1, 2): 7, (3, 2): 10, (1, 3): 9, (3, 6): 2, (4, 5): 6, (6, 1): 14, (3, 1): 9, (5, 4): 6, (2, 1): 7, (6, 3): 2, (5, 6): 9, (1, 6): 14, (4, 3): 11, (4, 2): 15, (2, 3): 10, (3, 4): 11, (2, 4): 15, (6, 5): 9}

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的网络爬虫在运行时什么都不返回? - Why does my webcrawler return nothing when i run it? Cron 运行我的 Python 脚本,但没有任何反应 - Cron run my Python script but nothing is happening 我尝试运行我的 python 程序,但是当我运行它时什么也没有发生 - I try to run my python program but when i run it nothing happens 为什么我的程序在运行时没有响应 - Why does my program not respond when I run it 当我尝试从脚本中的 function 更改我的全局变量时,没有任何反应吗? - When I try to change my global variable from a function in my script nothing is happening? 当我运行这个主要因素程序时,什么也没发生 - Nothing happens when I run this prime factors program QT代码,运行程序时什么也没发生 - QT code, nothing happens when I run the program 用 pygame 创建一个表面,但是当我运行程序时,没有显示 - Creating a surface with pygame but when I run program, nothing showing 为什么当我运行 `for glyph in font.iter(&#39;glyph&#39;)` 时,etree 没有从我的 SVG 返回任何内容? - Why is etree returning nothing from my SVG when I run `for glyph in font.iter('glyph')`? 为什么当我运行我的 tkinter GUI 代码时,window 出现了,但里面什么都没有? - Why when I run my tkinter GUI code the window shows up but there is nothing inside it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM