Senin, 04 Juli 2011

Graph is Connected or Not

Graph is a collection of nodes and arcs which mathematically.
There are several possibilities that could terbentu graph, which is like this bawawah:
A graph may consist of only one node
A graph is not necessarily all the vertices connected by arcs
A graph may have vertices are not connected to another node
A graph may be all the vertices are interconnected
Here I provide the output results in two types ie one example of a program graph node and other nodesconnected and the unconnected. Associated graph is between the other nodes connected with a bow and theproper installation of each sipul or (connected to another node), and if the node is not related because eachnode does not have the same arc and inter-node installation of wrong or wrong (not connected with laiinnode), in order to understand better observe the results of its output, and lehat difference graph is connected and not connected,



#include <iostream.h>
#include <conio.h>
void main(){
bool ketemu,nolsemua;
int matrix[10] [10];
int i,j,jumlah_simpul,jumlah_sisi,asal,tujuan;
//isnisialisasi 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;
//isi matrix sesuai input graf
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;}
//telusuri graf
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";
else
cout<<"graf terhubung";
getch();}



Tidak ada komentar:

Posting Komentar