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 time to work with these connections changes as the graph grows.
How does the number of points and connections affect the work needed?
Analyze the time complexity of the following code snippet.
// Simple graph representation using adjacency list
#include <vector>
using namespace std;
void addEdge(vector<vector<pair<int,int>>>& graph, int u, int v, int w) {
graph[u].push_back({v, w}); // Add edge from u to v with weight w
}
This code adds a weighted edge from one vertex to another in a directed graph using an adjacency list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding an edge to the adjacency list (push_back).
- How many times: Once per call to addEdge; repeated for each edge added.
Each edge addition takes constant time, but the total work depends on how many edges we add.
| Input Size (number of edges) | Approx. Operations |
|---|---|
| 10 | 10 push_back operations |
| 100 | 100 push_back operations |
| 1000 | 1000 push_back operations |
Pattern observation: The work grows linearly with the number of edges added.
Time Complexity: O(E)
This means the time to build or process the graph grows directly with the number of edges.
[X] Wrong: "Adding edges takes time proportional to the number of vertices."
[OK] Correct: Adding an edge is a simple insertion and depends on edges, not vertices. Vertices affect storage but not the cost of adding one edge.
Understanding how graph size affects operations helps you explain and optimize graph algorithms clearly and confidently.
"What if the graph was undirected and we added edges in both directions? How would the time complexity change?"
Practice
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 BQuick Check:
Points in graph = Vertices [OK]
- Confusing edges with vertices
- Thinking weights are points
- Mixing paths with vertices
Solution
Step 1: Understand adjacency list declaration
An adjacency list for a graph with 5 vertices can be declared as a vector of vectors and resized to 5.Step 2: Check options for correct syntax
vector graph; graph.resize(5); // Undirected graph uses vector graph; graph.resize(5); which is correct for undirected graph adjacency list.Final Answer:
vector graph; graph.resize(5); // Undirected graph -> Option AQuick Check:
Correct adjacency list syntax = vector graph; graph.resize(5); // Undirected graph [OK]
- Using fixed size array without resize
- Confusing weighted graph syntax
- Using vector<int> graph(5) which is vector of ints, not list
vector> graph[3];
graph[1].push_back({2, 10});
graph[2].push_back({1, 5});
for(auto &edge : graph[1]) {
if(edge.first == 2) {
cout << edge.second << endl;
}
}Solution
Step 1: Understand graph construction
graph[1] has an edge to vertex 2 with weight 10; graph[2] has edge to 1 with weight 5.Step 2: Trace the loop output
The loop checks edges from vertex 1; finds edge to 2 and prints weight 10.Final Answer:
10 -> Option CQuick Check:
Weight from 1 to 2 = 10 [OK]
- Confusing edge direction
- Printing weight from wrong vertex
- Assuming undirected graph
vector> graph[3];
graph[0].push_back({1, 4});
graph[1].push_back(0, 4);Solution
Step 1: Check push_back usage
push_back expects a single argument; graph[1].push_back(0, 4) is invalid syntax.Step 2: Identify correct syntax
It should be graph[1].push_back({0, 4}); to add a pair.Final Answer:
Incorrect syntax in push_back for second edge -> Option DQuick Check:
push_back needs one argument (pair) [OK]
- Passing multiple arguments to push_back
- Not using braces for pair
- Confusing vector size with error
vector> graph[4];
graph[0].push_back({1, 3});
graph[1].push_back({0, 3});
graph[1].push_back({2, 5});
graph[2].push_back({1, 5});
graph[2].push_back({3, 7});
graph[3].push_back({2, 7});How would you add an edge between vertex 0 and vertex 3 with weight 2 correctly?
Solution
Step 1: Understand undirected edge addition
In undirected graphs, edges must be added in both directions to represent connection.Step 2: Add edge in both adjacency lists
Add {3, 2} to graph[0] and {0, 2} to graph[3] to represent the edge with weight 2.Final Answer:
graph[0].push_back({3, 2}); graph[3].push_back({0, 2}); -> Option AQuick Check:
Undirected edge = add both directions [OK]
- Adding edge only one way
- Wrong syntax for push_back
- Trying to push triple instead of pair
Ready to prove it?
Pass the test and this pattern moves unknown
