📖 Scenario: Imagine you are organizing a group of friends and their connections. Some friends know each other directly, and some are connected through others. You want to represent these connections to understand who is connected to whom.Trees are like family trees where each person has only one parent, but in real life, friends can have many connections in different ways. This is why graphs exist -- to model complex connections that trees cannot.
🎯 Goal: You will create a simple graph using a dictionary to represent friends and their connections. You will see how this graph can model relationships that a tree cannot.
📋 What You'll Learn
Create a dictionary called friend_graph with specific friend connections
Add a variable called new_friend to represent a new friend
Add a new connection between new_friend and an existing friend
Print the updated friend_graph to see all connections
💡 Why This Matters
🌍 Real World
Graphs are used to model social networks, maps, and many real-world connections where relationships are not just one-to-one.
💼 Career
Understanding graphs is important for jobs in software development, data science, and network analysis where complex relationships need to be modeled.
Progress0 / 4 steps
1
Create the initial friend graph
Create a dictionary called friend_graph with these exact entries: 'Alice': ['Bob', 'Charlie'], 'Bob': ['Alice', 'David'], 'Charlie': ['Alice'], 'David': ['Bob']
DSA Python
Hint
Use a dictionary with friend names as keys and lists of connected friends as values.
2
Add a new friend variable
Add a variable called new_friend and set it to the string 'Eve'
DSA Python
Hint
Just create a variable named new_friend and assign it the value 'Eve'.
3
Add a new connection in the graph
Add new_friend as a key in friend_graph with a list containing 'Alice'. Also, add new_friend to 'Alice''s list of connections in friend_graph
DSA Python
Hint
Use friend_graph[new_friend] = ['Alice'] to add Eve, and friend_graph['Alice'].append(new_friend) to connect Alice back to Eve.
4
Print the updated friend graph
Print the friend_graph dictionary to show all friend connections
DSA Python
Hint
Use print(friend_graph) to display the dictionary.
Practice
(1/5)
1. Why do we need graphs when we already have trees in data structures?
easy
A. Because graphs use less memory than trees.
B. Because trees are faster to traverse than graphs.
C. Because graphs can represent connections between nodes in any pattern, including cycles and multiple parents.
D. Because trees can only store numbers, but graphs can store any data.
Solution
Step 1: Understand tree limitations
Trees have a strict hierarchy: each node has only one parent and no cycles.
Step 2: Understand graph capabilities
Graphs allow nodes to connect in any way, including cycles and multiple parents.
Final Answer:
Because graphs can represent connections between nodes in any pattern, including cycles and multiple parents. -> Option C
Quick Check:
Graphs model complex connections unlike trees [OK]
Hint: Graphs allow cycles and multiple parents; trees do not [OK]
Common Mistakes:
Thinking trees can have cycles
Confusing data types stored with structure type
Assuming graphs always use less memory
2. Which of the following is the correct way to represent an undirected graph using adjacency lists in Python?
easy
A. graph = [[1, 2], [2, 3], [3, 1]]
B. graph = {1: (2, 3), 2: (1), 3: (1)}
C. graph = {1: 2, 2: 3, 3: 1}
D. graph = {1: [2, 3], 2: [1], 3: [1]}
Solution
Step 1: Understand adjacency list format
Adjacency lists use a dictionary with keys as nodes and values as lists of connected nodes.
Step 2: Check each option
graph = {1: [2, 3], 2: [1], 3: [1]} uses dictionary with lists correctly; others use tuples, lists of pairs, or single values incorrectly.
Final Answer:
graph = {1: [2, 3], 2: [1], 3: [1]} -> Option D
Quick Check:
Adjacency list = dict with lists [OK]
Hint: Adjacency lists use dict keys with list values [OK]
Common Mistakes:
Using tuples instead of lists for adjacency
Using list of pairs instead of dict
Assigning single node instead of list
3. What will be the output of the following Python code that models a graph?