Beispiel #1
0
def makeGraphFromDict(name, dict):
    """
    Updates this graph to be equal to a dictionary representation of a graph.
    Essentially a secondary constructor using a dict
    :param dict: Dictionary representation of a graph
    :return: Graph object that is equivalent to dict
    """

    graph = Graph(name)
    communities = []
    nodes = []

    for nodeID, nodeValues in dict.items():
        c = Community(nodeValues[1])
        n = Node(nodeID)
        if n not in nodes:
            nodes.append(n)
        for node in nodes:
            if node == n:
                n = node
        for neighborID, arcWeight in nodeValues[0].items():
            n2 = Node(neighborID)
            if n2 not in nodes:
                nodes.append(n2)
            for node in nodes:
                if node == n2:
                    n2 = node
            e = Edge(n, n2, arcWeight)
            if e not in graph.getEdges():
                graph.addEdge(e)
                n.addConnection(e)
                n2.addConnection(e)
        if c not in communities:
            communities.append(c)
        for comm in communities:
            if c == comm:
                c = comm
                c.addMemberNode(n)

    for node in nodes:
        graph.addNode(node)
    for c in communities:
        graph.addCommunity(c)

    return graph
Beispiel #2
0
def phaseTwo(graph):
    """
    Consolidates communities toa  single supernode, and adds edges to that node based on sum of weights between nodes from those communities.
    Nodes will have self-edges with a weight equal to the number of edges between nodes in the community as described in the paper.
    :param graph: A graph object
    :return: A version of input graph corresponding to phase two of the blondel algorithm.
    """

    newNodes = {}
    newEdges = set()
    newCommunities = []
    # Create all supernodes, one per community
    for community in graph.getCommunities():
        id = community.getID()
        node = Node(id)
        id = community.getID()
        c = Community(id)
        c.addMemberNode(node)
        newNodes[id] = node
        newCommunities.append(c)

    # Create all edges between supernodes
    # Outer for loop will correspond to a supernode
    for community in graph.getCommunities():

        # This dictionary will store communityID as the key (these are same as supernode nodeID)
        # and a numerical value representing the weight between two supernodes
        newEdgeDict = {}
        # This for loop corresponds to the edges between each supernode
        for node in community.getMemberNodes():
            neighborList = node.getNeighborCommunities()
            # This loop calculates the weight of each new edge
            for edge in node.getConnections():
                otherNode = edge.getOtherNode(node)
                nCommID = otherNode.getCommunity().getID()
                if node == otherNode:
                    newEdgeDict[nCommID] = newEdgeDict.get(
                        nCommID, 0) + edge.getWeight()
                elif nCommID == node.getCommunity().getID():
                    newEdgeDict[nCommID] = newEdgeDict.get(
                        nCommID, 0) + edge.getWeight() / 2
                else:
                    newEdgeDict[nCommID] = newEdgeDict.get(
                        nCommID, 0) + edge.getWeight()
        for key, weight in newEdgeDict.items():
            n1 = newNodes[community.getID()]
            n2 = newNodes[key]
            e1 = Edge(n1, n2, weight)
            e2 = Edge(n2, n1, weight)
            if e1 not in newEdges and e2 not in newEdges:
                n1.addConnection(e1)
                n2.addConnection(e1)
                newEdges.add(e1)

    for nodeID, node in newNodes.items():
        node.computeSumEdgeWeights()

    for community in newCommunities:
        community.computeInCommunityWeight()

    p2graph = Graph(graph.getName(),
                    nodes=newNodes,
                    edges=newEdges,
                    communities=newCommunities)

    return p2graph