|
| 1 | +import heapq |
| 2 | +import networkx as nx |
| 3 | +import matplotlib.pyplot as plt |
| 4 | + |
| 5 | +def dijkstra(graph, start): |
| 6 | + distances = {node: float('infinity') for node in graph} |
| 7 | + distances[start] = 0 |
| 8 | + shortest_paths = {} |
| 9 | + |
| 10 | + priority_queue = [(0, start)] |
| 11 | + |
| 12 | + while priority_queue: |
| 13 | + current_distance, current_node = heapq.heappop(priority_queue) |
| 14 | + |
| 15 | + if current_distance > distances[current_node]: |
| 16 | + continue |
| 17 | + |
| 18 | + for neighbor, weight in graph[current_node].items(): |
| 19 | + distance = current_distance + weight |
| 20 | + if distance < distances[neighbor]: |
| 21 | + distances[neighbor] = distance |
| 22 | + shortest_paths[neighbor] = current_node |
| 23 | + heapq.heappush(priority_queue, (distance, neighbor)) |
| 24 | + |
| 25 | + return distances, shortest_paths |
| 26 | + |
| 27 | +def draw_graph(graph, shortest_paths, start_node, end_node): |
| 28 | + G = nx.DiGraph() |
| 29 | + |
| 30 | + for node, edges in graph.items(): |
| 31 | + for edge, weight in edges.items(): |
| 32 | + G.add_edge(node, edge, weight=weight) |
| 33 | + |
| 34 | + pos = nx.spring_layout(G) |
| 35 | + nx.draw(G, pos, with_labels=True, node_size=5000, node_color='lightblue', font_size=12) |
| 36 | + |
| 37 | + nx.draw_networkx_nodes(G, pos, nodelist=[start_node], node_color='green', node_size=7000) |
| 38 | + nx.draw_networkx_nodes(G, pos, nodelist=[end_node], node_color='red', node_size=7000) |
| 39 | + |
| 40 | + path = [end_node] |
| 41 | + current_node = end_node |
| 42 | + while current_node != start_node: |
| 43 | + current_node = shortest_paths[current_node] |
| 44 | + path.append(current_node) |
| 45 | + path.reverse() |
| 46 | + |
| 47 | + path_edges = [(path[i], path[i+1]) for i in range(len(path)-1)] |
| 48 | + nx.draw_networkx_edges(G, pos, edgelist=path_edges, edge_color='red', width=3) |
| 49 | + |
| 50 | + labels = nx.get_edge_attributes(G, 'weight') |
| 51 | + nx.draw_networkx_edge_labels(G, pos, edge_labels=labels) |
| 52 | + |
| 53 | + plt.title("Shortest Path from {} to {}".format(start_node, end_node)) |
| 54 | + plt.show() |
| 55 | + |
| 56 | +# Definimos el grafo que representa la red social |
| 57 | +graph = { |
| 58 | + 'Alice': {'Bob': 5, 'Charlie': 2}, |
| 59 | + 'Bob': {'Alice': 5, 'David': 3}, |
| 60 | + 'Charlie': {'Alice': 2, 'David': 7}, |
| 61 | + 'David': {'Bob': 3, 'Charlie': 7} |
| 62 | +} |
| 63 | + |
| 64 | +# Especificamos los nodos de inicio y fin para encontrar la distancia más corta entre ellos |
| 65 | +start_node = 'Alice' |
| 66 | +end_node = 'David' |
| 67 | + |
| 68 | +# Aplicamos el algoritmo de Dijkstra para encontrar la distancia más corta y el camino más corto |
| 69 | +distances, shortest_paths = dijkstra(graph, start_node) |
| 70 | + |
| 71 | +# Imprimimos la distancia más corta entre los nodos especificados |
| 72 | +print("Distancia más corta entre {} y {}: {}".format(start_node, end_node, distances[end_node])) |
| 73 | + |
| 74 | +# Imprimimos el camino más corto entre los nodos especificados |
| 75 | +path = [end_node] |
| 76 | +current_node = end_node |
| 77 | +while current_node != start_node: |
| 78 | + current_node = shortest_paths[current_node] |
| 79 | + path.append(current_node) |
| 80 | +path.reverse() |
| 81 | +print("Camino más corto:", ' -> '.join(path)) |
| 82 | + |
| 83 | +# Dibujamos el grafo y el camino más corto |
| 84 | +draw_graph(graph, shortest_paths, start_node, end_node) |
0 commit comments