markdown 算法,请注意

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 算法,请注意相关的知识,希望对你有一定的参考价值。

## Chapter7
狄克斯特拉算法 - 加权图, 该算法只适用于有向无环图, 只适用于没有负加权的情况, 有负加权的用 贝尔曼-福德算法

算法实现
```
In [3]: graph = {}

In [4]: graph['s'] = {'a':6, 'b':2}

In [5]: graph['a'] = {'e':1}

In [6]: graph['b'] = {'a':3, 'e': 8}

In [7]: graph['e'] = {}

In [8]: infinity = float('inf')

In [9]: costs = {}

In [10]: costs['a'] = 6

In [11]: costs['b'] = 2

In [12]: costs['e'] = infinity

In [13]: parent = {}

In [14]: parent['a'] = 's'

In [15]: parent['b'] = 's'

In [16]: parent['e'] = None

In [17]: def find_lowest_cost_node(costs):
    ...:     lowest_node = None
    ...:     lowest_cost = infinity
    ...:     for node in costs:
    ...:         if node not in processed and costs[node] < lowest_cost:
    ...:             lowest_cost = costs[node]
    ...:             lowest_node = node
    ...:     return lowest_node
    ...:

In [18]: processed = []

In [20]: node = find_lowest_cost_node(costs)

In [21]: while node:
    ...:     node_cost = costs[node]
    ...:     neighbors = graph[node]
    ...:     for kid in neighbors:
    ...:         new_cost = node_cost + neighbors[kid]
    ...:         if new_cost < costs[kid]:
    ...:             costs[kid] = new_cost
    ...:             parent[kid] = node
    ...:     processed.append(node)
    ...:     node = find_lowest_cost_node(costs)
    ...:

In [22]: costs
Out[22]: {'a': 5, 'b': 2, 'e': 6}

In [23]: parent
Out[23]: {'a': 'b', 'b': 's', 'e': 'a'}

```

以上是关于markdown 算法,请注意的主要内容,如果未能解决你的问题,请参考以下文章

简体中国版文档的Markdown语法

Markdown规则

markdown常用语法

使用 Markdown,如何将图像及其标题居中?

MarkDown快捷键学习笔记

2021-11-07:奇偶链表。给定一个单链表,把所有的奇数节点和偶数节点分别排在一起。请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性。请尝试使用原地算法完成。你的算法