반응형
Dijkstra algorithm(다익스트라 알고리즘)
열심히 그렸는데, 맞겠지. stack에 값을 넣어놓고 pop해서 꺼내면 된다.
*A에서 D로 가는 경우, A에서 B를 거쳐 D로 가게되면 8이 됨으로, 7A를 저장해 준다.
class Graph:
def __init__(self):
self.node = set()
self.edges = {}
self.distances = {}
def add_node(self, value):
self.nodes.add(value)
def add_edge(self, from_node, to_node, distance):
self._add_edge(from_node, to_node, distance)
self._add_edge(to_node, from_node, distance)
def _add_edge(self, from_node, to_node, distance):
self.edges.setdefault(from_node, [])
self.edges[from_node].append(to_node)
self.distances[(from_node, to_node)] = distance
def dijkstra(graph, initial_node):
visited = {initial_node: 0}
path = {}
nodes = set(graph.nodes)
while nodes:
min_node = None
for node in nodes:
if node in visited:
if min_node is None:
min_node = node
elif visited[node] < visited[min_node]:
min_node = node
if min_node is None:
break
nodes.remove(min_node)
cur_wt = visited[min_node]
for edge in graph.edges[min_node]:
wt = cur_wt + graph.distances[(min_node, edge)]
if edge not in visited or wt < visited[edge]:
visited[edge] = wt
path[edge] = min_node
return visited, path
def shortest_path(graph, initial_node, goal_node):
distance, path = dijkstra(graph, initial_node)
route = [goal_node]
while goal_node != initial_node:
route.append(path[goal_node])
goal_node = path[goal_node]
route.reverse()
return route
g = Graph()
g.nodes = set(range(1, 6))
g.add_edge(1, 2, 3)
g.add_edge(1, 3, 8)
g.add_edge(1, 4, 7)
g.add_edge(2, 5, 4)
g.add_edge(2, 4, 5)
g.add_edge(3, 4, 1)
g.add_edge(4, 5, 2)
print(shortest_path(g, 1, 5))
print(shortest_path(g, 1, 4))
출처: 허민석님 유투브
반응형
'Data > python·알고리즘' 카테고리의 다른 글
[Graph] BFS(Breadth first Search), 넓이우선탐색 (0) | 2018.09.25 |
---|---|
[Graph] DFS(Depth First Search), 깊이우선탐색 (0) | 2018.09.24 |
[Binary Tree] 이진트리(2) - pre order, in order, post order (0) | 2018.09.24 |
[Binary Tree] 이진트리 (1) - 삽입, 삭제 (0) | 2018.09.24 |
[heap sort] 힙 힙 힙! (0) | 2018.09.24 |
댓글