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 all points and connections.
How does the number of points and connections affect the time to explore or use the graph?
Analyze the time complexity of the following code snippet.
// Simple graph traversal example
void printEdges(int vertices, int edges[][2], int edgeCount) {
for (int i = 0; i < edgeCount; i++) {
int u = edges[i][0];
int v = edges[i][1];
printf("Edge from %d to %d\n", u, v);
}
}
This code prints all edges in a graph represented by an edge list.
- Primary operation: Loop over all edges to print each connection.
- How many times: Exactly once for each edge in the graph.
As the number of edges grows, the work grows directly with it.
| Input Size (edges) | Approx. Operations |
|---|---|
| 10 | 10 print operations |
| 100 | 100 print operations |
| 1000 | 1000 print operations |
Pattern observation: The work grows in a straight line with the number of edges.
Time Complexity: O(E)
This means the time to process the graph grows directly with the number of edges.
[X] Wrong: "The time depends only on the number of vertices, not edges."
[OK] Correct: Edges represent connections, and many operations must look at each edge, so ignoring edges misses the real work.
Understanding how vertices and edges affect time helps you explain graph problems clearly and choose the right approach confidently.
"What if the graph is directed versus undirected? How would that affect the time complexity of traversing all edges?"
Practice
vertex in a graph?Solution
Step 1: Understand the definition of vertex
A vertex is a fundamental unit in a graph representing an entity or point.Step 2: Differentiate vertex from edges and other terms
Edges connect vertices; paths and graph types are different concepts.Final Answer:
A point or node representing an entity in the graph -> Option AQuick Check:
Vertex = node/entity [OK]
- Confusing vertices with edges
- Thinking vertices are paths
- Mixing vertex with graph types
u to vertex v with weight 5 in C struct?Solution
Step 1: Identify the correct struct fields for weighted edge
A weighted edge needs source vertex, destination vertex, and weight as fields.Step 2: Check struct syntax correctness
struct Edge { int u; int v; int weight; }; correctly declares all three fields in one struct.Final Answer:
struct Edge { int u; int v; int weight; }; -> Option CQuick Check:
Weighted edge struct = u, v, weight [OK]
- Separating weight outside the edge struct
- Incorrect field order causing confusion
- Missing weight field
typedef struct { int u; int v; int weight; } Edge;
Edge edges[] = { {0, 1, 10}, {1, 2, 20}, {2, 0, 30} };
int n = 3;
for (int i = 0; i < n; i++) {
printf("%d -> %d (weight %d)\n", edges[i].u, edges[i].v, edges[i].weight);
}Solution
Step 1: Read the edges array values
Edges are: (0,1,10), (1,2,20), (2,0,30).Step 2: Trace the loop printing each edge
Loop prints each edge in order with format "u -> v (weight w)".Final Answer:
0 -> 1 (weight 10) 1 -> 2 (weight 20) 2 -> 0 (weight 30) -> Option BQuick Check:
Print edges as stored = 0 -> 1 (weight 10) 1 -> 2 (weight 20) 2 -> 0 (weight 30) [OK]
- Reversing u and v in print
- Mixing up weights between edges
- Printing edges in wrong order
typedef struct { int u; int v; } Edge;
Edge edges[2];
edges[0].u = 0; edges[0].v = 1;
edges[1].u = 1; edges[1].v = 0;
// Print edges
for (int i = 0; i <= 2; i++) {
printf("Edge from %d to %d\n", edges[i].u, edges[i].v);
}Solution
Step 1: Check array size and loop bounds
Edges array has size 2, valid indices are 0 and 1.Step 2: Identify loop error
Loop runs from i=0 to i=2 inclusive, which accesses edges[2] (out of bounds).Final Answer:
Loop condition should be i < 2, not i <= 2 -> Option DQuick Check:
Array size 2 means max index 1 [OK]
- Using <= instead of < in loop
- Assuming edges need weight for undirected
- Confusing edge direction in undirected graphs
(0,1,4), (1,2,6), (0,2,5)How many edges will be stored if you represent this graph using an edge list storing each edge only once?
Solution
Step 1: Understand undirected edge storage
In undirected graphs, each edge connects two vertices without direction, so store only one entry per edge.Step 2: Count unique edges given
Edges are (0,1), (1,2), (0,2) -- total 3 unique edges.Final Answer:
3 -> Option AQuick Check:
Undirected edges stored once = 3 [OK]
- Counting each edge twice
- Confusing directed with undirected storage
- Ignoring weights when counting edges
Ready to prove it?
Pass the test and this pattern moves unknown
