Adjacency Matrix -- c++实现

来源:互联网 发布:教育统计系统导出数据 编辑:程序博客网 时间:2024/05/24 03:24

1. Adjacency Matrix (Undirected Graph)

i. Header (Graph.h)

#ifndef GRAPH_H_INCLUDED#define GRAPH_H_INCLUDEDclass Graph{private:bool** adjMat;// implement the matrix by a 2-D bool arrayint countV;// number of vertices in the graphpublic:Graph(int);~Graph();void addEdge(int, int);void removeEdge(int, int);bool isConnected(int, int);// check if two vertices are connectedvoid showConnected(int);// show all connected vertices of one vertex};#endif // GRAPH_H_INCLUDED

ii. Implementation (Graph.cpp)

#include"Graph.h"#include<iostream>using namespace std;Graph::Graph(int N){countV = N;/* initialize the adjacency matrix */adjMat = new bool *[countV];for (int i = 0; i < countV; ++i){adjMat[i] = new bool[countV];for (int j = 0; j < countV; ++j)adjMat[i][j] = false;}}Graph::~Graph(){for (int i = 0; i < countV; ++i)delete[] adjMat[i];delete[] adjMat;}void Graph::addEdge(int i, int j){/* since the graph is undirected, set both a[i][j]and a[j][i] to 1 while adding edge between i and j */if (i >= 0 && i < countV && j >= 0 && j < countV){adjMat[i][j] = true;adjMat[j][i] = true;}}void Graph::removeEdge(int i, int j){/* as in addEdge(), set both a[i][j] and a[j][i]to 0 while removing edge between i and j*/if (i >= 0 && i < countV && j >= 0 && j < countV){adjMat[i][j] = false;adjMat[j][i] = false;}}bool Graph::isConnected(int i, int j){/* since the graph is undirected, we only need tocheck either a[i][j] or a[j][i], here check a[i][j]*/if (i >= 0 && i < countV && j >= 0 && j < countV)return adjMat[i][j];else return false;}void Graph::showConnected(int i){cout << "The connection of vertex " << i << ":" << endl;cout << i;for (int j = 0; j < countV; ++j){if (adjMat[i][j])cout << " -> " << j;}cout << endl;}

iii. Client (main.cpp)

#include<iostream>#include"Graph.h"using namespace std;int main(int argc, char* argv[]){int V = atoi(argv[1]);Graph *g = new Graph(V);char YN;for (int i = 0; i < V; ++i){cout << "Enter the connections of vertex " << i << ":" << endl;int j;while (cin >> j){if (j >= 0 && j < V && j != i){if (g->isConnected(i, j)){cout << "Vertices " << i << " and " << j << " have been connected." << endl;}else g->addEdge(i, j);}/* input two same number to quit the loop */else if (j == i){break;}else cout << "Out of range." << endl;}}for (int i = 0; i < V; ++i)g->showConnected(i);cout << "Do you wanna remove edges?";cin >> YN;if (YN == 'Y'){int i, j;cout << "Enter the edge you want to remove: " << endl;while (cin >> i >> j){if (i >= 0 && i < V && j >= 0 && j < V && j != i){if (!g->isConnected(i, j)){cout << "Vertices " << i << " and " << j << " are not connected." << endl;}else {g->removeEdge(i, j);}}else if (j == i){break;}else cout << "Out of range.";}}else if (YN == 'N')cout << "No edges to remove." << endl;for (int i = 0; i < V; ++i)g->showConnected(i);return 0;}

执行:

Enter the connections of vertex 0:
1 4 0
Enter the connections of vertex 1:
0 2 3 4 1
Vertices 1 and 0 have been connected.
Enter the connections of vertex 2:
1 3 2
Vertices 2 and 1 have been connected.
Enter the connections of vertex 3:
1 2 4 3
Vertices 3 and 1 have been connected.
Vertices 3 and 2 have been connected.
Enter the connections of vertex 4:
0 1 3 4
Vertices 4 and 0 have been connected.
Vertices 4 and 1 have been connected.
Vertices 4 and 3 have been connected.
The connection of vertex 0:
0 -> 1 -> 4
The connection of vertex 1:
1 -> 0 -> 2 -> 3 -> 4
The connection of vertex 2:
2 -> 1 -> 3
The connection of vertex 3:
3 -> 1 -> 2 -> 4
The connection of vertex 4:
4 -> 0 -> 1 -> 3
Do you wanna remove edges?Y
Enter the edge you want to remove:
1 4
0 0
The connection of vertex 0:
0 -> 1 -> 4
The connection of vertex 1:
1 -> 0 -> 2 -> 3
The connection of vertex 2:
2 -> 1 -> 3
The connection of vertex 3:
3 -> 1 -> 2 -> 4
The connection of vertex 4:
4 -> 0 -> 3


原创粉丝点击