📖 Scenario: You are helping a city planner represent a small map of locations and roads between them. Each location is a vertex, and each road is an edge. Some roads have directions (one-way), and some have distances (weights).
🎯 Goal: You will create a graph using a dictionary where keys are locations (vertices) and values are lists of connected locations with distances. You will mark if roads are one-way (directed) or two-way (undirected), and include distances (weights) for each road.
📋 What You'll Learn
Create a dictionary called city_map with exact vertices and edges
Add a boolean variable is_directed to indicate if the graph is directed
Use a loop to create a list of all edges with their weights
Print the list of edges showing connections and distances
💡 Why This Matters
🌍 Real World
City planners and map applications use graphs to represent locations and roads, including one-way streets and distances.
💼 Career
Understanding graph representations is essential for software engineers working on navigation, network routing, and data modeling.
Progress0 / 4 steps
1
Create the graph dictionary with vertices and weighted edges
Create a dictionary called city_map with these exact entries: 'A': [('B', 5), ('C', 10)], 'B': [('C', 3)], 'C': [('A', 2)]
DSA Python
Hint
Think of city_map as a dictionary where each key is a location and the value is a list of tuples. Each tuple has a connected location and the distance.
2
Add a variable to indicate if the graph is directed
Create a boolean variable called is_directed and set it to True because roads are one-way
DSA Python
Hint
Since roads are one-way, is_directed should be True.
3
Create a list of all edges with weights using a loop
Create an empty list called edges. Use a for loop with variables start and connections to iterate over city_map.items(). Inside, use another for loop with variables end and weight to iterate over connections. Append tuples (start, end, weight) to edges
DSA Python
Hint
Use nested loops: outer loop over city_map.items(), inner loop over each connections list.
4
Print the list of edges with their weights
Use print(edges) to display the list of edges with their weights
DSA Python
Hint
The output should be a list of tuples showing each edge with its start, end, and weight.
Practice
(1/5)
1. In a graph, what do we call the points or nodes where edges meet?
easy
A. Vertices
B. Edges
C. Weights
D. Paths
Solution
Step 1: Understand graph components
A graph consists of points called vertices and connections called edges.
Step 2: Identify the term for points
The points or nodes where edges meet are called vertices.
Final Answer:
Vertices -> Option A
Quick Check:
Points in graph = Vertices [OK]
Hint: Vertices are the points, edges are the lines connecting them [OK]
Common Mistakes:
Confusing edges with vertices
Thinking weights are points
Mixing paths with vertices
2. Which of the following is the correct way to represent a weighted edge from vertex A to vertex B with weight 5 in Python using a tuple?
easy
A. (5, A, B)
B. [A, B, 5]
C. {'A': 'B', 'weight': 5}
D. (A, B, 5)
Solution
Step 1: Understand tuple representation
A weighted edge is often represented as a tuple (start_vertex, end_vertex, weight).
Step 2: Match the correct tuple format
The tuple (A, B, 5) correctly shows an edge from A to B with weight 5.
Final Answer:
(A, B, 5) -> Option D
Quick Check:
Weighted edge tuple = (start, end, weight) [OK]
Hint: Weighted edges use (start, end, weight) tuple format [OK]
Common Mistakes:
Using list instead of tuple
Putting weight first
Using dictionary instead of tuple
3. What will be the output of the following Python code snippet?