创建类实例:
template <typename IndexType,typename DataType>
class Graph{
public:Graph() {}//构造函数Graph(const Graph&);//复制构造函数Graph& operator=(const Graph&);//赋值函数Graph(Graph&&);//移动构造函数Graph& operator=(Graph&&);//移动赋值函数void show();//显示图bool insert(IndexType, DataType, IndexType, bool = false);//插入bool del(IndexType, IndexType, bool = false);//删除bool update(IndexType, DataType, IndexType, bool = false);//更新void clear() { name2dex.clear(); dex2name.clear(); mat.clear(); }//清空DataType shortestpath(IndexType,IndexType);//求最短路径std::pair<bool, std::vector<IndexType>> topologicalsort();//求拓扑排序~Graph() {}
};
//例:
Graph<std::string,int> g;
1.插入边
bool insert(IndexType, DataType, IndexType, bool = false);//插入成功返回true
//当最后一个参数为true时,即插入无向边
//例如:g.insert("A",1,"B");
//这就插入了一条从A到B的边,边权值为1
2.删除边
bool del(IndexType, IndexType, bool = false);//删除成功返回true
//当最后一个参数为true时,删除无向边
//例如:g.del("A","B");
//这就删除了从A到B的边
3.更新边权值
bool update(IndexType, DataType, IndexType, bool = false);
//与insert方法一致
4.清空图
void clear();
5.求最短路径(Dijkstra)
DataType shortestpath(IndexType,IndexType);//返回某点到某点间的最短路径长度
6.求拓扑排序
std::pair<bool, std::vector<IndexType>> topologicalsort();
//返回pair类型
//其中pair.first=true时则有拓扑排序
//pair.second为拓扑排序结果
本文发布于:2024-02-04 08:18:17,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170702912353882.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |