Exemplo n.º 1
0
def build_graph(g_map) -> Graph:
    g = Graph()
    for u in g_map:
        for v, w in g_map[u].items():
            g.add_edge(u, v, w)

    return g
Exemplo n.º 2
0
def build_graph(word_list):
    d = {}
    g = Graph()
    # create buckets of words that differ by one letter
    for word in word_list:
        for i in range(len(word)):
            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.add_edge(word1, word2)
    return g
def build_graph(word_file):
    d = {}
    g = Graph()
    with open(word_file) as f:
        for line in f:
            word = line.strip()
            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, words in d.items():
        for word1 in words:
            for word2 in words:
                if word1 != word2:
                    g.add_edge(word1, word2)

    return g
Exemplo n.º 4
0
Arquivo: graph.py Projeto: xouan/DSA
def buildGraph(wordFile):
    d = {}
    g = Graph()
    with open(wordFile, 'r') as f:
        # 1 create bucket
        for line in f:
            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]
        # 2 add vertices and edges
        for bucket in d.keys():
            for word1 in d[bucket]:
                for word2 in d[bucket]:
                    if word1 != word2:
                        g.add_edge(word1, word2)
    return g
        currentvertex.set_visited()
        for childvertex in currentvertex.adjacent:
            #print childvertex.get_id()
            newcost = currentvertex.get_weight(childvertex)
            if childvertex in pq and newcost < childvertex.get_distance():
                childvertex.set_distance(newcost)
                childvertex.set_previous(currentvertex)
                pq.decreaseKey(childvertex, newcost)

    for i in mse:
        print i


g = Graph()

g.add_edge('a', 'b', 2)
g.add_edge('a', 'c', 3)
g.add_edge('c', 'f', 5)
g.add_edge('f', 'g', 1)
g.add_edge('b', 'e', 4)
g.add_edge('b', 'd', 1)
g.add_edge('b', 'c', 1)
g.add_edge('d', 'e', 1)

print 'Graph data:'
for v in g:
    for w in v.get_connections():
        vid = v.get_id()
        wid = w.get_id()
        print '( %s , %s, %3d)' % (vid, wid, v.get_weight(w))
Exemplo n.º 6
0
 def _add_edge(self, g: Graph, p1, p2):
     g.add_edge(self._convert2vertex(*p1), self._convert2vertex(*p2))