private double CalculateModularity(CommunityStructure pCs, DGraph pOriginalGraph) { double modularity = 0; int numEdge = pOriginalGraph.Edges.Count; foreach (DGraph csItem in pCs) { int l = 0; // tong bac cua dinh trong c, theo do thi moi => suy ra so canh int d = 0; // tong bac foreach (Node node in csItem.Nodes) { l += node.AdjacencyNodes.Count; if (node.Label.Contains('-')) { string[] temp_label = node.Label.Split('-'); d += pOriginalGraph.FindNode(temp_label[0], false).AdjacencyNodes.Count; } else { d += pOriginalGraph.FindNode(node.Label, false).AdjacencyNodes.Count; } } l /= 2; modularity += ((double)l / numEdge) - Math.Pow(((double)d / (2 * numEdge)), 2); } return(modularity); }
public static void Draw(CommunityStructure pCS, Graphics g) { int n = Format.Brushes.Length; for (int i = 0; i < pCS.Count; i++) { foreach (Node node in pCS[i].Nodes) { Rectangle bound = new Rectangle(node.Location.X - Format.Setting.NodeHaftSize, node.Location.Y - Format.Setting.NodeHaftSize, Format.Setting.NodeSize, Format.Setting.NodeSize); g.DrawEllipse(Pens.Black, bound); if (node.IsHover == false) { g.FillEllipse(Format.Brushes[i % n], bound); } else { g.FillEllipse(Format.NodeHoverBackground, bound); } if (Format.Setting.ShowNodeLabel) { g.DrawString(node.Label, Format.Setting.NodeLabelFont, Format.NodeLabelColor, node.Location, Format.StrFormat); } } } }
private DGraph RemoveEdge(Edge e, CommunityStructure pTempCS) { // xoa canh trong do thi graph.Edges.Remove(e); // Xoa canh ke trong nut e.NodeA.AdjacencyEdges.Remove(e); e.NodeB.AdjacencyEdges.Remove(e); // Xoa nut ke e.NodeA.AdjacencyNodes.Remove(e.NodeB); e.NodeB.AdjacencyNodes.Remove(e.NodeA); WriteLog("Remove: (" + e.NodeA.Label + ", " + e.NodeB.Label + ")\t" + edgeBetweenness[e].ToString("0.00")); // xoa edgebetweenness edgeBetweenness.Remove(e); // tim cong dong foreach (DGraph subgraph in pTempCS) { if (subgraph.Nodes.Contains(e.NodeA)) { return(subgraph); } } return(null); }
private void CalculateEdgeBetweenness(CommunityStructure pCS, List <DGraph> communites = null) { if (edgeBetweenness == null) { edgeBetweenness = new Dictionary <Edge, double>(); foreach (Edge e in graph.Edges) { edgeBetweenness.Add(e, 0); } } if (communites != null) { foreach (DGraph graph in communites) { _CalculateEdgeBetweenness(graph); } } else { foreach (DGraph subg in pCS) { _CalculateEdgeBetweenness(subg); } } }
private CommunityStructure _NewAlgorithm(GraphD.DGraph pGraph) { graph = pGraph.Clone(); int NodeCount = graph.Nodes.Count(); CommunityStructure tempCS = FindStructureWithMaxClique(graph); while (VisitedNodeCount < NodeCount) { while (true) { VisitNode(graph); if (CheckNewCommunity(graph)) { break; } } // Tính Q Q = CalculateModularity(tempCS, pGraph); if (Q > _BestQ) { _BestQ = Q; Cs = tempCS; } } return(Cs); }
public CommunityStructure FindCommunityStructure(DGraph pGraph) { // Clone graph này ra để xử lý graph = pGraph.Clone(); // Cộng đồng CommunityStructure tempCS = GetCommunityStructure(); // Số cộng đồng int initCount = tempCS.Count; int countCommunity = initCount; // Q _BestQ = 0; Q = 0; // Tính edge betweenness lần đầu CalculateEdgeBetweenness(tempCS); // tính thuật toán int j = 0; while (true) { while (countCommunity <= initCount) { WriteLog("Xóa lần " + j.ToString()); j++; // Xóa cạnh có edge betweenness lớn nhất List <DGraph> communities = RemoveMaxEdgeBetweenness(tempCS); // Xóa cạnh lớn nhất và cho biết community nào có cạnh được xóa // Tính lại Edgebetweenness CalculateEdgeBetweenness(tempCS, communities); // Đếm lại số cộng đồng tempCS = GetCommunityStructure(); countCommunity = tempCS.Count; } initCount = countCommunity; // Tính Q Q = CalculateModularity(tempCS, pGraph); if (Q > _BestQ) { _BestQ = Q; Cs = tempCS; } if (graph.Edges.Count == 0) { break; } } return(this.Cs); }
private void RemoveEdgeOrSplitVertex(CommunityStructure tempCS) { double maxEdge = double.MinValue; Edge e = null; foreach (KeyValuePair <Edge, double> keypair in edgeBetweenness) { if (keypair.Value >= maxEdge) { maxEdge = keypair.Value; e = keypair.Key; } } // tìm danh sách các đỉnh có vertex betweenness lớn hơn max edge betweeness List <Node> CandidateNode = new List <Node>(); foreach (KeyValuePair <Node, double> keypair in vertexBetweenness) { if (keypair.Value > maxEdge && keypair.Key.AdjacencyNodes.Count >= 4) { CandidateNode.Add(keypair.Key); } } if (CandidateNode.Count == 0) { RemoveEdge(e, tempCS); } else { // tính split betweenness của các đỉnh trong danh sách trên PairBetweenness maxSplitbetweenness = CalculatePairBetweenness(CandidateNode); if (maxSplitbetweenness.SplitBetweenness >= maxEdge) { SplitVertex(maxSplitbetweenness, tempCS); } else { RemoveEdge(e, tempCS); } } }
private CommunityStructure GetCommunityStructure() { CommunityStructure cs = new CommunityStructure(); int count = 0; int n = graph.Nodes.Count; Dictionary <Node, bool> visited = new Dictionary <Node, bool>(); for (int i = 0; i < n; i++) { visited.Add(graph.Nodes[i], false); } foreach (Node i in graph.Nodes) { if (visited[i] == false) { count++; DGraph subgraph = new DGraph(); Queue <Node> Q = new Queue <Node>(); visited[i] = true; Q.Enqueue(i); subgraph.Nodes.Add(i); while (Q.Count != 0) { Node v = Q.Dequeue(); foreach (Node j in v.AdjacencyNodes) { if (visited[j] == false) { subgraph.Nodes.Add(j); visited[j] = true; Q.Enqueue(j); } } } cs.Add(subgraph); } } return(cs); }
private void CalculateEdgeBetweenness(CommunityStructure pCS, DGraph community = null) { if (edgeBetweenness == null) { edgeBetweenness = new Dictionary <Edge, double>(); foreach (Edge e in graph.Edges) { edgeBetweenness.Add(e, 0); } } //if (community != null) _CalculateEdgeBetweenness(community); //else //{ // foreach (DGraph subg in pCS) // { // _CalculateEdgeBetweenness(subg); // } //} }
private void DrawOverlap(CommunityStructure _CS, Graphics graphics) { foreach (KeyValuePair <Node, List <DGraph> > keypair in _OverlapNodes) { int n = 360 / keypair.Value.Count; Node node = keypair.Key; Rectangle bound = new Rectangle(node.Location.X - Format.Setting.NodeHaftSize, node.Location.Y - Format.Setting.NodeHaftSize, Format.Setting.NodeSize, Format.Setting.NodeSize); int i = 0; foreach (DGraph g in keypair.Value) { graphics.FillPie(g.NodeBrush, bound, (i++ *n), n); } if (Format.Setting.ShowNodeLabel) { graphics.DrawString(node.Label, Format.Setting.NodeLabelFont, Format.NodeLabelColor, node.Location, Format.StrFormat); } } }
public static double CalculateModularity(CommunityStructure pCs, DGraph pOriginalGraph) { double modularity = 0; int numEdge = pOriginalGraph.Edges.Count; foreach (DGraph csItem in pCs) { int l = 0; // tong bac cua dinh trong c, theo do thi moi => suy ra so canh int d = 0; // tong bac foreach (Node node in csItem.Nodes) { l += node.AdjacencyNodes.Count; d += pOriginalGraph.FindNode(node.Label, false).AdjacencyNodes.Count; } l /= 2; modularity += ((double)l / numEdge) - Math.Pow(((double)d / (2 * numEdge)), 2); } return(modularity); }
private DGraph RemoveMaxEdgeBetweenness(CommunityStructure pTempCS) { double max = double.MinValue; Edge e = null; foreach (KeyValuePair <Edge, double> keypair in edgeBetweenness) { if (keypair.Value > max) { max = keypair.Value; e = keypair.Key; } } // xoa canh trong do thi graph.Edges.Remove(e); // Xoa canh ke trong nut e.NodeA.AdjacencyEdges.Remove(e); e.NodeB.AdjacencyEdges.Remove(e); // Xoa nut ke e.NodeA.AdjacencyNodes.Remove(e.NodeB); e.NodeB.AdjacencyNodes.Remove(e.NodeA); WriteLog(" - Remove: (" + e.NodeA.Label + ", " + e.NodeB.Label + ")\t" + edgeBetweenness[e].ToString("0.00")); // xoa edgebetweenness edgeBetweenness.Remove(e); // tim cong dong foreach (DGraph subgraph in pTempCS) { if (subgraph.Nodes.Contains(e.NodeA)) { return(subgraph); } } return(null); }
// Hàm xóa cạnh và trả về danh sách các subgraph có cạnh bị xóa // Nhằm tính lại edge betweenness private List <DGraph> RemoveMaxEdgeBetweenness(CommunityStructure pTempCS) { var maxValue = edgeBetweenness.Max(u => u.Value); List <Edge> lstEdge = (from e in edgeBetweenness where e.Value == maxValue select e.Key).ToList(); List <DGraph> lstGraph = new List <DGraph>(); // Xóa tất cả các cạnh này foreach (Edge e in lstEdge) { // xoa canh trong do thi graph.Edges.Remove(e); // Xoa canh ke trong nut e.NodeA.AdjacencyEdges.Remove(e); e.NodeB.AdjacencyEdges.Remove(e); // Xoa nut ke e.NodeA.AdjacencyNodes.Remove(e.NodeB); e.NodeB.AdjacencyNodes.Remove(e.NodeA); WriteLog(" - Remove: (" + e.NodeA.Label + ", " + e.NodeB.Label + ")\t" + edgeBetweenness[e].ToString("0.00")); // xoa edgebetweenness edgeBetweenness.Remove(e); // tim cong dong foreach (DGraph subgraph in pTempCS) { if (subgraph.Nodes.Contains(e.NodeA)) { lstGraph.Add(subgraph); } } } return(lstGraph); }
private double CalculateVAD(CommunityStructure tempCS) { double EC = 0; double C = 0; double VAD = 0; foreach (DGraph cs in tempCS) { double temp_EC = 0; foreach (Node node in cs.Nodes) { temp_EC += node.AdjacencyEdges.Count; } EC += temp_EC; C += cs.Nodes.Count; } VAD = EC / C; return(VAD); }
private void CalculateVertexBetweenness(CommunityStructure tempCS, DGraph community = null) { if (vertexBetweenness == null) { vertexBetweenness = new Dictionary <Node, double>(); foreach (Node node in graph.Nodes) { vertexBetweenness.Add(node, 0); } } if (community != null) { _CalculateVertexBetweenness(community); } else { foreach (DGraph subgraph in tempCS) { _CalculateVertexBetweenness(subgraph); } } }
public CommunityStructure FindCommunityStructure(DGraph pGraph) { // Clone graph này ra để xử lý graph = pGraph.Clone(); // Cộng đồng CommunityStructure tempCS = GetCommunityStructure(); // Số cộng đồng int initCount = tempCS.Count; int countCommunity = initCount; // Tinh similarity cua tat ca cac node Dictionary <Node, List <Similarity> > hashMaxSimilarityNode = new Dictionary <Node, List <Similarity> >(); // mỗi đỉnh là một community foreach (Node node in graph.Nodes) { // tinh similarity if (node.AdjacencyNodes.Count > 0) { List <Similarity> lst = new List <Similarity>(); double max = double.MinValue; foreach (Node neighborNode in node.AdjacencyNodes) { Similarity s = new Similarity(); s.node = neighborNode; s.similarity = ZLZSimilarity(node, neighborNode); lst.Add(s); if (s.similarity > max) { max = s.similarity; } } // xử lý cái list đó trước rồi mới add vào int n = lst.Count; for (int i = 0; i < n; i++) { if (lst[i].similarity < max) { lst.RemoveAt(i); n--; i--; } } if (lst.Count >= 2) { } // add cái list này vào danh sách hashMaxSimilarityNode.Add(node, lst); } // khoi tao cong dong DGraph com = new DGraph(); com.Nodes.Add(node); tempCS.Add(com); } // chọn nút bất kì Random r = new Random(); int nodeNumber = r.Next(0, graph.Nodes.Count); Node initNode = graph.Nodes[nodeNumber]; return(this.Cs); }
private DGraph SplitVertex(PairBetweenness pairBetweenness, CommunityStructure pTempCS) { countSplit++; string[] labelA = pairBetweenness.Clique[0].Label.Split('_'); string[] labelB = pairBetweenness.Clique[1].Label.Split('_'); Node splitNode = pairBetweenness.Vertex; // Tách nút splitNode thành 2 nút Node splitedNodeA = new Node(splitNode.Label + "-" + string.Join("-", labelA), new Point(splitNode.Location.X + Format.Setting.NodeSize, splitNode.Location.Y + Format.Setting.NodeSize)); Node splitedNodeB = new Node(splitNode.Label + "-" + string.Join("-", labelB), new Point(splitNode.Location.X - Format.Setting.NodeSize, splitNode.Location.Y - Format.Setting.NodeSize)); WriteLog("Split: " + splitedNodeA.Label + "/" + splitedNodeB.Label + "\t" + pairBetweenness.SplitBetweenness.ToString("0.00")); // xóa nút splitNode graph.Nodes.Remove(splitNode); vertexBetweenness.Remove(splitNode); // Thêm 2 nút mới tạo vào đồ thị graph.Nodes.Add(splitedNodeA); graph.Nodes.Add(splitedNodeB); vertexBetweenness.Add(splitedNodeA, 0); vertexBetweenness.Add(splitedNodeB, 0); foreach (Edge e in splitNode.AdjacencyEdges) { Edge newe = null; if (labelA.Contains(e.NodeA.Label)) { newe = graph.CreateLink(e.NodeA, splitedNodeA); } else if (labelA.Contains(e.NodeB.Label)) { newe = graph.CreateLink(e.NodeB, splitedNodeA); } else if (labelB.Contains(e.NodeA.Label)) { newe = graph.CreateLink(e.NodeA, splitedNodeB); } else if (labelB.Contains(e.NodeB.Label)) { newe = graph.CreateLink(e.NodeB, splitedNodeB); } graph.Edges.Remove(e); edgeBetweenness.Remove(e); edgeBetweenness.Add(newe, 0); if (e.NodeA != splitNode) { e.NodeA.AdjacencyEdges.Remove(e); e.NodeA.AdjacencyNodes.Remove(splitNode); } else { e.NodeB.AdjacencyEdges.Remove(e); e.NodeB.AdjacencyNodes.Remove(splitNode); } } foreach (DGraph subgraph in pTempCS) { if (subgraph.Nodes.Contains(splitNode)) { return(subgraph); } } return(null); }
public CommunityStructure FindCommunityStructure(DGraph pGraph) { // Clone graph này ra để xử lý originalGraph = pGraph; graph = pGraph.Clone(); // Cộng đồng CommunityStructure tempCS = GetCommunityStructure(); // Số cộng đồng int initCount = tempCS.Count; int countCommunity = initCount; // Q _BestVAD = 0; _VAD = 0; _BestM = 0; _M = 0; // Tính edge betweenness lần đầu CalculateEdgeBetweenness(tempCS); CalculateVertexBetweenness(tempCS); WriteLog("Start CONGA algorithm"); // tính thuật toán while (true) { while (countCommunity <= initCount) { // Tìm tập hợp các đỉnh có vertex betweenness lớn hơn max edge betweenness: // Thật chất là tìm max edge betweenness và max vertext betweenness // Xóa cạnh có edge betweenness lớn nhất RemoveEdgeOrSplitVertex(tempCS); // Đếm lại số cộng đồng tempCS = GetCommunityStructure(); countCommunity = tempCS.Count; CalculateEdgeBetweenness(tempCS); CalculateVertexBetweenness(tempCS); } initCount = countCommunity; // Tính Q = VAD if (_UseVAD) { _VAD = CalculateVAD(tempCS); if (_VAD > _BestVAD) { _BestVAD = _VAD; Cs = tempCS; } } else { _M = CalculateModularity(tempCS, originalGraph); if (_M > _BestM) { _BestM = _M; Cs = tempCS; } } if (graph.Edges.Count == 0) { break; } } return(this.Cs); }