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

  • 59Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Graph Foundations
A graph is a nonlinear data structure made up of vertices and
.

59 questions

  1. 1
    A graph is a nonlinear data structure made up of vertices and
    _____
    .
    Hint

    These are the connections between nodes.

    Answer

    edges

    Explanation

    Graphs consist of vertices, which hold data, and edges, which connect vertices.

  2. 2
    Another common name for a vertex in a graph is a
    _____
    .
    Hint

    It is the point or entity represented in the graph.

    Answer

    node

    Explanation

    A vertex is a single node in a graph and can hold data.

  3. 3
    A graph is an ideal data structure for modeling
    _____
    .
    Hint

    Examples include transportation systems and social connections.

    Answer

    networks

    Explanation

    Graphs model networks by representing entities as vertices and their connections as edges.

  4. 4
    In a neighborhood transportation graph, bus stops can be represented as
    _____
    , while bus routes between stops are edges.
    Hint

    These represent the individual locations in the network.

    Answer

    vertices

    Explanation

    Each bus stop is a vertex, and a route connecting stops is an edge.

  5. 5
    In a graph representing the web, web pages are vertices and the links connecting them are
    _____
    .
    Hint

    They connect one web page to another.

    Answer

    hyperlinks

    Explanation

    The example models pages as vertices and hyperlinks between them as edges.

  6. 6
    In a social network graph, people are vertices and their relationships can be represented by
    _____
    .
    Hint

    These are the connections between people in the example.

    Answer

    friendships

    Explanation

    The social network example represents each person as a vertex and each friendship as an edge.

  7. 7
    A sequence of vertices connected by edges, possibly through intermediate vertices, forms a
    _____
    .
    Hint

    It describes a way to travel through a graph from one vertex to another.

    Answer

    path

    Explanation

    A path connects vertices through edges and may include intermediate vertices.

  8. 8
    A graph is disconnected when no
    _____
    exists between some pair of vertices.
    Hint

    Without this, one vertex cannot be reached from the other.

    Answer

    path

    Explanation

    A disconnected graph has at least two vertices for which no connecting route through edges exists.

  9. 9
    An undirected edge represents a connection that works in
    _____
    directions.
    Hint

    The connection can be followed either way.

    Answer

    both

    Explanation

    In an undirected graph, the connection is bidirectional rather than having a single arrow direction.

  10. 10
    In a weighted graph, a number associated with an edge can represent the cost or
    _____
    of using that connection.
    Hint

    For example, it can indicate how far apart two bus stops are.

    Answer

    distance

    Explanation

    Edge weights can represent quantities such as distance or expense associated with traveling along an edge.

  11. 11
    A vertex in the described Java design stores its data and a list of connections represented by
    _____
    instances.
    Hint

    Each instance describes a connection to another vertex.

    Answer

    edge

    Explanation

    The vertex maintains a list of edge instances to represent its connections to other vertices.

  12. 12
    The Java vertex class stores its connections in an
    _____
    of edge objects.
    Hint

    It is a resizable list collection in Java.

    Answer

    ArrayList

    Explanation

    The vertex class uses an ArrayList to hold its edge instances.

  13. 13
    The vertex constructor assigns the supplied input data to the vertex's
    _____
    variable.
    Hint

    This variable holds the value associated with the vertex.

    Answer

    data

    Explanation

    The constructor initializes the vertex's data field from the input parameter.

  14. 14
    When a vertex is constructed, its edge list is initialized as a new
    _____
    .
    Hint

    The collection will hold the vertex's edge instances.

    Answer

    ArrayList

    Explanation

    The constructor creates a new ArrayList of edges so the vertex can maintain its connections.

  15. 15
    An edge object in the described design records a start vertex, an end vertex, and optionally a
    _____
    .
    Hint

    This value can represent the cost of traveling along the edge.

    Answer

    weight

    Explanation

    The edge stores its endpoints and a weight representing a value such as distance or expense.

  16. 16
    The edge class uses Java's
    _____
    type for a weight that may be absent and represented by null.
    Hint

    It is the object wrapper for the primitive integer type.

    Answer

    Integer

    Explanation

    An Integer can hold null, whereas the primitive int cannot.

  17. 17
    The graph class stores all of its vertices in a list of
    _____
    instances.
    Hint

    This is the class representing each node in the graph.

    Answer

    vertex

    Explanation

    The graph maintains a collection of vertex instances representing its nodes.

  18. 18
    The graph's directed and weighted Boolean properties describe whether edges have direction and whether they have
    _____
    .
    Hint

    These are numerical values associated with edges.

    Answer

    weights

    Explanation

    The graph tracks whether it is directed and whether its edges carry weights.

  19. 19
    The graph's add vertex method creates a vertex, stores it in the graph, and
    _____
    the new vertex.
    Hint

    The caller can save and use the newly created object.

    Answer

    returns

    Explanation

    The method adds the new vertex to the graph's vertex list and returns that vertex.

  20. 20
    A graph-level add edge method can hide lower-level details from its caller through a layer of
    _____
    .
    Hint

    This lets a user use functionality without needing to know its internal implementation.

    Answer

    abstraction

    Explanation

    The graph provides an interface for adding edges, so callers need not directly manage the vertex and edge implementation.

  21. 21
    For an undirected graph, adding an edge between two vertices must create connections in
    _____
    directions.
    Hint

    Each endpoint must be able to reach the other.

    Answer

    both

    Explanation

    The graph-level add edge operation adds the reverse connection as well when the graph is undirected.

  22. 22
    For a directed graph, the graph-level add edge operation adds a connection from the start vertex to the
    _____
    vertex.
    Hint

    This is the destination endpoint of the directed edge.

    Answer

    end

    Explanation

    A directed edge points from its start vertex toward its end vertex, without automatically adding a reverse edge.

  23. 23
    When adding an edge to an unweighted graph, the implementation can set its weight to
    _____
    .
    Hint

    The value indicates that no weight was supplied.

    Answer

    null

    Explanation

    The described graph method checks whether the graph is weighted and uses null when it is not.

  24. 24
    The vertex remove edge method uses a predicate to remove edges whose
    _____
    vertex matches the requested vertex.
    Hint

    This endpoint is the destination stored in the edge.

    Answer

    end

    Explanation

    The method filters the edge list by checking whether each edge's end vertex equals the supplied vertex.

  25. 25
    A vertex's get data helper method returns the value stored in that vertex's
    _____
    field.
    Hint

    The constructor initializes this field from its input.

    Answer

    data

    Explanation

    The get data method exposes the vertex's stored data value.

  26. 26
    A graph's get vertices method returns the list containing the graph's
    _____
    .
    Hint

    These are all the nodes stored in the graph.

    Answer

    vertices

    Explanation

    The method returns the graph's ArrayList of vertex instances.

  27. 27
    A graph's get vertex by value method searches vertex data and returns the matching vertex, or
    _____
    if no match exists.
    Hint

    This value can signal that the requested stop or vertex was not found.

    Answer

    null

    Explanation

    The method compares each vertex's data with the requested value and returns null if none matches.

  28. 28
    A visited list or set prevents a graph search from visiting the same vertex
    _____
    .
    Hint

    This is especially useful when the graph contains cycles.

    Answer

    multiple times

    Explanation

    Tracking visited vertices prevents repeated processing and helps avoid infinite loops in cyclical graphs.

  29. 29
    Depth-first search explores one path as far as it can before backing up, whereas breadth-first search explores in
    _____
    .
    Hint

    It visits the start vertex's neighbors before moving to the next level.

    Answer

    layers

    Explanation

    BFS expands outward from the starting point one layer of neighboring vertices at a time.

  30. 30
    A breadth-first search begins by examining the starting vertex and its direct
    _____
    .
    Hint

    These vertices are connected to the start by one edge.

    Answer

    neighbors

    Explanation

    The first layer in BFS consists of the start vertex's direct neighbors.

  31. 31
    In the described implementations, depth-first and breadth-first search each have an upper-bound runtime of
    _____
    .
    Hint

    The search examines vertices and edges; use VVV for vertices and EEE for edges.

    Answer

    O(V+E)O(V + E)O(V+E)

    Explanation

    In the upper-bound case, both algorithms examine every vertex and edge, giving runtime O(V+E)O(V + E)O(V+E).

  32. 32
    Besides finding paths, depth-first search can organize vertex values in a clear order of
    _____
    .
    Hint

    It describes the sequence in which vertices are encountered.

    Answer

    visitation

    Explanation

    DFS can produce an ordered list of vertices or their values based on when they are visited.

  33. 33
    The three DFS traversal orders discussed are preorder, postorder, and reverse
    _____
    .
    Hint

    The last of these is also called topological sort in the lesson.

    Answer

    postorder

    Explanation

    The lesson describes preorder, postorder, and reverse postorder as three DFS traversal orders.

  34. 34
    In the described preorder traversal, a vertex is added to the output list before it is added to the
    _____
    .
    Hint

    This structure keeps track of the search's pending vertices.

    Answer

    stack

    Explanation

    Preorder records the vertex in the visited and output lists before adding it to the stack.

  35. 35
    In the described postorder traversal, a vertex is added to the output list after it is popped off the
    _____
    .
    Hint

    It is the data structure from which the vertex is removed.

    Answer

    stack

    Explanation

    Postorder records a vertex in the output list after that vertex has been popped from the stack.

  36. 36
    Reverse postorder produces an output list that is exactly the
    _____
    of the postorder list.
    Hint

    The order of the postorder entries is flipped.

    Answer

    reverse

    Explanation

    Reverse postorder is formed by reversing the postorder output list.

  37. 37
    Reverse postorder is also referred to in the lesson as
    _____
    sort.
    Hint

    This ordering is associated with directed graph structure.

    Answer

    topological

    Explanation

    The lesson identifies reverse postorder as topological sort.

  38. 38
    When DFS has several available neighbors in the numerical example, it chooses the
    _____
    numbered option first.
    Hint

    For example, it selects 3 before 4.

    Answer

    smallest

    Explanation

    The walkthrough assumes the algorithm prefers the smallest numerical neighbor whenever there is a choice.

  39. 39
    In the example DFS starting at 1, the preorder visitation sequence is
    _____
    .
    Hint

    The search takes the smaller available neighbor and records vertices before finishing their branches.

    Answer

    1, 3, 2, 5, 4

    Explanation

    Following the example's neighbor choices, preorder visits 1, then 3, 2, 5, and finally 4.

  40. 40
    In the same example, the postorder sequence is
    _____
    .
    Hint

    A vertex is recorded after its explored descendants have been completed.

    Answer

    5, 2, 4, 3, 1

    Explanation

    The walkthrough's postorder output is 5, 2, 4, 3, 1.

  41. 41
    In the same example, reverse postorder is
    _____
    .
    Hint

    Reverse the order of the postorder sequence.

    Answer

    1, 3, 4, 2, 5

    Explanation

    Reversing 5, 2, 4, 3, 1 produces 1, 3, 4, 2, 5.

  42. 42
    The recursive DFS implementation explores a neighbor only if that vertex is not already in the
    _____
    list.
    Hint

    This collection tracks which vertices have already been encountered.

    Answer

    visited

    Explanation

    The implementation checks the visited vertices list before adding a neighbor and making the recursive call.

  43. 43
    After marking a new neighbor visited, recursive DFS calls itself with that
    _____
    as the next starting point.
    Hint

    It is the vertex reached by following the current edge.

    Answer

    neighbor

    Explanation

    The recursive call continues the depth-first exploration from the newly discovered neighboring vertex.

  44. 44
    Breadth-first search processes the start vertex, then its direct neighbors, then the neighbors of those vertices, proceeding layer by
    _____
    .
    Hint

    This pattern expands outward evenly from the origin.

    Answer

    layer

    Explanation

    BFS explores all vertices at one distance layer before proceeding to the next layer.

  45. 45
    Unlike the recursive DFS implementation, the described BFS implementation uses a
    _____
    to manage vertices waiting to be explored.
    Hint

    This data structure processes items in the order they are added.

    Answer

    queue

    Explanation

    The BFS implementation enqueues the start and neighboring vertices, then removes them as it explores the graph.

  46. 46
    BFS checks all of the current vertex's direct neighbors rather than following only its first
    _____
    .
    Hint

    Each one connects the current vertex to a neighboring vertex.

    Answer

    edge

    Explanation

    BFS iterates across every edge of the current vertex so that all of its direct neighbors can be considered.

  47. 47
    In BFS, a newly discovered neighbor is added to both the visited list and the
    _____
    .
    Hint

    The neighbor will be processed after earlier queued vertices.

    Answer

    queue

    Explanation

    The implementation marks each unseen neighbor visited and enqueues it for later exploration.

  48. 48
    For the example starting at 1, breadth-first traversal produces the order
    _____
    .
    Hint

    Visit the start's direct neighbors before exploring the next layer.

    Answer

    1, 3, 4, 2, 5

    Explanation

    The walkthrough's BFS order is 1, 3, 4, 2, 5, because it explores outward by layers.

  49. 49
    Dijkstra's algorithm is used to find the shortest
    _____
    between vertices in a weighted graph.
    Hint

    It measures the minimum total cost of traveling along edges.

    Answer

    distance

    Explanation

    The algorithm computes shortest distances from a starting vertex to other vertices using edge weights.

  50. 50
    Dijkstra's algorithm initializes the distance from the starting vertex to itself as
    _____
    .
    Hint

    No edges need to be traveled to remain at the starting point.

    Answer

    zero

    Explanation

    The source vertex has distance zero from itself; other vertices initially have an unknown, effectively infinite distance.

  51. 51
    At each iteration, Dijkstra's algorithm removes the unvisited vertex with the
    _____
    known distance.
    Hint

    It selects the smallest tentative distance first.

    Answer

    minimum

    Explanation

    The algorithm repeatedly processes the unvisited vertex whose current distance estimate is smallest.

  52. 52
    When processing an edge, Dijkstra's algorithm calculates a candidate distance by adding the current distance to the edge's
    _____
    .
    Hint

    This value represents the cost of traversing that edge.

    Answer

    weight

    Explanation

    The alternative distance to a neighbor is the distance to the current vertex plus the edge weight.

  53. 53
    Dijkstra's algorithm updates a neighbor's distance only when the candidate distance is
    _____
    than its existing distance.
    Hint

    The new route must improve on the route already recorded.

    Answer

    less

    Explanation

    If the alternative route is shorter, the algorithm replaces the neighbor's current distance with the candidate value.

  54. 54
    A priority queue supports Dijkstra's algorithm by allowing it to retrieve the entry with the
    _____
    priority.
    Hint

    This entry represents the next closest unvisited vertex.

    Answer

    smallest

    Explanation

    The priority queue orders entries by priority so the minimum-distance vertex can be removed efficiently.

  55. 55
    In the described Dijkstra implementation, one dictionary maps vertex values to distances, while another records each vertex's
    _____
    vertex.
    Hint

    Following these references can reconstruct a route.

    Answer

    previous

    Explanation

    The distance dictionary stores shortest-distance estimates, and the previous dictionary tracks the vertex preceding each vertex on a shortest route.

  56. 56
    The previous-vertex dictionary is used to reconstruct the shortest
    _____
    after distances have been computed.
    Hint

    It is the sequence of vertices to follow from start to destination.

    Answer

    path

    Explanation

    Following the stored previous-vertex references backward from the target allows the implementation to build the path.

  57. 57
    The example's shortest route from A to E is A to C to E, with a total distance of
    _____
    .
    Hint

    Add the edge weights 2 and 1.

    Answer

    3

    Explanation

    The route through C has total weight 2+1=32 + 1 = 32+1=3, which is less than the alternative routes described.

  58. 58
    In the example, the shortest distance from A to D is
    _____
    .
    Hint

    The best route described goes from A to B and then B to D.

    Answer

    6

    Explanation

    The route A to B to D has weights 4 and 2, giving a total distance of 4+2=64 + 2 = 64+2=6.

  59. 59
    With a priority queue, the described Dijkstra implementation has runtime
    _____
    .
    Hint

    The lesson expresses the bound using the number of edges and the number of vertices multiplied by a logarithmic factor.

    Answer

    O(E+Vlog⁡V)O(E + V\log V)O(E+VlogV)

    Explanation

    The stated 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.

1 of 59
Question 1 of 59

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent