Esempio n. 1
0
        /// <summary>
        /// The method for finding the min span tree
        /// </summary>
        private List <Edge> FindMinSpanTree(NodesWithEdges inputData)
        {
            var result = new List <Edge>();

            // the forest contains all visited nodes
            // List<Node> forest = new List<Node>();

            foreach (var node in inputData.Nodes)
            {
                node.Cluster = new Cluster {
                    Label = node.Label
                };
                node.Cluster.AddNode(node);
            }

            var clusters = inputData.Nodes
                           .Select(p => p.Cluster)
                           .Distinct()
                           .ToList();

            // sort the edges by their length
            inputData.Edges.Sort();

            foreach (Edge currentEdge in inputData.Edges)
            {
                if (clusters.Count == 1)
                {
                    break;
                }

                Cluster cluster1 = currentEdge.FirstNode.Cluster;
                Cluster cluster2 = currentEdge.SecondNode.Cluster;

                if (cluster1.Label != cluster2.Label)
                {
                    result.Add(currentEdge);

                    // add the length to the total cost
                    // totalCost += currentEdge.Length;

                    currentEdge.Visited            = true;
                    currentEdge.FirstNode.Visited  = true;
                    currentEdge.SecondNode.Visited = true;

                    // merge two cluster and make a single one of them
                    List <Node> nodeList = cluster2.GetNodeList();
                    foreach (Node n in nodeList)
                    {
                        cluster1.AddNode(n);
                        n.Cluster = cluster1;
                    }

                    clusters.Remove(cluster2);
                }
            }

            return(result);
            // return totalCost;
        }
        /// <summary>
        /// The method for finding the min span tree
        /// </summary>
        private List<Edge> FindMinSpanTree(NodesWithEdges inputData)
        {
            var result = new List<Edge>();

            // the forest contains all visited nodes
            // List<Node> forest = new List<Node>();

            foreach (var node in inputData.Nodes)
            {
                node.Cluster = new Cluster { Label = node.Label };
                node.Cluster.AddNode(node);
            }

            var clusters = inputData.Nodes
                .Select(p => p.Cluster)
                .Distinct()
                .ToList();

            // sort the edges by their length
            inputData.Edges.Sort();

            foreach (Edge currentEdge in inputData.Edges)
            {
                if (clusters.Count == 1)
                    break;

                Cluster cluster1 = currentEdge.FirstNode.Cluster;
                Cluster cluster2 = currentEdge.SecondNode.Cluster;

                if (cluster1.Label != cluster2.Label)
                {
                    result.Add(currentEdge);

                    // add the length to the total cost
                    // totalCost += currentEdge.Length;

                    currentEdge.Visited = true;
                    currentEdge.FirstNode.Visited = true;
                    currentEdge.SecondNode.Visited = true;

                    // merge two cluster and make a single one of them
                    List<Node> nodeList = cluster2.GetNodeList();
                    foreach (Node n in nodeList)
                    {
                        cluster1.AddNode(n);
                        n.Cluster = cluster1;
                    }

                    clusters.Remove(cluster2);
                }
            }

            return result;
            // return totalCost;
        }
 public static void SaveToFile(string filePath, NodesWithEdges nodesWithEdges)
 {
     string json = JsonConvert.SerializeObject(nodesWithEdges, JsonSerializerSettings);
     File.WriteAllText(filePath, json);
 }