Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class AdjacencyMatrix {
- private:
- int n;
- int **adj;
- public:
- AdjacencyMatrix(int n) {
- this->n = n;
- adj = new int*[n];
- for (int k = 0; k < n; k++) {
- adj[k] = new int[n];
- for(int j = 0; j < n; j++) {
- adj[k][j] = 0;
- }
- }
- }
- //Add a new edge to the Graph
- void addEdge(int orig, int dest) {
- if( orig > n || dest > n || orig < 1 || dest < 1) {
- cout << "Trying to add an invalid edge";
- cout << "(" << orig << ", " << dest << ")\n";
- }
- else {
- adj[orig - 1][dest - 1] = 1;
- }
- }
- //remove an edge
- void removeEdge(int orig, int dest) {
- if( orig > n || dest > n || orig < 1 || dest < 1) {
- cout << "Trying to remove an invalid edge";
- cout << "(" << orig << ", " << dest << ")\n";
- }
- else {
- adj[orig - 1][dest - 1] = 0;
- }
- }
- //Print the graph
- void display() {
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < n; j++)
- cout << adj[i][j] <<" ";
- cout << endl;
- }
- }
- };
- int main() {
- AdjacencyMatrix M(5);
- M.addEdge(2, 5);
- M.addEdge(1, 5);
- M.addEdge(5, 1);
- M.addEdge(3, 3);
- M.removeEdge(1, 5);
- M.display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement