Dear Sir/Madam/Friends,

Sir, i have written code for first step of euclidean distance . now i need to merge the smallest distance points as single point. The class which i have written is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;


/// <summary>
/// Summary description for CreatingDistanceMatrix
/// </summary>
public class CreatingDistanceMatrix
{
DataSet ds;
DataTable dtsource;
public ArrayList p1, p2;
public DataTable dt;
public double smallval;
public CreatingDistanceMatrix(DataSet ds)
{this.ds = ds;
dtsource = ds.Tables[0];
CrateDisMat();
GetPair();
}


private void GetPair()
{
p1 = new ArrayList();
p2 = new ArrayList();
p1.Clear();
p2.Clear();
double temp1 = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = i; j < dt.Columns.Count; j++)
{
temp1 = Convert.ToDouble(dt.Rows[j]);
if (temp1 == smallval)
{
p1.Add(i);
p2.Add(j);
}
}
}
}


private void CrateDisMat()
{
dt = new DataTable();
smallval = 0;
double temp, temp1;
bool flage = true;
double sum = 0;


for (int i = 0; i < dtsource.Rows.Count; i++)
{
DataColumn dc = new DataColumn(i.ToString(), typeof(double));
dt.Columns.Add(dc);
}


for (int i = 0; i < dtsource.Rows.Count; i++)
{
//DataRow dr = new DataRow(i);
dt.Rows.Add(i);
}


for (int i = 0; i < dtsource.Rows.Count; i++)
{
for (int j = 0; j < dtsource.Rows.Count; j++)
{
//sum = 0;
for (int k = 0; k < dtsource.Columns.Count; k++)
{
temp = Convert.ToDouble(dtsource.Rows[k]) - Convert.ToDouble(dtsource.Rows[j][k]);
temp = temp * temp;
sum = sum + temp;
}


temp1 = Math.Sqrt(sum);
if (flage && temp1 != 0)
{
smallval = temp1;
flage = false;
}
if (temp1 != 0 && smallval > temp1 && !flage)
{
smallval = temp1;
}
sum = 0;


dt.Rows[j] = temp1;


}
}
}
}

Out put of my code i have given in attachfiles now i need to merge points (0,1) as single point and in the second step (2,3) as single point. From the new point again i need to calculate Distance.

Sir, Please Kindly help me out that how i can merge two datapoints as single point.

1   2   3   4   5   6
1   0   1.4142  13.4536 14.8660 25  28.6530
2   1.4142  0   12.0415 13.4536 23.6008 27.2946
3   13.4536 12.0415 0   1.4142  11.6619 15.8113
4   14.8660 13.4536 1.4142  0   10.2956 14.5602
5   25  23.6008 11.6619 10.2956 0   5.0990
6   28.6530 27.2946 15.8113 14.5602 5.0990  0

Data set

ID  A   B
1   5   5
2   6   6
3   15  14
4   16  15
5   25  20
6   30  19

First step Euclidean Distance.

After merging (1,2) and (3,4) the resultant table looks like this

(1,2)   (3,4)   5   6
(1,2)   0   13.4536 24.3004 27.9735
(3,4)   13.4536 0   10.9787 15.1857
5   24.3004 10.9787 0   5.0990
6   27.9735 15.1857 5.0990  0

So please help me out that how to merge two points as single point.

Why are you posting twice?
Why are you not using code tags?
What exactly do you mean by "merging" points?

Dear Sir,
First i apologise for posting two times as my internet connection was slow i could not confirm that whether my post has been submitted or not. Henceforth, i will not committ this mistake sir. Sir, merging two points in the sense that if my smallest distance between the points 1 and 2 is 1.4142. Now, i have to take point 1 and 2 as single point as (1,2) as mentioned below (still i if want to explain consider points 1 and 2 as single point in the form(1,2)
(1,2) (3,4) 5 6

(1,2) 0 13.4536 24.3004 27.9735
(3,4) 13.4536 0 10.9787 15.1857
5 24.3004 10.9787 0 5.0990
6 27.9735 15.1857 5.0990 0

Ok so if the two points are less than a distance 1.4142 (guess it is the square root of 2) apart in the euclidian plane, you "merge" them. Would you take the first point the second point or the mid distance between the two points? Please describe your "merging" process, because I still don't get it.
EDIT: And if you post any more code put it between code tags, read the member rules. Most members here tend to not read your posted code if it is not between tags. :)

Dear Sir,

Merging of two points in the sense that, just considering the points which are minimum distance as single point in the first step of the calculation. suppose point 1 and 2 having minimum distance so these two points as single point. Then now we will calculate the distance between rest of the points with (1,2).
Eg.
let, D(1,2) is distance between 1 and 2.
now distance between D(1,2) and point 3 = D(1,2).Point3
= (D(1,3)+D(2,3))/2
And this iteration will be continued until we reach the single point.

Likewise the points will be merged until we reach the single value.

Need a little bit more before I can offer a solution :) Let's say we have 4 points located at (0,0), (0,1), (5,5) and (5,7) {Points 1 through 4, respectively}.

First time we'd find that Points 1 & 2 are closest, so we 'merge' them into Point (1,2). Next we'd find that Points 3 & 4 are the closest, so we 'merge' them into Point (3,4). Now we just have to merge Points (1,2) and (3,4). Is this done by:
(D(1,2)+D(1,3)+D(1,4)+D(2,3)+D(2,4)+D(3,4))/6? If not, how is it done?

Now we are given points at (0,0), (0,1), (0,2) and (0,5). Do we combine 1 & 2, and combine 2 & 3 since they are the same distance apart? Or do we just combine 1 & 2, then next time add in 3, then 4? If so, how does the equation for that look?

Dear Sir,

First time we'd find that Points 1 & 2 are closest, so we 'merge' them into Point (1,2). Next we'd find that Points 3 & 4 are the closest, so we 'merge' them into Point (3,4). Now we just have to merge Points (1,2) and (3,4). Is this done by:
(D(1,2)+D(1,3)+D(1,4)+D(2,3)+D(2,4)+D(3,4))/6? If not, how is it done?

Sir, Exactly this is to be done for the further merging of points. I need solution for this. Thank you for understanding my query.

Lastly, and I should have asked this before, do I need to show each step, or can I do it all at once?

Sir, I need some idea that how i can take points which are smallest distance as single point. sir, no need of each step. In my software development project for my M.Sc, just need to show only the result not all the steps. I need to show only final solution.

Example datset:

Subject ID A B
S1 5 5
S2 6 6
S3 15 14
S4 16 15
S5 25 20
S6 30 19

Clusters Joining at Distance No. of Members
s2 s1 1.4142 2
s4 s3 1.4142 2
s6 s5 5.0990 2
s4 s6 13.0822 4
s2 s4 19.7952 6

This should be the result of the problem

Sir, I am new to Computer programming. It would be better if you show the steps clearly. I will be grateful to you sir.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.