diff --git a/Adjacency matrice.cpp b/Adjacency matrice.cpp new file mode 100644 index 0000000..f01272b --- /dev/null +++ b/Adjacency matrice.cpp @@ -0,0 +1,137 @@ + #include + + #include + + using namespace std; + + #define MAX 20 + + /* Adjacency Matrix Class */ + + class AdjacencyMatrix + + { + + private: + + int n; + + int **adj; + + bool *visited; + + public: + + AdjacencyMatrix(int n) + + { + + this->n = n; + + visited = new bool [n]; + + adj = new int* [n]; + + for (int i = 0; i < n; i++) + + { + + adj[i] = new int [n]; + + for(int j = 0; j < n; j++) + + { + + adj[i][j] = 0; + + } + + } + + } + + /* Adding Edge to Graph */ + + void add_edge(int origin, int destin) + + { + + if( origin > n || destin > n || origin < 0 || destin < 0) + + { + + cout<<"Invalid edge!\n"; + + } + + else + + { + + adj[origin - 1][destin - 1] = 1; + + } + + } + + /* Print the graph */ + + void display() + + { + + int i,j; + + for(i = 0;i < n;i++) + + { + + for(j = 0; j < n; j++) + + cout<>nodes; + + AdjacencyMatrix am(nodes); + + max_edges = nodes * (nodes - 1); + + for (int i = 0; i < max_edges; i++) + + { + + cout<<"Enter edge (-1 -1 to exit): "; + + cin>>origin>>destin; + + if((origin == -1) && (destin == -1)) + + break; + + am.add_edge(origin, destin); + + } + + am.display(); + + return 0; + + }