g.add_vertex(i) for j in range(len(m)): x = None y = None cnt = 0 for i in range(len(m[0]) - 1): if cnt == 0 and m[j][i] == 1: x = i cnt = 1 continue if m[j][i] == 1 and cnt == 1: y = i cnt = 2 if cnt == 2: g.add_edge(x, y, weight[j]) break g.print_graph() print('Vertices:', g.vertices()) print('Edges: ', g.edges()) print('Number of edges:', g.num_edges()) print('Number of vertices:', g.num_vertices()) print('Get vertex: ', g.get_vertex(2)) print('Get edge: ', g.get_edge(3, 2)) print('Get adjacents: ', g.adj_vertices(1)) print(prims_algorithm(g.get_data()))
from mygraph import Node from mygraph import Graph g = Graph() g.add_node('a') g.add_node('b') g.add_node('c') g.add_node('d') g.add_node('e') g.add_node('f') g.add_edge('a', 'b', 7) g.add_edge('a', 'c', 9) g.add_edge('a', 'f', 14) g.add_edge('b', 'c', 10) g.add_edge('b', 'd', 15) g.add_edge('c', 'd', 11) g.add_edge('c', 'f', 2) g.add_edge('d', 'e', 6) g.add_edge('e', 'f', 9) print("Nodes") for n in g: print(g.nodes_dict[n.name]) print("Connections") for n in g: for w in n.get_connections(): print('( %s , %s, %3d)' % (n.id, w.id, n.get_weight(w)))
graph.add_vertex(i) new_graph[i] = {} for j in range(len(incidence_matrix)): first_one = None second_one = None counter = 0 for i in range(len(incidence_matrix[0]) - 1): if counter == 0 and incidence_matrix[j][i] == 1: first_one = i counter = 1 continue if incidence_matrix[j][i] == 1 and counter == 1: second_one = i counter = 2 if counter == 2: graph.add_edge(first_one, second_one) new_graph[first_one][second_one] = weight[j] graph.print_graph() print('Number of edges: ', graph.num_edges()) print('Number of vertices: ', graph.num_vertices()) print('Vertices: ', graph.vertices()) print('Edges: ', graph.edges()) print('Get vertex: ', graph.get_vertex(2)) print('Get edge: ', graph.get_edge(3, 2)) print('Get adjacents: ', graph.adj_vertices(1)) print(dijkstra(new_graph, 2, 9))
for line in f: mymatrix.append([int(x) for x in line.split()]) graph.add_vertex(count) new_graph[count] = {} count += 1 for i in range(len(mymatrix)): weights.append(mymatrix[i][len(mymatrix[i]) - 1]) for j in range(len(mymatrix)): col = 0 row = 0 counter = 0 for i in range(len(mymatrix[0]) - 1): if counter == 0 and mymatrix[j][i] == 1: col = i counter = 1 continue if mymatrix[j][i] == 1 and counter == 1: row = i counter = 2 if counter == 2: graph.add_edge(col, row) new_graph[col][row] = weights[j] print('Number of edges: ', graph.num_edges()) print('Number of vertices: ', graph.num_vertices()) print('Minimum path from 1 to 4 is: ', dijkstra(new_graph, 1))
for i in range(len(incidence_matrix)): weight.append(incidence_matrix[i][len(incidence_matrix[i]) - 1]) for i in range(len(incidence_matrix[0]) - 1): graph.add_vertex(i) for j in range(len(incidence_matrix)): first_one = None second_one = None counter = 0 for i in range(len(incidence_matrix[0]) - 1): if counter == 0 and incidence_matrix[j][i] == 1: first_one = i counter = 1 continue if incidence_matrix[j][i] == 1 and counter == 1: second_one = i counter = 2 if counter == 2: graph.add_edge(first_one, second_one, weight[j]) graph.print_graph() print('Number of edges: ', graph.num_edges()) print('Number of vertices: ', graph.num_vertices()) print('Vertices: ', graph.vertices()) print('Edges: ', graph.edges()) print('Get vertex: ', graph.get_vertex(2)) print('Get edge: ', graph.get_edge(5, 9)) print('Get adjacents: ', graph.adj_vertices(1)) print(kruskals_algorithm(graph))