Graph Terminology Vertices Edges Directed Undirected Weighted in DSA Java - Time & Space Complexity
Graphs are made of points and connections. We want to understand how the time to work with these parts grows as the graph gets bigger.
How does the number of points and connections affect the work done?
Analyze the time complexity of the following code snippet.
// Simple graph representation
class Graph {
int vertices; // number of points
LinkedList<Integer>[] edges; // connections list
Graph(int v) {
vertices = v;
edges = new LinkedList[v];
for (int i = 0; i < v; i++) {
edges[i] = new LinkedList<Integer>();
}
}
void addEdge(int from, int to) {
edges[from].add(to); // add connection
}
}
This code creates a graph with points and connections using lists. It adds a connection from one point to another.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop to initialize the list of connections for each point.
- How many times: Once for each point (vertices times).
As the number of points grows, the initialization work grows in a straight line. Adding one connection is a quick step.
| Input Size (vertices) | Approx. Operations |
|---|---|
| 10 | 10 loops to create lists |
| 100 | 100 loops to create lists |
| 1000 | 1000 loops to create lists |
Pattern observation: The work grows directly with the number of points.
Time Complexity: O(V)
This means the time to set up the graph grows directly with the number of points.
[X] Wrong: "Adding a connection takes time depending on all points or connections."
[OK] Correct: Adding a connection is just putting one item in a list, which is quick and does not depend on the total size.
Understanding how graph size affects work helps you explain and write efficient graph code confidently in interviews.
"What if we used a matrix instead of lists to store connections? How would the time complexity change?"
Practice
vertex in a graph?Solution
Step 1: Understand the definition of vertex
A vertex is a fundamental unit in a graph representing a point or node.Step 2: Differentiate vertex from edges and graph types
Edges connect vertices; weights and graph types describe edges or the whole graph, not vertices.Final Answer:
A point or node where edges meet -> Option AQuick Check:
Vertex = point/node [OK]
- Confusing edges with vertices
- Thinking weights define vertices
- Mixing graph types with vertex definition
Solution
Step 1: Identify data structure for weighted edges
Weighted edges need to store both neighbor and weight, so Pair is used.Step 2: Choose correct map and list types
HashMap with List of pairs is standard for adjacency list of weighted graphs.Final Answer:
Map>> graph = new HashMap<>(); -> Option DQuick Check:
Weighted graph uses Pair for edges [OK]
- Using List<Integer> for weighted edges
- Using TreeSet instead of List for adjacency
- Confusing Map types for graph
Map graph = new HashMap<>(); graph.put(1, Arrays.asList(2, 3)); graph.put(2, Arrays.asList(3)); graph.put(3, Arrays.asList()); System.out.println(graph.get(1));What will be the output?
Solution
Step 1: Check what is stored for key 1
Key 1 maps to list containing 2 and 3.Step 2: Print the list for key 1
Printing graph.get(1) outputs the list [2, 3].Final Answer:
[2, 3] -> Option CQuick Check:
graph.get(1) = [2, 3] [OK]
- Expecting all vertices in output
- Confusing keys and values
- Assuming empty list prints null
void addEdge(Map graph, int u, int v) {
graph.get(u).add(v);
}Solution
Step 1: Understand undirected graph edge addition
Undirected edges must be added both ways: u to v and v to u.Step 2: Analyze given code
Code adds edge only from u to v, missing the reverse edge.Final Answer:
It adds edge only from u to v, missing v to u -> Option AQuick Check:
Undirected edges need both directions [OK]
- Adding edge only one way in undirected graph
- Confusing get() and put() usage
- Clearing adjacency list mistakenly
Map>> graph = new HashMap<>(); graph.put(1, Arrays.asList(new Pair<>(2, 5), new Pair<>(3, 10))); graph.put(2, Arrays.asList(new Pair<>(3, 2))); graph.put(3, Arrays.asList());How would you find the total weight of edges going out from vertex 1?
Solution
Step 1: Identify edges from vertex 1
Edges from 1 go to 2 with weight 5, and to 3 with weight 10.Step 2: Sum the weights of these edges
Total weight = 5 + 10 = 15.Final Answer:
Sum weights 5 + 10 = 15 -> Option BQuick Check:
Total outgoing weight from 1 = 15 [OK]
- Adding weights from wrong vertices
- Counting edges instead of summing weights
- Mixing weights of different edges
Ready to prove it?
Pass the test and this pattern moves unknown
