Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare an adjacency list for a graph with 5 vertices.
DSA C++
std::vector<int> adj[[1]];Attempts:
3 left
💡 Hint
Common Mistakes
Using a size less than the number of vertices causes out-of-bound errors.
Using zero size means no adjacency list is created.
✗ Incorrect
The adjacency list is declared as an array of vectors with size equal to the number of vertices, which is 5 here.
2fill in blank
mediumComplete the code to add an edge from vertex 2 to vertex 3 in the adjacency list.
DSA C++
adj[2].[1](3);
Attempts:
3 left
💡 Hint
Common Mistakes
Using insert without an iterator causes errors.
Using append or add are not valid vector methods in C++.
✗ Incorrect
The push_back method adds an element to the end of the vector, which is how edges are added in adjacency lists.
3fill in blank
hardFix the error in the code to correctly print all neighbors of vertex 1.
DSA C++
for (int i = 0; i < adj[1].[1](); i++) { std::cout << adj[1][i] << " "; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() causes compilation errors.
Using capacity() returns allocated space, not actual elements.
✗ Incorrect
The size() method returns the number of elements in the vector, which is needed to loop through neighbors.
4fill in blank
hardFill both blanks to add an undirected edge between vertices 0 and 4.
DSA C++
adj[[1]].push_back([2]); adj[[2]].push_back([1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Adding edge only one way creates a directed edge.
Mixing vertex numbers causes wrong connections.
✗ Incorrect
For an undirected edge, add vertex 4 to vertex 0's list and vertex 0 to vertex 4's list.
5fill in blank
hardFill all three blanks to create an adjacency list for a graph with 3 vertices and edges (0-1), (1-2).
DSA C++
std::vector<int> adj[[1]]; adj[0].push_back([2]); adj[1].push_back([3]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong vertex numbers for edges.
Setting wrong array size causes errors.
✗ Incorrect
The graph has 3 vertices. Edge (0-1) adds 1 to adj[0]. Edge (1-2) adds 2 to adj[1].
