Exemple #1
0
def buildGraph(wordFile):
    d = {}
    g = Graph()
    wfile = open(wordFile, 'r')
    # 创建词桶
    for line in wfile:
        word = line[:-1]
        for i in range(len(word)):
            bucket = word[:i] + '_' + word[i+1:]
            if bucket in d:
                d[bucket].append(word)
            else:
                d[bucket] = [word]
    # 为同一个桶中的单词添加顶点和边
    for bucket in d.keys():
        for word1 in d[bucket]:
            for word2 in d[bucket]:
                if word1 != word2:
                    g.addEdge(word1, word2)
    return g
def buildGraph():
    d = {}
    g = Graph()
    # create buckets of words that differ by one letter
    with open('words4', 'r') as wfile:
        for line in wfile:
            word = line.strip()
            for i in range(4):
                bucket = word[:i] + '_' + word[i + 1:]
                if bucket in d:
                    d[bucket].append(word)
                else:
                    d[bucket] = [word]
    # add vertices and edges for words in the same bucket
    for bucket in d.keys():
        for word1 in d[bucket]:
            for word2 in d[bucket]:
                if word1 != word2:
                    g.addEdge(word1, word2)
    return g
Exemple #3
0
def KruskalMST(g):
    c = []  # empty set of clusters
    for v in g.vertices:
        c.append(Graph(v))
    q = sorted(g.edges, reverse=True) # use the edge weights as keys
    T = Graph() # empty tree
    counter = 0
    while len(T) < len(g) - 1:
        counter += 1
        e = q.pop()
        origGraph = graphWithVertex(c, e.origin)
        destGraph = graphWithVertex(c, e.destination)
        if origGraph is not None and destGraph is not None:
            if origGraph is not destGraph:
                T.addEdge(e)
                # remove the graphs for union
                c.remove(origGraph)
                c.remove(destGraph)
                c.append(origGraph.getUnion(destGraph))
        T.render('Step {}'.format(counter))
    return T
Exemple #4
0
def generateFullMaze(width, height):
    g = Graph()
    for i in range(0, width * height):
        v = Vertex(i)
        g.addVertex(v)

    # Create a fully connected maze graph
    for i in range(0, width * height):
        # Connect up
        if i > width - 1:
            g.addEdge(g.getVertex(i), g.getVertex(i - width))

        # Connect down
        if i < (width * height) - width:
            g.addEdge(g.getVertex(i), g.getVertex(i + width))

        # Connect right
        if i % width != (width - 1):
            g.addEdge(g.getVertex(i), g.getVertex(i + 1))
    return g
Exemple #5
0
def buildAtomTraceGraph(ag, tracedAtoms, pairScores = None, weighting = TRACE_GRAPH_WEIGHTS_UNWEIGHTED,
                        sources = None, target = None, minCommonAtoms = 1, skipnodes = set(),
                        reactionDirConstraints = {}):
    g = Graph()
    #ccc = 0
    for u in ag.edges:
        #ccc += 1
        #print ccc, len(ag.edges)
        types = ag.atomTypes[u]
        for v in ag.edges[u]:
            pairs = ag.edges[u][v]

            # Each molecule pair might have multiple rpairs
            for pair in pairs:

                amtypes = ag.atomMapTypes[pair]
                tracedCount = 0
                for type in amtypes:
                    if type in tracedAtoms:
                        tracedCount += 1
               
                if tracedCount < minCommonAtoms: # Does this rpair carry enough traced atoms?
                    continue 

                # Find out the score of this rpair
                if weighting == TRACE_GRAPH_WEIGHTS_RSCORE:
                    assert(pairScores)
                    if pair in pairScores:
                        #print pairScores[pair]
                        score = convScore(pairScores[pair])
                        #score = 1
                    else:
                        score = convScore(NONE_SCORE)
                elif weighting == TRACE_GRAPH_WEIGHTS_NATOMS:
                    #score = 2**(-tracedCount)     # As described in the 03/2009 manuscript
                    score = 1.0 / tracedCount
                    #score = 1.0
                elif weighting == TRACE_GRAPH_WEIGHTS_UNWEIGHTED:
                    score = 1.0
                else:
                    print "unknown TRACE_GRAPH_SCORE: %d" % (TRACE_GRAPH_SCORE)
                    assert(0)

                # Generate edges for each mapped atom pair of this rpair
                amap = ag.atomMap[pair]

                for src in amap:

                    if types[src] not in tracedAtoms:
                        continue

                    tgt = amap[src]

                    srcnode = "%s-%d" % (u, src)
                    tgtnode = "%s-%d" % (v, tgt)
                    fwdpair = "%s_f-%d-%d" % (pair, src, tgt)
                    revpair = "%s_r-%d-%d" % (pair, src, tgt)

                    if srcnode in skipnodes or tgtnode in skipnodes:
                        continue

                    # forward direction
                    g.addEdge(srcnode, fwdpair, score)
                    g.addEdge(fwdpair, tgtnode, 0.0)

                    # reverse direction
                    g.addEdge(tgtnode, revpair, score)
                    g.addEdge(revpair, srcnode, 0.0)

    # Add source and target root nodes, if necessary
    if sources != None:
        for source in sources: # source = mol
            g.addEdge("root", source, 0.0)
            for i in sources[source]: # i = mol-atom
                g.addEdge(source, i)
                if target != None:
                    g.addEdge(i, target, DUMMY_DIST)
    if target != None:
        for i in range(ag.atomCount[target]):
            g.addEdge("%s-%d" % (target, i + 1), target, 0.0)
            g.addEdge(target, "%s-%d" % (target, i + 1), DUMMY_DIST)
    return g