Пример #1
0
def read(path):
    trie = Trie()
    parser = Parser()
    graph = Graph()

    # Adding words into the trie and links into the graph:
    for root, dirs, files in os.walk(path):
        for path in files:
            if path.endswith(".html") or path.endswith(".htm"):
                current = os.path.join(root, path)
                u = graph.insert_vertex(current)
                links, words = parser.parse(current)
                for link in links:
                    v = graph.insert_vertex(link)
                    graph.insert_edge(u, v)
                for word in words:
                    trie.add(word.lower(), current)
    return trie, graph
Пример #2
0
def create_graph(currencies: Set[Currency]) -> Graph:
    """
    Creates a directed graph representing currencies.
    :param currencies: the currencies to insert in the graph
    :return: the graph representing currencies
    """
    g = Graph(directed=True)
    for currency in currencies:
        if currency._code not in g.vertices():
            g.insert_vertex(currency._code)
        for c in currencies:
            code = c._code
            try:
                change = currency.get_change(code)
                if code not in g.vertices():
                    g.insert_vertex(code)
                g.insert_edge(currency._code, code, round(change, 2))
            except KeyError:
                continue
    return g
Пример #3
0
    def find_path(self, start, end):
        path = []

        if (start == end or end == -1):
            path.append(start)
        else:
            path += self.find_path(start, self.parent[end])
            path.append(end)

        return path


print "Initialize Graph:"

graph = Graph(1000, False)
graph.insert_edge(1, 2)
graph.insert_edge(1, 3)
graph.insert_edge(1, 4)
graph.insert_edge(10, 1)
graph.insert_edge(10, 2)
graph.insert_edge(20, 2)
graph.print_graph()

print "\nInitialize DFS and process graph"

dfs = DFS(graph)
dfs.process_graph(1)

print "\nFind shortest path from 1 to 20:"
print " - ".join(str(i) for i in dfs.find_path(1, 20))