Python的teachmedijkstra库
Dijkstra 的算法与 Prim 的最小生成树算法非常相似。与 Prim 的 MST 一样,我们以给定的源作为根生成 SPT(最短路径树)。我们维护两个集合,一个集合包含最短路径树中包含的顶点,另一个集合包含最短路径树中尚未包含的顶点。在算法的每一步,我们都会找到一个顶点,它在另一个集合(尚未包含的集合)中,并且与源的距离最小。
trainmedijkstra:帮助渲染的实用程序库解释了用于教学目的的乳胶格式的 Dijkstra 算法。矩阵的行代表图的顶点,列代表解释的每个单位时间步长,得到为最短距离算法构造的最短路径树。
安装
这个模块没有内置于Python。要安装它,请在终端中键入以下命令。
pip install teachmedijkstra
安装库后,继续的步骤是:
- 初始化无向/有向图
- 向图中添加顶点和边
- 使用 Dijaskstra(),输入创建的带有起点的图形。
- run(),运行算法。
- 最后生成的latex文件可以按需要的名字保存。
函数:
- trainmedijkstra.UndirectedGraph() :用无向图初始化图。
- graph.addVertex(名称,坐标) :添加顶点的名称以及它应该属于的图形坐标。
- graph.addEdge(strt, end, weight) :将 1 个节点的边添加到具有特定权重的另一个节点。
- trainmedijkstra.Dijkstra(graph_obj, strt_point) :使用带有起点的图形实例初始化变量。
- dijkstra.run() :运行 dijkstra 算法。
- dijkstra.saveToLaTeXFile (file_name) :将构建的图形以乳胶格式保存在 file_name 中。
示例 1:
Python3
import teachmedijkstra
# getting graph
graph = teachmedijkstra.UndirectedGraph()
# initializing vertices
graph.addVertex("i", (0, 1))
graph.addVertex("j", (2, 1))
graph.addVertex("k", (2, 2))
graph.addVertex("l", (0, 2))
# initializing edges
graph.addEdge("i", "j", 7)
graph.addEdge("i", "l", 8)
graph.addEdge("j", "k", 6)
graph.addEdge("l", "j", 1)
graph.addEdge("k", "i", 5)
# creating graph from i.
dijkstra = teachmedijkstra.Dijkstra(graph, "i")
dijkstra.run()
# saving file
dijkstra.saveToLaTeXFile("undirectedDij.tex")
Python3
import teachmedijkstra
# initializing Directed graph
graph = teachmedijkstra.DirectedGraph()
# initializing vertices
graph.addVertex("a", (0, 2))
graph.addVertex("b", (1, 2))
graph.addVertex("c", (2, 2))
graph.addVertex("d", (0, 1))
graph.addVertex("e", (1, 1))
graph.addVertex("f", (2, 1))
graph.addVertex("g", (0, 0))
graph.addVertex("h", (1, 0))
graph.addVertex("i", (2, 0))
# adding edges
graph.addEdge("a", "b", 9)
graph.addEdge("b", "c", 3)
graph.addEdge("c", "f", 4)
graph.addEdge("e", "f", 3)
graph.addEdge("e", "d", 6)
graph.addEdge("d", "g", 1)
graph.addEdge("g", "h", 3)
graph.addEdge("h", "i", 8)
graph.addEdge("a", "d", 1)
graph.addEdge("e", "b", 3)
graph.addEdge("e", "h", 9)
graph.addEdge("f", "i", 6)
graph.addEdge("a", "e", 4)
graph.addEdge("c", "e", 5)
graph.addEdge("g", "e", 1)
graph.addEdge("i", "e", 4)
graph.addEdge("c", "i", 6)
graph.addEdge("a", "g", 1)
# calling Dijkstra fnc to perform algo.
dijkstra = teachmedijkstra.Dijkstra(graph, "a")
dijkstra.run()
# saving file
dijkstra.saveToLaTeXFile("directedDij.tex")
生产的乳胶文件:
输出(转换 Latex 后):
示例 2:有向图
在这个例子中,我们将使用这个模块创建一个有向图。
蟒蛇3
import teachmedijkstra
# initializing Directed graph
graph = teachmedijkstra.DirectedGraph()
# initializing vertices
graph.addVertex("a", (0, 2))
graph.addVertex("b", (1, 2))
graph.addVertex("c", (2, 2))
graph.addVertex("d", (0, 1))
graph.addVertex("e", (1, 1))
graph.addVertex("f", (2, 1))
graph.addVertex("g", (0, 0))
graph.addVertex("h", (1, 0))
graph.addVertex("i", (2, 0))
# adding edges
graph.addEdge("a", "b", 9)
graph.addEdge("b", "c", 3)
graph.addEdge("c", "f", 4)
graph.addEdge("e", "f", 3)
graph.addEdge("e", "d", 6)
graph.addEdge("d", "g", 1)
graph.addEdge("g", "h", 3)
graph.addEdge("h", "i", 8)
graph.addEdge("a", "d", 1)
graph.addEdge("e", "b", 3)
graph.addEdge("e", "h", 9)
graph.addEdge("f", "i", 6)
graph.addEdge("a", "e", 4)
graph.addEdge("c", "e", 5)
graph.addEdge("g", "e", 1)
graph.addEdge("i", "e", 4)
graph.addEdge("c", "i", 6)
graph.addEdge("a", "g", 1)
# calling Dijkstra fnc to perform algo.
dijkstra = teachmedijkstra.Dijkstra(graph, "a")
dijkstra.run()
# saving file
dijkstra.saveToLaTeXFile("directedDij.tex")
乳胶输出:
输出 :