adjacency matrix is used to representation of directed graph code example

Example: adjacency matrix of a directed 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 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);
    return 0;
}

Tags:

Cpp Example