| 1 |
/* knbhd - Return the k-neighborhood of a node, i.e., allnodes |
| 2 |
* whose path length from the given node is <= k. |
| 3 |
* ARGV[] = k node_name |
| 4 |
*/ |
| 5 |
BEG_G { |
| 6 |
node_t ctr; |
| 7 |
int maxlen; |
| 8 |
|
| 9 |
graph_t comp = subg($, "kcomp"); |
| 10 |
int sid = 0, eid = 0; |
| 11 |
int curlen; |
| 12 |
node_t curnode; |
| 13 |
int nlen[node_t]; |
| 14 |
node_t stk[int]; |
| 15 |
node_t other; |
| 16 |
edge_t e; |
| 17 |
|
| 18 |
if (ARGC != 2) { |
| 19 |
printf (2, "Two arguments required\n"); |
| 20 |
exit(1); |
| 21 |
} |
| 22 |
|
| 23 |
if (!sscanf(ARGV[0],"%d",&maxlen)) { |
| 24 |
printf (2, "Improper length parameter \"%s\"\n", ARGV[0]); |
| 25 |
exit(1); |
| 26 |
} |
| 27 |
maxlen++; /* length of 0 means unset */ |
| 28 |
|
| 29 |
ctr = isNode ($, ARGV[1]); |
| 30 |
if (!ctr) { |
| 31 |
printf (2, "node %s not found\n", ARGV[1]); |
| 32 |
exit(1); |
| 33 |
} |
| 34 |
|
| 35 |
subnode (comp,ctr); |
| 36 |
nlen[ctr] = 1; |
| 37 |
curnode = ctr; |
| 38 |
while (curnode) { |
| 39 |
curlen = nlen[curnode]; |
| 40 |
if (curlen == maxlen) break; |
| 41 |
|
| 42 |
for (e = fstedge(curnode); e; e = nxtedge(e,curnode)) { |
| 43 |
other = e.head; |
| 44 |
if (other == curnode) other = e.tail; |
| 45 |
if (nlen[other]) continue; /* already seen */ |
| 46 |
subnode(comp,other); |
| 47 |
nlen[other] = curlen+1; |
| 48 |
stk[eid++] = other; |
| 49 |
} |
| 50 |
|
| 51 |
if (sid < eid) curnode = stk[sid++]; |
| 52 |
else curnode = NULL; |
| 53 |
} |
| 54 |
|
| 55 |
induce(comp); |
| 56 |
write(comp); |
| 57 |
} |