示例#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
def generate_pascal_triangle(n) -> list[list]:
    triangle: list[list] = []
    zero_th_row = [1]
    triangle.append(zero_th_row)

    for i in range(1, n):  # start from 1th row
        previous_row = triangle[i - 1]
        length_of_previous_row = len(previous_row)

        new_row: list = [None] * (length_of_previous_row + 1)
        new_row[0] = 1  # as we know the first and last element will be 1
        new_row[-1] = 1

        for idx, element in enumerate(new_row):
            if element is None:  # as for first and last, elements are already updated to 1
                new_row[idx] = previous_row[idx - 1] + previous_row[idx]

        triangle.append(new_row)  # adding this new row in the triangle

    return triangle


if __name__ == "__main__":
    number_of_rows = int(input())
    pascal_triangle = generate_pascal_triangle(number_of_rows)
    print_matrix(pascal_triangle)
"""
5
10
"""
示例#3
0
                zero_value_columns.add(j)

    # print(zero_value_rows, zero_value_columns)

    # going through all the matrix element, and where the i or j belongs to our set, we make that matrix element 0
    for i in range(n):
        for j in range(m):
            if i in zero_value_rows or j in zero_value_columns:
                matrix[i][j] = 0

    print()


if __name__ == "__main__":
    array_2d = input_matrix()
    print_matrix(array_2d)
    set_matrix_zero(array_2d)
    print_matrix(array_2d)
"""
3
1 1 1
1 0 1
1 1 1
"""
"""
3
0 1 2 0
3 4 5 2
1 3 1 5
"""
示例#4
0
                         node_color='pink',
                         alpha=1)
        # create edge label
        labels = {
            edge: pair[2]
            for edge, pair in zip(only_edges, self.edge_pairs)
        }
        # Draw edge labels according to node positions
        nx.draw_networkx_edge_labels(graph, pos, edge_labels=labels)
        plt.axis('off')
        plt.show()


if __name__ == "__main__":
    weighted_bi_directed_graph = WeightedBiDirectedGraph()
    print_matrix(weighted_bi_directed_graph.adj_matrix)
    weighted_bi_directed_graph.print_depth_first()
    weighted_bi_directed_graph.print_breadth_first()
    weighted_bi_directed_graph.draw()
"""
    2 --9--  1 --0-- 3

3
2
1 2 9
1 3 1
"""
"""
    2 --9--  1  --0-- 3           4 --88-- 5

5
示例#5
0
from Recursion.Aditya_Verma.Input_Output_Method.Subsets_Powerset_Subsequence.Of_List.Not_Handling_Duplicate_Elements.Get__All_Subsets__Idx_Impl import \
    get_all_list_subsets
from Utils.Array import input_array
from Utils.Matrix import print_matrix


def subset_of_array(arr) -> list:
    return get_all_list_subsets(arr)  # To handle duplicate : call get_all_unique_list_subsets(arr) & print it


if __name__ == "__main__":
    n = int(input())
    array = input_array()
    print_matrix(subset_of_array(array))
示例#6
0
    n = len(nums)
    row_size = n + 1
    col_size = sum + 1

    dp = [[False for x in range(col_size)] for y in range(row_size)]

    # Base Case -> Initialization
    for j in range(1, col_size):  # Filling 0th row by F, leaving dp[0][0]
        dp[0][j] = False

    for i in range(row_size):  # Filling 0th col by T (including dp[0][0] )
        dp[i][0] = True

    # Coding the choice diagram
    # DO NOT override the initialization values, NOTE
    for i in range(1, row_size):
        for j in range(1, col_size):
            curr_num = nums[i - 1]  # as i is 1_indexed
            if curr_num <= j:
                dp[i][j] = dp[i - 1][j - curr_num] or dp[i - 1][j]
            else:
                dp[i][j] = dp[i - 1][j]

    # return dp[n][sum]
    return dp  # notice : extra change


if __name__ == "__main__":
    print_matrix(subset_sum__table([3, 34, 4, 12, 5, 2], 4))
    print_matrix(subset_sum__table([3, 34, 4, 12, 5, 2], 1))
                         node_color='pink',
                         alpha=1)
        # create edge label
        labels = {
            edge: pair[2]
            for edge, pair in zip(only_edges, self.edge_pairs)
        }
        # Draw edge labels according to node positions
        nx.draw_networkx_edge_labels(graph, pos, edge_labels=labels)
        plt.axis('off')
        plt.show()


if __name__ == "__main__":
    weighted_unidirected_graph = WeightedUniDirectedGraph()
    print_matrix(weighted_unidirected_graph.adj_matrix)
    weighted_unidirected_graph.print_depth_first()
    weighted_unidirected_graph.print_breadth_first()
    weighted_unidirected_graph.draw()
"""
    2 <--9-- 1 --0-> 3

3
2
1 2 9
1 3 1
"""
"""
    2 <--9-- 1 --0--> 3           4 <--88-- 5

5