शुरू करेंमुफ़्त में शुरू करें

Weighted graph बनाना

पिछले वीडियो में, आपने 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)

इस अभ्यास में दो स्टेप हैं। पहले स्टेप में, आप इस कोड को मॉडिफ़ाइ करेंगे ताकि इसका इस्तेमाल weighted graph बनाने के लिए किया जा सके। इसके लिए, आप adjacent vertices और उनके वज़न को दर्शाने के लिए एक hash table का उपयोग कर सकते हैं। दूसरे स्टेप में, आप नीचे दिखाया गया weighted graph बनाएँगे:

Representation of a weighted graph.

यह अभ्यास पाठ्यक्रम का हिस्सा है

Python में Data Structures और Algorithms

पाठ्यक्रम देखें

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

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([____, ____])
कोड संपादित करें और चलाएँ