Mzsmunna
10/16/2019 - 1:59 PM

graph_algo


#include<iostream>

using namespace std;

class Graph
{
    private:
        int **adjacencyMatrix;
        int vertexCount;
    public:
        Graph(int vertexCount);
        ~Graph()
        {

        }
        void addEdge(int i, int j);
        void removeEdge(int i, int j);
        bool isEdge(int i, int j);
        void display();
};
Graph::Graph(int VertexCount)
{
    this->vertexCount=vertexCount;
    adjacencyMatrix= new int*[vertexCount];
    for(int i=0;i<vertexCount;i++)
    {
        adjacencyMatrix[i]=new int[vertexCount];
        for(int j=0;j<vertexCount;j++)
        {
            adjacencyMatrix[i][j]=0;
        }
    }

}
void Graph::addEdge(int i,int j)
{
    if (i >= 0 && i < vertexCount && j >= 0 && j < vertexCount)
    {
        adjacencyMatrix[i][j] = 1;
        adjacencyMatrix[j][i] = 1;
    }
}
void Graph::removeEdge(int i,int j)
{
    if (i >= 0 && i < vertexCount && j >= 0 && j < vertexCount)
    {
        adjacencyMatrix[i][j] = 0;
        adjacencyMatrix[j][i] = 0;
    }
}
bool Graph::isEdge(int i,int j)
{
    /*if (i >= 0 && i < vertexCount && j >= 0 && j < vertexCount)
    {
        return adjacencyMatrix[i][j];
    }
    else
    {
        return 0;
    }*/
}
void Graph::display()
{
    int u,v;
    for(int u=0; u<vertexCount;u++)
    {
        cout << "\nnode[" << (char) (u+65) << "] -> ";
        for(int v=0; v<vertexCount;v++)
        {
            cout << " " << adjacencyMatrix[u][v];
        }
    }
    cout << "\n\n";
}
int main()
{
    Graph g(5);
    g.addEdge(3,4);
    g.addEdge(2,2);
    g.addEdge(1,0);
    g.addEdge(2,3);
    g.display();


    return 0;

}