Rabu, 29 Juni 2011

GRAPH

Graph is a collection of nodes and arcs that are mathematically expressed as:
G = (V, E)
where
G = Graph
V = Vertex or Vertex, or Node, or Point
E = arc or edge, or arc
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
Not trending graph (undirected graph or non-directed graph):
Sequence of vertices in an arc does not matter. Mis e1 arc can be called an arc AB or BA
Directed graph (directed graph):
Knot sequence has meaning. Mis arc AB is the arc while the BA is e1 e8.
Weighted graph (Weighted Graph)
If each arc has a value expressing the relation between 2 pieces of a node, then the arc is expressed as a weight.
The weight of an arc to the length of a path of 2 pieces of the point, the average number of vehicles per day through a street, etc..


#include <iostream.h>
#include <conio.h>

class graph{
        private:int n;
               int **a;
               int *reach;
               int *pos;
        public:graph(int k=10);
               void create();
               void dfs();
               void dfs(int v,int label);
               int begin(int v);
               int nextvert(int v);
};
void graph::graph(int k){
        n=k;
        a=new int *[n+1];
        reach=new int[n+1];
        pos=new int [n+1];
        for(int i=1;i<=n;i++)
               pos[i]=0;
        for(int j=1;j<=n;j++)
               a[j]=new int[n+1];}
void graph::create(){
        for(int i=1;i<=n;i++){
               cout<<"Enter the "<<i<<"th row of matrix a:";
               for(int j=1;j<=n;j++)
                       cin>>a[i][j];}
        for(int k=1;k<=n;k++)
               reach[k]=0;}
void graph::dfs(){
        int label=0;
        for(int i=1;i<=n;i++)
               if(!reach[i]){
                       label++;
                       dfs(i,label);}
        cout<<"The contents of the reach array is:”;
        for(int j=1;j<=n;j++)
               cout<<reach[j]<<"      ";}
void graph::dfs(int v,int label){
        cout<<v<<"     ";
        reach[v]=label;
        int u=begin(v);
        while(u){
               if(!reach[u])
                       dfs(u,label);
               u=nextvert(v);}
}
int graph::begin(int v){
        if((v<1)&&(v>n))
               cout<<"Bad input";
        else
               for(int i=1;i<=n;i++)
                       if(a[v][i]==1){
                               pos[v]=i;
                               return i;}
        return 0;}
int graph::nextvert(int v){
        if((v<1)&&(v>n))
               cout<<"Bad input";
        else
               for(int i=pos[v]+1;i<=n;i++)
                       if(a[v][i]==1){
                               pos[v]=i;
                               return i;}
        return 0;}
void main(){
        clrscr();
        int x;
        cout<<"Enter the no of vertices:";
        cin>>x;
        graph g(x);
        g.create();
        cout<<"dfs is.....";
        g.dfs();
        getch();}



Perulangan untuk Mencetak Data


The program is somewhat similar to the previous program but the program inserts the node into which has entered some data there are three criteria that can be inserted namely head, body and tail of the data input node that already exist, the program also contained several steps such as determining a new data node and enter the data nodes to be inserted head, body or tail of the use provisions that have been determined. For example observe the following programs and their output results.

#include <cstdlib>
#include <iostream>

using namespace std;
class node {
      public:
             int data;
             node*berikut;};
int main(int argc, char *argv[]){
    //langkah pertama
                  node*baru;
                  baru=new node;
                  baru->data=1;
                  baru->berikut=NULL;
       //langkah dua
                node*lain;
                lain=new node;
                lain->data=3;
                lain->berikut=NULL;
                    node*lainya;
                lainya=new node;
                lainya->data=4;
                lainya->berikut=NULL;
                baru->berikut=lain;
                lain->berikut=lainya;
                node*kepala=baru;
                 cout <<"\t\t\t Desmala Dewi "<< endl;
                  cout <<"\t\t\t      10018061           "<< endl;
                  cout <<"\t\t\t          B              "<< endl;
     cout << endl;
     cout << endl;
      cout<<"Menggunakan perulangan untuk mencetak setiap data pada rantai\n";
                         node*jalan=kepala;
      int i=1;
      while(jalan!=NULL){
                         cout<<"Data ke ["<<i<<"] -> "<<jalan->data<<endl;
                         i++;
                         jalan=jalan->berikut;
                         }
                node*barunya;
                barunya=new node;
                barunya->data=2;
                barunya->berikut=NULL;
                baru->berikut=barunya;
                barunya->berikut=lain;
                lain->berikut=lainya;
                node*badan=baru;
      cout << endl;
      cout <<"Setelah disisip ditengah"<<endl;
      cout <<"Menggunakan perulangan untuk mencetak setiap data pada rantai\n";
                         node*jalan1=badan;
      int j=1;
      while(jalan1!=NULL){
                         cout<<"Data ke ["<<j<<"] -> "<<jalan1->data<<endl;
                         j++;
                         jalan1=jalan1->berikut;}
    cout << endl;
    cout << endl;
    cout << endl;
    system("PAUSE");
    return EXIT_SUCCESS;}

print data node of the new node






#include <cstdlib>
#include <iostream>
using namespace std;
class node{
public :
       int data;
       node *berikut;
};
int main(int argc, char *argv[])
{//langkah satu
     node *baru;
     baru = new node;
     baru->data=5;
     baru->berikut=NULL;
     cout<<"Data ke 1 :"<<baru->data<<endl;
     //langkah dua
     node *lain;
     lain = new node;
     lain->data=6;
     lain->berikut = NULL;
     cout<<"Data ke 2 :"<<lain->data<<endl;
     node *lain2;
     lain2 = new node;
     lain2->data=7;
     lain2->berikut = NULL;
     cout<<"Data ke 3 :"<<lain2->data<<endl;   
      node *lain3;
     lain3 = new node;
     lain3->data=8;
     lain3->berikut = NULL;
     cout<<"Data ke 4 :"<<lain3->data<<endl;

      node *lain4;
     lain4 = new node;
     lain4->data=9;
     lain4->berikut = NULL;
     cout<<"Data ke 5 :"<<lain4->data<<endl;
     //langkah tiga : menyambung rantai
     baru->berikut = lain;
     lain->berikut = lain2;
     lain2->berikut = lain3;
     lain3->berikut = lain4;
     cout<<"Isi data node lain dicetak dari node baru adalah :"<<lain->data<<" "<<lain2->data<<" "<<lain3->data<<" "<<lain4->data<<endl;
     cout<<baru->berikut->data;
     cout<<lain->berikut->data;
      cout<<lain2->berikut->data;
      cout<<lain3->berikut->data<<endl;
    system("color a0");
    system("PAUSE");
    return EXIT_SUCCESS;
}

Scored the second node from the head pointer






#include <cstdlib>
#include <iostream>
using namespace std;
class node {
public:
    int data;
    node *berikut;
};
int main(int argc, char** argv) {
    //langkah satu
    node *baru;
    baru = new node;
    baru->data = 5;
    baru->berikut = NULL;
    cout << " Isi data node baru adalah : " << baru->data << endl;
   
    //langkah dua
    node *lain;
    lain = new node;
    lain->data = 6;
    lain->berikut = NULL;
    cout << " Isi data node lain adalah : " << lain->data << endl;
    //langkah tiga : menyambung rantai
    baru->berikut = lain;
    cout << " Isi data node lain di cetak dari node baru adalah : " ;
    cout << baru->berikut->data << endl;
    //langkah empat
    node *kepala = baru;
    cout << " Mencetak node pertama dari pointer kepala : ";
    cout << kepala->data << endl;
    cout << " Mencetak node kedua dari pointer kepala : ";
    cout << kepala->berikut->data << endl;
    //langkah lima : pointer yang jalan - jalan
    cout << " Menggunakan perulangan untuk mencetak setiap data pada rantai " << endl;
    node *jalan = kepala;
    int i = 1;
    while (jalan != NULL){
        cout << " Data ke- " << i << " > " << jalan->data << endl;
        i++;
        jalan = jalan->berikut;
    }
    //langkah enam : bukti bahwa pointer kepala tidak kehilangan data
    cout << " Mencetak node pertama dari pointer kepala : ";
    cout << kepala->data << endl;
    cout << " Mencetak node kedua dari pointer kepala : ";
    cout << kepala->berikut->data << endl;
  system ("color a0");
  system ("PAUSE");
}