简体   繁体   English

区分有向图和无向图

[英]Distinguish directed and undirected graph

I need to write a graph using C++ and I have a little problem. 我需要使用C ++编写图形,但有一个小问题。 My graph should be directed or undirected, weighted or unweighted, based on matrix or list all on user's choice. 我的图形应该是有向的,无向的,加权的或无权的,基于矩阵或根据用户的选择列出全部。 And distinguishing matrix from list graph is not a big deal, since it's two different classes, I got some problem with other parameters. 并且从列表图中区分矩阵不是什么大问题,因为它是两个不同的类,所以我在其他参数上遇到了一些问题。 The most obvious way to distinguish them is to make two bool variables and check them on every adding and deleting of vertex. 区分它们的最明显方法是制作两个bool变量,并在每次添加和删除顶点时检查它们。 It is quite obvious and easy to understand, but I doubt it's efficiency, because every time I add or delete vertex I have to do additional if . 这很明显并且易于理解,但是我怀疑它的效率,因为每次添加或删除顶点时, if都要做额外的事情。 I also could write subclasses for it, but I seriously doubt if it's worth it. 我也可以为此编写子类,但是我严重怀疑是否值得。

Every library is okay to use, if it's not representing graph itself. 如果每个库都不代表图形本身,则可以使用。

For directed and undirected best case is using bool variable for your graph, however You can assume your graph is weighted and directed, but for undirected edges add one edge from a→b and one edge from b→a. 对于有向和无向最佳情况,是对图形使用bool变量,但是可以假定图形是加权和有向的,但是对于无向边,请从a→b添加一条边,从b→a添加一条边。 Also if there isn't weight function set its weight to 1 . 另外,如果没有权重函数,请将其权重设置为1

But if you looking for graph library it depends to your programming language, but I'd suggest graph boost library which implemented fully in c++, and too many other people implement it partially in other languages. 但是,如果您要查找图形库,则取决于您的编程语言,但是我建议使用完全用c ++实现的图形升压库 ,太多的人会用其他语言部分实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM