-
Notifications
You must be signed in to change notification settings - Fork 3
/
nn.c
190 lines (158 loc) · 3.2 KB
/
nn.c
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int n;
int i,j,k;
int th;
int actual_op=0,insertion_row=0;
FILE *fp;
fp=fopen("/home/jay/jaycprogram/nnio","r");
char ch[10];
fscanf(fp,"%s",ch);
n=atoi(ch);
// printf("enter the number of elements:\n");
// scanf("%d",&n);
fscanf(fp,"%s",ch);
th=atoi(ch);
// printf("enter the threshold value:");
// scanf("%d",&th);
int elements[n];
// printf("enter the elements:");
for(i=0;i<n;i++)
{
fscanf(fp,"%s",ch);
elements[i]=atoi(ch);
//scanf("%d",&elements[i]);
}
int distance[n][n];
//printf("enter the distance rowwise:");
for(i=0;i<n;i++)
{fscanf(fp,"%s",ch);// neglecting 1st i/p of the row
for(j=0;j<n;j++)
{
fscanf(fp,"%s",ch);
distance[i][j]=atoi(ch);
//scanf("%d",&distance[i][j]);
}
}
int clusters[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
clusters[i][j]=0;
}
}
/*
clusters is a nXn array keeping in mind that the worst case can be tht each element is in its owm cluster.
fisrt element is issigned to first cluster(i.e first row).
ele_dist is an array tht stores the distances of an element that is to be assigned to a cluster with the elements tht already hv been.
smallest_dist=smallest of ele_dist.
ecount=pointer to elements.
edcount=pointer to ele_dist
*/
clusters[0][0]=elements[0];
int c=n;
int operands[n];
int op=0;
int ele_dist[c];
for(i=0;i<c;i++)
ele_dist[i]=0;
int smallest_dist=0;
int a,b;
int apos=0;int bpos=0;
int elem;
int ecount=1;
int edcount=0;
for(ecount=1;ecount<n;ecount++)
{
edcount=op=actual_op=smallest_dist=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a=b=apos=bpos=elem=0;
if(clusters[i][j]!=0)
{
a=clusters[i][j];
b=elements[ecount];
for(elem=0;elem<n;elem++)
{
if(elements[elem]==a)
break;
apos++;
}
for(elem=0;elem<n;elem++)
{
if(elements[elem]==b)
break;
bpos++;
}
ele_dist[edcount]=distance[apos][bpos];
operands[op]=a;
op++;
edcount++;
}
}
}
smallest_dist=ele_dist[0];
actual_op=operands[0];
//for(i=1;i<ele_dist.length;i++)
//for(i=1;i<c;i++)
for(i=1;i<c;i++)
{
if(ele_dist[i]<smallest_dist && ele_dist[i]!=0)
{
smallest_dist=ele_dist[i];
actual_op=operands[i];
}
}
//System.out.println("***************"+smallest_dist+","+actual_op);
if(smallest_dist<=th)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(clusters[i][j]==actual_op)
{
insertion_row=i;
break;
}
}
}
for(k=0;k<n;k++)
{
if(clusters[insertion_row][k]==0)
{
clusters[insertion_row][k]=elements[ecount];
break;
}
}
}
else
{
for(i=1;i<n;i++)
{
j=0;
if(clusters[i][j]==0)
{
clusters[i][j]=elements[ecount];
break;
}
}
}
}//elements
printf("Clusters:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(clusters[i][j]!=0)
printf("%d %d %d \t",i,j,clusters[i][j]);
}
printf("\n");
}
}