Exemplo n.º 1
0
    def __init__(self, word_list):
        node_count = len(word_list)
        self.word_list = word_list
        self.graph = Graph(node_count)
        self.word_to_vertex_id = {}
        for i in xrange(node_count - 1):
            self.word_to_vertex_id[word_list[i]] = i
            for j in xrange(i + 1, node_count):
                word_distance = 0
                for k in xrange(len(word_list[i])):
                    if word_list[i][k] == word_list[j][k]:
                        continue
                    else:
                        word_distance += 1

                    if word_distance > 1:
                        break
                if word_distance == 1:
                    # add to graph as they are adjacent nodes in the graph
                    self.graph.add_edge(i, j)
Exemplo n.º 2
0
        :param graph_obj:
        :param src:
        :return:
        """
        self.dist_to[src] = 0
        self._marked[src] = True
        q = deque()
        q.append(src)
        while q:
            v = q.popleft()
            for u in graph_obj.adj(v):
                if not self._marked[u]:
                    self.dist_to[u] = self.dist_to[v] + 1
                    self.edge_to[u] = v
                    self._marked[u] = True
                    q.append(u)


if __name__ == '__main__':
    l = [
        13, 13, (0, 5), (4, 3), (0, 1), (9, 12), (6, 4), (5, 4), (0, 2),
        (11, 12), (9, 10), (0, 6), (7, 8), (9, 11), (5, 3)
    ]
    g = Graph(l)
    bfp = BreadthFirstPaths(g, 6)
    print bfp.edge_to  # [6, 0, 0, 4, 6, 0, None, None, None, None, None, None, None]
    print bfp._marked  # [True, True, True, True, True, True, True, False, False, False, False,
    # False, False]
    print bfp.dist_to  # [1, 2, 2, 2, 1, 2, 0, inf, inf, inf, inf, inf, inf]
    # shortest path array from src 6
Exemplo n.º 3
0
                x = vertex
                while x != adj:
                    try:
                        x = self.prev_to[x]
                        self.cycle.append(x)
                    except KeyError:
                        break
                self.cycle.append(adj)
                return


if __name__ == '__main__':

    from graph_api import Graph

    g = Graph()
    g.add_edge(0)
    g.add_edge(1, 0)
    g.add_edge(2, 0)
    g.add_edge(6, 0)
    g.add_edge(5, 0)
    g.add_edge(3, 1)
    g.add_edge(3, 2)
    g.add_edge(4, 2)
    g.add_edge(4, 6)
    g.add_edge(5, 0)
    g.add_edge(5, 4)

    bp = BipartiteGraph(g)
    assert bp.is_bipartite