Graph Terminology Vertices Edges Directed Undirected Weighted in DSA Go - Time & Space Complexity
Graphs are made of points and connections. We want to understand how the work grows when we explore these connections.
How does the number of points and connections affect the time to process a graph?
Analyze the time complexity of the following code snippet.
// Visit all vertices and edges in a graph
func TraverseGraph(adjList map[int][]int) {
for vertex := range adjList {
// Process each vertex
for _, neighbor := range adjList[vertex] {
// Process each edge
_ = neighbor
}
}
}
This code visits every vertex and then every edge connected to that vertex in an adjacency list graph.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over all vertices and their edges.
- How many times: Outer loop runs once per vertex (V times), inner loop runs once per edge connected to that vertex.
As the number of vertices (V) and edges (E) grow, the total work grows roughly with both.
| Input Size (V, E) | Approx. Operations |
|---|---|
| V=10, E=15 | ~25 (10 vertices + 15 edges) |
| V=100, E=200 | ~300 (100 + 200) |
| V=1000, E=5000 | ~6000 (1000 + 5000) |
Pattern observation: The work grows linearly with the sum of vertices and edges.
Time Complexity: O(V + E)
This means the time to process the graph grows in direct proportion to the number of points plus the number of connections.
[X] Wrong: "The time depends only on the number of vertices or only on the number of edges."
[OK] Correct: Both vertices and edges matter because you must visit each point and all its connections to fully explore the graph.
Understanding how graph size affects processing time helps you explain your approach clearly and shows you know what matters when working with graphs.
"What if the graph was represented as an adjacency matrix instead of a list? How would the time complexity change?"
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 DQuick 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 map from a vertex to a list of connected vertices.Step 2: Choose correct Go syntax
Using map[int][]int creates a map from int to slice of ints, perfect for adjacency list.Final Answer:
graph := make(map[int][]int) -> Option AQuick Check:
Map of int to slice[int] = adjacency list [OK]
- Using map[int]int which stores single int, not list
- Using slice of slices without map for keys
- Using slice of ints which can't map nodes
graph := map[int]map[int]int{
1: {2: 5, 3: 10},
2: {3: 2},
3: {},
}
fmt.Println(graph[1][3])Solution
Step 1: Understand graph structure
graph[1] is a map with keys 2 and 3, values 5 and 10 respectively.Step 2: Access graph[1][3]
graph[1][3] accesses the weight from vertex 1 to 3, which is 10.Final Answer:
10 -> Option CQuick Check:
Weight from 1 to 3 = 10 [OK]
- Confusing weight 5 (edge 1->2) with 1->3
- Assuming 0 means no edge but 3 has empty map
- Mixing up keys and values in nested maps
func addEdge(graph map[int][]int, u, v int) {
graph[u] = append(graph[u], v)
graph[v] = append(graph[u], v)
}Solution
Step 1: Analyze append statements
First append adds v to graph[u], correct for edge u->v.Step 2: Check second append
Second append incorrectly appends v to graph[u] again instead of graph[v].Final Answer:
Appending v to graph[v] incorrectly -> Option BQuick Check:
Second append should be graph[v] = append(graph[v], u) [OK]
- Appending to wrong slice variable
- Forgetting to append both directions
- Not initializing map slices before append
Solution
Step 1: Understand undirected graph edge representation
In undirected graphs, edges go both ways, so weights must be stored both ways.Step 2: Update both directions in map
Assign weight w to graph[u][v] and graph[v][u] to reflect undirected edge.Final Answer:
graph[u][v] = w; graph[v][u] = w -> Option AQuick Check:
Both directions updated = undirected edge [OK]
- Updating only one direction
- Assigning weight twice to same direction
- Confusing directed and undirected edge updates
Ready to prove it?
Pass the test and this pattern moves unknown
