Thursday, March 13, 2014

EULAR PATH in C++



                                                        


#include<iostream>
using namespace std;
struct vertex
{
 int num,visit,dist,vi;
};
int n,edge[7][7],ov[2]={0,0};
struct node
{
int key;
node* next;
};
 void gcreate(vertex v1[7],int mt[7][7],int n)
{
int i,j,k,a,b,w;
char ch='y',wg,dg;

for(i=0;i<n;i++)
for(j=0;j<n;j++)
mt[i][j]=0;

for(i=0;i<n;i++)
{
v1[i].dist=99;
v1[i].visit=0;
v1[i].vi=-1;
v1[i].num=i+1;
}
cout<<"weight graph or not & directed or not";
cin>>wg>>dg;
while(ch=='y')
{
cout<<"enter 1st,2nd vertex";
cin>>a>>b;
if(wg=='w')
{
cout<<"weight";
cin>>w;
mt[a-1][b-1]=w;
}
else mt[a-1][b-1]=1;
if(dg!='d')
mt[b-1][a-1]=mt[a-1][b-1];
cout<<"again ";
cin>>ch;
}
}
node* eular(node *q,int a)
{
int i,m=1,z=0,u=0; node *s,*t,*p=NULL;
while(m==1 && z<20)
{m=0;z++;
for(i=0;i<n;i++)
     if(edge[a-1][i]!=0)
      {m=1;
      edge[a-1][i]=0;
      edge[i][a-1]=0;
      a=i+1;
      if(p==NULL)
      {
      p=new(node);
      p->key=a;
      p->next=NULL;
      s=p;
      }
      else
      {
      t=new(node);
 t->key=a;
 t->next=NULL;
 p->next=t;
 p=t;
      }
      break;
      }
}
p->next=q->next;
q->next=s;
p=q;z=0;
while(p!=NULL && z<20)
{z++;
for(i=0;i<n;i++)
if(edge[p->key-1][i]!=0)
{
if(z!=1)
p=eular(p,p->key);
else
u=1;
break;
}
p=p->next;
}
if(u==1)
{
p=q;
while(p->next!=NULL)
p=p->next;
t=new(node);
t->next=NULL;
if(p->key==ov[0])
t->key=ov[1];
else
t->key=ov[0];
p->next=t;
}
return q;
}
int main()
{
vertex v[7]; node *p=NULL,*q=NULL;
int i,j,a,v1[7],z,u=0;
cout<<"enter no. of vertex";
cin>>n;
gcreate(v,edge,n);
for(i=0;i<n;i++)
{z=0;
for(j=0;j<n;j++)
{
cout<<edge[i][j]<<" ";
if(edge[i][j]!=0)
z++;
}
if(z%2==1)
ov[u++];
cout<<endl;
}
cout<<"enter vertex for starting for eular path\n";
cout<<"if odd degree vertex is there start from that vertex ";
cin>>a;
cout<<"eular path";
p=new(node);
p->key=a;
p->next=NULL;
p=eular(p,a);
while(p!=NULL)
{
cout<<p->key<<" ";
p=p->next;
}
return 0;
}

No comments:

Post a Comment

Contributors

Translate