-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfastcci_subcatstats.cc
122 lines (99 loc) · 2.35 KB
/
fastcci_subcatstats.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <stdlib.h>
#if !defined(__APPLE__)
#include <malloc.h>
#endif
#include <string.h>
#include "fastcci.h"
// the graph
int *cat;
tree_type *tree;
// depth buffer
int *dbuf;
int depth(int v) {
int w, below = 0, belowsub;
// lock current category
dbuf[v] = -2;
// Iterate over subcategories
int c = cat[v], cend = tree[c];
c += 2;
while (c < cend) {
w = tree[c];
if (dbuf[w] >= 0)
{
// Successor w has been visited (use the previously computed value)
belowsub = 1 + dbuf[w];
}
else if (dbuf[w] == -2)
{
// loop (hitting the current parent path of locked categories)
belowsub = 0;
}
else
{
// Successor w has not yet been visited; recurse on it
belowsub = 1 + depth(w);
}
// remember deepest branch
if (belowsub > below)
below = belowsub;
c++;
}
dbuf[v] = below;
return below;
}
int main() {
int maxcat = readFile("../fastcci.cat", cat);
maxcat /= sizeof(int);
dbuf = (int*)malloc(maxcat * sizeof(*dbuf));
memset(dbuf, 255, maxcat * sizeof(*dbuf));
readFile("../fastcci.tree", tree);
// generate depth buffer
for (int v=0; v<maxcat; ++v)
if (cat[v]>-1 && dbuf[v]==-1)
depth(v);
// find maximum depth
int maxdepth = 0;
for (int v=0; v<maxcat; ++v)
if (cat[v]>-1 && dbuf[v]>maxdepth)
maxdepth = dbuf[v];
printf("maxdepth = %d\n", maxdepth);
// output histogram
int *hist = (int*)malloc((maxdepth+1) * sizeof(int));
memset(hist, 0, (maxdepth+1) * sizeof(*hist));
for (int v=0; v<maxcat; ++v)
if (cat[v]>-1)
hist[dbuf[v]]++;
for (int i=0; i<=maxdepth; ++i)
printf("%d %d\n", i, hist[i]);
// output maximum path(s)
for (int v=0; v<maxcat; ++v)
if (cat[v]>-1 && dbuf[v]==maxdepth)
{
int needdepth = maxdepth, w = v;
printf("%d",v);
while(needdepth>0)
{
needdepth--;
// Iterate over subcategories
bool match = false;
int c = cat[w], cend = tree[c];
c += 2;
while (c<cend) {
if (dbuf[tree[c]] == needdepth) {
w = tree[c];
printf("|%d", w);
match = true;
break;
}
c++;
}
if (!match) printf("|x");
}
printf("\n");
}
free(cat);
free(tree);
free(dbuf);
return 0;
}