0
0
Data Structures Theoryknowledge~30 mins

Weighted graphs in Data Structures Theory - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding Weighted Graphs
📖 Scenario: You are learning about weighted graphs, which are used to represent networks where connections have values like distances or costs. Imagine a map of cities connected by roads, where each road has a length.
🎯 Goal: Build a simple weighted graph using a dictionary to represent cities and the distances between them. Then, add a threshold to find roads shorter than a certain distance.
📋 What You'll Learn
Create a dictionary called graph with exact city connections and weights
Add a variable called max_distance to set a distance limit
Use a for loop with variables city and connections to iterate over graph.items()
Inside the loop, use a nested for loop with variables neighbor and distance to iterate over connections.items()
Create a list called short_roads to store tuples of roads shorter than max_distance
💡 Why This Matters
🌍 Real World
Weighted graphs are used in maps, network routing, and scheduling to represent connections with costs or distances.
💼 Career
Understanding weighted graphs is important for roles in software development, data analysis, and network engineering.
Progress0 / 4 steps
1
Create the weighted graph dictionary
Create a dictionary called graph with these exact entries: 'A': {'B': 5, 'C': 10}, 'B': {'A': 5, 'C': 3}, 'C': {'A': 10, 'B': 3}
Data Structures Theory
Need a hint?

Use a dictionary where each key is a city and each value is another dictionary of connected cities with distances.

2
Add a maximum distance threshold
Add a variable called max_distance and set it to 6 to represent the maximum road length to consider.
Data Structures Theory
Need a hint?

This variable will help filter roads by length.

3
Find roads shorter than the threshold
Create an empty list called short_roads. Use a for loop with variables city and connections to iterate over graph.items(). Inside it, use another for loop with variables neighbor and distance to iterate over connections.items(). Append a tuple (city, neighbor, distance) to short_roads if distance is less than max_distance.
Data Structures Theory
Need a hint?

Use nested loops to check each road's distance and add it to the list if it is less than max_distance.

4
Complete the weighted graph project
Add a final line that creates a variable called result and assigns it the value of short_roads to complete the project.
Data Structures Theory
Need a hint?

This final step stores the filtered roads in a variable for further use.