Ejemplo n.º 1
0
def graph_randomize(n: int):
    graph = Graph()
    random = Random()

    for i in range(0, n):
        graph.insert_vertex(i)

    for v in graph.vertices():
        for u in graph.vertices():
            if bool(random.getrandbits(1)) and u != v and graph.get_edge(
                    u, v) is None:
                graph.insert_edge(u, v)

    print("number of vertices " + str(graph.vertex_count()) +
          "\nnumber of edges " + str(graph.edge_count()))

    return graph
Ejemplo n.º 2
0
def bacefook_greedy_algorithm(graph: Graph):
    """
        greedy algorithm to compute the minimum number of nodes
        with software

        map structure:

        -Key store all vertices of the graph
        -Value is a dict

            dict structure:

            -token store locator from priority queue
            -so_sw_count store the number of adj nodes
            -has_sw bool if the node has the software

        Priority queue store all the nodes of the graph ordered by number of adj nodes
    """
    priority_queue = AdaptableHeapPriorityQueue()
    map = {}
    for v in graph.vertices():
        token = priority_queue.add(-graph.degree(v), v)
        map[v] = {
            'token': token,
            'no_sw_count': graph.degree(v),
            'has_sw': False
        }

    for i in range(0, graph.vertex_count()):
        k, v = priority_queue.remove_min()
        if graph.degree(v) == 0:
            continue
        if k == 0:
            break
        map[v]['has_sw'] = True
        for e in graph.incident_edges(v):
            adj_v = e.opposite(v)
            if not map[adj_v]['has_sw']:
                map[adj_v]['no_sw_count'] -= 1
                priority_queue.update(map[adj_v]['token'],
                                      -map[adj_v]['no_sw_count'], adj_v)

    return {v._element: map[v]['has_sw'] for v in map}