1. Home
  2. Explore
  3. Computer Science
  4. Java Data Structures and Algorithms

Study methods

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

Java Data Structures and Algorithms

  • 97Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Foundational Concepts

What do the terms data and structure mean in the context of a data structure?

97 questions

  1. 1

    What do the terms data and structure mean in the context of a data structure?

    Model answer

    Data is information stored in a computer, while structure is the way that information is arranged.

    Explanation

    A data structure combines stored information with an organization for that information.

  2. 2

    How can the choice of data structure affect an algorithm’s efficiency?

    Model answer

    A data structure influences an algorithm’s time complexity, memory use, and the efficiency of operations and CPU use.

    Explanation

    Organization affects both the work required for operations and the memory used to store data.

  3. 3

    Compare the memory-allocation requirements of arrays and linked lists.

    Model answer

    An array requires contiguous memory allocation; a linked list can use separate available memory chunks by linking its nodes.

    Explanation

    Arrays need one continuous block, whereas linked-list nodes can occupy separate memory locations and be connected by links.

  4. 4

    What is the difference between linear and non-linear data structures?

    Model answer

    Linear structures arrange elements sequentially; non-linear structures arrange them in forms such as hierarchies or graphs.

    Explanation

    The distinction is based on how elements are arranged and connected.

  5. 5

    How does the arrangement of elements in a linear structure differ from that in a non-linear structure?

    Model answer

    Linear structures arrange data one item after another. Non-linear structures can organize data hierarchically or allow a node to have multiple neighbors.

    Explanation

    Sequential arrangement gives elements a neighboring order, while non-linear structures can connect a node to multiple others.

  6. 6

    What is an algorithm, and what does it mean for an algorithm to be language-independent?

    Model answer

    An algorithm is a step-by-step approach to solving a problem; it should express the solution clearly and independently of a specific programming language.

    Explanation

    An algorithm describes steps for solving a problem, rather than a particular programming language.

  7. 7

    What is the purpose of algorithm analysis?

    Model answer

    Algorithm analysis estimates resource requirements, especially time and space, to help compare algorithms and choose an appropriate one.

    Explanation

    Analysis helps compare candidate methods and understand their resource trade-offs.

  8. 8

    Distinguish time complexity from space complexity.

    Model answer

    Time complexity describes how the number of operations grows with input size; space complexity describes the memory required as input size grows.

    Explanation

    Time complexity concerns operations or execution growth; space complexity concerns memory requirements.

  9. 9

    What do best-case, average-case, and worst-case analysis describe, and why is worst-case analysis useful?

    Model answer

    Best-case analysis describes the most favorable input, average-case analysis describes typical performance, and worst-case analysis describes the least favorable performance and maximum resource requirement.

    Explanation

    The source emphasizes worst-case analysis because it gives an upper estimate of resource needs.

  10. 10

    What is a unit operation, and what time complexity results when a loop performs constant work for each of nnn inputs?

    Model answer

    A unit operation is a basic operation counted when estimating an algorithm’s work. A loop that processes each of nnn inputs once has time complexity O(n)O(n)O(n).

    Explanation

    A single loop over all input elements performs work proportional to the input size.

  11. 11

    Describe the basic storage and access characteristics of an array.

    Model answer

    An array stores elements in contiguous memory and supports direct access by index. Its size is fixed when allocated.

    Explanation

    The array’s indices provide direct positions, but its allocated capacity does not automatically expand.

  12. 12

    How does zero-based indexing relate an array element’s index to its address?

    Model answer

    For an array whose elements occupy www bytes, the address of index iii is the base address plus iimeswi imes wiimesw. Zero-based indexing makes index 000 refer to the first element at the base address.

    Explanation

    The array name identifies the start of its storage; index offsets are calculated from the element width.

  13. 13

    What does it mean for an array to store homogeneous elements?

    Model answer

    A given array stores elements of the same data type, rather than mixing types such as integers, floating-point values, and characters.

    Explanation

    An array is homogeneous in the content described: its elements share a data type.

  14. 14

    How do the indices used to access one-dimensional, two-dimensional, and multidimensional arrays differ?

    Model answer

    A one-dimensional array uses one index, a two-dimensional array uses two indices, and higher-dimensional arrays use additional indices.

    Explanation

    The number of array indices used corresponds to the number of dimensions.

  15. 15

    How is a two-dimensional array organized and accessed?

    Model answer

    A two-dimensional array represents data in rows and columns and is accessed with two indices, typically one for the row and one for the column.

    Explanation

    A two-dimensional array can be viewed as rows and columns, and its elements are laid out contiguously.

  16. 16

    Describe two ways to create and initialize an array in Java.

    Model answer

    In Java, an array can be allocated with new and a specified length. If it is declared with initial values, the length can be inferred from those values.

    Explanation

    Java allocates an array with new and a specified size; supplying initial values can determine the size from the values.

  17. 17

    What loop-bound condition safely visits every element of a Java array?

    Model answer

    A loop should continue while the index is less than the array’s length, so it visits indices from 000 through length - 1.

    Explanation

    In Java, indices begin at zero and the final valid index is one less than the array length.

  18. 18

    Give two advantages of arrays and describe a situation in which an array is suitable.

    Model answer

    Arrays provide direct indexed access and make it straightforward to iterate over elements; they are useful when a fixed-size, contiguous collection is suitable.

    Explanation

    Arrays are useful when contiguous storage and frequent indexed access suit the application.

  19. 19

    Why can an array’s fixed size lead either to insufficient capacity or wasted memory?

    Model answer

    An array has fixed capacity: it cannot hold more elements than allocated, while unused positions waste memory if the array is larger than needed.

    Explanation

    Capacity that is too small prevents additional storage; capacity that is too large leaves allocated positions unused.

  20. 20

    Why can insertion and deletion be difficult in an array?

    Model answer

    Insertion or deletion at an array position can require shifting other elements; insertion can also be impossible if the array is full.

    Explanation

    Moving elements creates the work associated with inserting or deleting at a position in a contiguous array.

  21. 21

    How does linear search find an element in an array?

    Model answer

    Linear search compares the key with each element in order and returns the matching index, or reports failure if no element matches.

    Explanation

    Linear search tests elements in sequence until it finds the key or exhausts the array.

  22. 22

    What are the best- and worst-case time complexities of linear search, and when does the worst case occur?

    Model answer

    Linear search has best-case time complexity O(1)O(1)O(1) and worst-case time complexity O(n)O(n)O(n); the worst case occurs when the key is last or absent.

    Explanation

    The number of comparisons can range from one to the entire array length.

  23. 23

    Explain how binary search works and state the condition required for its use.

    Model answer

    Binary search repeatedly examines the middle element and discards half the remaining search range; it requires the data to be sorted.

    Explanation

    Binary search relies on ordering to know which half can be discarded after a comparison.

  24. 24

    How does binary search choose which part of a sorted array to search next?

    Model answer

    Compare the key with the middle element. Search the right half if the key is larger, the left half if it is smaller, and stop if they are equal.

    Explanation

    If the key is larger than the middle element, the lower half cannot contain it; if smaller, the upper half can be discarded.

  25. 25

    What are the worst-case time and extra-space complexities of iterative binary search?

    Model answer

    Binary search has worst-case time complexity O(logn)O(\\log n)O(logn) and uses O(1)O(1)O(1) extra space when implemented iteratively.

    Explanation

    The search interval is reduced by approximately half at each step.

  26. 26

    How does recursive binary search proceed, and what additional resource do its recursive calls use?

    Model answer

    A recursive binary search calls itself on the selected half of the range and stops when the key is found or the range is empty; the calls use stack space.

    Explanation

    A recursive implementation makes repeated calls on a smaller range until a stopping condition is reached.

  27. 27

    Define a stack and explain its last-in, first-out behavior.

    Model answer

    A stack is a linear structure that follows last-in, first-out order: the most recently inserted element is the first removed.

    Explanation

    The stack restricts access and updates to its top, matching the plates analogy.

  28. 28

    What do the push, pop, and peek operations do on a stack?

    Model answer

    Push inserts an element at the top, pop removes the top element, and peek returns the top element without removing it.

    Explanation

    Push and pop change the top; peek reads the top without removing it.

  29. 29

    Why can common stack operations take constant time when the top is tracked?

    Model answer

    With a top pointer, push, pop, peek, and an emptiness check can each take constant time, or O(1)O(1)O(1).

    Explanation

    A top pointer allows the stack’s main operations to work on one known position.

  30. 30

    Name three applications of stacks and explain what makes a stack suitable for them.

    Model answer

    Stacks support recursion and function-call records, parsing, browser back navigation, and undo/redo operations.

    Explanation

    A stack’s access restriction makes it useful wherever the most recent unfinished task must be handled first.

  31. 31

    What limitation follows from a stack’s top-only insertion and removal rule?

    Model answer

    A stack’s main limitation is that insertion and removal occur only at the top, restricting access to other elements.

    Explanation

    A stack permits operations only at one end, so it is less flexible for accessing arbitrary elements.

  32. 32

    Define a queue and explain its first-in, first-out behavior.

    Model answer

    A queue is a linear structure that follows first-in, first-out order: the earliest inserted element is the first removed.

    Explanation

    A queue processes elements in the order they arrive.

  33. 33

    Where do insertion and deletion occur in a queue, and what do the front and rear track?

    Model answer

    A queue inserts at the rear and removes from the front; the front and rear identify those positions.

    Explanation

    The front and rear track the ends used for removal and insertion.

  34. 34

    What are enqueue, dequeue, and peek operations, and how can their time complexity be constant?

    Model answer

    Enqueue inserts, dequeue removes, and peek operations inspect an end element; with the relevant ends tracked, these operations can take O(1)O(1)O(1) time.

    Explanation

    The described queue operations act on tracked ends rather than searching through every element.

  35. 35

    Give examples of applications that use queues and explain the common ordering principle.

    Model answer

    Queues are used in scheduling, including first-in, first-out and round-robin scheduling, as well as in playlists and interrupt handling.

    Explanation

    Operating-system scheduling and ordered playback process queued items in turn.

  36. 36

    What conditions must an array-based queue check before enqueueing or dequeueing?

    Model answer

    In the described array implementation, insertion is disallowed when the rear reaches capacity, and deletion is disallowed when the queue is empty.

    Explanation

    An array-based queue must check whether there is space before insertion and whether an item exists before deletion.

  37. 37

    How does the described array-based queue represent a dequeue operation?

    Model answer

    After removing the front element, the implementation shifts later elements one position forward and reduces the rear position.

    Explanation

    The described array dequeue moves subsequent values forward to represent removal from the front.

  38. 38

    What is a limitation of a queue resulting from its endpoint restrictions?

    Model answer

    A queue is less flexible for arbitrary insertion and removal because insertion is restricted to the rear and deletion to the front.

    Explanation

    The queue’s endpoint rules restrict where items can be added or removed.

  39. 39

    What is a linked list, and how does its storage differ from an array’s?

    Model answer

    A linked list is a linear structure of linked nodes, allowing elements to be stored in separate available memory locations.

    Explanation

    Links connect nodes without requiring those nodes to occupy adjacent memory locations.

  40. 40

    What information does a singly linked-list node contain, and what does the last node’s link contain?

    Model answer

    A singly linked-list node contains data and a link to the next node; the last node’s next link is null.

    Explanation

    Each singly linked node carries its value and a reference to the next node.

  41. 41

    What is the role of the head pointer, and why must it be preserved?

    Model answer

    The head points to the first node. Losing or misdirecting it can make the rest of the list inaccessible.

    Explanation

    The head gives access to the first node, from which the remaining nodes can be reached.

  42. 42

    What does it mean for a linked list to have dynamic size?

    Model answer

    A linked list has dynamic size: nodes can be added or removed as needed, using available memory without reserving a fixed array capacity.

    Explanation

    The list’s size can change as nodes are added or removed, without reallocating one contiguous block.

  43. 43

    Why can insertion and deletion be more efficient in a linked list than in an array?

    Model answer

    Insertion or deletion can often be performed by changing links, rather than shifting many elements as an array may require.

    Explanation

    Only the references around the affected node need to be updated for a local insertion or deletion.

  44. 44

    Why is random access not available in a linked list?

    Model answer

    A linked list does not provide direct random access by index; reaching a node generally requires traversing earlier nodes in sequence.

    Explanation

    A linked list must be followed from node to node to reach a later position.

  45. 45

    Compare singly linked, doubly linked, and circular linked lists.

    Model answer

    A singly linked list has a next link, a doubly linked list has previous and next links, and a circular linked list links its last node back to its first.

    Explanation

    Singly linked nodes point forward; doubly linked nodes also point backward; circular lists connect their end back to their beginning.

  46. 46

    How does a doubly linked list address a traversal limitation of a singly linked list?

    Model answer

    A doubly linked list supports traversal in both directions, whereas a singly linked list supports traversal only forward.

    Explanation

    The previous link provides the backward traversal that a singly linked list lacks.

  47. 47

    What makes a linked list circular?

    Model answer

    A circular linked list’s last node points to the first node, forming a cycle through the list.

    Explanation

    In a circular list, reaching the end leads back to the first node rather than to null.

  48. 48

    When can a linked list be preferable because of memory availability or operation patterns?

    Model answer

    Linked lists can use fragmented available memory and are suitable when dynamic size or frequent insertion and deletion is needed.

    Explanation

    The ability to use separate free memory chunks is useful when a large contiguous block is unavailable.

  49. 49

    Name several operations available when using Java’s LinkedList collection.

    Model answer

    Java’s LinkedList supports operations such as adding and removing elements, checking for an element, obtaining the size, and iterating through the list.

    Explanation

    Java’s collection classes provide methods for adding, removing, searching, measuring, and iterating over list elements.

  50. 50

    What is a tree data structure?

    Model answer

    A tree is a non-linear data structure that stores data hierarchically in nodes connected by edges.

    Explanation

    A tree arranges nodes in levels, unlike a sequential linear structure.

  51. 51

    Define root, parent, child, sibling, and leaf in a tree.

    Model answer

    The root is the starting node; a parent has child nodes; siblings share a parent; and a leaf has no children.

    Explanation

    These terms describe node positions and relationships in the hierarchy.

  52. 52

    Compare the number of children allowed in a general tree and a binary tree.

    Model answer

    A node in a general tree may have any number of children; a node in a binary tree has at most two, usually called left and right children.

    Explanation

    A general tree permits any number of children, while a binary tree imposes a maximum.

  53. 53

    State the ordering rule for a binary search tree.

    Model answer

    In a binary search tree, values smaller than a node are placed in its left subtree and larger values in its right subtree.

    Explanation

    The search-tree ordering lets a comparison determine which subtree can contain a key.

  54. 54

    How does the ordering property guide a search in a binary search tree?

    Model answer

    To search a binary search tree, compare the key with the current node and continue left if it is smaller or right if it is larger, stopping if it matches.

    Explanation

    A comparison with the current node directs the search to one subtree rather than requiring a scan of all nodes.

  55. 55

    What is the visit order for preorder, inorder, and postorder tree traversals?

    Model answer

    Preorder visits root, left, right; inorder visits left, root, right; postorder visits left, right, root.

    Explanation

    The order in which nodes are visited distinguishes these traversal methods.

  56. 56

    Why does inorder traversal of a binary search tree produce values in sorted order?

    Model answer

    Inorder traversal of a binary search tree visits its values in ascending order.

    Explanation

    Inorder traversal visits the left subtree before each node and its right subtree.

  57. 57

    Name applications of preorder traversal described in the content.

    Model answer

    Preorder traversal can be used to copy a tree, count nodes or leaves, and produce prefix notation from an expression tree.

    Explanation

    Traversal provides a systematic way to visit every node, supporting tasks such as copying, counting, and expression conversion.

  58. 58

    What order does postorder use, and what applications are associated with it?

    Model answer

    Postorder traversal visits a node after its left and right subtrees; it can be used when deleting tree nodes and to produce postfix notation from an expression tree.

    Explanation

    Postorder processes descendants before their parent, which is useful when child nodes must be handled first.

  59. 59

    Why can a tree be useful for hierarchical data, and how can a binary search tree improve searching over a linear scan?

    Model answer

    Trees represent hierarchical information such as directory structures. Compared with a linear structure, a binary search tree can direct a search into one subtree using its ordering rule.

    Explanation

    A tree can represent nested folders, while a binary search tree adds ordering that helps direct searches.

  60. 60

    How do the cases of deleting a binary search tree node with zero, one, or two children differ?

    Model answer

    A node with no children can be removed directly; a node with one child can be replaced by that child. For a node with two children, choose a replacement that preserves ordering, such as the largest value in the left subtree or the smallest in the right.

    Explanation

    Deleting a node with two children requires maintaining the search-tree ordering after removal.

  61. 61

    How do nested loops and sequential code sections contribute to time complexity?

    Model answer

    A loop with O(n)O(n)O(n) work nested inside another O(n)O(n)O(n) loop takes O(n2)O(n^2)O(n2) time. Sequential sections add their costs, and the dominant growth term determines the simplified complexity.

    Explanation

    Nested loops repeat the inner work for each outer iteration; sequential sections contribute additively before simplification.

  62. 62

    How should the time complexity of an if/else statement be determined?

    Model answer

    For an if/else, use the greater of the two branch costs as the worst-case time complexity.

    Explanation

    An if/else executes one branch, so the slower branch bounds the running time.

  63. 63

    What time complexities result from one and two nested loops that each range over nnn items?

    Model answer

    A single loop that performs constant work for each of nnn inputs is O(n)O(n)O(n); two nested such loops are O(n2)O(n^2)O(n2).

    Explanation

    A loop can take linear time while nested loops can multiply the amount of work.

  64. 64

    What is the overall Big-O complexity of sequential sections that take O(n)O(n)O(n) and O(n2)O(n^2)O(n2) time?

    Model answer

    For sequential sections with costs such as O(n)O(n)O(n) and O(n2)O(n^2)O(n2), the total simplifies to O(n2)O(n^2)O(n2) because the higher-growth term dominates.

    Explanation

    When a program uses several sequential sections, its overall growth is governed by the section that grows fastest.

  65. 65

    What kinds of memory use should be considered when analyzing space complexity?

    Model answer

    Space complexity concerns memory used by the algorithm, including auxiliary arrays, lists, stacks, or other additional storage.

    Explanation

    An auxiliary structure is extra storage used beyond the input data itself.

  66. 66

    Why does recursion use extra space even when no explicit auxiliary array is created?

    Model answer

    Recursion uses stack space for pending function calls and their activation records; the amount depends on the recursion depth.

    Explanation

    Recursive calls remain pending until deeper calls return, so they require a call stack.

  67. 67

    What is the time-space trade-off in algorithm design?

    Model answer

    There can be a trade-off between time and space: some problems prioritize faster execution while permitting extra memory, while others prioritize lower memory use.

    Explanation

    The desired balance depends on the constraints of the problem.

  68. 68

    What is recursion, and what role does a base case play?

    Model answer

    A recursive function calls itself on a smaller or changed problem; a base case stops further calls and allows the calls to return.

    Explanation

    A recursive algorithm must eventually stop making recursive calls.

  69. 69

    What distinguishes a tail-recursive call from a recursive call followed by additional work?

    Model answer

    A tail-recursive function makes its recursive call as its final operation; there are no further statements in that call to execute after the recursive call returns.

    Explanation

    In a tail-recursive form, the recursive call is the final operation, so no later work in that function remains pending.

  70. 70

    What is backtracking, and when does an algorithm backtrack?

    Model answer

    Backtracking explores possible paths or choices and returns to an earlier decision point when the current path cannot continue or succeed.

    Explanation

    Backtracking explores alternatives, then returns to an earlier choice point when a path cannot produce a solution.

  71. 71

    What is the greedy approach, and why might it fail to find a globally optimal solution?

    Model answer

    A greedy algorithm chooses a locally optimal option at each step. These choices may produce a feasible result without producing the globally optimal result.

    Explanation

    A locally best choice is not guaranteed to lead to the best complete result.

  72. 72

    Using the route example, explain how a greedy choice can differ from the globally shortest route.

    Model answer

    A greedy route choice selects the locally quicker next leg, but a different complete route may take less total time.

    Explanation

    The route example illustrates the difference between choosing the best next step and comparing complete routes.

  73. 73

    Name four problems or algorithms associated with greedy methods in the content.

    Model answer

    Examples include the traveling salesperson problem, Prim’s and Kruskal’s minimum-spanning-tree algorithms, Dijkstra’s shortest-path algorithm, vertex cover, and knapsack variants.

    Explanation

    The content presents these as examples of problems for which greedy methods can be applied.

  74. 74

    How does the use of earlier subproblem results differ between greedy and dynamic programming?

    Model answer

    Greedy methods choose a local best option without relying on prior subproblem solutions, whereas dynamic programming uses stored earlier subproblem results to build a solution.

    Explanation

    Dynamic programming retains and uses earlier subproblem results; the described greedy method makes each choice locally.

  75. 75

    How does the route example illustrate the difference between greedy and dynamic programming?

    Model answer

    In the example, the greedy method chooses the quicker next leg and can produce a longer overall trip; dynamic programming compares the accumulated alternatives and can select the shorter total route.

    Explanation

    The content contrasts a locally chosen route with a route selected by considering total travel time.

  76. 76

    What distinguishes the fractional knapsack problem from the zero-one knapsack problem?

    Model answer

    The fractional knapsack problem selects whole items and may select a fraction of an item to maximize profit without exceeding the capacity.

    Explanation

    The fractional version permits taking part of an item when its full weight will not fit.

  77. 77

    What objective and capacity constraint define the knapsack problem?

    Model answer

    Each item has a weight and a profit, and the total selected weight must not exceed the knapsack capacity while the total profit is maximized.

    Explanation

    Profit and weight must both be considered when selecting items for a capacity-limited bag.

  78. 78

    What is the greedy procedure for solving fractional knapsack?

    Model answer

    For fractional knapsack, calculate each item’s profit-to-weight ratio, sort the ratios in descending order, and choose items in that order until capacity is reached.

    Explanation

    The greedy procedure prioritizes items with the greatest profit per unit of weight.

  79. 79

    What should be done when the next item in fractional knapsack does not fit completely?

    Model answer

    If the next item’s full weight exceeds the remaining capacity, take only the fraction that fits and include the corresponding fraction of its profit.

    Explanation

    A final partial item fills the remaining capacity when the next whole item is too heavy.

  80. 80

    Compare merge sort and quicksort in their basic strategies, space use, and worst-case time complexity.

    Model answer

    Merge sort divides the data and merges sorted subarrays, using O(n)O(n)O(n) auxiliary space and taking O(nlogn)O(n \\log n)O(nlogn) time. Quicksort partitions around a pivot and is typically in-place, but can take O(n2)O(n^2)O(n2) time in the worst case.

    Explanation

    The two approaches differ in the sorting strategy and in the extra storage used to merge results.

  81. 81

    Describe the divide-and-merge process used by merge sort.

    Model answer

    Merge sort recursively divides the array into smaller parts, sorts them, and merges sorted parts into a sorted result.

    Explanation

    Each merge combines two sorted portions by repeatedly choosing the smaller next element.

  82. 82

    How does quicksort use a pivot and recursion to sort an array?

    Model answer

    Quicksort partitions the array around a pivot so smaller values lie to its left and larger values to its right, then recursively sorts the two sides.

    Explanation

    Quicksort’s partition places the pivot between values smaller and larger than it, then recursively sorts the sides.

  83. 83

    What does the partition step accomplish in quicksort?

    Model answer

    The partition step places the pivot in its final sorted position and returns its index; quicksort then sorts the portions on either side.

    Explanation

    The pivot’s final position separates the subarrays considered by later recursive calls.

  84. 84

    Why is merge sort described as out-of-place and quicksort as in-place?

    Model answer

    Merge sort is out-of-place in the described implementation because it uses auxiliary arrays; quicksort is described as in-place because it partitions within the array.

    Explanation

    The merge procedure needs extra arrays to compare and combine values, while quicksort partitions within its array in the described implementation.

  85. 85

    What does stability mean in sorting, and how do merge sort and quicksort compare in the content?

    Model answer

    Merge sort is stable in the described comparison, while quicksort is unstable: a stable sort preserves the relative order of equal-valued elements.

    Explanation

    Stability concerns whether equal-valued elements retain their original relative order.

  86. 86

    What are the basic components of a graph?

    Model answer

    A graph is a data structure made up of vertices (nodes) and edges connecting pairs of vertices.

    Explanation

    Graphs represent relationships or connections among entities.

  87. 87

    Compare adjacency-matrix and adjacency-list representations of a graph.

    Model answer

    An adjacency matrix represents connections in a two-dimensional table; an adjacency list stores, for each vertex, the vertices connected to it.

    Explanation

    A matrix records pairwise connections, while a list records neighbors for each vertex.

  88. 88

    How do directed and undirected graphs differ?

    Model answer

    A directed graph gives edges a direction; an undirected graph has no edge direction, so a connection is mutual.

    Explanation

    Direction determines whether a connection can be followed only one way or both ways.

  89. 89

    What distinguishes a connected graph from a disconnected graph?

    Model answer

    A connected graph has paths connecting its vertices. A graph is disconnected if it has an isolated vertex or separate groups with no connection between them.

    Explanation

    A connected graph links its vertices, whereas an isolated vertex or separate component indicates disconnection.

  90. 90

    What is the traversal order of breadth-first search?

    Model answer

    Breadth-first search visits vertices level by level, exploring the current breadth before proceeding to more distant vertices.

    Explanation

    Breadth-first search processes nearby vertices before moving farther through the graph.

  91. 91

    What roles do a queue and a visited record play in breadth-first search?

    Model answer

    Breadth-first search uses a queue to process discovered vertices and a visited array or set to avoid revisiting vertices.

    Explanation

    A queue preserves the order in which vertices are discovered, while a visited record prevents repeated processing.

  92. 92

    How does depth-first search traverse a graph?

    Model answer

    Depth-first search follows a path deeply, then backtracks to explore remaining paths.

    Explanation

    Depth-first search follows one branch as far as possible before returning to explore another.

  93. 93

    What data structures support depth-first search, and what is the purpose of the visited record?

    Model answer

    Depth-first search can use a stack to track vertices to explore; a visited record prevents vertices from being processed repeatedly.

    Explanation

    A stack supports the last-discovered-first-processed behavior used to descend and backtrack.

  94. 94

    Define a spanning tree and state its edge-count relationship to the number of vertices.

    Model answer

    A spanning tree includes all vertices of a connected graph, is connected and acyclic, and has V−1V-1V−1 edges for VVV vertices.

    Explanation

    A spanning tree connects every vertex while avoiding cycles, and has one fewer edge than vertices.

  95. 95

    Why can a graph have more than one spanning tree?

    Model answer

    A graph may have multiple spanning trees because different edge selections can connect all vertices without creating a cycle.

    Explanation

    Different selections of edges can connect all vertices without cycles.

  96. 96

    What property makes a spanning tree a minimum spanning tree?

    Model answer

    A minimum spanning tree is a spanning tree whose total edge weight is as small as possible.

    Explanation

    The minimum spanning tree is the spanning tree with the least total edge weight.

  97. 97

    Compare the basic edge-selection strategies of Prim’s and Kruskal’s algorithms.

    Model answer

    Prim’s repeatedly adds a minimum edge extending the current tree to a new vertex. Kruskal’s considers the globally lightest available edges and adds them if they do not form a cycle.

    Explanation

    Prim’s grows a tree from a starting vertex; Kruskal’s considers edges in increasing weight order while avoiding cycles.

1 of 97
Question 1 of 97

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent