Graph Terminology Vertices Edges Directed Undirected Weighted in DSA C# - Time & Space Complexity
Graphs are made of points and connections. We want to understand how the work grows when we look at these points and connections.
How does the time to check or use these parts change as the graph gets bigger?
Analyze the time complexity of the following code snippet.
// Simple graph representation
class Graph {
int vertices;
List<Tuple<int, int>> edges = new List<Tuple<int, int>>();
public Graph(int v) {
vertices = v;
}
public void AddEdge(int from, int to) {
edges.Add(Tuple.Create(from, to));
}
}
This code creates a graph with a set number of points (vertices) and stores connections (edges) between them.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding edges to the list one by one.
- How many times: Once per edge added, total edges count is E.
As we add more edges, the work to store them grows directly with the number of edges.
| Input Size (Edges E) | Approx. Operations |
|---|---|
| 10 | 10 adds |
| 100 | 100 adds |
| 1000 | 1000 adds |
Pattern observation: The work grows in a straight line with the number of edges.
Time Complexity: O(E)
This means the time to add edges grows directly with how many edges we have.
[X] Wrong: "Adding edges depends on the number of vertices, so it is O(V)."
[OK] Correct: Adding edges depends on how many edges we add, not just vertices. Vertices count alone doesn't tell us how many connections exist.
Understanding how graph size affects work helps you explain and choose the right methods when working with graphs in real problems.
"What if the graph was directed versus undirected? How would that affect the number of edges and time complexity?"
Practice
Solution
Step 1: Understand graph components
A graph consists of points called nodes and connections called edges.Step 2: Identify the term for nodes
The points or nodes where edges meet are called vertices.Final Answer:
Vertices -> Option AQuick Check:
Nodes = Vertices [OK]
- Confusing edges with vertices
- Thinking weights are nodes
- Mixing paths with vertices
Solution
Step 1: Understand adjacency list representation
An adjacency list uses a dictionary mapping each vertex to a list of connected vertices.Step 2: Check the correct C# syntax
Dictionary correctly represents a graph where each int key has a list of connected nodes.Final Answer:
Dictionary<int, List<int>> graph = new Dictionary<int, List<int>>(); -> Option CQuick Check:
Adjacency list = Dictionary with List values [OK]
- Using List<int> alone
- Using array instead of dictionary
- Using Dictionary which stores single values
var graph = new Dictionary>();
graph[1] = new List<(int, int)> { (2, 5), (3, 10) };
graph[2] = new List<(int, int)> { (1, 5), (3, 3) };
graph[3] = new List<(int, int)> { (1, 10), (2, 3) };
// What is the weight of the edge between vertex 2 and vertex 3?Solution
Step 1: Locate edge from vertex 2 to 3
In graph[2], the list contains (1,5) and (3,3). The tuple (3,3) means vertex 3 with weight 3.Step 2: Identify the weight value
The weight of the edge between 2 and 3 is 3.Final Answer:
3 -> Option DQuick Check:
Edge (2,3) weight = 3 [OK]
- Confusing vertex numbers
- Picking weight from wrong tuple
- Adding weights incorrectly
var graph = new Dictionary();
graph[1] = new List<int> { 2 };
// Add edge from 2 to 1
graph[2].Add(1);
What is the cause of the error?Solution
Step 1: Check initialization of graph[2]
graph[2] is not assigned any list before calling Add, so it is null.Step 2: Understand why Add causes error
Calling Add on null causes a NullReferenceException at runtime.Final Answer:
graph[2] is not initialized before adding -> Option AQuick Check:
Null list causes error [OK]
- Assuming dictionary auto-initializes lists
- Thinking undirected graphs disallow adding edges
- Confusing syntax error with runtime error
var graph = new Dictionary>
{
{1, new List<(int, int)>{ (2, 4), (3, 1) }},
{2, new List<(int, int)>{ (3, 2) }},
{3, new List<(int, int)>{ (4, 7) }},
{4, new List<(int, int)>{}}
};
// What is the total weight of the path 1 -> 3 -> 4?Solution
Step 1: Find weight from 1 to 3
From graph[1], edge to 3 has weight 1.Step 2: Find weight from 3 to 4
From graph[3], edge to 4 has weight 7.Step 3: Calculate total path weight
Total weight = 1 + 7 = 8.Final Answer:
8 -> Option BQuick Check:
1->3=1 + 3->4=7 = 8 [OK]
- Adding wrong edges
- Ignoring direction of edges
- Using wrong weights from graph
Ready to prove it?
Pass the test and this pattern moves unknown
