-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamic Programming Algorithm.py
More file actions
59 lines (48 loc) · 1.84 KB
/
Dynamic Programming Algorithm.py
File metadata and controls
59 lines (48 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import networkx as nx
import matplotlib.pyplot as plt
class Graph:
def __init__(self):
self.graph = nx.DiGraph()
def add_node(self, value):
self.graph.add_node(value)
def add_edge(self, from_node, to_node, weight):
self.graph.add_edge(from_node, to_node, weight=weight)
def dp_shortest_path(graph, source, destination):
nodes = list(graph.nodes)
costs = {node: float('inf') for node in nodes}
costs[source] = 0
for _ in range(len(nodes)):
for node in nodes:
for successor in graph.successors(node):
cost = graph[node][successor]['weight']
costs[successor] = min(costs[successor], cost + costs[node])
return costs[destination]
def visualize_graph(graph):
pos = nx.spring_layout(graph.graph)
nx.draw(graph.graph, pos, with_labels=True, font_weight='bold', node_size=700, node_color='skyblue', font_size=8, connectionstyle="arc3,rad=0.1")
edge_labels = nx.get_edge_attributes(graph.graph, 'weight')
nx.draw_networkx_edge_labels(graph.graph, pos, edge_labels=edge_labels)
plt.title("Dynamic Programming Algorithm - Plot representation")
plt.show()
def print_costs(costs):
for node, cost in costs.items():
print(f"Cost from {node} to destination: {cost}")
# Test Case 1
g1 = Graph()
g1.add_node("A")
g1.add_node("B")
g1.add_node("C")
g1.add_node("D")
g1.add_edge("A", "B", 2)
g1.add_edge("B", "C", 1)
g1.add_edge("A", "C", 4)
g1.add_edge("C", "D", 3)
g1.add_edge("B", "D", 7)
visualize_graph(g1)
src1, dest1 = "A", "D"
cost1 = dp_shortest_path(g1.graph, src1, dest1)
print(f"Cost from {src1} to {dest1}: {cost1}")
# Additional paths
print(f"Cost from B to D: {dp_shortest_path(g1.graph, 'B', 'D')}")
print(f"Cost from C to D: {dp_shortest_path(g1.graph, 'C', 'D')}")
print(f"Cost from D to D: {dp_shortest_path(g1.graph, 'D', 'D')}")