Jumat, 08 Juli 2011

PROGRAMS SEEK OR NOT CONNECTED GRAF

This week I want to talk about the graph. a little explanation about the graph as follows:

For example, graph is about 4 The set V (Vertex) that its elements are called vertices (or points or nodes or points). The set E (Edge) which is not sequential pairs of vertices, its members called a segment (rib or side).
the program below to find out this graph is connected or not

let's look at the program..

 
    #include <cstdlib>
    #include <iostream>


    using namespace std;

  int main(int argc, char *argv[])

    {

    bool ketemu,nolsemua;

    int matrix[10] [10];

    int i,j,jumlah_simpul,jumlah_sisi,asal,tujuan;

    //initialization of matrix

    cout<<”jumlah simpul:”;

    cin>>jumlah_simpul;


    cout<<”jumlah_sisi:”;

    cin>>jumlah_sisi;

    for (i=1;i<=jumlah_simpul;i++)

    for (j=1;j<=jumlah_simpul;j++)

    matrix[i][j]=0;

    //fill in the matrix conform the graph input

    for (i=1;i<=jumlah_sisi;i++){

    cout<<”simpul asal:”;

    cin>>asal;

    cout<<”simpul tujuan:”;

    cin>>tujuan;

    matrix[asal][tujuan]=1;

    matrix[tujuan][asal]=1;

    }

    //checking graph

    i=1;nolsemua=false;

    while (i<=jumlah_simpul && !nolsemua){

    j=1;ketemu=false;

    while (j<=jumlah_simpul && !ketemu){

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

    ketemu=true;

    else

    j++;
    }
    if (!ketemu)
    nolsemua=true;
    else
    i++;
    }
    if(nolsemua)
    cout<<”graf tidak terhubung”<<endl;
    else
    cout<<”graf terhubung”<<endl;
        system(“PAUSE”);
        return EXIT_SUCCESS
    } 

Tidak ada komentar:

Posting Komentar