示例#1
0
def bestTrio(friends_nodes, friends_from, friends_to):
    minimum_friend_score = float("+inf")
    edges = len(friends_from)  # len(friends_to)

    n = friends_nodes
    matrix = get_filled_matrix(n + 1, n + 1, None)  # extra one size
    for i, j in zip(friends_from, friends_to):
        matrix[i][j] = True

    # going row wise
    for main_person in range(1, n):
        persons_friend = matrix[main_person]
        for person_1 in range(1, n):  # as we start from 1, and not 0
            for person_2 in range(person_1 + 1, n):
                if persons_friend[person_1] is True and persons_friend[person_2] is True:
                    # found a trio
                    print(main_person, person_1, person_2)
                    total_outside_friends = 0
                    total_outside_friends += find_friends_outside_trio(main_person, matrix, person_1, person_2)
                    total_outside_friends += find_friends_outside_trio(person_1, matrix, person_2, main_person)
                    total_outside_friends += find_friends_outside_trio(person_2, matrix, person_1, main_person)

                    minimum_friend_score = min(total_outside_friends, minimum_friend_score)
            pass
        pass

    print_matrix(matrix)
    print(minimum_friend_score)
    return minimum_friend_score
示例#2
0
def solve_01knapsack(weight, value, capacity):
    if len(weight) != len(value):
        raise ValueError("Invalid Item array")

    dp = get_filled_matrix(row_size=len(weight) + 1,
                           col_size=capacity + 1,
                           fill=-1)
    return max_profit_in_01knapsack(weight, value, capacity, dp)
    def __init__(self):
        print("V:  E:  and Edges")
        self.vertices, self.edges = list(map(
            int,
            input().strip().split()))  # No of Vertices, No of Edges

        self.edge_pairs: list[tuple] = []  # list of edge pairs
        self.adj_matrix = get_filled_matrix(self.vertices + 1,
                                            self.vertices + 1,
                                            UniDirectedGraph.NOT_CONNECTED)
        # we are adding 2 extra, row & columns, so that we can work easily with 0 based graph as well as 1 based graphs

        for i in range(self.edges):
            edge_from, edge_to = map(int, input().strip().split())
            self.edge_pairs.append((edge_from, edge_to))
            self.adj_matrix[edge_from][
                edge_to] = UniDirectedGraph.CONNECTED  # uni-directional  Notice
示例#4
0
    def __init__(self):
        print("V:  E:  and Edges")
        self.vertices, self.edges = list(map(
            int,
            input().strip().split()))  # No of Vertices, No of Edges

        self.edge_pairs: list[tuple] = []  # list of edge pairs
        self.adj_matrix = get_filled_matrix(self.vertices + 1,
                                            self.vertices + 1,
                                            BiDirectedGraph.NOT_CONNECTED)

        for i in range(self.edges):
            edge_from, edge_to = map(int, input().strip().split())
            self.edge_pairs.append((edge_from, edge_to))
            self.adj_matrix[edge_from][edge_to] = BiDirectedGraph.CONNECTED
            self.adj_matrix[edge_to][
                edge_from] = BiDirectedGraph.CONNECTED  # making bi-directional
def solve_subsetsum_count(nums, sum):
    n = len(nums)
    dp = get_filled_matrix(row_size=n + 1, col_size=sum + 1, fill=-1)
    return count_subsetsum(nums, sum, n, dp)
def solve_subsetsum(nums, sum) -> bool:
    n = len(nums)
    dp = get_filled_matrix(row_size=n + 1, col_size=sum + 1, fill=None)
    result = subsetsum(nums, sum, n, dp)
    return result