1. Home
  2. Explore
  3. Computer Science
  4. Graph Data Structures in Java

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Graph Data Structures in Java

  • 58Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Graph Fundamentals

What are the basic components of a graph, and what kind of data structure is it?

58 questions

  1. 1

    What are the basic components of a graph, and what kind of data structure is it?

    Model answer

    A graph is a nonlinear data structure made up of vertices, which hold data, and edges, which connect vertices.

    Explanation

    Vertices are also called nodes, and edges represent connections between them.

  2. 2

    Why are graphs useful for modeling real-world systems?

    Model answer

    Graphs represent entities and the relationships or connections between them, making them suitable for modeling networks and other connected systems.

    Explanation

    A graph can represent both the items in a system and how those items are linked.

  3. 3

    How could a neighborhood bus network be represented as a graph?

    Model answer

    Represent bus stops as vertices and bus routes between stops as edges.

    Explanation

    The vertices represent locations, while the edges represent connections between locations.

  4. 4

    How could web pages and hyperlinks be represented as a graph?

    Model answer

    Represent web pages as vertices and hyperlinks connecting pages as edges.

    Explanation

    The links between pages form the graph’s connections.

  5. 5

    How does a social network represent people and friendships in a graph?

    Model answer

    People are vertices, and friendships are edges connecting the corresponding people.

    Explanation

    This representation models social relationships as connections between people.

  6. 6

    What is a path in a graph?

    Model answer

    A path is a sequence of vertices connected by edges, possibly passing through intermediate vertices.

    Explanation

    For example, a path from Peter to Cody could go directly or through Jordan.

  7. 7

    When is a graph disconnected?

    Model answer

    A graph is disconnected when there is no path between at least some of its vertices.

    Explanation

    In a disconnected graph, some vertices cannot be reached from others.

  8. 8

    How does an undirected graph differ from a directed graph?

    Model answer

    An undirected edge represents a connection in both directions, while a directed edge has an orientation and represents a one-way connection.

    Explanation

    A directed connection is commonly shown with an arrow; an undirected connection has no direction.

  9. 9

    What distinguishes a weighted graph from an unweighted graph?

    Model answer

    A weighted graph assigns a value, or weight, to its edges; an unweighted graph does not assign edge weights.

    Explanation

    Weights can represent quantities such as distance or cost.

  10. 10

    What features must a flexible graph implementation support to represent the graph types described?

    Model answer

    It must support directed and undirected graphs as well as weighted and unweighted graphs.

    Explanation

    These properties determine whether connections have direction and whether edges carry weights.

  11. 11

    What information does a vertex store in the described Java graph model?

    Model answer

    A vertex stores arbitrary data and a list of edge instances representing its connections to other vertices.

    Explanation

    Its data identifies or describes the vertex, and its edge list records its neighbors.

  12. 12

    What does the vertex constructor do in the described implementation?

    Model answer

    It assigns the supplied input data to the vertex and initializes its edge list as a new list of edges.

    Explanation

    The initialized list is ready to store the vertex’s connections.

  13. 13

    What information does an edge store in the described Java model?

    Model answer

    An edge stores its start vertex, end vertex, and an optional integer weight.

    Explanation

    The start and end specify the connection’s endpoints; the weight can represent its cost or distance.

  14. 14

    Why does the implementation use an Integer for an optional edge weight rather than an int?

    Model answer

    An Integer can be null when no weight is supplied, whereas an int must contain a numeric value.

    Explanation

    The nullable wrapper type allows the implementation to represent an absent weight directly.

  15. 15

    What does the graph store, and what properties does it track?

    Model answer

    The graph stores its vertices in a list and tracks whether it is directed and whether it is weighted.

    Explanation

    These properties let the graph manage connections according to its chosen type.

  16. 16

    What does the graph’s add-vertex operation do?

    Model answer

    It creates a vertex from the supplied data, adds it to the graph’s vertex list, and returns the new vertex.

    Explanation

    Returning the vertex allows callers to retain and use it when adding edges.

  17. 17

    How does the vertex-level add-edge operation create a connection?

    Model answer

    It adds a new edge to the vertex’s edge list, using the current vertex as the start and the supplied vertex as the end, with the supplied optional weight.

    Explanation

    The vertex method provides a direct way to add one outgoing connection.

  18. 18

    How does the graph-level add-edge operation differ for directed and undirected graphs?

    Model answer

    It adds an edge from the first vertex to the second; for an undirected graph, it also adds the reverse edge from the second vertex to the first.

    Explanation

    Adding the reverse connection represents the two-way relationship of an undirected edge.

  19. 19

    How does the graph-level add-edge operation handle an unweighted graph?

    Model answer

    When the graph is unweighted, the implementation sets the edge weight to null before adding the connection.

    Explanation

    The graph’s weighted property determines whether the supplied weight is used.

  20. 20

    How does the vertex-level remove-edge operation identify which edge to remove?

    Model answer

    It removes edges whose end vertex equals the specified vertex.

    Explanation

    The method filters the vertex’s edge list based on the edge’s destination.

  21. 21

    How does the graph-level remove-edge operation handle an undirected connection?

    Model answer

    It removes the edge in the specified direction and, for an undirected graph, also removes the reverse edge.

    Explanation

    Removing both directions keeps the two-way connection consistent.

  22. 22

    What does the graph’s remove-vertex operation do in the described implementation?

    Model answer

    It removes the specified vertex from the graph’s vertex list.

    Explanation

    The operation acts on the list that stores the graph’s vertices.

  23. 23

    How does the graph find a vertex by its data value?

    Model answer

    It checks the stored vertices and returns the vertex whose data equals the requested value; if none matches, it returns null.

    Explanation

    This lets a caller look up a vertex by a value such as a bus-stop name.

  24. 24

    What information can the graph’s query methods provide?

    Model answer

    They can return the vertex list and indicate whether the graph is weighted or directed.

    Explanation

    These methods expose the graph’s stored contents and configuration.

  25. 25

    What is the purpose of a graph-printing method in the implementation?

    Model answer

    It displays vertices and their connections so the graph’s internal structure can be inspected.

    Explanation

    Printing is a way to visualize or check the implementation.

  26. 26

    What is the main purpose of a graph search or traversal algorithm?

    Model answer

    It visits vertices in a graph, for example to search for a particular vertex value or explore the graph’s structure.

    Explanation

    Traversal provides a systematic way to move through graph connections.

  27. 27

    How does depth-first search explore a graph?

    Model answer

    Depth-first search follows one path as far as possible before returning to explore other available paths.

    Explanation

    It proceeds down a path rather than visiting all immediate neighbors first.

  28. 28

    How does breadth-first search explore a graph?

    Model answer

    Breadth-first search visits the starting vertex’s neighbors first, then explores vertices in successive layers farther from the start.

    Explanation

    It expands outward from the origin one neighbor layer at a time.

  29. 29

    Why do graph searches keep track of visited vertices?

    Model answer

    A visited list or set prevents the search from revisiting vertices and can prevent infinite loops in cyclic graphs.

    Explanation

    Cycles can otherwise cause a search to keep returning to already explored vertices.

  30. 30

    What is the upper-bound runtime given for depth-first and breadth-first search when the whole graph is examined?

    Model answer

    The runtime is O(V+E)O(V + E)O(V+E), where VVV is the number of vertices and EEE is the number of edges.

    Explanation

    In the stated upper-bound case, the search examines every vertex and edge.

  31. 31

    How does the described recursive depth-first traversal proceed from a vertex?

    Model answer

    It examines the current vertex’s edges and recursively visits each neighbor that has not yet been visited.

    Explanation

    The visited collection is shared across recursive calls so vertices are not revisited.

  32. 32

    How does the described breadth-first traversal manage its search frontier?

    Model answer

    It uses a queue: it removes the next vertex, examines its neighbors, and adds unvisited neighbors to the queue.

    Explanation

    The queue causes vertices to be explored in layers rather than following one path deeply.

  33. 33

    What is the difference between recursive DFS and the described BFS implementation in how they manage exploration?

    Model answer

    The described DFS uses recursion to follow a path, while the described BFS uses a queue to process neighboring vertices layer by layer.

    Explanation

    These mechanisms produce different exploration orders.

  34. 34

    What assumption determines the order in which neighbors are visited in the illustrated traversal example?

    Model answer

    When there is a choice, the example assumes that the algorithm visits the neighbor with the smaller numerical value first.

    Explanation

    The chosen neighbor order affects the displayed traversal sequence.

  35. 35

    What does preorder mean in the described depth-first traversal?

    Model answer

    In preorder, a vertex is added to the visited and output lists before it is added to the stack for further exploration.

    Explanation

    The vertex is recorded when it is first encountered, before exploring onward.

  36. 36

    What does postorder mean in the described depth-first traversal?

    Model answer

    In postorder, a vertex is added to the visited list when encountered but is added to the output list after it is popped from the stack.

    Explanation

    A vertex is recorded in the output after its exploration is complete.

  37. 37

    How is reverse postorder related to postorder?

    Model answer

    Reverse postorder is the postorder output list in reverse order.

    Explanation

    The two orders contain the same vertices but in opposite sequences.

  38. 38

    What other name is given to reverse postorder in the content?

    Model answer

    Reverse postorder is also called topological sorting or topological sort.

    Explanation

    The content uses these names for the reversed postorder list.

  39. 39

    Using the illustrated graph and the smaller-neighbor-first rule, what is the preorder DFS sequence starting at vertex 1?

    Model answer

    The sequence is 1,3,2,5,41, 3, 2, 5, 41,3,2,5,4.

    Explanation

    The traversal follows the available smaller-numbered neighbor first and records vertices before completing their exploration.

  40. 40

    Using the illustrated graph, what is the postorder DFS sequence starting at vertex 1?

    Model answer

    The sequence is 5,2,4,3,15, 2, 4, 3, 15,2,4,3,1.

    Explanation

    Vertices are recorded as they are popped after their reachable unvisited neighbors have been explored.

  41. 41

    Using the illustrated graph, what is the reverse postorder sequence?

    Model answer

    The sequence is 1,3,4,2,51, 3, 4, 2, 51,3,4,2,5.

    Explanation

    This is the exact reverse of the postorder sequence.

  42. 42

    What shortest-distance problem does Dijkstra’s algorithm address in the content?

    Model answer

    It finds shortest distances from a starting vertex to other vertices in a graph.

    Explanation

    The content describes applying it to problems such as finding a best route or transferring data across a network.

  43. 43

    How does the algorithm initialize distances from a starting vertex?

    Model answer

    It sets the starting vertex’s distance to zero and the distances to other vertices to infinity, represented in the Java implementation by the maximum integer value.

    Explanation

    Initially, no route to the other vertices has yet been established.

  44. 44

    What vertex does Dijkstra’s algorithm select at each iteration?

    Model answer

    It selects and removes the unvisited vertex with the smallest known distance.

    Explanation

    The implementation uses a priority queue to retrieve the minimum-priority entry.

  45. 45

    How does the algorithm calculate a candidate distance to a neighbor?

    Model answer

    It adds the current vertex’s distance from the start to the weight of the edge leading to the neighbor.

    Explanation

    This sum represents the distance of a route that reaches the neighbor through the current vertex.

  46. 46

    When does Dijkstra’s algorithm update a neighbor’s recorded distance?

    Model answer

    It updates the distance when the candidate distance through the current vertex is less than the neighbor’s currently recorded distance.

    Explanation

    The smaller candidate replaces the existing value; a larger candidate does not improve it.

  47. 47

    What role does the priority queue play in the described Dijkstra implementation?

    Model answer

    It stores vertices with priorities based on their distances and allows the vertex with the smallest distance to be removed next.

    Explanation

    This supports the algorithm’s repeated selection of the minimum-distance unvisited vertex.

  48. 48

    What information is stored in the distance dictionary?

    Model answer

    It maps each vertex’s data value to its current shortest known distance from the starting vertex.

    Explanation

    The dictionary is updated as shorter routes are discovered.

  49. 49

    What information is stored in the previous-vertex dictionary?

    Model answer

    It maps each vertex’s data value to the vertex that precedes it on the currently known shortest route.

    Explanation

    These predecessor references can later be used to reconstruct a route.

  50. 50

    What happens to a neighbor’s previous-vertex entry when a shorter route is found through the current vertex?

    Model answer

    The neighbor’s previous entry is updated to the current vertex.

    Explanation

    This records how the improved route reaches that neighbor.

  51. 51

    In the example, what candidate distance from A to E is found by going through C?

    Model answer

    The candidate distance is 333, because the distance from A to C is 222 and the edge from C to E has weight 111.

    Explanation

    The route’s distance is the sum of those two values.

  52. 52

    In the example, why is the candidate route from A through B to C not used to update C’s distance?

    Model answer

    That route has distance 555, while C already has a shorter recorded distance of 222 from A.

    Explanation

    The algorithm keeps the existing distance when the newly calculated candidate is greater.

  53. 53

    What runtime does the content give for Dijkstra’s algorithm using its described priority-queue approach?

    Model answer

    The runtime is O(E+Vlog⁡V)O(E + V\log V)O(E+VlogV), where EEE is the number of edges and VVV is the number of vertices.

    Explanation

    The content states this bound for the implementation that uses a priority queue.

  54. 54

    How can the shortest path between a starting vertex and a target vertex be reconstructed from the previous-vertex dictionary?

    Model answer

    Start at the target and repeatedly follow its previous-vertex reference until reaching the starting vertex, then arrange the collected vertices from start to target.

    Explanation

    Following predecessors moves backward along the route, so the collected sequence must be placed in forward order.

  55. 55

    Why does the described path-reconstruction method add each vertex at the beginning of the path list?

    Model answer

    It follows predecessor references backward from the target, so adding each vertex at the beginning builds the final path in forward order.

    Explanation

    Each newly reached predecessor belongs before the vertex already collected.

  56. 56

    What two outputs does the shortest-path method report for a target vertex?

    Model answer

    It reports the shortest distance from the starting vertex and the sequence of vertices forming the path.

    Explanation

    The distance dictionary supplies the distance, and the previous-vertex dictionary supplies the path references.

  57. 57

    If the previous-vertex entry for the starting vertex is null, what does that indicate during path reconstruction?

    Model answer

    It indicates that the backward traversal has reached the starting vertex and should stop.

    Explanation

    The starting vertex has no predecessor on the path from itself.

  58. 58

    In the example, what shortest route from A to G is reported, and what is its distance?

    Model answer

    The reported route is A to D to E to G, with a total distance of −38-38−38.

    Explanation

    The route and distance are the example’s reported result from its graph and edge weights.

1 of 58
Question 1 of 58

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent