def __init__(self, G): self._Gr = G self._coupleDistances = {} for v in self._Gr.getVertices(): v_sssp = SSSP(self._Gr) v_sssp.run(v) self._coupleDistances[v] = v_sssp.getDistances()
def getCommunities(self, k): """ Returns a set of communities detected by removal of k edges with maximal betweenness centrality. @Invariant: all([all([self._G.isVertex(v) for v in C]) for C in self.getCommunities()]) """ unDirectG = self.directToUndirectGraph(self._G) unDirectG = deepcopy(unDirectG) community_vertices = set() if k <=0: return [unDirectG.getVertices()] if k > len(unDirectG.getEdges()): ans = [] for ver in unDirectG.getVertices(): ans.append([ver]) return ans for i in range(0,k): bweenS = EdgeBetweenness(unDirectG) bweenS.run() biggestEdgeBC = bweenS.getTheBiggestBC_Edges() if biggestEdgeBC is None: break unDirectG.deleteEdge(biggestEdgeBC) unDirectG.deleteEdge((biggestEdgeBC[1],biggestEdgeBC[0])) community_vertices.add(biggestEdgeBC[0]) community_vertices.add(biggestEdgeBC[1]) ans = [] for v in community_vertices: v_community = [] bfs_from_v = SSSP(unDirectG) bfs_from_v.run(v) for reachable_from_v in bfs_from_v._distances: if bfs_from_v._distances[reachable_from_v] >= 0: v_community.append(reachable_from_v) isExist = False for elm in ans: if elm == v_community: isExist = True break if isExist == False: ans.append(v_community) return ans
g.addEdge((1, 4, 7)) g.addEdge((4, 3, 6)) g.addEdge((3, 2, -5)) g.addEdge((1, 3, 1)) g.addEdge((3, 0, 2)) g.print() distance = fastDPAlgo(g) print("\nShortest path distances by BASIC DP ALGORITHM:") printMatrix(distance, g.v) distance = floydWarshall(g) print("\nShortest path distances by FLOYD-WARSHALL ALGORITHM:") printMatrix(distance, g.v) sparseGraph = SSSP() for i in range(5): sparseGraph.addVertex(i) sparseGraph.addDirectedEdge((0, 4, -4)) sparseGraph.addDirectedEdge((0, 1, 3)) sparseGraph.addDirectedEdge((2, 1, 4)) sparseGraph.addDirectedEdge((0, 2, 8)) sparseGraph.addDirectedEdge((1, 4, 7)) sparseGraph.addDirectedEdge((4, 3, 6)) sparseGraph.addDirectedEdge((3, 2, -5)) sparseGraph.addDirectedEdge((1, 3, 1)) sparseGraph.addDirectedEdge((3, 0, 2)) distance = johnson(sparseGraph) print("\nShortest path distances by JOHNSON ALGORITHM:") printMatrix(distance, len(distance))