📖 Scenario: Imagine you are organizing a small social network where people can be friends with multiple others, including mutual friendships and groups. Trees are great for simple hierarchies like family trees, but they cannot show all these complex connections.
🎯 Goal: You will create a simple graph using an adjacency list to represent friendships between people. This will show how graphs can model relationships that trees cannot, such as cycles and multiple connections.
📋 What You'll Learn
Create a graph using an adjacency list with exact people and connections
Add a variable to count the number of people in the graph
Use a loop to print each person and their friends
Print the total number of people in the graph
💡 Why This Matters
🌍 Real World
Social networks, maps, and many real-world systems have complex connections that trees cannot show. Graphs help model these.
💼 Career
Understanding graphs is essential for software engineers working on networking, recommendation systems, and data analysis.
Progress0 / 4 steps
1
Create the graph with people and their friends
Create a variable called graph as an unordered_map<string, vector<string>> with these exact entries: "Alice" connected to "Bob" and "Charlie", "Bob" connected to "Alice" and "Diana", "Charlie" connected to "Alice", and "Diana" connected to "Bob".
DSA C++
Hint
Use unordered_map<string, vector<string>> to create the graph. Each key is a person, and the vector lists their friends.
2
Add a variable to count the number of people
Create an int variable called num_people and set it to the size of the graph.
DSA C++
Hint
Use graph.size() to get the number of keys (people) in the graph.
3
Print each person and their friends
Use a for loop with variables person and friends to iterate over graph. Inside the loop, print the person followed by their friends separated by commas.
DSA C++
Hint
Use a range-based for loop with structured bindings to get person and friends. Print the person, then loop over friends to print them separated by commas.
4
Print the total number of people in the graph
Write a cout statement to print "Total people: " followed by the value of num_people.
DSA C++
Hint
Use cout << "Total people: " << num_people << endl; to print the total count.
Practice
(1/5)
1. Why do we need graphs when trees already exist as data structures?
easy
A. Because graphs can represent connections between nodes in any pattern, including cycles and multiple links.
B. Because trees are faster to traverse than graphs.
C. Because trees can only store numbers, but graphs can store any data.
D. Because graphs use less memory than trees.
Solution
Step 1: Understand tree limitations
Trees are hierarchical and cannot have cycles or multiple parents for a node.
Step 2: Understand graph capabilities
Graphs allow cycles and multiple connections between nodes, modeling complex relationships.
Final Answer:
Because graphs can represent connections between nodes in any pattern, including cycles and multiple links. -> Option A
Quick Check:
Graphs model cycles and complex links = B [OK]
Hint: Graphs allow cycles; trees do not. Remember this difference [OK]
Common Mistakes:
Thinking trees can have cycles
Confusing data types stored with structure capabilities
Assuming graphs always use less memory
2. Which of the following is the correct way to declare an undirected graph using adjacency list in C++?
easy
A. int graph[n][n];
B. std::vector<int> graph[n];
C. std::map graph;
D. std::vector graph(n);
Solution
Step 1: Identify adjacency list declaration
Adjacency list uses vector of vectors or array of vectors to store neighbors.
Step 2: Check options
std::vector graph(n); uses vector of vectors with size n, suitable for adjacency list.
Final Answer:
std::vector graph(n); -> Option D
Quick Check:
Adjacency list = vector of vectors [OK]
Hint: Use vector of vectors for adjacency list in modern C++ [OK]
Common Mistakes:
Using map which is not adjacency list
Using 2D array which is adjacency matrix
Using vector<int> graph[n]; which is legacy C++
3. Consider the following C++ code snippet for an undirected graph:
std::vector graph(3);
graph[0].push_back(1);
graph[1].push_back(0);
graph[1].push_back(2);
graph[2].push_back(1);
for (int i = 0; i < 3; i++) {
std::cout << i << ": ";
for (int neighbor : graph[i]) {
std::cout << neighbor << " ";
}
std::cout << std::endl;
}
What is the output?
medium
A. 0: 1
1: 0
2: 1 2
B. 0: 1
1: 2
2: 1 0
C. 0: 1
1: 0 2
2: 1
D. 0: 0 1
1: 0 2
2: 1
Solution
Step 1: Trace graph construction
graph[0] has 1, graph[1] has 0 and 2, graph[2] has 1.
Step 2: Trace output loop
Print neighbors for each node: 0->1, 1->0 2, 2->1.