带权有向图
2015-12-16 23:24
253 查看
//头文件Edge.h Graph.h //Edge.h //该函数的目的是记录带权有向图 #ifndef EDGE_H #define EDGE_H template<typename T> class Edge{ public: //该图主要包括(起点,终点,权重) int start; int end; T weight; Edge(); Edge(int st, int en, T wei); bool operator<(const Edge<T>& str); bool operator>(const Edge<T>& str); }; template<typename T> Edge<T>::Edge() { } template<typename T> Edge<T>::Edge(int st, int en, T wei) { this->start = st; this->end = en; this->weight = wei; } template<typename T> bool Edge<T>::operator<(const Edge<T>& str) { return this->weight < str->weight; } template<typename T> bool Edge<T>::operator>(const Edge<T>& str) { return this->weight>str->weight; } #endif // !EDGE_H //Graph.h #ifndef GRAPH_H #define GRAPH_H #include"Edge.h" #include<iostream> using namespace std; template<typename T> class Graph{ private: //该头文件中的私有变量应该包括(顶点的数目,边的数目,存储节点权重的二位数组,记录每个节点是否被访问过的数组) int vertexNum; int edgeNum; Edge<T> edge; bool *Mark; T **martrix; public: Graph(); Graph(int n); void Init(); ~Graph(); //该类应该包括的功能函数(添加边,删除边,获取权重, //获取顶点的第一条边,获取顶点的度数,获取顶点(以该顶点为起点的)的所有边的信息, //获取边的是否有边,判断两个节点之间是否有边的关系) bool isEdge(int start, int end)const; bool isHaveEdge(int start)const; void setEdge(int start, int end, T weight); void Delete(int start, int end); T getWeight(int start, int end)const; bool firstEdge(int start); //在计算有向图的度数是,要注意(计算方法是行的和加上列的和) int Number(int start)const; //得到以start为起点的边的信息(即输出所有边的权值) void getAllEdgeInfo(int start)const; void Print(int n) { if (firstEdge(n)) { cout << edge.weight << endl; } } }; template<typename T> Graph<T>::Graph() { this->vertexNum = 0; this->edgeNum = 0; this->edge = NULL; this->martrix = NULL; this->Mark = NULL; } template<typename T> Graph<T>::Graph(int n) { this->vertexNum = n; this->Mark = new bool[this->vertexNum]; this->martrix = new int*[this->vertexNum]; for (int i = 0; i < this->vertexNum; i++) { this->martrix[i] = new int[this->vertexNum]; } if (this->martrix == NULL || this->Mark == NULL) { cout << "空间未成功开辟" << endl; exit(true); } this->Init(); } template<typename T> void Graph<T>::Init() { for (int i = 0; i < this->vertexNum; i++) { this->Mark[i] = false; for (int j = 0; j < this->vertexNum; j++) { this->martrix[i][j] = 0; } } } template<typename T> Graph<T>::~Graph() { delete Mark; for (int i = 0; i < this->vertexNum; i++) { delete martrix[i]; } this->martrix = NULL; this->Mark = NULL; } template<typename T> bool Graph<T>::isEdge(int start, int end)const { if (start>this->vertexNum || end > this->vertexNum) { cout << "start/end已超出范围" << endl; exit(true); } if (this->martrix[start - 1][end - 1] != 0) { return true; } } template<typename T> bool Graph<T>::isHaveEdge(int start)const { if (start > this->vertexNum) { cout << "start已超出范围" << endl; exit(true); } for (int i = 0; i < this->vertexNum; i++) { if (this->martrix[start - 1][i] != 0) return true; } return false; } template<typename T> void Graph<T>::setEdge(int start, int end, T weight) { if (start>this->vertexNum || end > this->vertexNum) { cout << "start/end已超出范围" << endl; return; } this->martrix[start - 1][end - 1] = weight; } template<typename T> void Graph<T>::Delete(int start, int end) { if (start>this->vertexNum || end > this->vertexNum) { cout << "start/end已超出范围" << endl; return; } this->martrix[start - 1][end - 1] = 0; } template<typename T> T Graph<T>::getWeight(int start, int end)const { if (start>this->vertexNum || end > this->vertexNum) { cout << "start/end已超出范围" << endl; exit(true); } return this->martrix[start - 1][end - 1]; } template<typename T> bool Graph<T>::firstEdge(int start) { if (start>this->vertexNum) { cout << "start已超出范围" << endl; exit(true); } int i = 0; for (; i < this->vertexNum; i++) { if (this->martrix[start - 1][i] != 0) { edge.start = start; edge.end = i + 1; edge.weight = this->martrix[start - 1][i]; break; } } if (i < this->vertexNum) { return true; } else { return false; } } template<typename T> int Graph<T>::Number(int start)const { if (start>this->vertexNum) { cout << "start已超出范围" << endl; exit(true); } int count = 0; for (int i = 0; i < this->vertexNum; i++) { if (this->martrix[start - 1][i] != 0) { count++; } } return count; } template<typename T> void Graph<T>::getAllEdgeInfo(int start)const { if (start>this->vertexNum) { cout << "start已超出范围" << endl; exit(true); } for (int i = 0; i < this->vertexNum; i++) { if (this->martrix[start - 1][i] != 0) { cout << "(" << start << "," << i + 1 << ")" << this->martrix[start - 1][i] << " "; } } cout << endl; } #endif // !GRAPH_H //主函数 #include"Graph.h" int main(int argc, char argv[]) { Graph<int>graph(5); graph.setEdge(1, 1, 2); graph.setEdge(1, 2, 6); graph.setEdge(1, 6, 5); graph.Print(1); graph.Delete(1, 1); graph.Print(1); return 0; }
相关文章推荐
- 17、osg::Camera,osg::LOD,osgSim::Impostor类函数说明
- SqlLocalDB2014使用笔记
- [bigdata] hadoop 参数配置解析
- 2.1---删除链表中重复元素(CC150)
- Unity UGUI RectTransform图解
- CentOS下安装Gitlab
- 实战c++中的vector系列--可怕的迭代器失效之二(删除vector中元素)
- 用户及权限管理
- 实战c++中的vector系列--可怕的迭代器失效之二(删除vector中元素)
- 实现strcpy , strncpy, memcpy
- 图的基本操作及BFS与DFS的实现以及飞机换乘问题
- C语言点滴
- Eclipse 的快捷键以及文档注释、多行注释的快捷键
- Android
- Python之路【第六篇】:面向对象编程相关
- 功能测试机设计-MFC and C++ 以及单片机 C
- UINavigationController修改默认的动画
- 无法访问Windows Installer服务
- 用CSS来定义<p>标签,要求实现以下效果:字体颜色再IE6下为黑色,IE7下为红色,IE8下为绿色,其他浏览器下为黄色。
- ASP.NET + SqlSever 大数据解决方案 PK HADOOP