- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在查找可从给定节点访问的所有节点时遇到一些问题。除非使用 DFS 或 BFS,我尝试使用集合和队列来生成所有可到达的节点。据我了解,我可以将队列初始化为起始节点,然后将起始节点添加到集合中并将其从队列中删除。如果它成功添加到集合中(不重复),则将其可到达的节点添加回队列中。重复此操作,直到队列为空,然后返回集合。
public static Set<String> reachable (Map<String, Set<String>> graph, String startNode) {
//Set of nodes accessible from the startNode
Set<String> reachableNodes = new ArraySet<String>();
//Queue of accessible nodes from a given node to search from.
Queue<String> searchNodes = new ArrayQueue<String>(graph.get(startNode));
//Stuck here.
return reachableNodes;
}
我在实现过程中遇到了一些困难。我尝试过声明一个迭代器来迭代队列。当队列有另一个元素时,我将该元素添加到集合中并从队列中删除该元素。但我不确定如何将刚刚放入集合中的该元素中的所有可访问节点添加回队列中。
算法(类似于BFS):
示例:
{ c : [f,e] ,
f : [g,d] ,
e : [d] ,
d : [g] }
"Enter startNode: c"
add f,e -> queue
remove f from queue
add f to set
add g d to queue
remove e
add e to set
add d to queue
loop until queue is empty.
最佳答案
一般来说,您描述的算法是 BFS。您可以在“Stuck”部分中使用 while 循环来执行此操作:
while (searchNodes.peek() != null) {
String next = searchNodes.remove();
boolean isNewNode = reachableNodes.add(next);
if (isNewNode && graph.containsKey(next)) {
for (String node : graph.get(next)) {
searchNodes.add(node);
}
}
}
关于java - 如何使用集合和队列查找图的可达性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12743170/
我正在尝试通过可访问性来覆盖我的基础,以便我的应用程序不会被应用程序商店拒绝。我熟悉 Apple 在示例代码中提供的 Reachability 类。我的问题是,如何最好地实现这一点。我发现检查 WWA
我有一个 TableView,它使用方法调用 retrieveData 从服务器接收数据。我使用 Reachability 来测试用户是否有互联网连接。如果是,则调用 retrieveData。如果不
一个简单的问题:当应用程序被终止或未运行时,是否可以在互联网连接可用时收到消息、通知或类似信息? 出于我的目的,我需要一种方法来同步我的所有通知,因为 APNs 只能发送最后一条消息。 编辑:我想知道
我终于弄明白了如何使用 apple Reachability 文件,这是一件了不起的事情。 我的问题是,因为我有大约 6 个 View ,每个 View 都需要检查我是否已连接到互联网。在我的应用程序
我正在使用 Apple 提供的示例代码来监控可达性 - 但它仍然让我头疼。顺便说一句,我在 iOS 5 上运行。 // Initialise hostReach = [[Reachability re
Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"]; 这条线在设备上运行良好,但在模拟器上我崩溃了:
在我的应用程序中的 appDelegate.m 中,我插入了可达性代码,就像苹果说的那样: -(BOOL)checkInternet { Reachability *r = [Reachability
大家好,我在使用 Apple 的可达性代码时遇到了一些问题。我发现,即使设备正确连接到互联网,最初可达性代码也会发出 1 个错误通知(Networkstatus = NotReachable),然后是
我的 AppDelegate.m 中有以下代码 - NSLog 的结果始终是 (null),因此条件没有可达性永远不会被解雇。我想知道为什么会这样/我做错了什么。 - (BOOL)applicatio
我正在使用 github 上的 Reachability 库,发现 here .我想通过单击按钮来检查设备是否在线。我是 Reachability 的新手,所以据我所知,您必须开始监听更改,然后测试连
我正在使用这个库:https://github.com/tonymillion/Reachability 问题有时出现在我检查以下表达式时:[[[ReachabilityManager sharedM
我对 AFNetorking 的 Reachability 有点迷茫,没有找到很多有用的信息。 我有一个可以登录 Web API 的应用程序。每个 VC 都以某种方式连接到 API,因此每个 VC 都
我了解如何在我的应用程序中测试互联网可达性,但我需要做的是不断监听应用程序范围内的可达性。因此,如果应用程序中任何地方的任何一点连接状态发生变化,我都可以使用react。 我怎样才能实现这样的目标?
我正在开发一个使用网络的 iPhone 应用程序。 iPhone 通过 HTTP 请求与我的服务器通信,应该可以在 WiFi 和 3G 上工作。 我目前使用 NSURLConnection initW
我目前正在使用可达性来检测可用的连接。当什么都没有或有良好的连接时,这很有效。但是,如果我的用户在飞机上拥有自己的 wifi 节点,但没有连接到整个互联网怎么办?我的用户将在 wifi 开启的情况下处
我已经创建了一个方法来检查主机是否可达。我从苹果开发者网站下载了 Reachability 类(.h 和 .m)并导入到我的项目中。我已将 NSString 名称作为 URL(主机名)传递。主机名是
我正在使用这个库在设备上的连接状态发生变化时得到通知。我不明白为什么这不起作用!我正在做的正是我在 github 文档中阅读的内容! 当应用程序启动时,会发布通知,但当我打开设置、打开/关闭飞行模式并
我是 iOS 新手,刚开始使用它。我正在尝试实现网络可达性以检测网络何时断开连接以及何时通过使用第三方类恢复。我能够检测到网络丢失,但我无法检测到网络断开连接后何时恢复。我正在使用以下条件来检查工作正
我用了this检查互联网连接 (WiFi\3G) 是否有效的问题。 它工作得很好,但有一个小问题。 当我关闭互联网时,该应用程序会发出互联网已关闭的警报;但是当我再次打开它时,直到连接确实建立起来,它
我的 Reachability 完全按照此 thread 中的建议工作. 我正在使用 open source Reachability .但是我没有使用 block 而是使用通知,因此该过程与 App
我是一名优秀的程序员,十分优秀!