/// <summary> /// Starting run algorithm with specific source topology and K-diameter cut, /// and this round process will generate scope topology to scop_net_topo, /// and will add the deployment node to deployNodes structure. /// Finally, it will return the remain network topology with this process. /// </summary> /// <param name="src_net_topo">The source network topology</param> /// <param name="scope_net_topo">This round process that generte scope topology</param> /// <param name="nowDeployNodes">The result of the deployment node id</param> /// <returns>The remain network topology after process the algorithm.</returns> private NetworkTopology startAlgorithm(NetworkTopology src_net_topo, NetworkTopology scope_net_topo, List <int> nowDeployNodes) { List <int> neighbor = new List <int>(); int selectNode = scope_net_topo.Nodes[0].ID; while (true) { //if (scope_net_topo.Nodes.Count >= 2 * N - upperBoundOfMinDegree) // break; neighbor.Remove(selectNode); neighbor.AddRange(src_net_topo.GetNeighborNodeIDs(selectNode).Except(scope_net_topo.Nodes.Select(n => n.ID))); neighbor = neighbor.Distinct().ToList(); NetworkTopology concentrate_topo = new NetworkTopology(src_net_topo.Nodes); concentrate_topo.AdjacentMatrix = src_net_topo.AdjacentMatrix; concentrate_topo.Nodes.AddRange(src_net_topo.Nodes.Except(scope_net_topo.Nodes.Where(n => n.ID != scope_net_topo.Nodes[0].ID))); concentrate_topo.Edges.AddRange(src_net_topo.Edges.Where(e => !scope_net_topo.Nodes.Exists(n => n.ID == e.Node1 || n.ID == e.Node2))); foreach (int id in neighbor) { concentrate_topo.Edges.Add(new NetworkTopology.Edge() { Node1 = id, Node2 = scope_net_topo.Nodes[0].ID }); } selectNode = -1; List <int> tmp = new List <int>(neighbor); //neighbor.Sort((x, y) => concentrate_topo.ClusteringCoefficient(scope_net_topo, x).CompareTo(concentrate_topo.ClusteringCoefficient(scope_net_topo, y))); for (int i = neighbor.Count - 1; i >= 0; i--) { int max_hop_count = int.MinValue; double maxC = double.MinValue; foreach (int id in tmp) { double tmpC = concentrate_topo.ClusteringCoefficient(scope_net_topo, id); if (tmpC > maxC) { maxC = tmpC; selectNode = id; } } tmp.Remove(selectNode); foreach (var scopeNode in scope_net_topo.Nodes) { int hop_count = scope_net_topo.GetShortestPathCount(scopeNode.ID, selectNode) - 1; if (max_hop_count < hop_count) { max_hop_count = hop_count; } } if (max_hop_count <= K - 1) { break; } else { selectNode = -1; } } //if (scope_net_topo.Nodes.Count < upperBoundOfMinDegree) //{ // int minD = int.MaxValue; // foreach (int id in neighbor) // { // int tmpD = src_net_topo.Nodes.Find(n => n.ID == id).Degree; // if (minD > tmpD) // { // minD = tmpD; // selectNode = id; // } // } //} //else //{ //double maxC = double.MinValue; //foreach (int id in neighbor) //{ // double tmpc = concentrate_topo.ClusteringCoefficeint(id); // if (maxC < tmpc) // { // maxC = tmpc; // selectNode = id; // } //} //if (scope_net_topo.Nodes.Count >= N && maxC < 0.6) // selectNode = -1; //} // if nothing found, break the loop. if (selectNode == -1) { break; } // adding the node to the scope set, and computing the max hop count. else { scope_net_topo.Edges.AddRange(src_net_topo.Edges.Where(e => e.Node1 == selectNode && scope_net_topo.Nodes.Exists(n => n.ID == e.Node2) || e.Node2 == selectNode && scope_net_topo.Nodes.Exists(n => n.ID == e.Node1) )); scope_net_topo.Nodes.Add(src_net_topo.Nodes.Find(n => n.ID == selectNode)); //foreach (var scopeNode in scope_net_topo.Nodes) //{ // int hop_count = scope_net_topo.GetShortestPathCount(scopeNode.ID, selectNode); // if (max_hop_count < hop_count) // max_hop_count = hop_count; //} } } NetworkTopology remain_topo; // Handling the neighbor nodes of each node in the scope network topology. if (selectNode != -1) { neighbor.Remove(selectNode); neighbor.AddRange(src_net_topo.GetNeighborNodeIDs(selectNode).Except(scope_net_topo.Nodes.Select(n => n.ID))); neighbor = neighbor.Distinct().ToList(); } // During above process the tmp list will be deployment nodes, and add to deployNodes list. nowDeployNodes.AddRange(neighbor); // Adding deploy nodes to the scope network topology. foreach (int id in neighbor) { scope_net_topo.Edges.AddRange(src_net_topo.Edges.Where(e => e.Node1 == id && scope_net_topo.Nodes.Exists(n => n.ID == e.Node2) || e.Node2 == id && scope_net_topo.Nodes.Exists(n => n.ID == e.Node1) )); scope_net_topo.Nodes.Add(src_net_topo.Nodes.Find(n => n.ID == id)); } // Computing the complement set between source and scope network topology. remain_topo = src_net_topo - scope_net_topo; // Removing deployment nodes and edges from scope network topology. foreach (int id in neighbor) { scope_net_topo.Nodes.RemoveAll(x => x.ID == id); scope_net_topo.Edges.RemoveAll(x => x.Node1 == id || x.Node2 == id); } return(remain_topo); }
/// <summary> /// Starting run algorithm with specific source topology and K-diameter cut, /// and this round process will generate scope topology to scop_net_topo, /// and will add the deployment node to deployNodes structure. /// Finally, it will return the remain network topology with this process. /// </summary> /// <param name="src_net_topo">The source network topology</param> /// <param name="scope_net_topo">This round process that generte scope topology</param> /// <param name="nowDeployNodes">The result of the deployment node id</param> /// <returns>The remain network topology after process the algorithm.</returns> private NetworkTopology startAlgorithm(NetworkTopology src_net_topo, NetworkTopology scope_net_topo, List <int> nowDeployNodes) { List <NeighborIDWithMaxHopCount> neighbor = new List <NeighborIDWithMaxHopCount>(); int selectNode = scope_net_topo.Nodes[0].ID; int now_hop_count = 0; while (true) { neighbor.RemoveAll(n => n.NodeID == selectNode); foreach (int id in src_net_topo.GetNeighborNodeIDs(selectNode).Except(scope_net_topo.Nodes.Select(n => n.ID))) { neighbor.Add(new NeighborIDWithMaxHopCount() { NodeID = id, HopCount = int.MinValue }); } //neighbor.AddRange(src_net_topo.GetNeighborNodeIDs(selectNode).Except(scope_net_topo.Nodes.Select(n => n.ID))); neighbor = neighbor.GroupBy(n => n.NodeID).Select(n => n.First()).ToList(); NetworkTopology concentrate_topo = new NetworkTopology(src_net_topo.Nodes); concentrate_topo.AdjacentMatrix = src_net_topo.AdjacentMatrix; concentrate_topo.Nodes.AddRange(src_net_topo.Nodes.Except(scope_net_topo.Nodes.Where(n => n.ID != scope_net_topo.Nodes[0].ID))); concentrate_topo.Edges.AddRange(src_net_topo.Edges.Where(e => !scope_net_topo.Nodes.Exists(n => n.ID == e.Node1 || n.ID == e.Node2))); foreach (int id in neighbor.Select(n => n.NodeID)) { concentrate_topo.Edges.Add(new NetworkTopology.Edge() { Node1 = id, Node2 = scope_net_topo.Nodes[0].ID }); } selectNode = -1; //neighbor.Sort((x, y) => concentrate_topo.ClusteringCoefficient(x).CompareTo(concentrate_topo.ClusteringCoefficient(y))); if (now_hop_count < K / 2) { Dictionary <int, double> tmp = new Dictionary <int, double>(); foreach (int id in neighbor.Where(n => n.HopCount == int.MinValue).Select(n => n.NodeID)) { tmp.Add(id, concentrate_topo.ClusteringCoefficient(id)); } for (int i = 0; i < tmp.Count;) { int max_hop_count = int.MinValue; selectNode = tmp.Aggregate((kvp1, kvp2) => kvp1.Value >= kvp2.Value ? kvp1 : kvp2).Key; tmp.Remove(selectNode); NeighborIDWithMaxHopCount nowNode = neighbor.Find(n => n.NodeID == selectNode); if (nowNode.HopCount == int.MinValue) { foreach (var scopeNode in scope_net_topo.Nodes) { int hop_count = scope_net_topo.GetShortestPathCount(scopeNode.ID, selectNode) - 1; if (max_hop_count < hop_count) { max_hop_count = hop_count; } } nowNode.HopCount = max_hop_count; } else { max_hop_count = nowNode.HopCount; } now_hop_count = max_hop_count; if (now_hop_count <= K - 1) { break; } else { selectNode = -1; } } } else { Dictionary <int, int> tmp = new Dictionary <int, int>(); foreach (int id in neighbor.Where(n => n.HopCount == int.MinValue).Select(n => n.NodeID)) { tmp.Add(id, src_net_topo.Degree(id)); } for (int i = 0; i < tmp.Count;) { int max_hop_count = int.MinValue; selectNode = tmp.Aggregate((kvp1, kvp2) => kvp1.Value <= kvp2.Value ? kvp1 : kvp2).Key; tmp.Remove(selectNode); NeighborIDWithMaxHopCount nowNode = neighbor.Find(n => n.NodeID == selectNode); if (nowNode.HopCount == int.MinValue) { foreach (var scopeNode in scope_net_topo.Nodes) { int hop_count = scope_net_topo.GetShortestPathCount(scopeNode.ID, selectNode) - 1; if (max_hop_count < hop_count) { max_hop_count = hop_count; } } nowNode.HopCount = max_hop_count; } else { max_hop_count = nowNode.HopCount; } now_hop_count = max_hop_count; if (now_hop_count <= K - 1) { break; } else { selectNode = -1; } } } // if nothing found, break the loop. if (selectNode == -1) { break; } // adding the node to the scope set, and computing the max hop count. else { scope_net_topo.Edges.AddRange(src_net_topo.Edges.Where(e => e.Node1 == selectNode && scope_net_topo.Nodes.Exists(n => n.ID == e.Node2) || e.Node2 == selectNode && scope_net_topo.Nodes.Exists(n => n.ID == e.Node1) )); scope_net_topo.Nodes.Add(src_net_topo.Nodes.Find(n => n.ID == selectNode)); } } NetworkTopology remain_topo; // Handling the neighbor nodes of each node in the scope network topology. if (selectNode != -1) { neighbor.RemoveAll(n => n.NodeID == selectNode); foreach (int id in src_net_topo.GetNeighborNodeIDs(selectNode).Except(scope_net_topo.Nodes.Select(n => n.ID))) { neighbor.Add(new NeighborIDWithMaxHopCount() { NodeID = id, HopCount = int.MinValue }); } //neighbor.AddRange(src_net_topo.GetNeighborNodeIDs(selectNode).Except(scope_net_topo.Nodes.Select(n => n.ID))); neighbor = neighbor.GroupBy(n => n.NodeID).Select(n => n.First()).ToList(); } // During above process the tmp list will be deployment nodes, and add to deployNodes list. nowDeployNodes.AddRange(neighbor.Select(n => n.NodeID)); // Adding deploy nodes to the scope network topology. foreach (int id in neighbor.Select(n => n.NodeID)) { scope_net_topo.Edges.AddRange(src_net_topo.Edges.Where(e => e.Node1 == id && scope_net_topo.Nodes.Exists(n => n.ID == e.Node2) || e.Node2 == id && scope_net_topo.Nodes.Exists(n => n.ID == e.Node1) )); scope_net_topo.Nodes.Add(src_net_topo.Nodes.Find(n => n.ID == id)); } // Computing the complement set between source and scope network topology. remain_topo = src_net_topo - scope_net_topo; // Removing deployment nodes and edges from scope network topology. foreach (int id in neighbor.Select(n => n.NodeID)) { scope_net_topo.Nodes.RemoveAll(x => x.ID == id); scope_net_topo.Edges.RemoveAll(x => x.Node1 == id || x.Node2 == id); } return(remain_topo); }
/// <summary> /// Starting run algorithm with specific source topology and K-diameter cut, /// and this round process will generate scope topology to scop_net_topo, /// and will add the deployment node to deployNodes structure. /// Finally, it will return the remain network topology with this process. /// </summary> /// <param name="src_net_topo">The source network topology</param> /// <param name="scope_net_topo">This round process that generte scope topology</param> /// <param name="nowDeployNodes">The result of the deployment node id</param> /// <returns>The remain network topology after process the algorithm.</returns> private NetworkTopology startAlgorithm(NetworkTopology src_net_topo, NetworkTopology scope_net_topo, List <int> nowDeployNodes) { List <NeighborIDWithMaxHopCount> neighbor = new List <NeighborIDWithMaxHopCount>(); int selectNode = scope_net_topo.Nodes[0].ID; while (true) { neighbor.RemoveAll(n => n.NodeID == selectNode); foreach (int id in src_net_topo.GetNeighborNodeIDs(selectNode).Except(scope_net_topo.Nodes.Select(n => n.ID))) { neighbor.Add(new NeighborIDWithMaxHopCount() { NodeID = id, HopCount = int.MinValue }); } //neighbor.AddRange(src_net_topo.GetNeighborNodeIDs(selectNode).Except(scope_net_topo.Nodes.Select(n => n.ID))); neighbor = neighbor.GroupBy(n => n.NodeID).Select(n => n.First()).ToList(); NetworkTopology concentrate_topo = new NetworkTopology(src_net_topo.Nodes); concentrate_topo.AdjacentMatrix = src_net_topo.AdjacentMatrix; concentrate_topo.Nodes.AddRange(src_net_topo.Nodes.Except(scope_net_topo.Nodes.Where(n => n.ID != scope_net_topo.Nodes[0].ID))); concentrate_topo.Edges.AddRange(src_net_topo.Edges.Where(e => !scope_net_topo.Nodes.Exists(n => n.ID == e.Node1 || n.ID == e.Node2))); foreach (int id in neighbor.Select(n => n.NodeID)) { concentrate_topo.Edges.Add(new NetworkTopology.Edge() { Node1 = id, Node2 = scope_net_topo.Nodes[0].ID }); } selectNode = -1; Dictionary <int, double> tmp = new Dictionary <int, double>(); foreach (int id in neighbor.Where(n => n.HopCount == int.MinValue).Select(n => n.NodeID)) { tmp.Add(id, concentrate_topo.ClusteringCoefficient(id)); } //neighbor.Sort((x, y) => concentrate_topo.ClusteringCoefficient(x).CompareTo(concentrate_topo.ClusteringCoefficient(y))); for (int i = 0; i < tmp.Count;) { int max_hop_count = int.MinValue; selectNode = tmp.Aggregate((kvp1, kvp2) => kvp1.Value >= kvp2.Value ? kvp1 : kvp2).Key; tmp.Remove(selectNode); NeighborIDWithMaxHopCount nowNode = neighbor.Find(n => n.NodeID == selectNode); if (nowNode.HopCount == int.MinValue) { foreach (var scopeNode in scope_net_topo.Nodes) { int hop_count = scope_net_topo.GetShortestPathCount(scopeNode.ID, selectNode) - 1; if (max_hop_count < hop_count) { max_hop_count = hop_count; } } nowNode.HopCount = max_hop_count; } else { max_hop_count = nowNode.HopCount; } if (max_hop_count <= K - 1) { break; } else { selectNode = -1; } } // if nothing found, break the loop. if (selectNode == -1) { break; } // if K <= Diameter/2, checking the degree of selected node wether or not greater than threshold. else if (K <= Math.Ceiling(src_net_topo.Diameter / 2.0)) { List <NetworkTopology.Node> remain_nodes = src_net_topo.Nodes.Except(scope_net_topo.Nodes).ToList(); double avg = remain_nodes.Average(n => src_net_topo.Degree(n.ID)); double std = Math.Sqrt(remain_nodes.Sum(n => Math.Pow(src_net_topo.Degree(n.ID) - avg, 2)) / (double)remain_nodes.Count); double ratio_k = (double)K / (double)src_net_topo.Diameter; int threshold = Convert.ToInt32(Math.Round(avg + std * ratio_k, MidpointRounding.AwayFromZero)); //if (src_net_topo.Degree(selectNode) >= src_net_topo.Nodes.Except(scope_net_topo.Nodes).Average(n => src_net_topo.Degree(n.ID)) + K / 2) if (src_net_topo.Degree(selectNode) > threshold) { selectNode = -1; break; } } // adding the node to the scope set, and computing the max hop count. scope_net_topo.Edges.AddRange(src_net_topo.Edges.Where(e => e.Node1 == selectNode && scope_net_topo.Nodes.Exists(n => n.ID == e.Node2) || e.Node2 == selectNode && scope_net_topo.Nodes.Exists(n => n.ID == e.Node1) )); scope_net_topo.Nodes.Add(src_net_topo.Nodes.Find(n => n.ID == selectNode)); } NetworkTopology remain_topo; // Handling the neighbor nodes of each node in the scope network topology. if (selectNode != -1) { neighbor.RemoveAll(n => n.NodeID == selectNode); foreach (int id in src_net_topo.GetNeighborNodeIDs(selectNode).Except(scope_net_topo.Nodes.Select(n => n.ID))) { neighbor.Add(new NeighborIDWithMaxHopCount() { NodeID = id, HopCount = int.MinValue }); } //neighbor.AddRange(src_net_topo.GetNeighborNodeIDs(selectNode).Except(scope_net_topo.Nodes.Select(n => n.ID))); neighbor = neighbor.GroupBy(n => n.NodeID).Select(n => n.First()).ToList(); } // During above process the tmp list will be deployment nodes, and add to deployNodes list. nowDeployNodes.AddRange(neighbor.Select(n => n.NodeID)); // Adding deploy nodes to the scope network topology. foreach (int id in neighbor.Select(n => n.NodeID)) { scope_net_topo.Edges.AddRange(src_net_topo.Edges.Where(e => e.Node1 == id && scope_net_topo.Nodes.Exists(n => n.ID == e.Node2) || e.Node2 == id && scope_net_topo.Nodes.Exists(n => n.ID == e.Node1) )); scope_net_topo.Nodes.Add(src_net_topo.Nodes.Find(n => n.ID == id)); } // Computing the complement set between source and scope network topology. remain_topo = src_net_topo - scope_net_topo; // Removing deployment nodes and edges from scope network topology. foreach (int id in neighbor.Select(n => n.NodeID)) { scope_net_topo.Nodes.RemoveAll(x => x.ID == id); scope_net_topo.Edges.RemoveAll(x => x.Node1 == id || x.Node2 == id); } return(remain_topo); }