Пример #1
0
def main():
    symbol = ['a', 'b', 'c', 'd', 'e', 'f', 'h', 'p', 'q', 'r', 'S', 'G']

    graph_matrix = [
        # a, b, c, d, e, f, h, p, q, r, S, G
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0],
        [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    ]

    S = 10
    G = 11

    graph = GraphSearch(graph_matrix)

    dfs = graph.dfs(S, G)
    print('Depth first search: ')
    print_search_tree(dfs.root, display=lambda d: symbol[d])
    print('The route:')
    print('→'.join(map(lambda d: symbol[d], dfs.path)))

    bfs = graph.bfs(S, G)
    print('Breadth first search: ')
    print_search_tree(bfs.root, display=lambda d: symbol[d])
    print('The route:')
    print('→'.join(map(lambda d: symbol[d], bfs.path)))
Пример #2
0
def main():
    symbol = [
        'Arad', 'Bucharest', 'Craiova', 'Dobreta', 'Eforie', 'Fagaras',
        'Giurgiu', 'Hirsova', 'Iasi', 'Lugoj', 'Mehadia', 'Neamt', 'Oradea',
        'Pitesti', 'Rimnicu Vilcea', 'Sibiu', 'Timisoara', 'Urziceni',
        'Vaslui', 'Zerind'
    ]

    cost_graph = [
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 140, 118, 0, 0, 75],
        [0, 0, 0, 0, 0, 211, 90, 0, 0, 0, 0, 0, 0, 101, 0, 0, 0, 85, 0, 0],
        [0, 0, 0, 120, 0, 0, 0, 0, 0, 0, 0, 0, 0, 138, 146, 0, 0, 0, 0, 0],
        [0, 0, 120, 0, 0, 0, 0, 0, 0, 0, 75, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 86, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 211, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0],
        [0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 86, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 87, 0, 0, 0, 0, 0, 0, 92, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 70, 0, 0, 0, 0, 0, 111, 0, 0, 0],
        [0, 0, 0, 75, 0, 0, 0, 0, 0, 70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 87, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 151, 0, 0, 0, 71],
        [0, 101, 138, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 0, 0, 0, 0, 0],
        [0, 0, 146, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 0, 80, 0, 0, 0, 0],
        [140, 0, 0, 0, 0, 99, 0, 0, 0, 0, 0, 0, 151, 0, 80, 0, 0, 0, 0, 0],
        [118, 0, 0, 0, 0, 0, 0, 0, 0, 111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 85, 0, 0, 0, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 142, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 92, 0, 0, 0, 0, 0, 0, 0, 0, 142, 0, 0],
        [75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 71, 0, 0, 0, 0, 0, 0, 0]
    ]

    est_cost = [
        366, 0, 160, 242, 161, 178, 77, 151, 226, 244, 241, 234, 380, 98, 193,
        253, 329, 80, 199, 374
    ]

    graph_map = GraphSearch(cost_graph)

    print(
        'Uniform:', '→'.join(
            map(
                lambda d: symbol[d],
                graph_map.uniform(symbol.index('Zerind'),
                                  symbol.index('Bucharest')).path)))
    print(
        'Greedy:', '→'.join(
            map(
                lambda d: symbol[d],
                graph_map.greedy(symbol.index('Zerind'),
                                 symbol.index('Bucharest'), est_cost).path)))
    print(
        'A*:', '→'.join(
            map(
                lambda d: symbol[d],
                graph_map.a_star(symbol.index('Zerind'),
                                 symbol.index('Bucharest'), est_cost).path)))
Пример #3
0
def main():
    symbol = ['Arad',
              'Bucharest',
              'Craiova',
              'Dobreta',
              'Eforie',
              'Fagaras',
              'Giurgiu',
              'Hirsova',
              'Iasi',
              'Lugoj',
              'Mehadia',
              'Neamt',
              'Oradea',
              'Pitesti',
              'Rimnicu Vilcea',
              'Sibiu',
              'Timisoara',
              'Urziceni',
              'Vaslui',
              'Zerind']

    cost_graph = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 140, 118, 0, 0, 75],
                  [0, 0, 0, 0, 0, 211, 90, 0, 0, 0, 0, 0, 0, 101, 0, 0, 0, 85, 0, 0],
                  [0, 0, 0, 120, 0, 0, 0, 0, 0, 0, 0, 0, 0, 138, 146, 0, 0, 0, 0, 0],
                  [0, 0, 120, 0, 0, 0, 0, 0, 0, 0, 75, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 0, 0, 0, 86, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 211, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0],
                  [0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 86, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 0, 0],
                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 87, 0, 0, 0, 0, 0, 0, 92, 0],
                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 70, 0, 0, 0, 0, 0, 111, 0, 0, 0],
                  [0, 0, 0, 75, 0, 0, 0, 0, 0, 70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 0, 0, 0, 0, 87, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 151, 0, 0, 0, 71],
                  [0, 101, 138, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 0, 0, 0, 0, 0],
                  [0, 0, 146, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 0, 80, 0, 0, 0, 0],
                  [140, 0, 0, 0, 0, 99, 0, 0, 0, 0, 0, 0, 151, 0, 80, 0, 0, 0, 0, 0],
                  [118, 0, 0, 0, 0, 0, 0, 0, 0, 111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 85, 0, 0, 0, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 142, 0],
                  [0, 0, 0, 0, 0, 0, 0, 0, 92, 0, 0, 0, 0, 0, 0, 0, 0, 142, 0, 0],
                  [75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 71, 0, 0, 0, 0, 0, 0, 0]]

    est_cost = [366, 0, 160, 242, 161, 178, 77, 151, 226, 244, 241, 234, 380, 98, 193, 253, 329, 80, 199, 374]

    graph_map = GraphSearch(cost_graph)

    print('Uniform:', '→'.join(map(lambda d: symbol[d],
                                   graph_map.uniform(symbol.index('Zerind'), symbol.index('Bucharest')).path)))
    print('Greedy:', '→'.join(map(lambda d: symbol[d],
                                  graph_map.greedy(symbol.index('Zerind'), symbol.index('Bucharest'), est_cost).path)))
    print('A*:', '→'.join(map(lambda d: symbol[d],
                              graph_map.a_star(symbol.index('Zerind'), symbol.index('Bucharest'), est_cost).path)))
Пример #4
0
def main():
    symbol = [
        'a', 'b', 'c', 'd', 'e', 'f', 'h', 'p', 'q', 'r', 'S', 'G'
    ]

    graph_matrix = [
        # a, b, c, d, e, f, h, p, q, r, S, G
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0],
        [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    ]

    S = 10
    G = 11

    graph = GraphSearch(graph_matrix)

    dfs = graph.dfs(S, G)
    print('Depth first search: ')
    print_search_tree(dfs.root, display=lambda d: symbol[d])
    print('The route:')
    print('→'.join(map(lambda d: symbol[d], dfs.path)))

    bfs = graph.bfs(S, G)
    print('Breadth first search: ')
    print_search_tree(bfs.root, display=lambda d: symbol[d])
    print('The route:')
    print('→'.join(map(lambda d: symbol[d], bfs.path)))
Пример #5
0
def create_random_dag_iter(num_nodes, connectivity=.75):
    return GraphSearch.cut_cycles(create_random_graph_iter(num_nodes, connectivity, DirectedGraph()))
Пример #6
0
def bft_iter_linked_list(graph):
    print("bft_iter:", list_print(GraphSearch.bft_iter(graph)))
Пример #7
0
def bft_rec_linked_list(graph):
    print("bft_rec: ", list_print(GraphSearch.bft_rec(graph)))
Пример #8
0
from graph import Graph
from graphsearch import GraphSearch
import random

gs = GraphSearch()


def createRandomUnweightedGraphIter(n: int):
    g = Graph()
    #Create nodes
    for i in range(n):
        g.addNode(i)

    #shuffle nodes
    list_nodes = list(g.getAllNodes())
    random.shuffle(list_nodes)

    #connect edges in order, like a double-linked list
    prev = None
    for i in list_nodes:
        if prev:
            g.addUndirectedEdge(i, prev)
        prev = i
    return g


def createLinkedList(n: int):
    g = Graph()
    prev = None
    for i in range(n):
        n = g.addNode(i)