Subversion Repositories wimsdev

Rev

Rev 13587 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13103 bpr 1
pari_header=est_connexe(a)=my(n=#a,b=(1.+a)^(n-1));for(x=1,n,if(b[1,x]==0,return(0)));1\
2
/* graphe_connexe */\
3
graphe_connexe(n)={\
4
  my(a=matrix(n,n),i,j);\
5
  while(!est_connexe(a),i=1+random(n);j=1+random(n);if(i!=j,a[i,j]=a[j,i]=1));\
6
  a\
7
};\
8
\
9
/* M est la matrice d'un graphe connexe */\
10
graphe_eulerien(M)={\
11
  my(x,y,z,a=M,n=#a);\
12
  my(impdeg=vector(n,i,sum(j=1,n,a[i,j])%2));\
13
  while (sum(i=1,n,impdeg[i]),[x,y]=select(x->x,impdeg,1);\
14
    if (a[x,y],\
15
      z=1+(x==1)+(y==2);\
16
      if(a[x,z] && a[y,z], a[x,y]=a[y,x]=0,\
17
        a[x,z]=a[z,x]=1-a[x,z];a[y,z]=a[z,y]=1-a[y,z]),\
18
      a[x,y]=a[y,x]=1);\
19
      impdeg[x]=impdeg[y]=0\
20
  );\
21
  a\
22
};\
23
\
24
est_eulerien(a)=est_connexe(a)&&!vector(#a,i,sum(j=1,#a,a[i,j])%2)\
25
\
26
/* a  est la matrice d'un graphe eulerien trouve un cycle eulerien sur a */\
27
\
28
cycle_eulerien(a)={\
29
  my(n=#a,deg=vector(n,i,sum(j=1,n,a[i,j])),m=sum(i=1,n,deg[i])/2);\
30
  my(cdeg=vector(n),cyc=vector(m+1),c,x,x0,nb);\
31
  cyc[1]=nb=1;\
32
  while(nb <= m,\
33
   [c]=select(x->x&&cdeg[x]<deg[x],cyc,1);\
34
   x0 = x = cyc[c];\
35
   until (x==x0,\
36
     nb++; cdeg[x]++; y = 1; while (!a[x,y] || deg[y]==cdeg[y], y++);\
37
     cdeg[y]++; forstep (i=nb,c+1,-1,cyc[i]=cyc[i-1]);\
38
     a[x,y]--;a[y,x]--;\
39
     cyc[c]=y; x = y);\
40
  );\
41
  cyc\
42
};\
43