public static int FindIndex(Graph.HyperGraph inGraph, int nodeID) { //foreach(var c in clusters) //{ // foreach(var n in c.SingleNodes) // { // if (n == nodeID) // return clusters.IndexOf(c); // } //} //return -1; Graph.SingleNode v = inGraph.FindNode(nodeID); return(v.ClusterID); }
public Graph.HyperGraph BuildEdgesOfGraph(Graph.HyperGraph inGraph, int k1, int k2, double[] sigma, int radius, string outputGraphFilename) { //start to include the edges for (int i = 0; i < inGraph.NumModality; i++) { List <Graph.SingleNode> mNodes = inGraph.Nodes[i]; if (mNodes.Count() > 0) { //there are two types of edges //first type: intra-link type = 0 int nodeCount = mNodes.Count(); int nodeDim = mNodes[0].Data.Count(); double[,] inputData = new double[nodeCount, nodeDim]; for (int n = 0; n < nodeCount; n++) { if (mNodes[n].Data.Count() > 1) { for (int d = 0; d < nodeDim; d++) { inputData[n, d] = mNodes[n].Data[d]; } } } int[] tags = new int[nodeCount]; for (int t = 0; t < nodeCount; t++) { tags[t] = t; } //build the k-d tree alglib.kdtree kdt; alglib.kdtreebuildtagged(inputData, tags, nodeDim, 0, 2, out kdt); //find k-nn int countNode = 0; Dictionary <Preprocess.Pair <int, int>, double> mutual = new Dictionary <Preprocess.Pair <int, int>, double>(); foreach (var node in inGraph.Nodes[i]) { Console.WriteLine(i + "th modality: " + countNode++ + "th node"); if (node.Data.Count() == 1) { break; } double[] x = new double[nodeDim]; for (int j = 0; j < nodeDim; j++) { x[j] = node.Data[j]; } int k = alglib.kdtreequeryknn(kdt, x, k1); double[,] result = new double[0, 0]; alglib.kdtreequeryresultsx(kdt, ref result); double[] distances = new double[0]; alglib.kdtreequeryresultsdistances(kdt, ref distances); int[] index = new int[0]; alglib.kdtreequeryresultstags(kdt, ref index); List <int> foundBuffer = new List <int>(); List <double> css = new List <double>(); List <double> euDist = new List <double>(); for (int j = 1; j < k; j++) // j =0 is the data itself do not link itself { //List<double> tmp = new List<double>(); //for (int l = 0; l < nodeDim; l++) //{ // tmp.Add(result[j, l]); //} Graph.SingleNode neighbor = new Graph.SingleNode(); neighbor = mNodes[index[j]]; //foreach (var n in inGraph.Nodes[i]) //{ // bool equal = true; // for (int l = 0; l < nodeDim; l++) // { // if (n.Data[l] != result[j, l]) // { // equal = false; // break; // } // } // if (equal && n.NodeID != node.NodeID && !foundBuffer.Contains(n.NodeID)) // { // neighbor = n; // foundBuffer.Add(n.NodeID); // break; // } //} double weight = Function.CalculateGaussianSimilarity(distances[j], i, sigma); euDist.Add(weight); //css.Add(Function.CalculateGaussianSimilarity(Function.GetCosineSimilarity(node.Data, neighbor.Data), i, sigma)); css.Add(Function.GetCosineSimilarity(node.Data, neighbor.Data)); int nodeID1 = node.NodeID; int nodeID2 = neighbor.NodeID; bool success = false; if (mutual.ContainsKey(new Preprocess.Pair <int, int>(nodeID1, nodeID2))) { mutual[new Preprocess.Pair <int, int>(nodeID1, nodeID2)] = -1; } else if (mutual.ContainsKey(new Preprocess.Pair <int, int>(nodeID2, nodeID1))) { mutual[new Preprocess.Pair <int, int>(nodeID2, nodeID1)] = -1; } else { success = inGraph.SetEdge(node, neighbor, weight, 0); } if (success) { mutual.Add(new Preprocess.Pair <int, int>(nodeID1, nodeID2), weight); } //else //{ // if (mutual.ContainsKey(new Preprocess.Pair<int, int>(nodeID1, nodeID2))) // { // mutual[new Preprocess.Pair<int, int>(nodeID1, nodeID2)] = -1; // } // else // { // mutual[new Preprocess.Pair<int, int>(nodeID2, nodeID1)] = -1; // } //} } } List <Graph.Edge> delEdges = new List <Graph.Edge>(); //mutual knn foreach (var m in mutual) { //if(m.Value == 1) //{ // inGraph.DelEdge(m.Key.Value1, m.Key.Value2); //} if (m.Value > 0) { delEdges.Add(new Graph.Edge(m.Key.Value1, m.Key.Value2, m.Value, 0)); } } inGraph.Edges = inGraph.Edges.Except(delEdges).ToList(); //second type: inter-link type = 1 for (int j = i + 1; j < inGraph.NumModality; j++) { List <Graph.SingleNode> nNodes = inGraph.Nodes[j]; //inGraph.PrintGraph(@"D:\Result\Temporary\Graph\graph.txt"); foreach (var mNode in mNodes) { int countNN = 0; foreach (var nNode in nNodes) { //constraint of day bool dayConstriant = mNode.Day == nNode.Day || mNode.Day == -1 || nNode.Day == -1; if (radius > 0) { if (this.Grid.IsAdjacent(mNode.GridIndex, nNode.GridIndex, radius) && dayConstriant) //DO NOT LINK DIFFERENT DAYS OF A REGION { inGraph.SetEdge(mNode, nNode, radius == 0 ? 1 : 1 / radius, 1); if (countNN >= k2) { break; } countNN++; } } else { if (mNode.GridIndex == nNode.GridIndex && dayConstriant) { inGraph.SetEdge(mNode, nNode, 1, 1); break; } } } } } } } inGraph.PrintGraph(outputGraphFilename); return(inGraph); }