序言图深度优先搜索(prolog graph depth first search)
我已经看到关于深度优先搜索的其他问题,但我的问题稍有不同,我真的不明白。 在序言中,我代表我的无向图:
[0-[1,5], 1-[0,2], 2-[1,3], 3-[2,0], 5-[0]]
这是一组键值,其中键表示节点和列表:
-[]
表示其邻居。我不知道如何用这个模型进行深度优先搜索。 我尝试了很多解决方案。 我想要一个像这样的基本递归算法:
dfs(v, visited): if visited[v] then return visited.insert(v) foreach neighbor of v: dfs(neighbor, visited)
我在prolog中做不到的是pass作为一个可变引用,它将被每个邻居递归地修改为下一个邻居。
因为如果我把它翻译成序言:
% here Graph contains the entire key-value pairs list, % Node-Neighbor is the current node with its neighbors. dfs(Graph, Node-Neighbors, Visited) :- \+ member(Node, Visisted), % Now here I could get the next neighbor within neighbor, like so: member(Node1,Neighbors), % Then gets its own neighbors too by: member(Node1-Neighbors1, Graph), % but here I'm stuck...
我需要某种折叠,每个邻居都调用dfs,并将其结果传递给下一个邻居,但我不知道该怎么做......
谢谢。
I've seen other questions about depth first search but my problem is slightly different and I really don't get it. In prolog, I represent my undirected graph like this:
[0-[1,5], 1-[0,2], 2-[1,3], 3-[2,0], 5-[0]]
Which is a set of key-value, where the key represents the node and the list:
-[]
represents its neighbors.I don't know how to do a depth first search with this model. I've tried many solution. I want a really basic recursive algorithm like this one:
dfs(v, visited): if visited[v] then return visited.insert(v) foreach neighbor of v: dfs(neighbor, visited)
What I can't do in prolog is pass visited as a mutable reference which will be modified by each neighbor for the next neighbor recursively.
Because If I translate it into prolog:
% here Graph contains the entire key-value pairs list, % Node-Neighbor is the current node with its neighbors. dfs(Graph, Node-Neighbors, Visited) :- \+ member(Node, Visisted), % Now here I could get the next neighbor within neighbor, like so: member(Node1,Neighbors), % Then gets its own neighbors too by: member(Node1-Neighbors1, Graph), % but here I'm stuck...
I would need some kind of folding where each neighbors call dfs and its result is passed to the next neighbors, but I don't know how to do that...
Thanks.
原文:https://stackoverflow.com/questions/50766565
最满意答案
由于异常,你的close()调用是否有机会? 我通常这样编码:
try { new FileOutputStream(fileUri); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); outputStream.flush(); } finally { try { outputStream.close(); } catch (Exception ignored) { // ignore exceptions generated by close() } }
我看不出第一个代码片段有什么明显错误。
Any chance your close() call isn't being reached due to an exception? I generally code like this:
try { new FileOutputStream(fileUri); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); outputStream.flush(); } finally { try { outputStream.close(); } catch (Exception ignored) { // ignore exceptions generated by close() } }
I couldn't see anything obviously wrong with the first code fragment.
相关问答
更多-
VSC ++运行时内部存在限制。 应用程序只能通过运行时打开512个文件描述符,但如果应用程序调用_setmaxstdio (我认为节点没有),该值可以增加到2048。 (这是一个有点简单的解释,更多细节请看这里) 。你不能直接改变这个限制(如果你想让节点以某种方式调用这个函数,你只能提高512到2048)。 然而,你写的是使用节点v0.10.31,这是很旧的。 据我所知(尽管我对此并不是100%确定的),但节点同时已将文件I / O的VSC ++运行时切换到没有此限制的本机WinAPI调用,因此您可以尝试 ...
-
Node.js node-mysql错误:EMFILE,打开文件太多(Node.js node-mysql Error: EMFILE, Too many open files)[2022-09-29]
我认为你应该更多地使用闭包。 直接引用fn2将使用命名对象,这将使其不会超出范围。 通过闭包引用相同的代码将导致一层匿名,这将允许引用优雅地退出范围并获取垃圾。 I think you should be using closures more. References directly to fn2 are going to be using the named object, which will keep it from going out of scope. References to the sam ... -
最后使用readFile并放弃看起来像“同步”的openFileSync但实际上为什么不? fs.readFile(file, function read(err, data) {}) Finally use readFile and give up openFileSync which looks like "sync" but why actually not?? fs.readFile(file, function read(err, data) {})
-
React Native + Jest EMFILE:太多打开的文件错误(React Native + Jest EMFILE: too many open files error)[2023-12-20]
简短的回答:将'ulimit -n 4096'添加到〜.bash_profile并打开一个新的终端窗口解决了我的问题。 答案必须与我没有适当地设置ulimit。 sudo ulimit -n 10240 在我的Mac上默默不会改变ulimit。 我原本以为它没有做任何事,因为10240不是1024的增量。但是当我尝试2048,4096等时,它也没有做任何事情。 那么,“解决方案”是什么? ulimit -n(不带数字)会告诉你当前值是什么 对我来说,在终端窗口中键入sudo ulimit -n 2048并 ... -
你忘了安装看守 ,这里是你如何做到的: 通过Homebrew在OSX上安装 brew update brew install watchman 从源代码安装 git clone https://github.com/facebook/watchman.git cd watchman ./autogen.sh ./configure make sudo make install You forgot to install watchman, here is how you can do it: Insta ...
-
保存到文件时出错错误:EMFILE:打开的文件太多(error while saving to a file Error: EMFILE: too many open files)[2022-07-11]
假设您的目标是将100K随机字符串附加到文件,则不应使用fs.appendFile() 。 而是使用可写流将随机字符串附加到文件: var fs = require('fs'); var out = fs.createWriteStream('C:/file.txt', { flags : 'a' }); for (var i = 0; i < 100000; i++) { var a = randomString.generate({ length: 6, capitali ... -
由于异常,你的close()调用是否有机会? 我通常这样编码: try { new FileOutputStream(fileUri); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); outputStream.flush(); } finally { try { outputStream.close(); } catch (Exception ignored) { // ignore exc ...
-
我从jsmediatags改变了NodeFileReader,现在它似乎工作。 const fs = require('fs'); //added these lines var gracefulFs = require('graceful-fs'); gracefulFs.gracefulify(fs); I altered the NodeFileReader from jsmediatags and now it seems to work. const fs = require('fs'); // ...
-
使用graceful-fs模块,这是fs模块的直接替代品。 它是本机fs模块的包装器。 引用graceful-fs模块的文档 , 对open和readdir调用进行排队,如果有太多文件描述符的EMFILE错误,则在关闭某些内容后重试它们。 由于它公开了与本地fs模块相同的API,因此可以像使用普通的fs模块一样使用它。 // use just like fs var fs = require('graceful-fs'); 注意:这个库是由Isaac Z. Schlueter创建的,他是Node.js的核 ...
-
尝试将此作为fs替代品的弹性下降: 优美-FS 全球补丁fs是这样的: var realFs = require('fs') var gracefulFs = require('graceful-fs') gracefulFs.gracefulify(realFs) Try this as a more resilient drop in replacement for fs: graceful-fs Globally patch fs like this: var realFs = require('f ...