#include <limits.h>
#include <math.h>
#include "diam.h"
double treedist(int N, edge** A, int* D, int i, int p)
{
int j, n=0, t;
double m, M = -HUGE_VAL;
for (j=0; j<D[i]; j++) n += A[i][j].intree;
if (n == 1 && p != -1) {
M = 0.0; /* This is a leaf, but not the starting point */
} else {
for (j=0; j<D[i]; j++)
if (A[i][j].intree) {
t = A[i][j].dest;
if (t != p) { /* Do not walk back on our tracks */
#ifdef WEIGHTED
m = A[i][j].d + treedist(N, A, D, t, i);
#else
m = 1.0 + treedist(N, A, D, t, i);
#endif
if (m > M) M = m;
}
}
}
return M;
}