가중 그래프 만들기
이전 영상에서 Python으로 그래프를 구현하는 방법을 배웠어요.
class Graph:
def __init__(self):
self.vertices = {}
def add_vertex(self, vertex):
self.vertices[vertex] = []
def add_edge(self, source, target):
self.vertices[source].append(target)
이 연습 문제는 두 단계로 구성되어 있습니다. 먼저, 위 코드를 수정해 가중 그래프를 만들 수 있도록 해 보세요. 이를 위해 해시 테이블을 사용해 인접 정점과 그 가중치를 함께 표현할 수 있어요. 두 번째 단계에서는 아래와 같은 가중 그래프를 구성합니다:

이 연습은 강의의 일부입니다
Python으로 배우는 자료구조와 알고리즘
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
class WeightedGraph:
def __init__(self):
self.vertices = {}
def add_vertex(self, vertex):
# Set the data for the vertex
self.vertices[____] = []
def add_edge(self, source, target, weight):
# Set the weight
self.vertices[____].append([____, ____])