Graph Data Structures in Java
- 50Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Which components make up a graph data structure?
Which components make up a graph data structure?
50 questions
- 1
Which components make up a graph data structure?
- A
Queues that hold items and pointers that determine their order
- B
Arrays that hold values and indexes that sort them
- C
Vertices that hold data and edges that connect vertices
- D
Trees that hold roots and leaves that store connections
Correct answer
CVertices that hold data and edges that connect vertices
Explanation
A graph consists of vertices (also called nodes) and edges connecting vertices.
- A
- 2
Why are graphs useful for modeling networks?
- A
They represent relationships only when the entities are stored consecutively
- B
They require every entity to have exactly two relationships
- C
They automatically arrange every entity in numerical order
- D
They represent entities as vertices and relationships between them as edges
Correct answer
DThey represent entities as vertices and relationships between them as edges
Explanation
Graphs can model networks by representing entities as vertices and their connections or relationships as edges.
- A
- 3
In a graph representing a neighborhood bus network, what could the vertices and edges represent?
- A
Distances as vertices and bus schedules as edges
- B
Bus stops as vertices and routes between stops as edges
- C
Bus routes as vertices and distances as edges
- D
Passengers as vertices and bus stops as edges
Correct answer
BBus stops as vertices and routes between stops as edges
Explanation
The example models bus stops as vertices and routes connecting stops as edges.
- A
- 4
How could a network of web pages and hyperlinks be represented as a graph?
- A
Web pages are edges, and hyperlinks are vertices
- B
Web pages are vertices, and hyperlinks are edges
- C
Hyperlinks are vertices, and web pages are weights
- D
Each web page is a graph, and each hyperlink is a vertex
Correct answer
BWeb pages are vertices, and hyperlinks are edges
Explanation
A web page can be modeled as a vertex, while a hyperlink connecting pages can be modeled as an edge.
- A
- 5
In a social-network graph, what does an edge between two people typically represent in the example?
- A
A route that one person must follow to reach the other
- B
A friendship between the two people
- C
A shared data value stored by both people
- D
A ranking of one person above the other
Correct answer
BA friendship between the two people
Explanation
The social-network example represents people as vertices and friendships as edges.
- A
- 6
What is a path between two vertices?
- A
A list of all vertices in the graph, whether connected or not
- B
A sequence of vertices connected by edges, possibly through intermediate vertices
- C
A single edge that must connect every vertex in the graph
- D
A collection of weights that identifies the graph's direction
Correct answer
BA sequence of vertices connected by edges, possibly through intermediate vertices
Explanation
A path connects vertices through edges and may include intermediate vertices.
- A
- 7
When is a graph disconnected?
- A
When at least one pair of vertices has no path connecting them
- B
When the graph contains a cycle
- C
When every vertex stores more than one edge
- D
When every edge has a direction
Correct answer
AWhen at least one pair of vertices has no path connecting them
Explanation
A graph is disconnected if a path does not exist between some vertices; for example, separate groups of vertices cannot be reached from one another.
- A
- 8
If Peter can reach Cody by either a direct edge or a route through Jordan, what does this illustrate?
- A
A graph must have exactly one path between any two vertices
- B
There can be multiple paths between the same two vertices
- C
A direct edge prevents paths through intermediate vertices
- D
A path can exist only in an undirected graph
Correct answer
BThere can be multiple paths between the same two vertices
Explanation
The example gives two paths from Peter to Cody: a direct connection and one through Jordan.
- A
- 9
What distinguishes an undirected edge from a directed edge?
- A
An undirected edge represents a two-way connection; a directed edge has a specified direction
- B
An undirected edge connects only one pair; a directed edge connects many pairs
- C
An undirected edge stores data; a directed edge stores no data
- D
An undirected edge has a weight; a directed edge cannot have one
Correct answer
AAn undirected edge represents a two-way connection; a directed edge has a specified direction
Explanation
Undirected connections are bidirectional, while directed edges indicate a one-way direction from one vertex to another.
- A
- 10
If a bus route is represented by a directed edge from Stop A to Stop B, what can be concluded from that edge alone?
- A
Stop A and Stop B are disconnected
- B
The route has no associated travel cost
- C
The route must also go from Stop B to Stop A
- D
The route goes from Stop A to Stop B, but not necessarily from Stop B to Stop A
Correct answer
DThe route goes from Stop A to Stop B, but not necessarily from Stop B to Stop A
Explanation
A directed edge records a one-way connection. A return connection would need its own edge.
- A
- 11
What information does an edge weight represent in the transportation example?
- A
A numerical cost, distance, or expense associated with traveling along the edge
- B
The number of vertices in the graph
- C
The data value stored at the edge's starting vertex
- D
Whether the edge is directed or undirected
Correct answer
AA numerical cost, distance, or expense associated with traveling along the edge
Explanation
Weights label edges with numerical quantities such as distance or travel cost.
- A
- 12
A graph can be both directed and weighted. What does that combination mean?
- A
Its edges have specified directions and numerical weights
- B
Its vertices are weighted, but connections have no direction
- C
Its edges connect in both directions but cannot have costs
- D
Its vertices have directions and its edges store arbitrary text
Correct answer
AIts edges have specified directions and numerical weights
Explanation
Direction specifies which way an edge goes; a weight associates a numerical value with that edge. These properties can coexist.
- A
- 13
In the described graph design, where are the connections from a particular vertex stored?
- A
In a list of edge instances maintained by that vertex
- B
In the vertex's data string
- C
In a list of all graph vertices maintained by each edge
- D
In a single global integer stored by the graph
Correct answer
AIn a list of edge instances maintained by that vertex
Explanation
Each vertex maintains a list of edge instances representing its connections to other vertices.
- A
- 14
What is the purpose of the graph's list of vertex instances?
- A
It stores the weights of every edge
- B
It stores all vertices that belong to the graph
- C
It records the order in which edges are traversed
- D
It stores only the vertices with no outgoing edges
Correct answer
BIt stores all vertices that belong to the graph
Explanation
The graph maintains a list of its vertices, while each vertex separately maintains its edge list.
- A
- 15
Why does the graph object keep directed and weighted properties?
- A
So it can infer each vertex's data type from its neighbors
- B
So it can automatically sort vertices by their data
- C
So it can guarantee that all vertices are connected
- D
So its operations can reflect whether connections are one-way and whether edges have weights
Correct answer
DSo its operations can reflect whether connections are one-way and whether edges have weights
Explanation
The graph stores these properties to distinguish directed from undirected graphs and weighted from unweighted graphs.
- A
- 16
In the Java design, what does an Edge instance represent?
- A
The graph's full list of vertices and its directed setting
- B
A traversal queue and the order in which vertices are visited
- C
A vertex's data and its complete list of neighboring vertices
- D
A connection with a start vertex, an end vertex, and an optional weight
Correct answer
DA connection with a start vertex, an end vertex, and an optional weight
Explanation
The Edge class stores its start and end vertices and an integer weight that may be absent.
- A
- 17
Why is an Integer used for an optional edge weight rather than the primitive int in the described implementation?
- A
An Integer can be null to represent an absent weight
- B
An Integer can hold vertex objects as well as weights
- C
An Integer guarantees that every weight is nonnegative
- D
An Integer automatically stores the edge's direction
Correct answer
AAn Integer can be null to represent an absent weight
Explanation
The wrapper type Integer can hold null, which is used to indicate that no weight was supplied.
- A
- 18
When adding an edge to an undirected graph, what must happen in the described implementation?
- A
Add one connection and mark both vertices as visited
- B
Add an edge only if both vertices have no existing connections
- C
Add two edges with different weights in the same direction
- D
Add a connection from the first vertex to the second and a reciprocal connection back
Correct answer
DAdd a connection from the first vertex to the second and a reciprocal connection back
Explanation
An undirected connection is represented by adding edges in both directions.
- A
- 19
When adding an edge to a directed graph, what does the described graph method do?
- A
Adds the connection from the first vertex to the second without automatically adding its reverse
- B
Adds an edge between every pair of stored vertices
- C
Adds connections in both directions but marks one as inactive
- D
Adds a connection only from the second vertex to the first
Correct answer
AAdds the connection from the first vertex to the second without automatically adding its reverse
Explanation
For a directed graph, the method adds the specified one-way edge; the reverse edge is not automatic.
- A
- 20
What does the vertex's add-edge method do in the described Java design?
- A
Creates an Edge from the current vertex to the supplied end vertex and adds it to the vertex's edge list
- B
Removes all existing edges that end at the supplied vertex
- C
Changes the graph from directed to undirected
- D
Adds the supplied vertex to the graph's global vertex list
Correct answer
ACreates an Edge from the current vertex to the supplied end vertex and adds it to the vertex's edge list
Explanation
The vertex is the edge's start, and the supplied vertex is its end; the new Edge is added to the current vertex's edge list.
- A
- 21
How does the described vertex remove-edge method determine which edges to remove?
- A
It removes the edge with the smallest weight
- B
It removes all edges that start at the supplied vertex
- C
It removes every edge whose weight matches the supplied vertex's data
- D
It removes edges whose end vertex equals the supplied vertex
Correct answer
DIt removes edges whose end vertex equals the supplied vertex
Explanation
The method filters the current vertex's edge list for edges whose end vertex matches the requested vertex.
- A
- 22
What does the graph's add-vertex method return in the described design?
- A
The number of vertices already in the graph
- B
The graph's complete edge list
- C
The weight of the first edge added to the graph
- D
The newly created vertex instance
Correct answer
DThe newly created vertex instance
Explanation
The method constructs a vertex from the supplied data, adds it to the graph's list, and returns that vertex.
- A
- 23
How does the described get-vertex-by-value operation search for a vertex?
- A
It searches only the first vertex's edge list
- B
It checks each stored vertex's data against the requested value
- C
It checks each edge's weight against the requested value
- D
It follows a shortest path until it finds matching data
Correct answer
BIt checks each stored vertex's data against the requested value
Explanation
The operation loops through the graph's vertices and compares each vertex's data with the requested value; it returns null if no match is found.
- A
- 24
What does the graph-level remove-vertex method do in the described implementation?
- A
Removes every edge that points to the specified vertex from every other vertex
- B
Removes the specified vertex and automatically recalculates all shortest paths
- C
Removes the specified vertex from the graph's vertex list
- D
Removes the vertex's data but keeps the vertex and its edges
Correct answer
CRemoves the specified vertex from the graph's vertex list
Explanation
The described method removes the vertex from the graph's ArrayList. It does not describe also removing every edge that references it.
- A
- 25
What is the effect of choosing a weighted, directed graph for a bus network?
- A
The graph can contain only routes with identical distances
- B
Every bus route becomes bidirectional and has no numerical cost
- C
Edges can represent one-way routes and carry values such as route distances
- D
Each stop stores the distance to every other stop without edges
Correct answer
CEdges can represent one-way routes and carry values such as route distances
Explanation
A weighted, directed graph can represent one-way travel and attach a numerical cost or distance to each route.
- A
- 26
Why is a visited collection used during graph traversal?
- A
It stores the weights needed to calculate every path
- B
It prevents the traversal from processing the same vertex repeatedly
- C
It sorts all vertices by their stored data
- D
It converts a directed graph into an undirected graph
Correct answer
BIt prevents the traversal from processing the same vertex repeatedly
Explanation
Tracking visited vertices prevents repeated work and helps avoid infinite loops in cyclic graphs.
- A
- 27
What is the main exploration pattern of depth-first search (DFS)?
- A
It always selects the edge with the smallest weight
- B
It visits all immediate neighbors before exploring deeper vertices
- C
It visits vertices only when they have exactly one neighbor
- D
It follows one available path deeper until it cannot continue, then backtracks
Correct answer
DIt follows one available path deeper until it cannot continue, then backtracks
Explanation
DFS explores down a path before backtracking to explore other available paths.
- A
- 28
What is the main exploration pattern of breadth-first search (BFS)?
- A
It follows one path to its endpoint before considering other neighbors
- B
It chooses each next vertex by minimum total path cost
- C
It explores the starting vertex's neighbors before moving to more distant layers
- D
It visits only vertices connected by the graph's lightest edge
Correct answer
CIt explores the starting vertex's neighbors before moving to more distant layers
Explanation
BFS explores in layers: first the start vertex's direct neighbors, then neighbors of those vertices, and so on.
- A
- 29
Which data structure does the described iterative BFS use to decide which vertex to process next?
- A
A stack
- B
A queue
- C
A dictionary of previous vertices
- D
A priority queue ordered by edge weight
Correct answer
BA queue
Explanation
The described BFS enqueues discovered vertices and removes them from the queue for processing.
- A
- 30
Which approach does the described recursive DFS use to continue from a newly discovered neighbor?
- A
A search for the neighbor's minimum-weight edge
- B
A recursive call to DFS on that neighbor
- C
A queue insertion followed by processing all other neighbors
- D
A reset of the visited collection
Correct answer
BA recursive call to DFS on that neighbor
Explanation
The recursive DFS marks a neighbor as visited and then calls the traversal method on that neighbor.
- A
- 31
When should a traversal add a neighboring vertex to its visited collection and continue exploring it?
- A
Whenever the neighbor has an edge, even if it was visited
- B
When that neighbor has not already been visited
- C
Only when the neighbor has no outgoing edges
- D
Only when the neighbor has the smallest data value in the graph
Correct answer
BWhen that neighbor has not already been visited
Explanation
Checking whether a neighbor is unvisited prevents redundant exploration and repeated processing.
- A
- 32
What is the upper-bound runtime for DFS or BFS when the traversal examines every vertex and edge?
- A
O(VE)
- B
O(log V)
- C
O(V+E)
- D
O(V2+E2)
Correct answer
CO(V+E)
Explanation
In the stated upper-bound scenario, the traversal examines all vertices and edges, giving runtime O(V+E), where V is the number of vertices and E is the number of edges.
- A
- 33
In the demonstrated graph, why does DFS visit vertex 4 after returning from vertex 2?
- A
Vertex 4 has a smaller value than every vertex already visited
- B
Vertex 2 has no unvisited neighbor, so DFS backtracks to 3 and explores its remaining unvisited neighbor, 4
- C
Vertex 2 is removed from the graph after it is visited
- D
DFS always chooses the last vertex in the graph's vertex list
Correct answer
BVertex 2 has no unvisited neighbor, so DFS backtracks to 3 and explores its remaining unvisited neighbor, 4
Explanation
DFS backtracks when a vertex has no unvisited neighbor. From vertex 3, it can then continue to the still-unvisited vertex 4.
- A
- 34
What distinguishes preorder DFS from postorder DFS in the described traversal definitions?
- A
Preorder reverses edge directions; postorder preserves them
- B
Preorder outputs a vertex before exploring its descendants; postorder outputs it after it is popped from the traversal stack
- C
Preorder uses a queue; postorder uses a priority queue
- D
Preorder visits only leaves; postorder visits only starting vertices
Correct answer
BPreorder outputs a vertex before exploring its descendants; postorder outputs it after it is popped from the traversal stack
Explanation
Preorder adds each vertex to the output before continuing its exploration, whereas postorder adds it after it has finished exploring and is popped from the stack.
- A
- 35
Given the stated neighbor preference for smaller numbers, what is the preorder DFS output for the illustrated traversal starting at 1?
- A
1, 2, 3, 4, 5
- B
5, 2, 4, 3, 1
- C
1, 3, 4, 2, 5
- D
1, 3, 2, 5, 4
Correct answer
D1, 3, 2, 5, 4
Explanation
The traversal visits 1, then 3, then 2 and 5; after backtracking, it visits 4, producing preorder 1, 3, 2, 5, 4.
- A
- 36
What is the postorder DFS output for the illustrated traversal starting at 1?
- A
1, 3, 4, 2, 5
- B
1, 3, 2, 5, 4
- C
1, 2, 3, 4, 5
- D
5, 2, 4, 3, 1
Correct answer
D5, 2, 4, 3, 1
Explanation
Postorder records vertices after their descendants are explored: 5 and 2 are completed first, then 4, then 3, and finally 1.
- A
- 37
How is reverse postorder defined in the content?
- A
It is the BFS output ordered by edge weight
- B
It is the postorder output list written in reverse
- C
It is the postorder list with every other vertex removed
- D
It is the preorder list with the starting vertex removed
Correct answer
BIt is the postorder output list written in reverse
Explanation
Reverse postorder is exactly the reverse of the postorder list.
- A
- 38
For the illustrated graph, which list is identified as reverse postorder?
- A
1, 3, 4, 2, 5
- B
5, 2, 4, 3, 1
- C
1, 2, 3, 4, 5
- D
1, 3, 2, 5, 4
Correct answer
A1, 3, 4, 2, 5
Explanation
The stated postorder is 5, 2, 4, 3, 1; reversing it gives 1, 3, 4, 2, 5.
- A
- 39
Why can DFS be useful when the goal is to organize or display all reachable vertex values, not just find one path?
- A
It automatically includes vertices in disconnected components
- B
Its visitation order can provide an ordered listing of the vertices it traverses
- C
It calculates the shortest weighted distance to each vertex
- D
It guarantees that vertices are sorted by their numerical values
Correct answer
BIts visitation order can provide an ordered listing of the vertices it traverses
Explanation
DFS can produce an order of visitation from a starting point, which can be used to list the vertices it reaches.
- A
- 40
What is Dijkstra's algorithm used to compute in the described content?
- A
A preorder listing of all vertices in a directed graph
- B
Shortest distances from a starting vertex to other vertices in a weighted graph
- C
The number of disconnected components in an unweighted graph
- D
The maximum weight among all edges in a graph
Correct answer
BShortest distances from a starting vertex to other vertices in a weighted graph
Explanation
Dijkstra's algorithm tracks and updates distances from a start vertex to find shortest distances.
- A
- 41
How are initial distances set in the described Dijkstra example?
- A
Every vertex has distance zero until it is visited
- B
Each vertex begins with the weight of its lightest outgoing edge
- C
The start vertex has distance zero, and the other vertices initially have infinite distance
- D
The start vertex has infinite distance, and the others have distance one
Correct answer
CThe start vertex has distance zero, and the other vertices initially have infinite distance
Explanation
The starting vertex is zero distance from itself; distances to the other vertices are initially treated as infinite.
- A
- 42
Which vertex does the described Dijkstra procedure process next?
- A
The vertex with the largest tentative distance
- B
The unvisited vertex with the most outgoing edges
- C
The unvisited vertex with the smallest currently known distance
- D
The first vertex in the graph's stored list
Correct answer
CThe unvisited vertex with the smallest currently known distance
Explanation
The procedure repeatedly removes and processes the unvisited vertex with the minimum known distance, using a priority queue in the implementation.
- A
- 43
When examining an edge from the current vertex to a neighbor, how does Dijkstra determine a candidate distance to that neighbor?
- A
It replaces the current distance with the edge's weight alone
- B
It adds the neighbor's current distance to the number of graph vertices
- C
It subtracts the edge's weight from the current vertex's known distance
- D
It adds the current vertex's known distance to the edge's weight
Correct answer
DIt adds the current vertex's known distance to the edge's weight
Explanation
The candidate route length is the distance to the current vertex plus the weight of the edge to the neighbor.
- A
- 44
When should Dijkstra update a neighbor's stored distance?
- A
Whenever the neighbor has not yet been added to the graph
- B
When the candidate route through the current vertex is shorter than the stored distance
- C
Only when the neighbor has no other incoming edges
- D
Whenever the candidate route is longer than the stored distance
Correct answer
BWhen the candidate route through the current vertex is shorter than the stored distance
Explanation
Dijkstra replaces a stored distance when a shorter route to that vertex has been found.
- A
- 45
In the example, if the current best distance from A to C is 2 and a route through B would give 5, what should the algorithm do?
- A
Keep the distance to C at 2 because 5 is not an improvement
- B
Replace the distance to C with 5 because the route was found later
- C
Add the two candidate distances to obtain 7
- D
Set the distance to C to infinity until C is processed
Correct answer
AKeep the distance to C at 2 because 5 is not an improvement
Explanation
The algorithm keeps the smaller known distance; the candidate distance 5 does not improve on 2.
- A
- 46
What role does the priority queue serve in the described Dijkstra implementation?
- A
It stores every vertex's outgoing edges in alphabetical order
- B
It prevents the graph from containing cycles
- C
It records the complete path from the start to each vertex
- D
It makes it possible to retrieve the queued vertex with the smallest priority
Correct answer
DIt makes it possible to retrieve the queued vertex with the smallest priority
Explanation
The priority queue is organized by priority so the smallest-distance queued vertex can be removed next.
- A
- 47
What information does the implementation's previous-vertex dictionary store?
- A
For each edge, the list of all vertices connected to it
- B
For each vertex, the preceding vertex on the currently best-known route
- C
For each start vertex, the graph's direction setting
- D
For each vertex, the total number of paths leading to it
Correct answer
BFor each vertex, the preceding vertex on the currently best-known route
Explanation
The previous dictionary records predecessor vertices, allowing the chosen route to be reconstructed from a target back to the start.
- A
- 48
How does the described shortest-path method reconstruct a route after Dijkstra has computed distances?
- A
It repeats BFS from every vertex and selects the first matching path
- B
It sorts all vertices by their stored data and joins them in order
- C
It follows the graph's edges from the start without consulting recorded predecessors
- D
It starts at the target and follows previous-vertex references back toward the start
Correct answer
DIt starts at the target and follows previous-vertex references back toward the start
Explanation
The method walks backward from the target through predecessor references, inserting each vertex at the beginning of the path list.
- A
- 49
In the example, what route is reported as the shortest path from A to G?
- A
A to C to B to G
- B
A to B to C to G
- C
A to E to D to G
- D
A to D to E to G
Correct answer
DA to D to E to G
Explanation
The example reports the shortest route from A to G as A to D to E to G.
- A
- 50
What time complexity does the content give for Dijkstra's algorithm using a priority queue?
- A
O(V+E)
- B
O(ElogE+V2)
- C
O(E+VlogV)
- D
O(VE)
Correct answer
CO(E+VlogV)
Explanation
The stated complexity is O(E+VlogV), where E is the number of edges and V is the number of vertices.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.