重み付きグラフの構築
前のビデオでは、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)
この演習には2つのステップがあります。最初のステップでは、重み付きグラフを作成できるようにコードを修正します。そのために、ハッシュテーブルを使って隣接する頂点とその重みを表示します。2つ目のステップでは、以下のような重み付きグラフを構築します。

この演習はコースの一部です
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([____, ____])