def testAllPairs(self): for i in range(50): adjList = self.randG.randomGnmGraph(n=50, M=1000, isWeighted=True) nodes, edges = self.randG.toNodeEdges(adjList) src = random.choice(list(adjList.keys())) dist1, prev1 = floyd_warshall(nodes, edges) dist2, prev2 = johnson(nodes, edges) self.assertEqual(dist1, dist2)
def testFloydWarshall(self): for i in range(50): adjList = self.randG.randomGnmGraph(n=50, M=1000, isWeighted=True) nodes, edges = self.randG.toNodeEdges(adjList) src = random.choice(list(adjList.keys())) dist1, prev1 = dijkstra(adjList, src) dist2, prev2 = floyd_warshall(nodes, edges) self.assertEqual(dist1, dist2[src])
def test_floyd_warshall(): print("Floyd-Warshall alg") print("Graph:", directed_graph) print("Table with distances:") dist_table = floyd_warshall(directed_graph) for row in dist_table: for val in row: print('{:5}'.format(val), end="") print()
def import_datas_and_execute_algorithm(test_path): #We'll assume that the input file is well formatted with open(test_path) as test_file: #retrieve nodes and arcs amount node_count = int(test_file.readline()) arc_count = int(test_file.readline()) #initialize the input matrix for floyd warshall algorithm input_matrix = numpy.full((node_count, node_count), numpy.inf) for line in test_file.readlines(): #clean input origin, dest, weight = map(int, line.strip().split(' ')) input_matrix[origin - 1][origin - 1] = 0 #We'll assume that the weight from a node to itself is 0 input_matrix[origin - 1][dest - 1] = weight print "Input: " pprint.pprint(input_matrix) floyd_warshall(input_matrix)
def main(): import sys filename = sys.argv[1] method = sys.argv[2] edges = read_input(filename) graph = build_graph(edges) print 'a graph with %s nodes, %s edges' % (len(graph), len(edges)) if method in ('johnson', 'both'): paths1 = johnson(graph) paths = paths1 if method in ('floyd_warshall', 'both'): paths2 = floyd_warshall(graph) paths = paths2 if method == 'both': assert paths1 == paths2 if paths == 'negative cycle': print paths else: print min(paths, key=lambda x: x[2])
import argparse # # - black symbol alphabet = ['a', 'b', 'c', '#'] index = pd.Index(alphabet, name="rows") columns = pd.Index(alphabet, name="columns") # you may provide all penalties in this matrix # 0-1 penalties are the default ones penalties = np.array([[0, 1, 1, 1], [1, 0, 1, 1], [1, 1, 0, 1], [1, 1, 1, 0]]) # getting optimal penalties by Floyd-Warshall algorithm opt_penalties = floyd_warshall(penalties) # created dataframes just for being able to have string indices opt_penalties = pd.DataFrame(data=opt_penalties, index=index, columns=columns) def levenshtein_distance(x, y): dists = np.zeros((len(y) + 1, len(x) + 1)) for i in range(1, dists.shape[0]): dists[i, 0] = dists[i-1, 0] + opt_penalties.loc['#', y[i-1]] for j in range(1, dists.shape[1]): dists[0, j] = dists[0, j-1] + opt_penalties.loc[x[j-1], '#']
import weighted_graph import floyd_warshall node_numbers = {'N1':0, 'N2':1, 'N3':2, 'N4':3, 'N5':4, 'N6':5, 'N7':6, 'N8':7} G = weighted_graph.Graph(len(node_numbers)) for node in node_numbers: G.set_vertex(node_numbers[node],node) G.add_edge(node_numbers['N1'],node_numbers['N2'],3) G.add_edge(node_numbers['N1'],node_numbers['N4'],2) G.add_edge(node_numbers['N1'],node_numbers['N3'],3) G.add_edge(node_numbers['N2'],node_numbers['N4'],9) G.add_edge(node_numbers['N2'],node_numbers['N5'],4) G.add_edge(node_numbers['N2'],node_numbers['N7'],2) G.add_edge(node_numbers['N3'],node_numbers['N4'],-4) G.add_edge(node_numbers['N3'],node_numbers['N5'],1) G.add_edge(node_numbers['N3'],node_numbers['N7'],6) G.add_edge(node_numbers['N3'],node_numbers['N8'],6) G.add_edge(node_numbers['N4'],node_numbers['N6'],1) G.add_edge(node_numbers['N5'],node_numbers['N7'],7) floyd_warshall.floyd_warshall(G)
'N3': 2, 'N4': 3, 'N5': 4, 'N6': 5, 'N7': 6, 'N8': 7 } G = weighted_graph.Graph(len(node_numbers)) for node in node_numbers: G.set_vertex(node_numbers[node], node) G.add_edge(node_numbers['N1'], node_numbers['N2'], 3) G.add_edge(node_numbers['N1'], node_numbers['N4'], 2) G.add_edge(node_numbers['N1'], node_numbers['N3'], 3) G.add_edge(node_numbers['N2'], node_numbers['N4'], 9) G.add_edge(node_numbers['N2'], node_numbers['N5'], 4) G.add_edge(node_numbers['N2'], node_numbers['N7'], 2) G.add_edge(node_numbers['N3'], node_numbers['N4'], -4) G.add_edge(node_numbers['N3'], node_numbers['N5'], 1) G.add_edge(node_numbers['N3'], node_numbers['N7'], 6) G.add_edge(node_numbers['N3'], node_numbers['N8'], 6) G.add_edge(node_numbers['N4'], node_numbers['N6'], 1) G.add_edge(node_numbers['N5'], node_numbers['N7'], 7) floyd_warshall.floyd_warshall(G)