图的邻接矩阵实现(包括PRIM和DIJKSTRA算法)

来源:互联网 发布:手机淘宝上怎么给差评 编辑:程序博客网 时间:2024/05/16 09:21
 
#ifndef _GRAPH_H_#define _GRAPH_H_#include #include #define  INFINITE 0XFF00using namespace std;class AdjacencyWDiGraph{public:AdjacencyWDiGraph(int _n, int noEdge, bool d = true);virtual ~AdjacencyWDiGraph();bool Exist(int i, int j) const;AdjacencyWDiGraph& AddEdge(int i, int j, int t);AdjacencyWDiGraph& DeleteEdge(int i, int j);int InDegree(int i) const;int OutDegree(int i) const;void print();void Dijkstra(int v, int dist[], int prev[]);void Prim();void BFS(int v);//广度优先搜索void DFS(int v);//深度优先搜索int Edges() const{return e;}int Verticles() const{return n;}private:int n;int e;int **a;int infinite;bool isNonDiG;};AdjacencyWDiGraph::AdjacencyWDiGraph(int _n, int noEdge, bool b):n(_n), infinite(noEdge), isNonDiG(b){try{a = new int*[n + 1];for (int i = 0; i < n + 1; i++){a[i] = new int[n + 1];}}catch(bad_alloc &e){cout << e.what() << endl;}for (int i = 0; i < n + 1; i++){for (int j = 0; j < n + 1; j++){a[i][j] = INFINITE;}}}AdjacencyWDiGraph::~AdjacencyWDiGraph(){for (int i = 1; i < n + 1; i++){delete a[i];}delete a;}bool AdjacencyWDiGraph::Exist(int i, int j) const{assert(i > 0 && i <= n && j > 0 && j <= n);if (INFINITE == a[i][j]){return false;}else{return true;}}AdjacencyWDiGraph& AdjacencyWDiGraph::AddEdge(int i, int j, int t){assert(i > 0 && i <= n && j > 0 && j <= n);if (isNonDiG){a[i][j] = t;a[j][i] = t;}e++;return *this;}AdjacencyWDiGraph& AdjacencyWDiGraph::DeleteEdge(int i, int j){assert(i > 0 && i <= n && j > 0 && j <= n);if (a[i][j] != INFINITE){if (isNonDiG){a[i][j] = INFINITE;a[j][i] = INFINITE;}}e--;return *this;}void AdjacencyWDiGraph::print(){for (int i = 1; i < n + 1; i++){for (int j = 1; j < n + 1; j++){cout << a[i][j] << " ";}cout << endl;}}//求从节点v到其他节点的最短距离,dist返回每个节点对应的最短距离,//prev记录节点i到达v的最短路径的下一个节点。void AdjacencyWDiGraph::Dijkstra(int v, int dist[], int prev[]){bool *s = new bool[n + 1];//初始化dist和prevfor (int i = 1; i <= n; i++){dist[i] = a[v][i];//false表示没有加入s[i] = false;if (INFINITE == dist[i]){prev[i] = 0;}else{prev[i] = v;}}s[v] = true;dist[v] = 0;//循环n-1次,将其余的n-1个节点加入s[]中。for (i = 1; i < n; i++){int temp = INFINITE;int u = v;for (int j = 1; j <= n; j++){if ((!s[j]) && (dist[j] < temp)){u = j;temp = dist[j];}}s[u] = true;//检查每一个节点,确定是否修改其到v的距离。for (j = 1; j <= n; j++){if ((!s[j]) && (a[u][j] + dist[u] < dist[j])){dist[j] = a[u][j] + dist[u];prev[j] = u;}}}delete[] s;}/**************************************************************PRIM算法,求最小生成树***************************************************************/void AdjacencyWDiGraph::Prim(){//lowcost[i]是i与S中的节点最近的距离int *lowcost = new int[n + 1];//closet[i]是S中的节点,i与其距离最近int *closest = new int[n + 1];//标志节点i是否加入S中bool *s = new bool[n + 1];s[1] = true;for (int i = 2; i <= n; i++){lowcost[i] = a[i][1];closest[i] = 1;s[i] = false;}for (i = 1; i < n; i++){int min = INFINITE;int k = 1;for (int j = 2; j <= n; j++){if ((!s[j]) && lowcost[j] < min){k = j;min = lowcost[j];}}s[k] = true;cout << k << "--" << closest[k] << " " << a[k][closest[k]] << endl; for (j = 2; j <= n; j++){if ((!s[j]) && a[k][j] < lowcost[j]){lowcost[j] = a[k][j];closest[j] = k;}}}delete[] lowcost;delete[] closest;delete[] s;}//从节点v开始遍历void AdjacencyWDiGraph::BFS(int v){queue q;bool *visited = new bool[n + 1];for (int m = 0; m <= n; m++){visited[m] = false;}q.push(v);visited[v] = true;while (!q.empty()){int j = q.front();q.pop();cout << "visit " << j << endl;for (int k = 1; k <= n; k++){if ((!visited[k]) && (a[j][k] != INFINITE)){q.push(k);visited[k] = true;}}}delete[] visited;}void AdjacencyWDiGraph::DFS(int v){bool *visited = new bool[n + 1];for (int m = 0; m <= n; m++){visited[m] = false;}int count = 1;visited[v] = true;cout << "visit " << v << endl;while (count < n){int i = 1;int temp = v;while (i <= n) //若从v节点开始搜索,没有到尽头{for (i = 1; i <= n; i++ ){if ((!visited[i]) && (a[temp][i] != INFINITE)){temp = i;visited[i] = true;cout << "visit " << i << endl;count++;break;}}}}delete[] visited;}#endif