private void PartialBetweenessComp() { int numIndices = _work.Length; int numNodes = _g.NumNodes; double[] delta = new double[numNodes]; for (int i = 0; i < numIndices; i++) { int v = _work[i]; //Get a shortest path, if weighted use Dikstra, if unweighted use BFS ShortestPathProvider asp = (_g.IsWeighted) ? new DikstraProvider2(_g, v) : new BFSProvider(_g, v) as ShortestPathProvider; for (int j = 0; j < numNodes; j++) { delta[j] = 0.0f; } while (asp.S.Count > 0) { int w = asp.S.Pop(); var wList = asp.fromList[w]; foreach (int n in wList) { delta[n] += ((double)asp.numberOfShortestPaths[n] / (double)asp.numberOfShortestPaths[w]) * (1.0f + delta[w]); } if (w != v) { BcMap[w] += delta[w]; } } } }
/// <summary> /// Calculates Betweeness centrality of the edges in an undirected graph /// </summary> /// <param name="g"></param> /// <returns></returns> public static NodeEdgeBetweeness BrandesBcEdges(LightWeightGraph g) { var edgeMap = g.GetEdgeIndexMap(); int numNodes = g.NumNodes; int numEdges = edgeMap.Count; double[] bcEdge = new double[numEdges]; double[] bcNode = new double[numNodes]; for (int v = 0; v < numNodes; v++) { //Get a shortest path, if weighted use Dikstra, if unweighted use BFS ShortestPathProvider asp = (g.IsWeighted) ? new DikstraProvider2(g, v) : new BFSProvider(g, v) as ShortestPathProvider; //numberOfShortestPaths = sigma double[] deltaNode = new double[numNodes]; while (asp.S.Count > 0) { int w = asp.S.Pop(); double coeff = (1.0f + deltaNode[w]) / (double)asp.numberOfShortestPaths[w]; foreach (int n in asp.fromList[w]) { //make sure the first index is the smallest, this is an undirected graph KeyValuePair <int, int> edgeNodePair = (w < n) ? new KeyValuePair <int, int>(w, n) : new KeyValuePair <int, int>(n, w); int edgeIndex = edgeMap[edgeNodePair]; double contribution = asp.numberOfShortestPaths[n] * coeff; bcEdge[edgeIndex] += contribution; deltaNode[n] += contribution; } //Add the betweeness contribution to W if (v != w) { bcNode[w] += deltaNode[w]; } } } //divide all by 2 (undirected) for (int v = 0; v < numEdges; v++) { bcEdge[v] /= 2f; } for (int v = 0; v < numNodes; v++) { bcNode[v] /= 2f; } return(new NodeEdgeBetweeness() { EdgeBetweeness = bcEdge, NodeBetweeness = bcNode }); }
private void PartialBetweenessComp() { for (int i = 0; i < delta.Length; i++) { delta[i] = 0; } //Get a shortest path, if weighted use Dikstra, if unweighted use BFS ShortestPathProvider asp = (_g.IsWeighted) ? new DikstraProvider2(_g, v) : new BFSProvider(_g, v) as ShortestPathProvider; while (asp.S.Count > 0) { int w = asp.S.Pop(); var wList = asp.fromList[w]; foreach (int n in wList) { delta[n] += ((double)asp.numberOfShortestPaths[n] / asp.numberOfShortestPaths[w]) * (1.0f + delta[w]); } } delta[v] = 0; }
public static double[] BrandesBcNodes(LightWeightGraph g) { int numnodes = g.NumNodes; double[] bcMap = new double[numnodes]; double[] delta = new double[numnodes]; for (int v = 0; v < numnodes; v++) { //Get a shortest path, if weighted use Dikstra, if unweighted use BFS ShortestPathProvider asp = (g.IsWeighted) ? new DikstraProvider2(g, v) : new BFSProvider(g, v) as ShortestPathProvider; Array.Clear(delta, 0, numnodes); while (asp.S.Count > 0) { int w = asp.S.Pop(); var wList = asp.fromList[w]; foreach (int n in wList) { delta[n] += ((double)asp.numberOfShortestPaths[n] / (double)asp.numberOfShortestPaths[w]) * (1.0f + delta[w]); } if (w != v) { bcMap[w] += delta[w]; } } } //divide all by 2 (undirected) for (int v = 0; v < numnodes; v++) { bcMap[v] /= 2f; } return(bcMap); }