long
digoncount(graph *g, int m, int n) /* Number of digons (cycles of length 2). Useful for digraphs. */
{ int i,j;
set *gi;
setword w; long ans;
ans = 0;
if (m == 1)
{ for (i = 0; i < n; ++i)
{
w = g[i] & BITMASK(i); while (w)
{
TAKEBIT(j,w); if ((g[j] & bit[i])) ++ans;
}
}
} else
{ for (i = 0, gi = g; i < n; ++i, gi += m)
{ for (j = i; (j = nextelement(gi,m,j)) > 0; ) if (ISELEMENT(g+j*m,i)) ++ans;
}
}
long
pathcount1(graph *g, int start, setword body, setword last) /* Number of paths in g starting at start, lying within body and
ending in last. {start} and last should be disjoint subsets of body. */
{ long count;
setword gs,w; int i;
long
cyclecount(graph *g, int m, int n) /* The total number of cycles in g (assumed no loops) */
{ if (n == 0) return 0; if (m == 1) return cyclecount1(g,n);
gt_abort(">E cycle counting is only implemented for n <= WORDSIZE\n"); return 0;
}
long
indpathcount1(graph *g, int start, setword body, setword last) /* Number of induced paths in g starting at start, extravertices within * body and ending in last.
* {start}, body and last should be disjoint. */
{ long count;
setword gs,w; int i;
long
indcyclecount1(graph *g, int n) /* The total number of induced cycles in g (assumed no loops), m=1 only */
{
setword body,last,cni; long total; int i,j;
body = ALLMASK(n);
total = 0;
for (i = 0; i < n-2; ++i)
{
body ^= bit[i];
last = g[i] & body;
cni = g[i] | bit[i]; while (last)
{
TAKEBIT(j,last);
total += indpathcount1(g,j,body&~cni,last);
}
}
long
indcyclecount(graph *g, int m, int n) /* The total number of induced cycles in g (assumed no loops) */
{ if (n == 0) return 0; if (m == 1) return indcyclecount1(g,n);
gt_abort( ">E induced cycle counting is only implemented for n <= WORDSIZE\n"); return 0;
}
long
numtriangles(graph *g, int m, int n) /* The number of triangles in g; undirected only */
{ int i,j,k,kw;
setword *gi,*gj,w; long total;
if (m == 1) return numtriangles1(g,n);
total = 0; for (i = 0, gi = g; i < n-2; ++i, gi += m) for (j = i; (j = nextelement(gi,m,j)) > 0; )
{
gj = GRAPHROW(g,j,m);
kw = SETWD(j);
w = gi[kw] & gj[kw] & BITMASK(SETBT(j)); if (w) total += POPCOUNT(w); for (k = kw+1; k < m; ++k)
{
w = gi[k] & gj[k];
total += POPCOUNT(w);
}
}
long
numdirtriangles1(graph *g, int n)
{ long total; int i,j,k;
setword biti,bm,wi,wj;
total = 0; for (i = 0; i < n; ++i)
{
biti = bit[i];
bm = BITMASK(i);
wi = g[i] & bm; while (wi)
{
TAKEBIT(j,wi);
wj = g[j] & bm; while (wj)
{
TAKEBIT(k,wj); if ((g[k] & biti)) ++total;
}
}
}
long
numdirtriangles(graph *g, int m, int n) /* The number of directed triangles in g */
{ long total; int i,j,k;
set *gi,*gj;
if (m == 1) return numdirtriangles1(g,n);
total = 0; for (i = 0, gi = g; i < n-2; ++i, gi += m) for (j = i; (j = nextelement(gi,m,j)) >= 0;)
{
gj = GRAPHROW(g,j,m); for (k = i; (k = nextelement(gj,m,k)) >= 0; ) if (k != j && ISELEMENT(GRAPHROW(g,k,m),i)) ++total;
}
void
commonnbrs(graph *g, int *minadj, int *maxadj, int *minnon, int *maxnon, int m, int n) /* Count the common neighbours of pairs of vertices, and give the minimum and maximum for adjacent and non-adjacent vertices. Undirected only. Null minimums are n+1 and null maximums are -1.
*/
{ int j,k; int mina,maxa,minn,maxn; int cn;
set *gi,*gj;
setword w;
for (j = 0, gj = g; j < n; ++j, gj += m) for (gi = g; gi != gj; gi += m)
{
cn = 0; for (k = 0; k < m; ++k)
{
w = gi[k] & gj[k]; if (w) cn += POPCOUNT(w);
}
if (ISELEMENT(gi,j))
{ if (cn < mina) mina = cn; if (cn > maxa) maxa = cn;
} else
{ if (cn < minn) minn = cn; if (cn > maxn) maxn = cn;
}
}
void
contract1(graph *g, graph *h, int v, int w, int n) /* Contract distinct vertices v and w (not necessarily adjacent)
with result in h. No loops are created. */
{ int x,y;
setword bitx,bity,mask1,mask2; int i;
if (w < v)
{
x = w;
y = v;
} else
{
x = v;
y = w;
}
int
conncontent(graph *g, int m, int n) /* number of connected spanning subgraphs with an even number
of edges minus the number with an odd number of edges */
{
graph h[WORDSIZE];
setword gj; int i,j,v1,v2,x,y; int minv,mindeg,deg,goodv; long ne;
if (m > 1) ABORT("conncontent only implemented for m=1");
/* First handle tiny graphs */
if (n <= 3)
{ if (n == 1) return 1; if (n == 2) return (g[0] ? -1 : 0); if (!g[0] || !g[1] || !g[2]) return 0; /* disconnected */ if (g[0]^g[1]^g[2]) return 1; /* path */ return 2; /* triangle */
}
/* Now compute ne = number of edges mindeg = minimum degree minv = a vertex of minimum degree goodv = a vertex with a clique neighbourhood (-1 if none)
*/
mindeg = n;
ne = 0;
goodv = -1; for (j = 0; j < n; ++j)
{
gj = g[j];
deg = POPCOUNT(gj);
ne += deg; if (deg < mindeg)
{
mindeg = deg;
minv = j; if (deg == 1) goodv = j;
} if (deg >= 3 && deg <= 4 && goodv < 0)
{ while (gj)
{
TAKEBIT(i,gj); if (gj & ~g[i]) break;
} if (!gj) goodv = j;
}
}
ne /= 2;
/* Cases of isolated vertex or tree */
if (mindeg == 0) return 0;
#if 0 if (mindeg == 1 && ne == n-1)
{ if (isconnected1(g,n)) return ((n&1) ? 1 : -1); elsereturn 0;
} #endif
/* Cases of clique and near-clique */
if (mindeg == n-1)
{
j = -1; for (i = 2; i < n; ++i) j *= -i; return j;
}
if (mindeg == n-2 && n < 16)
{ if (!knm_computed)
{
knm_computed = TRUE;
knm[1][0] = 1; for (i = 2; i < 16; ++i)
{
knm[i][0] = -knm[i-1][0] * (i-1); for (j = 1; j+j <= i; ++j)
knm[i][j] = knm[i][j-1] + knm[i-1][j-1];
}
} return knm[n][(n*n-n)/2-ne];
}
boolean
stronglyconnected(graph *g, int m, int n) /* test if digraph g is strongly connected */
{ int sp,v,vc; int numvis;
set *gv; #if MAXN int num[MAXN],lowlink[MAXN],stack[MAXN]; #else
DYNALLSTAT(int,num,num_sz);
DYNALLSTAT(int,lowlink,lowlink_sz);
DYNALLSTAT(int,stack,stack_sz); #endif
long
numsquares(graph *g, int m, int n) /* Number of 4-cycles. Undirected graphs only, loops ok. */
{
setword w,bitij; int i,j,k;
graph *gi,*gj; unsignedlong total,t;
boolean iloop,jloop;
total = 0;
if (m == 1)
{ for (j = 1; j < n; ++j) for (i = 0; i < j; ++i)
{
w = (g[i] & g[j]) & ~(bit[i] | bit[j]);
t = POPCOUNT(w);
total += t*(t-1)/2;
} return (long)(total / 2);
} else
{ for (j = 1, gj = g + m; j < n; ++j, gj += m)
{
jloop = ISELEMENT(gj,j); if (jloop) DELELEMENT(gj,j); for (i = 0, gi = g; i < j; ++i, gi += m)
{
iloop = ISELEMENT(gi,i); if (iloop) DELELEMENT(gi,i);
t = 0; for (k = 0; k < m; ++k)
t += POPCOUNT(gi[k]&gj[k]);
total += t*(t-1)/2; if (iloop) ADDELEMENT(gi,i);
} if (jloop) ADDELEMENT(gj,j);
} return (long)(total / 2);
}
}
long
numdiamonds(graph *g, int m, int n) /* Number of diamonds (squares with diagonal). Undirected only, no loops. */
{ int i,j,k;
setword w; long ans,c;
set *gi,*gj;
ans = 0;
if (m == 1)
{ for (i = 0; i < n; ++i)
{
w = g[i] & BITMASK(i); while (w)
{
TAKEBIT(j,w);
c = POPCOUNT(g[i]&g[j]);
ans += c*(c-1)/2;
}
}
} else
{ for (i = 0, gi = g; i < n; ++i, gi += m)
{ for (j = i; (j = nextelement(gi,m,j)) >= 0; )
{
gj = g + m*j;
c = 0; for (k = 0; k < m; ++k) c += POPCOUNT(gi[k]&gj[k]);
ans += c*(c-1)/2;
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.