📖 Scenario: You are working on a simple social network app where users can be friends with each other. You want to represent the friendship connections between users using graphs.There are two common ways to represent these connections: adjacency list and adjacency matrix. Each has its own advantages depending on the number of users and connections.
🎯 Goal: Build two graph representations for the same set of users and their friendships: an adjacency list and an adjacency matrix. Learn when to use each representation by comparing their structures.
📋 What You'll Learn
Create an adjacency list for 4 users with given friendships
Create an adjacency matrix for the same 4 users
Use a variable to store the number of users
Print both the adjacency list and adjacency matrix to see their structure
💡 Why This Matters
🌍 Real World
Graphs are used in social networks, maps, and recommendation systems to represent connections between entities.
💼 Career
Understanding graph representations helps in roles like software development, data science, and network engineering where efficient data structures are key.
Progress0 / 4 steps
1
Create the adjacency list for 4 users
Create a dictionary called adjacencyList where keys are integers 0 to 3 representing users, and values are lists of integers representing friends. Use these exact friendships: user 0 is friends with 1 and 2, user 1 is friends with 0 and 3, user 2 is friends with 0, user 3 is friends with 1.
DSA C#
Hint
Use a Dictionary<int, List<int>> to represent the adjacency list. Each key is a user number, and the value is a list of friends.
2
Create a variable for the number of users
Create an integer variable called numUsers and set it to 4 to represent the total number of users.
DSA C#
Hint
Just create an integer variable numUsers and assign it the value 4.
3
Create the adjacency matrix for the same users
Create a 2D integer array called adjacencyMatrix of size numUsers by numUsers. Fill it with 0s and 1s to represent the same friendships: 1 means friends, 0 means no friendship. Use these exact connections: user 0 friends with 1 and 2, user 1 friends with 0 and 3, user 2 friends with 0, user 3 friends with 1.
DSA C#
Hint
Use a 2D array int[,] adjacencyMatrix with rows and columns equal to numUsers. Fill rows with 0s and 1s to show friendships.
4
Print both adjacency list and adjacency matrix
Print the adjacency list by iterating over adjacencyList and printing each user and their friends. Then print the adjacency matrix row by row. Use Console.WriteLine for output.
DSA C#
Hint
Use a foreach loop to print the adjacency list. Use nested for loops to print the adjacency matrix rows.
Practice
(1/5)
1. Which data structure is generally better for representing a graph with many nodes but very few edges?
easy
A. Adjacency List
B. Adjacency Matrix
C. Array
D. Stack
Solution
Step 1: Understand graph density
A graph with many nodes but few edges is called a sparse graph.
Step 2: Choose data structure for sparse graph
Adjacency List stores only existing edges, saving space for sparse graphs.
Final Answer:
Adjacency List -> Option A
Quick Check:
Sparse graph = Adjacency List [OK]
Hint: Sparse graph? Use adjacency list for space efficiency [OK]
Common Mistakes:
Choosing adjacency matrix for sparse graphs wastes space
Confusing sparse with dense graphs
Thinking arrays or stacks represent graphs directly
2. Which of the following is the correct way to declare an adjacency matrix for a graph with 4 nodes in C#?
easy
A. int[][] adjacencyMatrix = new int[4][]; for(int i=0; i<4; i++) adjacencyMatrix[i] = new int[4];
B. int adjacencyMatrix = new int[4];
C. List<int>[] adjacencyMatrix = new List<int>[4];
D. int[,] adjacencyMatrix = new int[4,4];
Solution
Step 1: Identify adjacency matrix type
An adjacency matrix is a 2D array representing edges between nodes.
Step 2: Check C# syntax for 2D arrays
In C#, 2D arrays use syntax like int[,] array = new int[rows, columns];
Final Answer:
int[,] adjacencyMatrix = new int[4,4]; -> Option D
Only matrix[0,1], matrix[1,2], and matrix[2,0] are set to 1; matrix[1,0] remains 0.
Step 2: Understand default values in int[] arrays
Unassigned int elements default to 0 in C#.
Final Answer:
0 because matrix[1,0] was never set -> Option B
Quick Check:
Unset matrix elements = 0 [OK]
Hint: Unset matrix cells default to 0, not symmetric [OK]
Common Mistakes:
Assuming adjacency matrix is symmetric by default
Expecting runtime error for valid indices
Confusing default values with assigned values
5. You need to represent a social network graph with 10,000 users where each user has on average 10 friends. Which representation is best and why?
Choose between adjacency list and adjacency matrix.
hard
A. Adjacency list, because it uses less memory for sparse graphs
B. Adjacency matrix, because it allows quick edge checks
C. Adjacency matrix, because it is simpler to implement
D. Adjacency list, because it stores all possible edges
Solution
Step 1: Calculate graph density
10,000 users with 10 friends each means about 100,000 edges, which is sparse compared to 100 million possible edges.
Step 2: Choose data structure for sparse graph
Adjacency list stores only existing edges, saving huge memory compared to adjacency matrix.
Final Answer:
Adjacency list, because it uses less memory for sparse graphs -> Option A
Quick Check:
Sparse large graph = adjacency list [OK]
Hint: Large sparse graph? Use adjacency list for memory efficiency [OK]