Kayıtlar

Python'da Graph'ın En kısa yol problemi

def find_shortest_path(graph, start, end, path=[]):         path = path + [start]         if start == end:             return path         if not graph.has_key(start):             return None         shortest = None         for node in graph[start]:             if node not in path:                 newpath = find_shortest_path(graph, node, end, path)                 if newpath:                     if not shortest or len(newpath) < len(shortest):                         shortest = newpath         return shortest graph = {'A': ['B', 'C'],              'B': ['C', 'D'],              'C': ['D'],              'D': ['C'],              'E': ['F'],              'F': ['C']} find_shortest_path(graph, 'A', 'D') #    ['A', 'C', 'D'] # O(V^2)

Python'da N tane Elemana Sahip Listenin Alt Kümesini Bulma

Untitled In [1]: from random import randint def create_A_vector ( size ): my_vector = [] for i in range ( size ): my_vector . append ( randint ( 0 , 9 )) return my_vector In [2]: import itertools size = 10 #stuff = [1, 2, 3] stuff = create_A_vector ( size ) for L in range ( 0 , len ( stuff ) + 1 ): for subset in itertools . combinations ( stuff , L ): print ( subset ) () (3,) (2,) (4,) (1,) (7,) (6,) (7,) (9,) (7,) (4,) (3, 2) (3, 4) (3, 1) (3, 7) (3, 6) (3, 7) (3, 9) (3, 7) (3, 4) (2, 4) (2, 1) (2, 7) (2, 6) (2, 7) (2, 9) (2, 7) (2, 4) (4, 1) (4, 7) (4, 6) (4, 7) (4, 9) (4, 7) (4, 4) (1, 7) (1, 6) (1, 7) (1, 9) (1, 7) (1, 4) (7, 6) (7, 7) (7, 9) (7, 7) (7, 4) (6, 7) (6, 9) (6, 7) (6, 4) (7, 9) (7, 7) (7, 4) (9, 7) (9, 4) (7, 4) (3, 2, 4) (3, 2, 1) (3, 2, 7) (3, 2, 6) (3, 2, 7) (3, 2, 9) (3, 2, 7) (3, 2, 4) (3, 4, 1) (3, 4, 7) (3, 4, 6) (