Ejemplo n.º 1
0
def randomEdge(graph, nodes=1000):
    edges = G.getEdges(graph, weight=True)
    nodes = min(nodes, G.getNodes(graph))
    sampleGraph = {}
    sampleEdges = set()
    while True:
        edge = random.choice(edges)
        if not edge in sampleEdges:
            G.addEdge(sampleGraph, edge[0], edge[1], edge[2])
            sampleEdges.add(edge)
        if len(G.getNodes(sampleGraph)) == nodes:
            break
    return sampleGraph
Ejemplo n.º 2
0
def weightDist(graph):
    inDict = defaultdict(int)
    outDict = defaultdict(int)
    inWeightDict = defaultdict(int)
    outWeightDict = defaultdict(int)
    inAvgWeightDict = defaultdict(int)  #should use float
    outAvgWeightDict = defaultdict(int)  #should use float
    for src, dest, weight in G.getEdges(graph, weight=True):
        inDict[dest] += 1
        inWeightDict[dest] += weight
        outDict[src] += 1
        outDict[src] += weight
    inDist = defaultdict(int)
    outDist = defaultdict(int)
    #calculating average now
    for node in inDict:
        inAvgWeightDict[node] = inWeightDict[node] / inDict[node]

    for node in outDict:
        outAvgWeightDict[node] = outWeightDict[node] / outDict[node]

    for node in G.getNodes(graph):
        inDist[inAvgWeightDict[node]] += 1
        outDist[outAvgWeightDict[node]] += 1
    return inDist, outDist
 def __init__(self, graph):
     super(Stage, self).__init__(name="stage")
     self.nodes = generator.getNodes(graph)
     self.input_nodes = generator.getInputNodes(graph)
     self.output_nodes = generator.getOutputNodes(graph)
     self.stage = []
     for node in self.nodes:
         self.stage.append(NodeToLayer(node))
Ejemplo n.º 4
0
def sampleRN(graph, nodes=1000):
    sampleGraph = {}
    nodesOriginal = G.getNodes(graph)
    edgesOriginal = G.getEdges(graph, weight=False)
    nodesSample = set()
    size = min(nodes, len(nodesOriginal))
    while (len(nodesSample) < size):
        nodesSample.add(random.choice(nodesOriginal))
    nodesSample = list(nodesSample)
    for src in nodesSample:
        if src in graph.keys():
            for dest, weight in graph[src]:
                if dest in nodesSample:
                    G.addEdge(sampleGraph, src, dest, weight)
    currentNodes = G.getNodes(sampleGraph)
    for node in nodesSample:
        if node not in currentNodes:
            sampleGraph[node] = []
    return sampleGraph
Ejemplo n.º 5
0
def randomNodeNeighbor(graph, nodes=1000):
    nodes = min(nodes, len(G.getEdges(graph)))
    #dataNodes = G.getNodes(getEdges)
    sampleGraph = {}
    sampleNodes = {}
    while True:
        if len(G.getNodes(sampleGraph)) >= nodes:
            break
        randomNode = random.choice(graph.keys())
        sampleGraph[randomNode] = graph[randomNode]

    return sampleGraph
Ejemplo n.º 6
0
def degreeDist(graph):
    inDict = defaultdict(int)
    outDict = defaultdict(int)
    for src, dest in G.getEdges(graph, weight=False):
        inDict[dest] += 1
        outDict[src] += 1
    inDist = defaultdict(int)
    outDist = defaultdict(int)
    for node in G.getNodes(graph):
        inDist[inDict[node]] += 1
        outDist[outDict[node]] += 1
    return inDist, outDist
Ejemplo n.º 7
0
def deleteRandomNodes(graph, resizeRatio=0.7):
    sampleNodes = G.getNodes(graph)
    sampleNodeCount = len(sampleNodes)
    sampleGraph = graph.copy()
    nodes = sampleNodeCount
    while True:
        #print sampleNodeCount, nodes
        if 1.0 * sampleNodeCount / nodes <= resizeRatio:
            break
        sampleNodeCount -= 1
        randomNode = random.choice(sampleNodes)
        sampleNodes.remove(randomNode)
        G.removeNode(sampleGraph, randomNode)
    return sampleGraph
Ejemplo n.º 8
0
 def __init__(self, graph, nodes=1000, minP=0.0, weights=True):
     self.sampleGraph = {}
     self.graph = graph
     self.weights = weights
     self.nodesOriginal = G.getNodes(self.graph)
     self.edgesOriginal = G.getEdges(self.graph, weight=True)
     self.nodes = min(nodes, len(self.nodesOriginal))
     self.nodesSample = set()
     self.minP = minP  #min probability required to select a edge
     self.startNode = random.choice(self.nodesOriginal)
     self.currentNode = self.startNode
     while True:
         if len(self.nodesSample) == self.nodes:
             #print 'processing done'
             #print self.sampleGraph
             break
         self.neighbours = G.getNeighbours(self.graph, self.currentNode)
         self.chooseRandomNeighbour()
Ejemplo n.º 9
0
def clustCoff(graph):
    nodes = sorted(G.getNodes(graph))
    clustDist = dict()
    for node in nodes:
        neighbors = G.getNeighbours(graph, node)
        n_neighbors = len(neighbors)
        if n_neighbors not in clustDist.keys():
            clustDist[n_neighbors] = set()
        total = 0
        for neighbor in neighbors:
            for neighborPair in neighbors:
                if G.isNeighbour(graph, neighbor, neighborPair):
                    total += 1
        if n_neighbors == 0 or n_neighbors == 1:
            clustDist[n_neighbors].add(1)
        else:
            clustDist[n_neighbors].add(total / (n_neighbors *
                                                (n_neighbors - 1) * 1.0))
    return clustDist
Ejemplo n.º 10
0
def hopDist(graph):
    nodes = sorted(G.getNodes(graph))
    dist = {}
    for node in nodes:
        dist[node] = {}
        for n in nodes:
            dist[node][n] = sys.maxint
    for src, dest, weight in G.getEdges(graph):
        dist[src][dest] = weight
    for node in nodes:
        dist[node][node] = 0
    for k in nodes:
        for i in nodes:
            for j in nodes:
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
    hop = defaultdict(int)
    for i in nodes:
        for j in nodes:
            hop[dist[i][j]] += 1
    hop.pop(sys.maxint, None)
    return hop
Ejemplo n.º 11
0
def sccDist(graph):
    sys.setrecursionlimit(5000)
    transpose = G.computeTranspose(graph)
    nodes = G.getNodes(graph)
    visited = [False] * (sorted(nodes)[-1] + 1)
    st = []
    dist = defaultdict(int)
    for node in nodes:
        if not visited[node]:
            dfs(graph, node, visited, stack=st)
    visited = [False] * (sorted(nodes)[-1] + 1)
    while len(st) > 0:
        node = st.pop()
        newScc = set([node])
        dfs(transpose, node, visited, scc=newScc)
        dist[len(newScc)] += 1
        while len(newScc) > 0:
            val = newScc.pop()
            if val != node:
                st.remove(val)
    return dist