コード例 #1
0
    def min_skeleton(self):
        after_reset = deepcopy(self)  # copy object
        after_reset.empty()  # empty it

        weights, inverted = parse.invert_dict(self.vertices)

        to_cover = set(self.nodes.keys())
        covered = set()

        while covered != to_cover:
            for weight in weights:
                for vertex in inverted[weight]:
                    a, b = vertex.split(" - ")
                    new = deepcopy(after_reset)

                    # connect nodes in graph
                    new.nodes[a].connect(b)
                    new.nodes[b].connect(a)
                    # add vertice to vertices
                    new.vertices[vertex] = weight

                    if not new.has_loop():
                        after_reset = new
                        covered.add(a)
                        covered.add(b)

        return after_reset
コード例 #2
0
def run(data):
    transformers = Network(data, Transformer, DELIM, ": ")

    after_reset = deepcopy(transformers)  # copy object
    after_reset.empty()  # empty it

    weights, inverted = invert_dict(transformers.vertices)

    to_cover = set(transformers.nodes.keys())
    covered = set()

    while covered != to_cover:
        for weight in weights:
            for vertex in inverted[weight]:
                a, b = vertex.split(DELIM)
                new = deepcopy(after_reset)

                # connect nodes in graph
                new.nodes[a].connect(b)
                new.nodes[b].connect(a)
                # add vertice to vertices
                new.vertices[vertex] = weight

                if not new.has_loop():
                    after_reset = new
                    covered.add(a)
                    covered.add(b)

    total_cost = 0
    for vertex, cost in after_reset.vertices.items():
        print(f"{vertex}: {cost}")
        total_cost += cost

    print("Hodnoceni:", total_cost)
コード例 #3
0
ファイル: car.py プロジェクト: Tiboris/TGR
def run(data):
    start = "Vy"
    car = Graph(data, Transformer, " - ", ": ")
    endpoints = car.nodes.keys()

    res = {}
    for endpoint in endpoints:
        path, cost = car.dijkstra(start, endpoint)
        res[endpoint] = 0 - cost

    inverted = invert_dict(res)
    weights = sorted(inverted)
    for weight in reversed(weights):
        for endpoint in sorted(inverted[weight]):
            print("{}: {}".format(endpoint, weight))
コード例 #4
0
ファイル: race.py プロジェクト: Tiboris/TGR
def run(data):
    crossroads = Graph(data, Crossroad)
    start = "A"
    distances, predecessors = crossroads.bellman_ford(start)
    successors = invert_dict(predecessors)
    path = start + " - " + successors[start][0]
    while (path.count(" - ") + 1) != len(crossroads.nodes):
        path = path + " - " + successors[path[-1]][0]

    bonuses = 0  # get all bonuses
    for node in crossroads.nodes:
        if crossroads.nodes[node].has_bonus:
            bonuses += path.count(node)

    print(path + ":", (-distances[path[-1]]) + bonuses)