- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
显然我的老师认为即使我们没有时间学习东西(也没有足够的示例)我们应该继续前进,所以我现在需要知道如何在 clisp 中制作 Floyd-Warshall 和 Warshall 的算法。
正如我对 prolog 所做的那样,我的问题是从图中生成邻接矩阵,在这种情况下它将是列表的列表,例如:
((A B) (A C) (A D) (B C) (C D))
应该生成:
((0 1 1 1) (1 0 1 9) (1 1 0 1) (1 9 1 0))
我有这个:
(defun floyd(graph)
(setf l (length graph))
(setf mat (matrix l graph))
)
(defun matrix(l graph)
(setf matrix (make-array (list l l)))
(dotimes (i l)
(dotimes (j l)
(if (= i j)
(setf (aref matrix i j) 0)
(setf (aref matrix i j) ???)
)
)
)
matrix
)
非常感谢任何帮助。
另外,有点跑题了:如果我能解决我自己的问题,我是否应该为了得到一个已回答的问题而回复自己?
最佳答案
我将 Wikipedia 伪代码转换为具有类型声明的 Common Lisp。返回类型声明是非标准的,我使用了 SBCL。我想这不会运行,但它可能会让您了解 Lisp 代码应该是什么样子。
(defparameter *n* 5)
(defparameter *path*
(make-array (list *n* *n*)
:element-type '(unsigned-byte 64)))
(defun floyd-warshall (path)
(declare (type (simple-array (unsigned-byte 64) 2) path)
(values (simple-array (unsigned-byte 64) 2) &optional))
(destructuring-bind (y x) (array-dimensions path)
(unless (= y x)
(break "I expect a square matrix, not ~ax~a." x y))
(macrolet ((p (j i)
`(aref path ,j ,i)))
(dotimes (k x)
(dotimes (i x)
(dotimes (j x)
(setf (p j i)
(min (p j i) (+ (p k i)
(p j k)))))))))
path)
注1:如果你有一个 3D 体积图像,你应该有这样的索引(aref vol k j i) 其中 k 索引 z、j y 和 i x 方向。那样在 SBCL 和大概所有其他实现中,切片在内存中是连续的。
注2:macrolet 可以节省大量的输入。还要看看这个漂亮的库中 with-arrays 的实现:git://github.com/nikodemus/raylisp.git/objects/box.lisp
关于graph - lisp 中的邻接矩阵/Floyd/Warshall,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6116793/
一 问题描述 求节点0到节点2的最短路径。 二 代码 package graph.floyd; import java.util.Scanner; public class Floyd {
一 背景介绍 如果求解任意两个节点之间的最短路径,则需要以每个节点为源点,重复调用 n 次 DijKstra 算法。其实是完全没有必要这么麻烦,Floyd 算法可用于求解任意两个节点之间的最短距离。F
我已经为一个 100 x 100 的邻接矩阵编写了代码,它表示以下有向图: 我正在尝试使用 Floyd-Warshall 算法为图中所有蓝色节点对找到最短路径。你如何只找到所选节点的所有对最短路径?这
我收到一个关于 Floyd's cycle-finding algorithm 的面试问题: Floyd 的循环查找算法何时会失败? 我的意思是,是否有规则可以找到快指针和慢指针之间的步长? 最佳答案
我正在尝试使用 java.awt.image.BufferedImage 在 Java 中实现 Floyd Steinberg 算法。 我使用了描述的算法 here使用自定义调色板,我希望获得与维基百
我正在尝试实现 Floyd-Warshall 算法(所有对最短路径)。在下面的代码中,当我输入一些数字时,它会给出最后一个数字作为输入。我知道代码不完整。 现在我应该怎么做才能为每个 i 和 j 打印
我试图在 .NET 的 C++ 上找到这个算法,但找不到,我找到了这个: // Best solution function boolean hasLoop(Node startNode){ No
我想在这个图问题中重建从源到目标顶点的路径。 如何存储路径,以及在找到从 s 到 d 的最小成本后如何检索它? 请帮我找到一个简单的答案? 例如在这一点上, adjmat[i][j] = Math.m
是否可以使用邻接表对 Floyd Warshall 进行编码?我必须处理文本文件中的一百万个顶点,因此邻接矩阵不是解决方案。任何实现已经可用?请帮忙。 最佳答案 您不能将 Floyd Warshall
我实现了 Floyd-Warshall 算法。根据他们的矩阵,我可以得到正确的结果,关于两个地方之间的最短路径和他们的距离。我的问题是如何打印从 i 到 j 的最短距离。我做了一些研究,发现了一个类似
这可能是一个糟糕的问题,因为我的代表很低,但我已经研究了几个小时的其他解决方案,我的代码似乎与我遇到的工作解决方案几乎相同。请不要忽略基于低代表的问题。 输出矩阵 d[][] 包含给定顶点对之间最短路
Mr. Rowan plans to make a walking tour of Paris. However, since he is a little lazy, he wants to tak
使用 Floyd 时如何使用我电脑上的数据文件? 运行后: floyd init floyd run --gpu --env tensorflow-1.3 "python model.py" 我得到的
假设我有 9 个顶点。所以我有 9x9 解决方案矩阵和 matrix[6,0] = infinity, matrix[6,9]=1, matrix[9,0]=1 现在算法的工作原理如下: for k
This维基百科页面解释了 Floyd Warshall 算法,用于查找图中节点之间的最短路径。维基百科页面使用图像左侧的图表 作为起始图(在 k = 0 时的第一次迭代之前),然后显示剩余的迭代(k
考虑以下链表: 1->2->3->4->5->6->7->8->9->4->...->9->4..... 上面的列表有一个循环如下: [4->5->6->7->8->9->4] 在白板上绘制链表,我尝
我知道当图中有负权重环时,没有找到最小距离的方法,也就没有最小距离的意义了。我的问题是,如果我们向 Floyd Warshall 算法提供具有负权重循环的图,会发生什么情况?它会在 O(n3) 内无限
有人可以告诉我这个过程在 for 迭代中的时间复杂度吗?这段代码是FloydWarshall算法的“重构路径”部分。prev[n][n]是最短路径中源节点和目的节点之间的节点矩阵。printAllSP
问题陈述:https://www.hackerrank.com/challenges/floyd-city-of-blinding-lights 代码: import scala.io.StdIn._
我能够理解 Floyd 循环查找算法工作原理的基本原理。我唯一无法理解的是 while 循环条件,如下所示: while(slow && fast && fast->next){ sl
我是一名优秀的程序员,十分优秀!