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

  • 80Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Graph Concepts
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What is a graph data structure?

Tap to flipClick to flipspace

A nonlinear data structure made up of vertices and edges, used to represent connections and networks.

Or swipe the card

80 cards

  1. What is a graph data structure?

    A nonlinear data structure made up of vertices and edges, used to represent connections and networks.

  2. What is a vertex in a graph?

    A single node that holds data.

  3. What is an edge in a graph?

    A connection between vertices.

  4. What is a path in a graph?

    A sequence of vertices connected by edges, possibly through intermediate vertices.

  5. When is a graph disconnected?

    When at least one pair of vertices has no path connecting them.

  6. How can a neighborhood bus network be modeled as a graph?

    Bus stops are vertices, and bus routes between stops are edges.

  7. How can the web be modeled as a graph?

    Web pages are vertices, and hyperlinks connecting them are edges.

  8. How can a social network be modeled as a graph?

    People are vertices, and friendships are edges.

  9. In the social-network example, why is there no path between Peter and Caleb?

    Caleb is in a separate, unconnected part of the graph.

  10. What are some major uses of graphs?

    Modeling networks and solving problems involving connections, such as route planning and computer-network data transfer.

  11. What distinguishes a directed graph from an undirected graph?

    A directed graph's edges have direction; an undirected graph's edges represent two-way connections.

  12. How are edges typically shown in directed and undirected graph diagrams?

    Directed edges use arrows; undirected edges are shown without arrows.

  13. What does a weighted graph associate with its edges?

    A numeric weight, such as a distance, cost, or expense.

  14. What is an unweighted graph?

    A graph whose edges have no associated weights.

  15. Can a graph be both directed and weighted?

    Yes. Direction and weight are separate properties of a graph.

  16. How does an undirected friendship between Peter and Jordan differ from a directed connection?

    An undirected friendship connects them both ways; a directed edge may point from Peter to Jordan without implying a connection back.

  17. In the bus-network example, what could an edge weight represent?

    The distance or cost of traveling between two bus stops.

  18. What does an edge's direction represent in a one-way bus network?

    The allowed direction of travel from one stop to another.

  19. What information does a Java vertex object store in the described design?

    Its data and an ArrayList of its edge objects.

  20. What does the Java vertex constructor initialize?

    It assigns the input data and creates a new edge list.

  21. What information does an edge object store in the described design?

    A start vertex, an end vertex, and an optional integer weight.

  22. Why is the edge weight represented as an Integer rather than an int in the example?

    An Integer can be null to represent an edge with no supplied weight.

  23. What does the edge's getStart method return?

    The edge's start vertex.

  24. What does the edge's getEnd method return?

    The edge's end vertex.

  25. What does the edge's getWeight method return?

    The edge's weight.

  26. How does the described Java graph represent its vertices?

    As an ArrayList of vertex instances.

  27. What graph properties are stored in the described Java graph class?

    Whether the graph is weighted and whether it is directed.

  28. What does the graph constructor receive and initialize?

    It receives weighted and directed flags, initializes the vertex list, and stores those flags.

  29. What does addVertex do in the described graph implementation?

    It creates a vertex from the supplied data, stores it in the graph, and returns it.

  30. What kind of data does the example Java vertex store?

    A string, though the graph concept itself allows vertices to hold data.

  31. How does the vertex-level addEdge method create an edge?

    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.

  32. How does the graph-level addEdge method handle a directed graph?

    It adds an edge from the first vertex to the second vertex only.

  33. How does the graph-level addEdge method handle an undirected graph?

    It adds an edge in both directions, one from each vertex to the other.

  34. What weight is used when adding an edge to an unweighted graph in the described implementation?

    The weight is set to null.

  35. How does the described vertex-level removeEdge method select an edge to remove?

    It removes edges whose end vertex equals the supplied vertex.

  36. How does the graph-level removeEdge method handle an undirected graph?

    It removes the edge in both directions.

  37. What does the described removeVertex method do?

    It removes the supplied vertex from the graph's vertex list.

  38. What does getVertexByValue do if it finds a matching data value?

    It returns the vertex whose data matches the supplied value.

  39. What does getVertexByValue return when no vertex matches?

    null.

  40. What are the three graph algorithms highlighted for traversal or graph search?

    Depth-first search (DFS), breadth-first search (BFS), and Dijkstra's algorithm.

  41. What is the goal of a graph search algorithm?

    To traverse graph vertices, often while searching for a particular vertex value or path.

  42. What is the central strategy of depth-first search?

    Follow one path as far as possible before backtracking to explore other paths.

  43. What is the central strategy of breadth-first search?

    Explore vertices layer by layer, expanding outward from the starting vertex.

  44. Why do graph searches keep track of visited vertices?

    To avoid visiting the same vertex repeatedly.

  45. Why is tracking visited vertices especially important in a cyclic graph?

    It prevents the search from repeatedly following a cycle, which could otherwise cause an infinite loop.

  46. What is the upper-bound runtime for DFS and BFS when examining every vertex and edge?

    O(∣V∣+∣E∣)O(|V| + |E|)O(∣V∣+∣E∣), where ∣V∣|V|∣V∣ is the number of vertices and ∣E∣|E|∣E∣ is the number of edges.

  47. How can DFS be useful even when the goal is to list or organize all graph values rather than find a path?

    It can produce an ordered visitation of vertices or their values.

  48. What does a DFS stack help track?

    The vertices along the current exploration path and which vertex to resume from after backtracking.

  49. In preorder DFS, when is a vertex added to the output list?

    When it is first visited, before it is pushed onto the stack.

  50. In postorder DFS, when is a vertex added to the output list?

    After it has been popped off the stack.

  51. How is reverse postorder produced?

    Reverse the postorder output list.

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

    Topological sort.

  53. If DFS starts at vertex 1 and chooses the smallest available neighbor first, what preorder does the example produce?

    1, 3, 2, 5, 4.

  54. What postorder does the example DFS produce?

    5, 2, 4, 3, 1.

  55. What reverse postorder does the example DFS produce?

    1, 3, 4, 2, 5.

  56. In a recursive DFS implementation, what happens after the current vertex is processed?

    For each adjacent vertex not yet visited, mark it visited and recursively search from it.

  57. How does the described DFS implementation find a vertex's neighbors?

    It iterates through that vertex's edge list and obtains each neighbor from the edge's end vertex.

  58. How does DFS backtrack when the current vertex has no unvisited neighbors?

    It pops the current vertex from the stack and resumes at the previous vertex.

  59. What does a complete DFS traversal produce if every vertex is reachable from the start?

    A visitation list containing all reachable vertices.

  60. How does BFS organize its exploration of a graph?

    It visits the start vertex and its direct neighbors first, then explores the neighbors of those vertices in successive layers.

  61. What data structure does the described BFS implementation use to manage vertices to explore?

    A queue.

  62. What is the first vertex placed in the BFS queue?

    The starting vertex.

  63. What does BFS do with a vertex removed from the front of its queue?

    It processes the vertex and examines its adjacent vertices.

  64. When does the described BFS traversal finish?

    When its queue is empty.

  65. How does BFS avoid enqueuing an already encountered neighbor again?

    It checks the visited list and adds the neighbor to both the visited list and queue only if it has not been visited.

  66. Using the example's neighbor order, what BFS traversal starts at vertex 1?

    1, 3, 4, 2, 5.

  67. What problem does Dijkstra's algorithm solve?

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

  68. How is the starting vertex initialized in the described Dijkstra process?

    Its distance is set to 0; the other vertices initially have distance infinity.

  69. What vertex does Dijkstra's algorithm process next?

    The unvisited vertex with the smallest known distance.

  70. How does Dijkstra's algorithm update a neighbor's distance?

    It adds the current vertex's distance to the connecting edge's weight and replaces the neighbor's distance if this alternative is smaller.

  71. What is the purpose of the distance dictionary in the described Dijkstra implementation?

    It maps each vertex's data value to its current known distance from the start.

  72. What is the purpose of the previous-vertex dictionary in Dijkstra's algorithm?

    It records the preceding vertex on the currently known shortest route, enabling path reconstruction.

  73. Why does the described Dijkstra implementation use a priority queue?

    It can efficiently retrieve the queued vertex with the smallest distance priority.

  74. What does the priority queue's priority represent in the described implementation?

    The vertex's current known distance from the starting vertex.

  75. In the example starting at A, what are the final shortest distances to A, B, C, D, and E?

    A:0A: 0A:0, B:4B: 4B:4, C:2C: 2C:2, D:6D: 6D:6, and E:3E: 3E:3.

  76. In the example, why is the route from A to C not updated through B?

    The route through B has length 4+1=54 + 1 = 54+1=5, which is greater than the existing distance of 222.

  77. In the example, what route gives a distance of 6 from A to D?

    A to B to D, with total weight 4+2=64 + 2 = 64+2=6.

  78. How does the described implementation reconstruct a shortest path after computing distances?

    Start at the target, follow the previous-vertex references back toward the start, and insert each vertex at the beginning of a path list.

  79. What runtime does the content give for the priority-queue-based Dijkstra implementation?

    O(∣E∣+∣V∣log⁡∣V∣)O(|E| + |V|\log |V|)O(∣E∣+∣V∣log∣V∣), where ∣E∣|E|∣E∣ is the number of edges and ∣V∣|V|∣V∣ is the number of vertices.

  80. What is the example's shortest route from A to G, and what distance does it report?

    A to D to E to G, with a reported distance of −38-38−38.

Card 1 of 80
Card 1 of 80

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent