Solved programming problem

Find Total Vertex of A Graph in C++.

Source code

#include <iostream>

using namespace std;

int main()

{

    int graph[100][100];

    char vertex[26];

    int i,j,node,count=0;

    char item;

    ///Create all vertex

    for(i=0; i<26; i++)

    {

        vertex[i]=65+i;

    }

    cout<<"Please Enter Total Vertex Number: ";

    cin>>node;

    ///set all value 0 for each node

    for(i=0; i<node; i++)

    {

        for(j=0; j<node; j++)

        {

            graph[i][j]=0;

        }

    }

    ///input

    cout<<"
Press 1 for yes 0 for No: "<<endl;

    for(i=0; i<node; i++)

    {

        for(j=i+1; j<node; j++)

        {

            cout<<vertex[i]<<"->"<<vertex[j]<<": ";

            cin>>graph[i][j];

            if(graph[i][j]==1) ///if input is 1

            {

                graph[j][i]; /// make the duplicate node 1  like if A->B is 1 make B->A =1

            }

        }

    }

    ///count total node

    for(i=0; i<node; i++)

    {

        for(j=0; j<node; j++)

        {

            if(graph[i][j]==1)

            {

                count++;

            }

        }

    }

    if(count==0)

    {

        cout<<" Opps !! This Graph don't have any vertex connected with."<<endl;

    }

    else

    {

        cout<<"
Total Vertex: "<<count<<endl;

    }

    return 0;

}

Output

Please Enter Total Vertex Number: 5

Press 1 for yes 0 for No:
A->B: 1
A->C: 1
A->D: 0
A->E: 1
B->C: 0
B->D: 1
B->E: 1
C->D: 0
C->E: 0
D->E: 1

Total Vertex: 6

Join the discussion

Add your comment