我开始尝试 boost 图类。为此,我创建了一个简单的示例,如下所示。当通过深度优先搜索算法遍历图形时,我没有添加一个节点。这是代码:
#include <boost\graph\adjacency_list.hpp>
#include <boost\graph\depth_first_search.hpp>
#include <iostream>
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS> GraphType;
typedef boost::graph_traits<GraphType>::vertex_descriptor VertexType;
class VertexVisitor : public boost::default_dfs_visitor
{
public:
void discover_vertex(VertexType v, GraphType g)
{
std::cout << v << std::endl;
}
};
int main()
{
GraphType g;
boost::add_edge(1,2,g);
boost::add_edge(1,3,g);
boost::add_edge(2,3,g);
boost::add_edge(1,4,g);
boost::add_edge(4,5,g);
VertexVisitor vis;
boost::depth_first_search(g, boost::visitor(vis));
return 0;
}
这个的输出是
0
1
2
3
4
5
但是 0 是从哪里来的,我从来没有添加过它?这是某种虚拟节点吗?但如果是这样,为什么在遍历时会访问它,我如何才能实现所需的行为?
编辑 1: 在尝试之后,根据 PlasmaHH 的建议,并通过我发现的 boost 代码进行调试,boost::add_edge 调用了图形顶点结构的大小调整。因此搜索算法添加和访问了更多元素,尽管它们彼此没有连接。顺便说一句:我正在使用 boost 1.47。
编辑 2: 它表明,depth_first_search 的行为(除了它的原生差异)不同于 breadth_first_search 算法,因为DFS遍历图中的所有节点,即使它们没有连接。我看不到这样做的好处,因为我只想找到一条从一个节点到另一个节点的路径,该路径与该节点相连,但是没关系。如前所述,我的问题的解决方案是使用 BFS 算法,该算法不会遍历所有子图。对于那些有兴趣的人,我添加一个小例子:
#include <boost\graph\adjacency_list.hpp>
#include <boost\graph\depth_first_search.hpp>
#include <boost\graph\breadth_first_search.hpp>
#include <iostream>
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS> GraphType;
typedef boost::graph_traits<GraphType>::vertex_descriptor VertexType;
class DFSVertexVisitor : public boost::default_dfs_visitor
{
public:
void discover_vertex(GraphType::vertex_descriptor v, GraphType g)
{
std::cout << v << std::endl;
}
};
class BFSVertexVisitor : public boost::default_bfs_visitor
{
public:
void discover_vertex(GraphType::vertex_descriptor v, GraphType g)
{
std::cout << v << std::endl;
}
};
int main(int argc, char *argv[])
{
GraphType g;
boost::add_edge(1, 2, g);
boost::add_edge(2, 3, g);
boost::add_edge(1, 3, g);
boost::add_edge(4, 5, g);
std::cout << "Performing BFS" << std::endl;
BFSVertexVisitor bfsVisitor;
boost::breadth_first_search(g, boost::vertex(1, g), boost::visitor(bfsVisitor));
std::cout << "Performing DFS" << std::endl;
DFSVertexVisitor dfsVisitor;
boost::depth_first_search(g, boost::visitor(dfsVisitor).root_vertex(1));
return 0;
}
注意,节点 4 和 5 没有连接到节点 1、2 和 3!
输出:
Performing BFS
1
2
3
Performing DFS
1
2
3
0
4
5
编辑 3: 我不得不重新考虑。我用 add_edge 连接的数字不是节点本身,而只是它们的索引,正如 n.m 刚刚建议的那样。因此,我认为仅添加边并不是最终解决方案,因为删除其中一个顶点无法按预期工作。
最佳答案
来自文档:
If the VertexList of the graph is vecS, then the graph has a builtin vertex indices accessed via the property map for the vertex_index_t property. The indices fall in the range [0, num_vertices(g)) and are contiguous. When a vertex is removed the indices are adjusted so that they retain these properties.
我认为文档没有明确说明 vertex_descriptor 只是索引。文档中的一些示例表明确实如此。其他示例将 vertex_descriptor 视为黑盒。
关于c++ - boost图遍历显示 "hidden"节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11468837/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article