Created
April 18, 2022 18:08
-
-
Save 0xPratik/86057c570dae1720a05ce26d64e2d972 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Graph { | |
private: | |
int n; | |
int g[10][10]; | |
public: | |
Graph(int x) | |
{ | |
n = x; | |
for (int i = 0; i < n; ++i) { | |
for (int j = 0; j < n; ++j) { | |
g[i][j] = 0; | |
} | |
} | |
} | |
void displayAdjacencyMatrix() | |
{ | |
cout << "\n\n Adjacency Matrix:"; | |
for (int i = 0; i < n; ++i) { | |
cout << "\n"; | |
for (int j = 0; j < n; ++j) { | |
cout << " " << g[i][j]; | |
} | |
} | |
} | |
void addEdge(int x, int y) | |
{ | |
if ((x >= n) || (y > n)) { | |
cout << "Vertex does not exists!"; | |
} | |
if (x == y) { | |
cout << "Same Vertex!"; | |
} | |
else { | |
g[y][x] = 1; | |
g[x][y] = 1; | |
} | |
} | |
void addVertex() | |
{ | |
n++; | |
int i; | |
for (i = 0; i < n; ++i) { | |
g[i][n - 1] = 0; | |
g[n - 1][i] = 0; | |
} | |
} | |
void removeVertex(int x) | |
{ | |
if (x > n) { | |
cout << "\nVertex not present!"; | |
return; | |
} | |
else { | |
int i; | |
while (x < n) { | |
for (i = 0; i < n; ++i) { | |
g[i][x] = g[i][x + 1]; | |
} | |
for (i = 0; i < n; ++i) { | |
g[x][i] = g[x + 1][i]; | |
} | |
x++; | |
} | |
n--; | |
} | |
} | |
}; | |
int main() | |
{ | |
Graph obj(4); | |
obj.addEdge(0, 1); | |
obj.addEdge(0, 2); | |
obj.addEdge(1, 2); | |
obj.addEdge(2, 3); | |
obj.addVertex(); | |
obj.displayAdjacencyMatrix(); | |
obj.removeVertex(1); | |
obj.displayAdjacencyMatrix(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment