def grau(g: Grafo, vert): N = g.N M = g.M contador = 0 for i in N: if g.existe_vertice(vert) and M[N.index(vert)][N.index( i)] != '-' and g.existe_aresta(vert + '-' + i): contador += M[N.index(vert)][N.index(i)] elif g.existe_vertice(vert) and M[N.index(i)][N.index( vert)] != '-' and g.existe_aresta(i + '-' + vert): contador += M[N.index(i)][N.index(vert)] return contador
def arestas_sobre_vertice(g: Grafo, vertice): M = g.M N = g.N lista = [] for i in range(len(M)): if g.existe_vertice(vertice) and M[i][N.index( vertice)] != '-' and M[i][N.index(vertice)] > 0: lista.append(vertice + '-' + N[i]) if g.existe_vertice(vertice) and M[N.index(vertice)][i] != '-' and M[ N.index(vertice)][i] > 0: lista.append(vertice + '-' + N[i]) return lista
def setUp(self): self.g_p = Grafo([], []) for i in range(33): self.g_p.adiciona_vertice('V{}'.format(i)) for i in [ 'V0-V1', 'V0-V2', 'V0-V3', 'V1-V2', 'V2-V5', 'V1-V4', 'V3-V7', 'V3-V11', 'V4-V5', 'V4-V8', 'V5-V6', 'V5-V10', 'V5-V9', 'V6-V3', 'V6-V10', 'V7-V6', 'V8-V12', 'V9-V13', 'V10-V14', 'V10-V11', 'V11-V15', 'V12-V16', 'V13-V17', 'V14-V18', 'V14-V17', 'V14-V16', 'V15-V19', 'V15-V17', 'V16-V17', 'V17-V18', 'V17-V20', 'V18-V19', 'V18-V22', 'V19-V23', 'V20-V24', 'V20-V25', 'V21-V17', 'V22-V26', 'V22-V27', 'V23-V28', 'V23-V27', 'V25-V21', 'V25-V30', 'V28-V29', 'V29-V30', 'V30-V31', 'V30-V32', 'V31-V32' ]: self.g_p.adiciona_aresta(i)
def setUp(self): self.r5 = Grafo([], []) self.vertices = [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g" ] self.arestas = [ 'A-B', 'A-C', 'A-D', 'B-E', 'B-I', 'C-G', 'D-C', 'D-H', 'E-F', 'F-B', 'F-J', 'G-F', 'G-J', 'G-K', 'H-G', 'H-L', 'I-M', 'J-I', 'J-N', 'K-O', 'L-P', 'M-Q', 'M-S', 'N-R', 'N-S', 'N-T', 'O-S', 'P-T', 'Q-U', 'R-Q', 'R-V', 'S-R', 'S-X', 'U-Y', 'U-Z', 'V-b', 'V-Z', 'V-W', 'W-S', 'X-c', 'X-d', 'Y-a', 'a-e', 'c-e', 'c-W', 'e-f', 'e-g', 'T-S' ] for i in self.vertices: self.r5.adiciona_vertice(i) for i in self.arestas: self.r5.adiciona_aresta(i)
def main(): g_p = Grafo([], []) for i in ['J', 'C', 'E', 'P', 'M', 'T', 'Z', 'Y']: g_p.adiciona_vertice(i) for i in [ 'J-C', 'C-E', 'C-P', 'C-M', 'C-T', 'E-Y', 'M-T', 'P-M', 'T-Z', 'M-Z', 'Z-Y', 'Y-P' ]: g_p.adiciona_aresta(i) print() print(g_p) print(Dijkstra(g_p, 'Z', 'T')) print()
def main(): g_p = Grafo([], []) for i in ['J', 'C', 'E', 'P', 'M', 'T', 'Z']: g_p.adiciona_vertice(i) for i in ['J-C', 'C-E', 'C-E', 'C-P', 'C-P', 'C-M', 'C-T', 'M-T', 'T-Z']: g_p.adiciona_aresta(i) print(g_p) E = warshall(g_p) for i in range(len(E)): for j in range(len(E)): print(E[i][j], end=" ") print() #main()
def setUp(self): # Grafo da Paraíba self.g_p = Grafo([], []) for i in ['J', 'C', 'E', 'P', 'M', 'T', 'Z']: self.g_p.adicionaVertice(i) for i in [ 'J-C', 'C-E', 'C-E', 'C-P', 'C-P', 'C-M', 'C-T', 'M-T', 'T-Z' ]: self.g_p.adicionaAresta(i) # Grafo da Paraíba sem arestas paralelas self.g_p_sem_paralelas = Grafo([], []) for i in ['J', 'C', 'E', 'P', 'M', 'T', 'Z']: self.g_p_sem_paralelas.adicionaVertice(i) for i in ['J-C', 'C-E', 'C-P', 'C-M', 'C-T', 'M-T', 'T-Z']: self.g_p_sem_paralelas.adicionaAresta(i) # Grafos completos self.g_c = Grafo([], []) for i in ['J', 'C', 'E', 'P']: self.g_c.adicionaVertice(i) for i in [ 'J-C', 'J-E', 'J-P', 'C-J', 'C-E', 'C-P', 'E-J', 'E-C', 'E-P', 'P-J', 'P-C', 'P-E' ]: self.g_c.adicionaAresta(i) self.g_c3 = Grafo([], []) self.g_c3.adicionaVertice('J') # Grafos com laco self.g_l1 = Grafo([], []) for i in ['A', 'B', 'C', 'D']: self.g_l1.adicionaVertice(i) for i in ['A-A', 'B-A', 'A-A']: self.g_l1.adicionaAresta(i) self.g_l2 = Grafo([], []) for i in ['A', 'B', 'C', 'D']: self.g_l2.adicionaVertice(i) for i in ['A-B', 'B-B', 'B-A']: self.g_l2.adicionaAresta(i) self.g_l3 = Grafo([], []) for i in ['A', 'B', 'C', 'D']: self.g_l3.adicionaVertice(i) for i in ['C-A', 'C-C', 'D-D']: self.g_l3.adicionaAresta(i) self.g_l4 = Grafo([], []) self.g_l4.adicionaVertice('D') self.g_l4.adicionaAresta('D-D') self.g_l5 = Grafo([], []) for i in ['C', 'D']: self.g_l5.adicionaVertice(i) for i in ['D-C', 'C-C']: self.g_l5.adicionaAresta(i)
class TestGrafo(unittest.TestCase): def setUp(self): # Grafo da Paraíba self.g_p = Grafo([], []) for i in ['J', 'C', 'E', 'P', 'M', 'T', 'Z']: self.g_p.adicionaVertice(i) for i in [ 'J-C', 'C-E', 'C-E', 'C-P', 'C-P', 'C-M', 'C-T', 'M-T', 'T-Z' ]: self.g_p.adicionaAresta(i) # Grafo da Paraíba sem arestas paralelas self.g_p_sem_paralelas = Grafo([], []) for i in ['J', 'C', 'E', 'P', 'M', 'T', 'Z']: self.g_p_sem_paralelas.adicionaVertice(i) for i in ['J-C', 'C-E', 'C-P', 'C-M', 'C-T', 'M-T', 'T-Z']: self.g_p_sem_paralelas.adicionaAresta(i) # Grafos completos self.g_c = Grafo([], []) for i in ['J', 'C', 'E', 'P']: self.g_c.adicionaVertice(i) for i in [ 'J-C', 'J-E', 'J-P', 'C-J', 'C-E', 'C-P', 'E-J', 'E-C', 'E-P', 'P-J', 'P-C', 'P-E' ]: self.g_c.adicionaAresta(i) self.g_c3 = Grafo([], []) self.g_c3.adicionaVertice('J') # Grafos com laco self.g_l1 = Grafo([], []) for i in ['A', 'B', 'C', 'D']: self.g_l1.adicionaVertice(i) for i in ['A-A', 'B-A', 'A-A']: self.g_l1.adicionaAresta(i) self.g_l2 = Grafo([], []) for i in ['A', 'B', 'C', 'D']: self.g_l2.adicionaVertice(i) for i in ['A-B', 'B-B', 'B-A']: self.g_l2.adicionaAresta(i) self.g_l3 = Grafo([], []) for i in ['A', 'B', 'C', 'D']: self.g_l3.adicionaVertice(i) for i in ['C-A', 'C-C', 'D-D']: self.g_l3.adicionaAresta(i) self.g_l4 = Grafo([], []) self.g_l4.adicionaVertice('D') self.g_l4.adicionaAresta('D-D') self.g_l5 = Grafo([], []) for i in ['C', 'D']: self.g_l5.adicionaVertice(i) for i in ['D-C', 'C-C']: self.g_l5.adicionaAresta(i) def test_vertices_nao_adjacentes(self): self.assertEqual( set(self.g_p.vertices_nao_adjacentes()), set([ 'C-C', 'C-Z', 'E-E', 'E-M', 'E-P', 'E-T', 'E-Z', 'J-E', 'J-J', 'J-M', 'J-P', 'J-T', 'J-Z', 'M-M', 'M-Z', 'P-M', 'P-P', 'P-T', 'P-Z', 'T-T', 'Z-Z' ])) self.assertEqual(self.g_c.vertices_nao_adjacentes(), ['J-J', 'C-C', 'E-E', 'P-P']) self.assertEqual(self.g_c3.vertices_nao_adjacentes(), ['J-J'])
def ha_laco(g: Grafo): N = g.N for i in N: if g.existe_aresta(i + '-' + i): return True return False
class TestGrafo(unittest.TestCase): def setUp(self): self.g_p = Grafo([], []) for i in range(33): self.g_p.adiciona_vertice('V{}'.format(i)) for i in [ 'V0-V1', 'V0-V2', 'V0-V3', 'V1-V2', 'V2-V5', 'V1-V4', 'V3-V7', 'V3-V11', 'V4-V5', 'V4-V8', 'V5-V6', 'V5-V10', 'V5-V9', 'V6-V3', 'V6-V10', 'V7-V6', 'V8-V12', 'V9-V13', 'V10-V14', 'V10-V11', 'V11-V15', 'V12-V16', 'V13-V17', 'V14-V18', 'V14-V17', 'V14-V16', 'V15-V19', 'V15-V17', 'V16-V17', 'V17-V18', 'V17-V20', 'V18-V19', 'V18-V22', 'V19-V23', 'V20-V24', 'V20-V25', 'V21-V17', 'V22-V26', 'V22-V27', 'V23-V28', 'V23-V27', 'V25-V21', 'V25-V30', 'V28-V29', 'V29-V30', 'V30-V31', 'V30-V32', 'V31-V32' ]: self.g_p.adiciona_aresta(i) def test_dijkstra_drone(self): self.assertEqual( self.g_p.dijkstra_drone('V0', 'V32', 5, 5, ['V8', 'V17', 'V23', 'V29']), ['V0', 'V3', 'V11', 'V15', 'V17', 'V20', 'V25', 'V30', 'V32']) self.assertEqual( self.g_p.dijkstra_drone('V0', 'V32', 3, 5, ['V8', 'V17', 'V23', 'V29']), [ 'V0', 'V1', 'V4', 'V8', 'V12', 'V16', 'V17', 'V20', 'V25', 'V30', 'V32' ]) self.assertEqual( self.g_p.dijkstra_drone('V0', 'V32', 2, 5, ['V8', 'V17', 'V23', 'V29']), 'Fuel Low') self.assertEqual( self.g_p.dijkstra_drone( 'V0', 'V32', 3, 3, ['V8', 'V17', 'V23', 'V29'], ), [ 'V0', 'V1', 'V4', 'V8', 'V12', 'V16', 'V17', 'V18', 'V19', 'V23', 'V28', 'V29', 'V30', 'V32' ]) self.assertEqual( self.g_p.dijkstra_drone('V0', 'V32', 4, 3, ['V8', 'V17', 'V23', 'V29']), [ 'V0', 'V3', 'V11', 'V15', 'V17', 'V18', 'V19', 'V23', 'V28', 'V29', 'V30', 'V32' ]) self.assertEqual( self.g_p.dijkstra_drone('V0', 'V32', 3, 5, ['V10', 'V16', 'V19', 'V27']), [ 'V0', 'V2', 'V5', 'V10', 'V14', 'V16', 'V17', 'V20', 'V25', 'V30', 'V32' ]) self.assertEqual( self.g_p.dijkstra_drone('V0', 'V32', 3, 3, ['V10', 'V16', 'V19', 'V27']), 'Fuel Low') self.assertEqual( self.g_p.dijkstra_drone('V0', 'V32', 5, 5, ['V10', 'V16', 'V19', 'V27']), [ 'V0', 'V3', 'V11', 'V15', 'V19', 'V23', 'V28', 'V29', 'V30', 'V32' ]) self.assertEqual( self.g_p.dijkstra_drone('V0', 'V32', 2, 5, ['V10', 'V16', 'V19', 'V27']), 'Fuel Low') self.assertEqual( self.g_p.dijkstra_drone('V0', 'V32', 8, 8, ['V10', 'V16', 'V19', 'V27']), ['V0', 'V3', 'V11', 'V15', 'V17', 'V20', 'V25', 'V30', 'V32'])
def setUp(self): def process_data(data): final_data = [] for i in range(len(data)): final_data.append([data[i], 1]) return final_data # Busca em profundidade grafo 1 do roteiro self.roteiro_nao_direcionado = Grafo([], []) for i in (['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11']): self.roteiro_nao_direcionado.adiciona_vertice(i) for i in (process_data([ '1-2', '1-3', '1-4', '2-3', '2-8', '2-9', '2-10', '2-11', '3-4', '3-5', '3-6', '3-7', '4-5', '4-6', '7-8', '9-10', '9-11' ])): self.roteiro_nao_direcionado.adiciona_aresta(i) # Busca em profundidade grafo 2 do roteiro self.roteiro_direcionado = Grafo([], []) for i in ([ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13' ]): self.roteiro_direcionado.adiciona_vertice(i) for i in (process_data([ '1-2', '1-4', '1-7', '2-3', '2-6', '2-6', '3-4', '3-5', '5-2', '6-4', '6-7', '8-9', '9-10', '9-12', '10-11', '10-12', '11-6', '11-9', '13-8', '13-9', '13-12' ])): self.roteiro_direcionado.adiciona_aresta(i) # Ordenação Topológica SLIDE self.slide = Grafo([], []) for i in (['2', '3', '5', '7', '8', '9', '10', '11']): self.slide.adiciona_vertice(i) for i in (process_data([ '7-8', '7-11', '3-11', '5-8', '5-10', '8-9', '11-2', '11-9', '11-10' ])): self.slide.adiciona_aresta(i) # Ordenação Topológica TECNOLOGIA EM CONSTRUÇÃO DE EDIFÍCIOS self.edificios = Grafo([], []) for i in ([ '11', '12', '13', '14', '15', '16', '17', '18', '21', '22', '23', '24', '25', '26', '27', '31', '32', '33', '34', '35', '36', '37', '38', '41', '42', '43', '44', '45', '46', '47', '51', '52', '53', '54', '55', '56', '57', '61', '62', '63', '64', '65', '66', '67', '68', '71', '72', '73', '74', '75', '76' ]): self.edificios.adiciona_vertice(i) for i in (process_data([ '15-21', '14-23', '11-24', '17-24', '15-25', '17-26', '17-27', '15-32', '21-32', '21-33', '15-34', '23-35', '26-36', '23-37', '24-38', '17-41', '21-41', '17-42', '21-42', '11-43', '23-43', '35-43', '35-45', '17-46', '32-46', '21-47', '37-51', '45-51', '46-52', '51-52', '17-53', '32-53', '47-54', '17-55', '32-55', '46-56', '43-57', '31-62', '57-62', '37-64', '45-64', '22-66', '31-67', '57-67', '13-71', '22-71' ])): self.edificios.adiciona_aresta(i) # Ordenação Topológica LICENCIATURA EM FÍSICA self.fisica = Grafo([], []) for i in ([ '11', '12', '13', '14', '15', '16', '17', '21', '22', '23', '24', '25', '26', '27', '31', '32', '33', '34', '35', '36', '37', '41', '42', '43', '44', '45', '46', '51', '52', '53', '54', '55', '56', '57', '61', '62', '63', '64', '65', '66', '68', '71', '72', '73', '74', '75', '76', '81', '82', '83', '84', '85', '86' ]): self.fisica.adiciona_vertice(i) for i in (process_data([ '11-21', '12-21', '11-22', '12-22', '12-23', '12-24', '14-24', '15-25', '21-31', '23-31', '21-32', '22-32', '23-33', '31-41', '31-42', '32-42', '33-45', '31-46', '41-51', '45-51', '41-52', '42-52', '45-53', '31-54', '43-55', '21-57', '43-57', '51-61', '51-62', '52-62', '21-63', '53-63', '51-64', '56-66', '31-68', '57-68', '61-71', '41-72', '45-72', '66-73', '31-74', '43-74', '41-76', '68-76', '65-81', '74-82', '73-83', '54-84', '71-84', '16-85', '25-85', '51-86', '76-86' ])): self.fisica.adiciona_aresta(i) # Ordenação Topológica LICENCIATURA EM MATEMÁTICA self.matematica = Grafo([], []) for i in ([ '11', '12', '13', '14', '15', '16', '17', '21', '22', '23', '24', '25', '26', '27', '31', '32', '33', '34', '35', '36', '41', '42', '43', '44', '45', '46', '51', '52', '53', '54', '55', '56', '57', '61', '62', '63', '64', '65', '66', '67', '71', '72', '73', '74', '75', '76', '77', '81', '82', '83', '84', '85', '86', '87' ]): self.matematica.adiciona_vertice(i) for i in (process_data([ '11-21', '11-22', '13-22', '16-26', '21-31', '22-32', '12-33', '12-34', '21-41', '23-41', '23-42', '32-42', '36-43', '34-44', '27-45', '33-51', '12-52', '32-53', '44-54', '44-55', '44-57', '51-61', '52-62', '32-63', '54-64', '46-65', '57-67', '42-71', '22-72', '41-73', '42-73', '64-74', '65-75', '67-77', '62-81', '75-82', '32-83', '74-84', '77-87' ])): self.matematica.adiciona_aresta(i) # Ordenação Topológica ENGENHARIA DE COMPUTACÃO self.engenhariaDeComputacao = Grafo([], []) for i in [ '11', '12', '13', '14', '15', '16', '17', '21', '22', '23', '24', '25', '26', '27', '31', '32', '33', '34', '35', '36', '41', '42', '43', '44', '45', '51', '52', '53', '54', '55', '61', '62', '63', '64', '65', '71', '72', '73', '74', '75', '81', '82', '83', '84', '85', '91', '92', '93', '94', '101', '102', '103' ]: self.engenhariaDeComputacao.adiciona_vertice(i) for i in process_data([ '11-21', '21-31', '21-41', '31-51', '51-61', '34-81', '35-81', '54-81', '31-52', '43-62', '24-72', '73-82', '83-92', '24-33', '24-43', '24-53', '34-63', '35-63', '63-73', '74-83', '44-93', '45-93', '92-103', '14-24', '15-24', '14-34', '15-34', '24-44', '36-44', '24-54', '31-64', '61-84', '64-84', '61-94', '75-94', '14-25', '15-25', '14-35', '15-35', '36-45', '36-55', '44-55', '55-65', '52-75', '64-75', '75-85', '16-26', '26-36' ]): self.engenhariaDeComputacao.adiciona_aresta(i) # Ordenação Topológica LETRAS self.letras = Grafo([], []) for i in [ '11', '12', '13', '14', '15', '16', '17', '21', '22', '23', '24', '25', '26', '27', '31', '32', '33', '34', '35', '36', '37', '41', '42', '43', '44', '45', '46', '47', '51', '52', '53', '54', '55', '56', '57', '61', '62', '63', '64', '65', '66', '67', '68', '71', '72', '73', '74', '75', '76', '77', '78', '79', '81', '82', '83', '84', '85', '86', '87', '88' ]: self.letras.adiciona_vertice(i) for i in process_data([ '11-21', '21-31', '31-41', '31-51', '31-61', '31-71', '11-22', '21-32', '33-42', '35-52', '31-62', '31-72', '12-23', '21-33', '25-43', '13-53', '35-63', '31-73', '17-83', '24-34', '25-44', '36-44', '45-54', '54-64', '64-74', '74-84', '12-25', '35-25', '35-55', '35-75', '17-26', '23-46', '35-46', '22-56', '45-76', '37-47', '37-57', '37-67', '27-77', '53-77', '77-87', '54-68', '64-78', '68-78', '74-88', '78-88', '65-79' ]): self.letras.adiciona_aresta(i) # Ordenação Topológica TELEMÁTICA self.telematica = Grafo([], []) for i in [ '11', '12', '13', '14', '15', '16', '17', '21', '22', '23', '24', '25', '26', '27', '31', '32', '33', '34', '35', '36', '37', '41', '42', '43', '44', '45', '46', '47', '51', '52', '53', '54', '55', '56', '61', '62', '63', '64', '65' ]: self.telematica.adiciona_vertice(i) for i in process_data([ '11-21', '21-31', '31-41', '41-51', '42-61', '51-61', '12-22', '16-22', '26-32', '31-42', '41-52', '53-62', '12-23', '16-23', '22-33', '23-33', '26-33', '32-43', '44-53', '13-24', '25-34', '32-44', '33-44', '44-54', '25-35', '33-45', '37-55', '41-55', '44-55', '16-26', '21-36', '24-36', '21-46', '34-46' ]): self.telematica.adiciona_aresta(i)
class TestGrafo(unittest.TestCase): def setUp(self): # Grafo da Paraíba self.g_p = Grafo([], []) for i in ['J', 'C', 'E', 'P', 'M', 'T', 'Z']: self.g_p.adiciona_vertice(i) for i in [ 'J-C', 'C-E', 'C-E', 'C-P', 'C-P', 'C-M', 'C-T', 'M-T', 'T-Z' ]: self.g_p.adiciona_aresta(i) # Grafo da Paraíba sem arestas paralelas self.g_p_sem_paralelas = Grafo([], []) for i in ['J', 'C', 'E', 'P', 'M', 'T', 'Z']: self.g_p_sem_paralelas.adiciona_vertice(i) for i in ['J-C', 'C-E', 'C-P', 'C-M', 'C-T', 'M-T', 'T-Z']: self.g_p_sem_paralelas.adiciona_aresta(i) # Grafos completos self.g_c = Grafo([], []) for i in ['J', 'C', 'E', 'P']: self.g_c.adiciona_vertice(i) for i in [ 'J-C', 'J-E', 'J-P', 'C-J', 'C-E', 'C-P', 'E-J', 'E-C', 'E-P', 'P-J', 'P-C', 'P-E' ]: self.g_c.adiciona_aresta(i) self.g_c3 = Grafo([], []) self.g_c3.adiciona_vertice('J') # Grafos com laco self.g_l1 = Grafo([], []) for i in ['A', 'B', 'C', 'D']: self.g_l1.adiciona_vertice(i) for i in ['A-A', 'B-A', 'A-A']: self.g_l1.adiciona_aresta(i) self.g_l2 = Grafo([], []) for i in ['A', 'B', 'C', 'D']: self.g_l2.adiciona_vertice(i) for i in ['A-B', 'B-B', 'B-A']: self.g_l2.adiciona_aresta(i) self.g_l3 = Grafo([], []) for i in ['A', 'B', 'C', 'D']: self.g_l3.adiciona_vertice(i) for i in ['C-A', 'C-C', 'D-D']: self.g_l3.adiciona_aresta(i) self.g_l4 = Grafo([], []) self.g_l4.adiciona_vertice('D') self.g_l4.adiciona_aresta('D-D') self.g_l5 = Grafo([], []) for i in ['C', 'D']: self.g_l5.adiciona_vertice(i) for i in ['D-C', 'C-C']: self.g_l5.adiciona_aresta(i) def teste_warshall(self): self.assertEqual(warshall(self.g_p), [[0, 1, 1, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0]]) self.assertEqual(warshall(self.g_p_sem_paralelas), [[0, 1, 1, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0]]) self.assertEqual( warshall(self.g_c), [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) self.assertEqual(warshall(self.g_c3), [[0]]) self.assertEqual( warshall(self.g_l1), [[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) self.assertEqual( warshall(self.g_l2), [[1, 1, 0, 0], [1, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) self.assertEqual( warshall(self.g_l3), [[0, 0, 0, 0], [0, 0, 0, 0], [1, 0, 1, 0], [0, 0, 0, 1]]) self.assertEqual(warshall(self.g_l4), [[1]]) self.assertEqual(warshall(self.g_l5), [[1, 0], [1, 0]])
# print("de {} ate {}".format("A", "S")) # print("{} --> actual".format(g_challenger.Dijkstra_with_drone(vertice_inicial="A", vertice_final="S", inicial_drone_load=5, max_drone_load=5, load_places=["L", "R", "U", "6"]))) # print("{} --> expected".format(["A", "B", "I", "P", "R", "Y", "1", "3", "S"])) # indian_graph = Grafo([], []) # for v in ["A", "B", "C", "D", "E", "F"]: # indian_graph.adiciona_vertice(v) # for a in ["A-B", "A-D", "A-E", "B-C", "C-D", "E-F", "F-D"]: # indian_graph.adiciona_aresta(a) # # print("indian_graph") # print(indian_graph) # print("de {} ate {}".format("A", "D")) # print(indian_graph.Dijkstra_with_drone_indian(vertice_inicial="A", vertice_final="F")) g_challenger = Grafo([], []) for i in [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "W", "Z", "1", "2", "3", "4", "5", "6", "7" ]: g_challenger.adiciona_vertice(i) for i in [ 'A-B', 'A-C', 'A-D', 'B-H', 'B-I', 'C-F', 'D-C', 'D-E', 'E-F', 'E-L', 'F-G', 'F-J', 'F-K', 'G-B', 'G-J', 'H-G', 'I-P', 'J-I', 'J-O', 'K-N', 'L-M', 'M-Q', 'N-R', 'O-5', 'O-R', 'O-Q', 'P-R', 'P-T', 'Q-R', 'R-5', 'R-Y', 'T-U', 'U-7', 'U-W', 'V-2', 'V-W', 'V-X', 'X-R', 'Y-Z', 'Y-1', '1-3', '1-X', '3-4', '3-S', '5-T', '5-V', '6-3', '7-6' ]: g_challenger.adiciona_aresta(i)
v = menor_vertice(g, fi, beta) fi[v] = 1 for j in range(len(g.M)): if (fi[j] == 0 and g.M[v][j] > 0 and beta[j] > beta[v] + g.M[v][j]): beta[j] = beta[v] + g.M[v][j] phi[j] = v if phi[g.N.index(v_final)] != None: caminho = list() a = g.N.index(v_final) while a != None: caminho.insert(0, a) a = phi[a] if a == g.N.index(v_inicial): caminho.insert(0, a) break for y in caminho: if y != g.N.index(v_final): print(g.N[y], end="->") else: print(g.N[y]) else: print("Não existe um caminho entre os vertices!") grafo = Grafo(['A', 'B', 'C', 'D', 'E'], [[0, 1, 0, 0, 1], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 0]]) Dijkstra(grafo, 'A', 'D')
class TestGrafo(unittest.TestCase): def setUp(self): self.r5 = Grafo([], []) self.vertices = [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g" ] self.arestas = [ 'A-B', 'A-C', 'A-D', 'B-E', 'B-I', 'C-G', 'D-C', 'D-H', 'E-F', 'F-B', 'F-J', 'G-F', 'G-J', 'G-K', 'H-G', 'H-L', 'I-M', 'J-I', 'J-N', 'K-O', 'L-P', 'M-Q', 'M-S', 'N-R', 'N-S', 'N-T', 'O-S', 'P-T', 'Q-U', 'R-Q', 'R-V', 'S-R', 'S-X', 'U-Y', 'U-Z', 'V-b', 'V-Z', 'V-W', 'W-S', 'X-c', 'X-d', 'Y-a', 'a-e', 'c-e', 'c-W', 'e-f', 'e-g', 'T-S' ] for i in self.vertices: self.r5.adiciona_vertice(i) for i in self.arestas: self.r5.adiciona_aresta(i) def test_dijkstra(self): self.assertEqual( self.r5.dijkstra(self.r5.N, self.r5.aresta_com_peso(), "G", "X", 2, ['S', 'F']), "G --> F --> J --> N --> S --> X") self.assertEqual( self.r5.dijkstra(self.r5.N, self.r5.aresta_com_peso(), "B", "S", 1, ['I', 'E']), "B --> I --> M --> S") self.assertEqual( self.r5.dijkstra(self.r5.N, self.r5.aresta_com_peso(), "A", "g", 3, ['L', 'S', 'U', 'a']), "A --> D --> H --> L --> P --> T --> S --> X --> c --> e --> g") self.assertFalse( self.r5.dijkstra(self.r5.N, self.r5.aresta_com_peso(), "A", "g", 2, ['L', 'S', 'U', 'a'])) self.assertFalse( self.r5.dijkstra(self.r5.N, self.r5.aresta_com_peso(), "A", "g", 1, ['L', 'S', 'U', 'a'])) self.assertFalse( self.r5.dijkstra(self.r5.N, self.r5.aresta_com_peso(), "A", "g", 1, ['L', 'S', 'U', 'a'])) self.assertEqual( self.r5.dijkstra(self.r5.N, self.r5.aresta_com_peso(), "A", "H", 3, ['D', 'G']), "A --> D --> H") self.assertEqual( self.r5.dijkstra(self.r5.N, self.r5.aresta_com_peso(), "I", "b", 4, ['L', 'O', 'S', 'W', 'b']), "I --> M --> S --> R --> V --> b")
from grafo_adj import Grafo g_p = Grafo([], []) for i in ['J', 'C', 'E', 'P', 'M', 'T', 'Z']: g_p.adiciona_vertice(i) for i in [['J-C', 3], ['C-E', 7], ['C-E', 2], ['C-P', 6], ['C-P', 2], ['C-M', 5], ['C-T', 9], ['M-T', 7], ['T-Z', 4]]: g_p.adiciona_aresta(i) # Grafos completos g_c = Grafo([], []) for i in ['J', 'C', 'E', 'P']: g_c.adiciona_vertice(i) for i in [['J-C', 5], ['J-E', 10], ['J-P', 8], ['C-J', 9], ['C-E', 7], ['C-P', 1], ['E-J', 7], ['E-C', 4], ['E-P', 6], ['P-J', 3], ['P-C', 9], ['P-E', 2]]: g_c.adiciona_aresta(i) g_c3 = Grafo([], []) g_c3.adiciona_vertice('J') # Grafos com laco g_l1 = Grafo([], []) for i in ['A', 'B', 'C', 'D']: g_l1.adiciona_vertice(i) for i in [['A-A', 7], ['B-A', 8], ['A-A', 10]]: g_l1.adiciona_aresta(i) g_l4 = Grafo([], []) g_l4.adiciona_vertice('D') g_l4.adiciona_aresta(['D-D', 1]) g_l5 = Grafo([], [])
from grafo_adj import Grafo from math import inf g_challenger = Grafo([], []) for i in [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "W", "Z", "1", "2", "3", "4", "5", "6", "7" ]: g_challenger.adiciona_vertice(i) for i in [ 'A-B', 'A-C', 'A-D', 'B-I', 'B-H', 'C-F', 'D-C', 'D-E', 'E-F', 'E-L', 'F-G', 'F-J', 'F-K', 'G-B', 'G-J', 'H-G', 'I-P', 'J-I', 'J-O', 'K-N', 'L-M', 'M-Q', 'N-R', 'O-5', 'O-R', 'O-Q', 'Q-R', 'P-R', 'P-T', 'R-5', 'R-Y', 'T-U', 'U-7', 'U-W', 'V-2', 'V-W', 'V-X', 'X-R', 'Y-Z', 'Y-1', '1-3', '1-X', '3-4', '3-S', '5-T', '5-V', '6-3', '7-6' ]: g_challenger.adiciona_aresta(i) lista_carga = ['L', 'R', 'U', '6'] n = Grafo([], []) for i in ['a', 'b', 'c', 'd', 'e']: n.adiciona_vertice(i) for i in ['a-b', 'b-c', 'c-d', 'd-e']: n.adiciona_aresta(i) l = ['a'] print(n.dijkstra(4, 4, l, 'a', 'e')) c2 = Grafo([], []) for i in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']: c2.adiciona_vertice(i)
def setUp(self): self.Slide = Grafo([], []) SlideVertices = ['2', '3', '5', '7', '8', '9', '10', '11'] SlideArestas = [['7-8', 1], ['7-11', 1], ['3-11', 1], ['5-8', 1], ['5-10', 1], ['8-9', 1], ['11-2', 1], ['11-9', 1], ['11-10', 1]] for v in SlideVertices: self.Slide.adiciona_vertice(v) for ed in SlideArestas: self.Slide.adiciona_aresta(ed) self.TecnologiaEmConstruçãoDeEdifícios = Grafo([], []) TecnologiaEmConstruçãoDeEdifíciosVertices = [ '11', '12', '13', '14', '15', '16', '17', '18', '21', '22', '23', '24', '25', '26', '27', '31', '32', '33', '34', '35', '36', '37', '38', '41', '42', '43', '44', '45', '46', '47', '51', '52', '53', '54', '55', '56', '57', '61', '62', '63', '64', '65', '66', '67', '68', '71', '72', '73', '74', '75', '76' ] TecnologiaEmConstruçãoDeEdifíciosArestas = [['15-21', 1], ['14-23', 1], ['11-24', 1], ['17-24', 1], ['15-25', 1], ['17-26', 1], ['17-27', 1], ['15-32', 1], ['21-32', 1], ['21-33', 1], ['15-34', 1], ['23-35', 1], ['26-36', 1], ['23-37', 1], ['24-38', 1], ['17-41', 1], ['21-41', 1], ['17-42', 1], ['21-42', 1], ['11-43', 1], ['23-43', 1], ['35-43', 1], ['35-45', 1], ['17-46', 1], ['32-46', 1], ['21-47', 1], ['37-51', 1], ['45-51', 1], ['46-52', 1], ['51-52', 1], ['17-53', 1], ['32-53', 1], ['47-54', 1], ['17-55', 1], ['32-55', 1], ['46-56', 1], ['43-57', 1], ['31-62', 1], ['57-62', 1], ['37-64', 1], ['45-64', 1], ['22-66', 1], ['31-67', 1], ['57-67', 1], ['13-71', 1], ['22-71', 1]] for v in TecnologiaEmConstruçãoDeEdifíciosVertices: self.TecnologiaEmConstruçãoDeEdifícios.adiciona_vertice(v) for ed in TecnologiaEmConstruçãoDeEdifíciosArestas: self.TecnologiaEmConstruçãoDeEdifícios.adiciona_aresta(ed) self.LicenciaturaEmFísica = Grafo([], []) LicenciaturaEmFísicaVertices = [ '11', '12', '13', '14', '15', '16', '17', '21', '22', '23', '24', '25', '26', '27', '31', '32', '33', '34', '35', '36', '37', '41', '42', '43', '44', '45', '46', '51', '52', '53', '54', '55', '56', '57', '61', '62', '63', '64', '65', '66', '68', '71', '72', '73', '74', '75', '76', '81', '82', '83', '84', '85', '86' ] LicenciaturaEmFísicaArestas = [['11-21', 1], ['12-21', 1], ['11-22', 1], ['12-22', 1], ['12-23', 1], ['12-24', 1], ['14-24', 1], ['15-25', 1], ['21-31', 1], ['23-31', 1], ['21-32', 1], ['22-32', 1], ['23-33', 1], ['31-41', 1], ['31-42', 1], ['32-42', 1], ['33-45', 1], ['31-46', 1], ['41-51', 1], ['45-51', 1], ['41-52', 1], ['42-52', 1], ['45-53', 1], ['31-54', 1], ['43-55', 1], ['21-57', 1], ['43-57', 1], ['51-61', 1], ['51-62', 1], ['52-62', 1], ['21-63', 1], ['53-63', 1], ['51-64', 1], ['56-66', 1], ['31-68', 1], ['57-68', 1], ['61-71', 1], ['41-72', 1], ['45-72', 1], ['66-73', 1], ['31-74', 1], ['43-74', 1], ['41-76', 1], ['68-76', 1], ['65-81', 1], ['74-82', 1], ['73-83', 1], ['54-84', 1], ['71-84', 1], ['16-85', 1], ['25-85', 1], ['51-86', 1], ['76-86', 1]] for v in LicenciaturaEmFísicaVertices: self.LicenciaturaEmFísica.adiciona_vertice(v) for ed in LicenciaturaEmFísicaArestas: self.LicenciaturaEmFísica.adiciona_aresta(ed) self.LicenciaturaEmMatemática = Grafo([], []) LicenciaturaEmMatemáticaVertices = [ '11', '12', '13', '14', '15', '16', '17', '21', '22', '23', '24', '25', '26', '27', '31', '32', '33', '34', '35', '36', '41', '42', '43', '44', '45', '46', '51', '52', '53', '54', '55', '56', '57', '61', '62', '63', '64', '65', '66', '67', '71', '72', '73', '74', '75', '76', '77', '81', '82', '83', '84', '85', '86', '87' ] LicenciaturaEmMatemáticaArestas = [['11-21', 1], ['11-22', 1], ['13-22', 1], ['16-26', 1], ['21-31', 1], ['22-32', 1], ['12-33', 1], ['12-34', 1], ['21-41', 1], ['23-41', 1], ['23-42', 1], ['32-42', 1], ['36-43', 1], ['34-44', 1], ['27-45', 1], ['33-51', 1], ['12-52', 1], ['32-53', 1], ['44-54', 1], ['44-55', 1], ['44-57', 1], ['51-61', 1], ['52-62', 1], ['32-63', 1], ['54-64', 1], ['46-65', 1], ['57-67', 1], ['42-71', 1], ['22-72', 1], ['41-73', 1], ['42-73', 1], ['64-74', 1], ['65-75', 1], ['67-77', 1], ['62-81', 1], ['75-82', 1], ['32-83', 1], ['74-84', 1], ['77-87', 1]] for v in LicenciaturaEmMatemáticaVertices: self.LicenciaturaEmMatemática.adiciona_vertice(v) for ed in LicenciaturaEmMatemáticaArestas: self.LicenciaturaEmMatemática.adiciona_aresta(ed) self.EngenhariaDeComputacão = Grafo([], []) EngenhariaDeComputacãoVertices = [ '11', '12', '13', '14', '15', '16', '17', '21', '22', '23', '24', '25', '26', '27', '31', '32', '33', '34', '35', '36', '41', '42', '43', '44', '45', '51', '52', '53', '54', '55', '61', '62', '63', '64', '65', '71', '72', '73', '74', '75', '81', '82', '83', '84', '85', '91', '92', '93', '94', '101', '102', '103' ] EngenhariaDeComputacãoArestas = [['11-21', 1], ['21-31', 1], ['21-41', 1], ['31-51', 1], ['51-61', 1], ['34-81', 1], ['35-81', 1], ['54-81', 1], ['31-52', 1], ['43-62', 1], ['24-72', 1], ['73-82', 1], ['83-92', 1], ['24-33', 1], ['24-43', 1], ['24-53', 1], ['34-63', 1], ['35-63', 1], ['63-73', 1], ['74-83', 1], ['44-93', 1], ['45-93', 1], ['92-103', 1], ['14-24', 1], ['15-24', 1], ['14-34', 1], ['15-34', 1], ['24-44', 1], ['36-44', 1], ['24-54', 1], ['31-64', 1], ['61-84', 1], ['64-84', 1], ['61-94', 1], ['75-94', 1], ['14-25', 1], ['15-25', 1], ['14-35', 1], ['15-35', 1], ['36-45', 1], ['36-55', 1], ['44-55', 1], ['55-65', 1], ['52-75', 1], ['64-75', 1], ['75-85', 1], ['16-26', 1], ['26-36', 1]] for v in EngenhariaDeComputacãoVertices: self.EngenhariaDeComputacão.adiciona_vertice(v) for ed in EngenhariaDeComputacãoArestas: self.EngenhariaDeComputacão.adiciona_aresta(ed) self.Letras = Grafo([], []) LetrasVertices = [ '11', '12', '13', '14', '15', '16', '17', '21', '22', '23', '24', '25', '26', '27', '31', '32', '33', '34', '35', '36', '37', '41', '42', '43', '44', '45', '46', '47', '51', '52', '53', '54', '55', '56', '57', '61', '62', '63', '64', '65', '66', '67', '68', '71', '72', '73', '74', '75', '76', '77', '78', '79', '81', '82', '83', '84', '85', '86', '87', '88' ] LetrasArestas = [['11-21', 1], ['21-31', 1], ['31-41', 1], ['31-51', 1], ['31-61', 1], ['31-71', 1], ['11-22', 1], ['21-32', 1], ['33-42', 1], ['35-52', 1], ['31-62', 1], ['31-72', 1], ['12-23', 1], ['21-33', 1], ['25-43', 1], ['13-53', 1], ['35-63', 1], ['31-73', 1], ['17-83', 1], ['24-34', 1], ['25-44', 1], ['36-44', 1], ['45-54', 1], ['54-64', 1], ['64-74', 1], ['74-84', 1], ['12-25', 1], ['35-25', 1], ['35-55', 1], ['35-75', 1], ['17-26', 1], ['23-46', 1], ['35-46', 1], ['22-56', 1], ['45-76', 1], ['37-47', 1], ['37-57', 1], ['37-67', 1], ['27-77', 1], ['53-77', 1], ['77-87', 1], ['54-68', 1], ['64-78', 1], ['68-78', 1], ['74-88', 1], ['78-88', 1], ['65-79', 1]] for v in LetrasVertices: self.Letras.adiciona_vertice(v) for ed in LetrasArestas: self.Letras.adiciona_aresta(ed) self.Telemática = Grafo([], []) TelemáticaVertices = [ '11', '12', '13', '14', '15', '16', '17', '21', '22', '23', '24', '25', '26', '27', '31', '32', '33', '34', '35', '36', '37', '41', '42', '43', '44', '45', '46', '47', '51', '52', '53', '54', '55', '56', '61', '62', '63', '64', '65' ] TelemáticaArestas = [['11-21', 1], ['21-31', 1], ['31-41', 1], ['41-51', 1], ['42-61', 1], ['51-61', 1], ['12-22', 1], ['16-22', 1], ['26-32', 1], ['31-42', 1], ['41-52', 1], ['53-62', 1], ['12-23', 1], ['16-23', 1], ['22-33', 1], ['23-33', 1], ['26-33', 1], ['32-43', 1], ['44-53', 1], ['13-24', 1], ['25-34', 1], ['32-44', 1], ['33-44', 1], ['44-54', 1], ['25-35', 1], ['33-45', 1], ['37-55', 1], ['41-55', 1], ['44-55', 1], ['16-26', 1], ['21-36', 1], ['24-36', 1], ['21-46', 1], ['34-46', 1]] for v in TelemáticaVertices: self.Telemática.adiciona_vertice(v) for ed in TelemáticaArestas: self.Telemática.adiciona_aresta(ed)