delete and search edge in adjacency matrix of a graph code example

Example: delete and search edge in adjacency matrix of a graph

/*adjacency matrix implementation of directed graph:
 0---->1---|
 |         |---->4
 |-> 2-->3-|*/
#include <iostream>

using namespace std;
#define v 5
void inti(int mat[][v])
{
    for(int i=0;i<v;i++)
    {
        for(int j=0;j<v;j++)
        {
            mat[i][j]=0;
        }
    }
}
void addedge(int mat[][v],int start,int endvertix)
{
    mat[start][endvertix]=1;
}
void check(int mat[][v],int start,int endvertix)
{
    if(mat[start][endvertix]==1)
    {
        cout<<"edge exists:"<<endl;
    }
    else
    {
        cout<<"edge does not exist:"<<endl;
    }

}
void removeedge(int mat[][v],int start,int edgeend)
{
    mat[start][edgeend]=0;
}
void printmat(int mat[][v])
{
    for(int i=0;i<v;i++)
    {
        for(int j=0;j<v;j++)
        {
            cout<<mat[i][j]<<" ";
        }
        cout<<endl;

    }
}

int main()
{
    int array_in_graph[v][v];
    inti(array_in_graph);
    addedge(array_in_graph,0,1);
    addedge(array_in_graph,0,2);
    addedge(array_in_graph,0,3);
    addedge(array_in_graph,1,3);
    addedge(array_in_graph,1,4);
    addedge(array_in_graph,2,3);
    addedge(array_in_graph,3,4);
    printmat(array_in_graph);
    cout<<"now above graph is not consider as we will remove edge '3->4' AND edge '1->3':"<<endl;
    removeedge(array_in_graph,3,4);
    removeedge(array_in_graph,1,3);
    printmat(array_in_graph);
    check(array_in_graph,1,4);
    check(array_in_graph,3,4);
    return 0;
}

Tags:

Cpp Example