Hint: Print edges in array order with their weights [OK]
Common Mistakes:
Reversing u and v in print
Mixing up weights between edges
Printing edges in wrong order
4. Identify the error in this code snippet that tries to create an undirected graph edge list:
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);
}
medium
A. Edge direction is incorrect for undirected graph
B. Edges should have weight field
C. Edges array size should be 1, not 2
D. Loop condition should be i < 2, not i <= 2
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 D
Quick Check:
Array size 2 means max index 1 [OK]
Hint: Loop index must be less than array size [OK]
Common Mistakes:
Using <= instead of < in loop
Assuming edges need weight for undirected
Confusing edge direction in undirected graphs
5. You have a weighted undirected graph with vertices 0, 1, 2 and edges: (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?
medium
A. 3
B. 6
C. 2
D. 1
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.