Week-08 Assignment - July - 2024
Week-08 Assignment - July - 2024
QUESTION 1:
Please read the following statements and then answer the question that follows.
1. A Hamiltonian path is one in which all vertices are traversed exactly once
2. Always use generics and diamond operators to declare a new map.
3. keySet(): returns a Set view of the keys contained in the map.
4. entrySet(): returns a Set view of the mappings contained in this map.
5. The minimum spanning tree problem is related to the weighted graph, where we find a
spanning tree so that the sum of all the weights of all the edges in the tree is minimum.
6. The shortest path problem is the single source shortest path problem. In this problem,
there is a distinct vertex, called source vertex and it requires finding the shortest path
from this source vertex to all other vertices.
7. A Map is a data structure that's designed for fast lookups
8. For the key-object association, a hash function is required. It is system-dependent and for
the internal use of the system
9. A Map is an object that maps keys to values or is a collection of key-value pairs. It
models the function abstraction in mathematics.
10. All the operations learned for the HashMap class is equally applicable with TreeMap
class.
Which of the above statement(s) is(are) true?
___________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
When you attempt to retrieve a value from a HashMap using a key that does not exist,
which of the following statements is correct?
a. It returns null.
b. It throws a NoSuchElementException.
c. It throws a NullPointerException.
d. It's essential to check for null to avoid potential NullPointerExceptions in your code when dealing
with HashMaps.
Correct Answer: a, d
Detailed Solution:
When you try to retrieve a value using a key that does not exist in the HashMap, it returns null. It's
essential to check for null to avoid potential NullPointerExceptions in your code when dealing with
HashMaps.
___________________________________________________________________________
QUESTION 3:
Consider an adjacency matrix (M) representation for a simple unweighted graph having V
vertices. What is the maximum number of non-zero entries possible in M?
a. V*V
b. V*(V-1)
c. (V*(V+1))/2
d. V*(V+1)
Correct Answer: b
Detailed Solution:
Maximum number of edges = V*V. Since, it is a simple graph there won’t be any self-loops, so
diagonal elements are always 0. Therefore, maximum non-zero entries=V*(V-1).
___________________________________________________________________________
QUESTION 4:
Consider a weighted, directed graph with V vertices and E edges. Which data structure
is/are not typically suitable for efficiently finding the shortest paths between all pairs of
vertices?
a. Adjacency matrix
b. Priority queue
c. Stack
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
d. Hash table
Detailed Solution:
The adjacency matrix is most suitable for efficiently finding the shortest paths between all pairs of
vertices in a weighted, directed graph. It allows constant-time access to edge weights between any two
vertices. Options B, C, and D are not typically used for this purpose in this context.
___________________________________________________________________________
QUESTION 5:
Which Java Collection is commonly used to represent a graph as an adjacency list?
a. ArrayList
b. HashMap
c. TreeMap
d. HashSet
Correct Answer: b
Detailed Solution:
A HashMap is commonly used to represent an adjacency list in graph representations.
___________________________________________________________________________
QUESTION 6:
Which of the following statement/s is/are true in the context of retrieving a value from a
HashMap in Java? (assuming a good hash function and a reasonably low load factor)
a. The time complexity of retrieving a value from a HashMap O(1)
b. The time complexity of retrieving a value from a HashMap O(log n)
c. The time complexity of retrieving a value from a HashMap O(n)
d. The time to retrieve a value from HashMap does not depend on its size.
Correct Answer: a, d
Detailed Solution:
On average, retrieving a value from a HashMap in Java is done in constant time (O(1)), assuming a good
hash function and a reasonably low load factor. This means that the time it takes to retrieve a value does
not depend on the size of the HashMap.
___________________________________________________________________________
QUESTION 7:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Detailed Solution:
In a HashMap, keys must be unique, but values can be duplicated. If you attempt to put a duplicate key
with a different value, it will overwrite the previous value associated with that key.
___________________________________________________________________________
QUESTION 8:
When implementing a breadth-first search (BFS) algorithm on a graph, what data
structure is typically used to keep track of vertices in the order they are visited?
a. Stack
b. Queue
c. Heap
d. Linked list
Correct Answer: b
Detailed Solution:
In a breadth-first search (BFS) algorithm, a queue is typically used to keep track of vertices in the order
they are visited. This ensures that vertices are processed in the order they are encountered, leading to a
level-by-level traversal of the graph. Options A, C, and D are not typically used for this purpose in BFS.
___________________________________________________________________________
QUESTION 9:
In the context of data structures, what is the primary purpose of an adjacency matrix?
a. To store the values associated with vertices in the graph.
b. To efficiently represent and store the relationships between vertices in a graph.
c. To determine the degree of a vertex in the graph.
d. To find the shortest path between two vertices.
Correct Answer: b
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Detailed Solution:
An adjacency matrix is primarily used to efficiently represent and store the relationships (edges) between
vertices in a graph. It provides a compact way to represent connectivity information between all pairs of
vertices. Options A, C, and D are not the primary purpose of an adjacency matrix.
_________________________________________________________________________
QUESTION 10:
In the context of graph traversal, what is the time complexity of determining whether a
graph is bipartite using depth-first search (DFS)?
a. O(V)
b. O(V x E)
c. O(V^2)
d. O(E + V)
Correct Answer: d
Detailed Solution:
Determining whether a graph is bipartite using DFS has a time complexity of O(V + E), where V is the
number of vertices and E is the number of edges. This involves visiting each vertex and each edge once
during the DFS traversal. Options A, C, and D do not accurately represent the time complexity of this
problem.
___________________________________________________________________________
************END************