1. Home
  2. Explore
  3. Computer Science
  4. JavaScript Data Structures

Study methods

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

JavaScript Data Structures

  • 126Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Stacks

What principle describes a stack data structure?

126 questions

  1. 1

    What principle describes a stack data structure?

    Model answer

    A stack follows last in, first out (LIFO): the most recently added item is the first item removed.

    Explanation

    A stack of books illustrates this because the last book placed on top must be removed first.

  2. 2

    How does a browser back button use a stack?

    Model answer

    Each visited URL is pushed onto a stack, and pressing Back pops the most recently visited URL, exposing the previous one.

    Explanation

    The browser returns to the earlier page by removing the current page from the top of the history stack.

  3. 3

    What are the traditional operations provided by a stack?

    Model answer

    Push adds an item, pop removes the top item, peek displays the top item without removing it, and size or length reports the number of items.

    Explanation

    These operations provide the standard ways to insert, remove, inspect, and count stack elements.

  4. 4

    Why can a JavaScript array be used as a stack?

    Model answer

    JavaScript arrays already provide the operations needed for stacks, especially push and pop.

    Explanation

    Adding and removing elements at the array's end produces LIFO behavior.

  5. 5

    What is a palindrome?

    Model answer

    A palindrome is a word that is spelled the same forward and backward, such as “bob” or “racecar.”

    Explanation

    The original sequence of characters and its reversed sequence are identical.

  6. 6

    How can a stack be used to test whether a word is a palindrome?

    Model answer

    Push each character onto a stack, pop the characters to construct the reversed word, and compare the reversed word with the original.

    Explanation

    Because the stack pops characters in reverse order, it produces the word's reversal.

  7. 7

    What happens when pop is called on the custom stack while it is empty?

    Model answer

    It returns undefined because there is no item to remove.

    Explanation

    The stack checks whether its count is zero before attempting to access storage.

  8. 8

    How does a custom JavaScript stack track its contents?

    Model answer

    It stores items in an object and uses a count variable to track the number of items.

    Explanation

    The count identifies the next insertion position and the current top of the stack.

  9. 9

    What is the difference between pop and peek?

    Model answer

    pop returns and removes the top item, whereas peek returns the top item without removing it.

    Explanation

    After peeking, the stack is unchanged; after popping, its size decreases by one.

  10. 10

    If a stack receives 1 and then 2, what does a peek return before and after popping once?

    Model answer

    The first peek returns 2; after popping 2, the next peek returns 1.

    Explanation

    The most recently pushed item is at the top and is removed first.

  11. 11

    What is the defining property of a set data structure?

    Model answer

    A set stores unique items without duplicate values, and its values are not maintained in a particular order.

    Explanation

    Sets are commonly used to test whether an item is present rather than to preserve sequence.

  12. 12

    What does the has operation of a set return?

    Model answer

    It returns true if the element is present and false otherwise.

    Explanation

    The implementation checks whether the element's index is different from negative one.

  13. 13

    How does a custom set prevent duplicate elements?

    Model answer

    Before adding an element, it checks whether the element is already present; it pushes the element only if it is absent.

    Explanation

    The membership check ensures that the backing array remains free of duplicates.

  14. 14

    What does the custom set's add method return?

    Model answer

    It returns true when a new element is added and false when the element is already present.

    Explanation

    The return value indicates whether the set changed.

  15. 15

    How does the custom set remove an element?

    Model answer

    It finds the element's index and uses splice to remove one element from that position.

    Explanation

    If the element is absent, the method returns false instead.

  16. 16

    What is the difference between remove in the custom set and delete in an ES6 set?

    Model answer

    They perform the same general operation, but ES6 calls the operation delete rather than remove.

    Explanation

    The built-in ES6 set includes delete, add, has, values, and size.

  17. 17

    How is size represented in an ES6 set?

    Model answer

    size is a property, not a method, so it is accessed without parentheses.

    Explanation

    For example, a set's size is read as a property rather than called like a function.

  18. 18

    What is the union of two sets?

    Model answer

    The union contains every element found in either set, with duplicates removed.

    Explanation

    The implementation adds values from both sets to a new set, whose uniqueness rule removes duplicates.

  19. 19

    What is the intersection of two sets?

    Model answer

    The intersection contains only the elements that appear in both sets.

    Explanation

    Each element of the first set is added only if the second set also contains it.

  20. 20

    What is the difference between two sets?

    Model answer

    The difference contains elements in one set that are not present in the other set.

    Explanation

    The implementation adds values from the first set only when the second set does not contain them.

  21. 21

    When is one set a subset of another?

    Model answer

    A set is a subset when every element in it is also contained in the other set.

    Explanation

    The subset operation tests whether all elements of the first set pass the membership test against the second.

  22. 22

    What is the difference between the values returned by a custom set and an ES6 set?

    Model answer

    The custom set's values method returns an array, while the ES6 set returns an iterator.

    Explanation

    The ES6 iterator can still be used to iterate through the set's elements.

  23. 23

    What principle describes a queue data structure?

    Model answer

    A queue follows first in, first out (FIFO): the first item added is the first item removed.

    Explanation

    A checkout line and a print queue both process items in arrival order.

  24. 24

    How does a print queue illustrate FIFO behavior?

    Model answer

    Documents are printed in the order in which they were sent to the printer.

    Explanation

    The earliest submitted document reaches the printer first.

  25. 25

    What are the main operations of a queue?

    Model answer

    enqueue adds an item, dequeue removes the front item, front inspects the front item, size reports the number of items, and isEmpty checks whether the queue has no items.

    Explanation

    Together these operations insert, remove, inspect, count, and test queue contents.

  26. 26

    How is the basic queue implementation arranged in an array?

    Model answer

    Items are added to the end of the array with push and removed from the beginning with shift.

    Explanation

    This arrangement preserves FIFO order.

  27. 27

    What does the front operation do in a queue?

    Model answer

    It returns the item at index zero without removing it.

    Explanation

    The front operation examines the next item to be dequeued while leaving the queue unchanged.

  28. 28

    If a queue receives A, B, and C and then dequeues once, what remains?

    Model answer

    A is removed, leaving B followed by C.

    Explanation

    The first item inserted is at the front and is removed first.

  29. 29

    What is a priority queue?

    Model answer

    A priority queue stores each item together with a priority and removes or positions items according to priority.

    Explanation

    Items with higher priority are placed ahead of items with lower priority.

  30. 30

    How are items with priorities 5 and 4 ordered in the described priority queue?

    Model answer

    Items with priority 5 are placed before items with priority 4.

    Explanation

    The implementation treats the larger priority number as the higher priority.

  31. 31

    How are items with equal priority ordered in a priority queue?

    Model answer

    Items with equal priority remain in the order in which they were enqueued.

    Explanation

    Equal priorities produce ordinary FIFO behavior among those items.

  32. 32

    How does the priority queue insert an item when it is empty?

    Model answer

    It directly pushes the item into the queue.

    Explanation

    No comparison with existing priorities is necessary when there are no existing items.

  33. 33

    How does the priority queue find the insertion position for a new item?

    Model answer

    It scans the existing items and inserts the new item before the first item with a lower priority.

    Explanation

    If no such position is found, the item is appended to the end.

  34. 34

    What is a tree data structure?

    Model answer

    A tree stores data in nodes arranged in a branching structure that resembles a natural tree.

    Explanation

    Nodes connect to child nodes, forming hierarchical subtrees.

  35. 35

    Define root, parent, child, and leaf nodes.

    Model answer

    The root is the top node; a parent has branches to child nodes; a child is connected below a parent; and a leaf has no children.

    Explanation

    A node may be a child of one node while also serving as the parent of its own subtree.

  36. 36

    What distinguishes a binary tree from a general tree?

    Model answer

    A binary tree allows each node to have at most two branches, while a general tree may allow any number of branches.

    Explanation

    The two possible branches in a binary tree are commonly called left and right.

  37. 37

    What ordering rule defines a binary search tree?

    Model answer

    Values in the left subtree are less than or equal to the parent, and values in the right subtree are greater than or equal to the parent.

    Explanation

    This ordering allows searches to choose a left or right subtree based on comparison.

  38. 38

    Why can binary search trees provide efficient lookup, insertion, and deletion?

    Model answer

    Their ordering allows operations to skip about half the tree on average, giving average time proportional to the logarithm of the number of stored items.

    Explanation

    This is generally faster than searching an unsorted array but slower than the corresponding average operations in a hash table.

  39. 39

    What information does a binary search tree node store in the described implementation?

    Model answer

    Each node stores its data and references to its left and right child nodes.

    Explanation

    The left and right references organize the tree structure.

  40. 40

    How is the first node added to an empty binary search tree?

    Model answer

    The new node becomes the root node.

    Explanation

    An empty tree has a null root, so the first inserted value occupies that position.

  41. 41

    How does insertion place a non-root value in a binary search tree?

    Model answer

    The value is compared with each node; smaller values are searched into the left subtree, larger values into the right subtree, and equal values are not added.

    Explanation

    The recursive search continues until an empty child position is found.

  42. 42

    What happens when an insertion reaches a null left or right child?

    Model answer

    The new node is assigned to that child reference.

    Explanation

    A null child position is the correct location for the new value according to the comparisons.

  43. 43

    How are duplicate values handled by the described binary search tree?

    Model answer

    Duplicate values are not added; the insertion operation returns without changing the tree.

    Explanation

    The recursive comparison detects equality separately from less-than and greater-than cases.

  44. 44

    How can the minimum value be found in a binary search tree?

    Model answer

    Start at the root and repeatedly follow left children until no left child remains.

    Explanation

    The leftmost node contains the minimum value.

  45. 45

    How can the maximum value be found in a binary search tree?

    Model answer

    Start at the root and repeatedly follow right children until no right child remains.

    Explanation

    The rightmost node contains the maximum value.

  46. 46

    How does the binary search tree's find operation determine whether a value is present?

    Model answer

    It compares the target with the current node, returns true on equality, searches left if the target is smaller, and searches right if it is larger.

    Explanation

    If the search reaches a null node, the value is absent and the operation returns false.

  47. 47

    What are the three cases when removing a node from a binary search tree?

    Model answer

    The node may have no children, one child, or two children.

    Explanation

    Each case requires a different pointer adjustment to preserve the search-tree ordering.

  48. 48

    How is a leaf node removed from a binary search tree?

    Model answer

    The reference to the node is replaced with null.

    Explanation

    A leaf has no children that need to be reconnected.

  49. 49

    How is a node with one child removed from a binary search tree?

    Model answer

    The node is replaced by its only child.

    Explanation

    Returning the non-null child reconnects the subtree without losing its descendants.

  50. 50

    How is a node with two children removed from a binary search tree?

    Model answer

    It is replaced with the leftmost node of its right subtree, and the replacement node is then removed from its original position.

    Explanation

    The replacement is the smallest value greater than the removed node and preserves the ordering rule.

  51. 51

    What does tree height measure?

    Model answer

    Tree height is the distance from the root to a specified leaf, measured in levels or edges.

    Explanation

    The root has height zero in the described convention.

  52. 52

    What are minimum and maximum height in a tree?

    Model answer

    Minimum height is the distance from the root to the nearest leaf, while maximum height is the distance to the deepest leaf.

    Explanation

    Different root-to-leaf paths can have different lengths.

  53. 53

    When is a tree considered balanced in the described implementation?

    Model answer

    A tree is balanced when its minimum and maximum heights differ by at most one.

    Explanation

    Balanced trees generally allow more efficient searching because paths have similar lengths.

  54. 54

    What is depth-first search in a tree?

    Model answer

    Depth-first search explores a subtree as deeply as possible before continuing to another subtree.

    Explanation

    In-order, pre-order, and post-order traversals are depth-first traversal methods.

  55. 55

    Describe in-order traversal of a binary search tree.

    Model answer

    In-order traversal visits the left subtree, then the current node, then the right subtree.

    Explanation

    For a binary search tree, this produces the values in sorted order.

  56. 56

    Describe pre-order traversal of a binary search tree.

    Model answer

    Pre-order traversal visits the current node before its left and right subtrees.

    Explanation

    It explores parent or root nodes before their descendants.

  57. 57

    Describe post-order traversal of a binary search tree.

    Model answer

    Post-order traversal visits the left subtree and right subtree before the current node.

    Explanation

    It explores descendant or leaf nodes before their parents.

  58. 58

    What is level-order traversal?

    Model answer

    Level-order traversal visits all nodes at one tree level before proceeding to the next level.

    Explanation

    It is a breadth-first search rather than a depth-first search.

  59. 59

    How does the described level-order traversal use a queue?

    Model answer

    It enqueues the root, repeatedly dequeues a node, records it, and enqueues its non-null children.

    Explanation

    The queue ensures that nodes are processed level by level.

  60. 60

    What is a binary heap?

    Model answer

    A binary heap is a partially ordered binary tree that satisfies a heap property.

    Explanation

    Each node has at most two children, but the ordering differs from a binary search tree.

  61. 61

    What is the heap property of a max-heap?

    Model answer

    Each parent node is greater than or equal to its child nodes.

    Explanation

    Consequently, the largest value is at the root.

  62. 62

    What is the heap property of a min-heap?

    Model answer

    Each parent node is less than or equal to its child nodes.

    Explanation

    Consequently, the smallest value is at the root.

  63. 63

    Does the order of sibling nodes matter in a binary heap?

    Model answer

    No. The relative order of child nodes on the same level is not required to follow a particular arrangement.

    Explanation

    Only the parent-child heap relationship is required.

  64. 64

    What does it mean for a binary heap to be complete?

    Model answer

    Every level is full except possibly the last, and a partially filled last level is filled from left to right.

    Explanation

    Completeness makes an array representation possible and compact.

  65. 65

    Why can a binary heap be represented efficiently as an array?

    Model answer

    Its complete structure allows parent and child positions to be calculated directly from array indices.

    Explanation

    The partial ordering means explicit left and right references are unnecessary.

  66. 66

    In the one-based heap representation, what are the indices of a node's children?

    Model answer

    For a node at index iii, the left child is at 2i2i2i and the right child is at 2i+12i + 12i+1.

    Explanation

    The heap leaves index zero unused so these formulas work directly.

  67. 67

    In the one-based heap representation, how is a node's parent found?

    Model answer

    The parent of the node at index iii is at floor i/2 \rfloor.

    Explanation

    The index is divided by two and rounded down to obtain the parent position.

  68. 68

    What does the last array index represent in the described heap implementation?

    Model answer

    It represents the size of the heap because index zero is unused.

    Explanation

    The remaining occupied indices run from one through the last index.

  69. 69

    How is an item inserted into a min-heap?

    Model answer

    The item is appended at the end and repeatedly swapped with its parent while it is smaller than the parent.

    Explanation

    This upward movement restores the min-heap property.

  70. 70

    Why is an inserted heap item initially placed at the end?

    Model answer

    Placing it at the end preserves the heap's complete-tree shape before its value is moved into the correct position.

    Explanation

    The item then moves upward without creating gaps in the tree.

  71. 71

    How is the minimum item removed from a min-heap?

    Model answer

    The root is saved, the last item moves to the root, and that item is repeatedly swapped downward with the smaller child until the heap property is restored.

    Explanation

    The removed root is the smallest item, and downward reordering repairs the remaining heap.

  72. 72

    What does heap sort do?

    Model answer

    It inserts all items from an unsorted array into a min-heap and repeatedly removes the minimum into a new array.

    Explanation

    The resulting array contains the original items from least to greatest.

  73. 73

    What are the average and worst-case time complexities of heap sort?

    Model answer

    Both the average and worst-case time complexities are O(nlog⁡n)O(n \log n)O(nlogn).

    Explanation

    Heap operations maintain the ordering efficiently while repeatedly extracting elements.

  74. 74

    What is a hash table used to implement?

    Model answer

    A hash table implements associative arrays or mappings of key-value pairs.

    Explanation

    JavaScript commonly uses this idea to implement objects and map-like structures.

  75. 75

    What is the average time complexity of hash-table search, insertion, and deletion?

    Model answer

    The average time complexity of each is O(1)O(1)O(1).

    Explanation

    The operations use a hash-derived index rather than scanning all stored elements.

  76. 76

    What does a hash function do?

    Model answer

    A hash function maps a key, often a string, to a numerical index used to select a storage bucket.

    Explanation

    The index identifies where the key-value pair should be stored or searched for.

  77. 77

    What consistency property must a hash function have?

    Model answer

    The same key must always produce the same hash value.

    Explanation

    Otherwise, a later lookup might search a different bucket from the one used during insertion.

  78. 78

    What is a collision in a hash table?

    Model answer

    A collision occurs when two different keys are mapped to the same numerical index or bucket.

    Explanation

    For example, different names may hash to the same array position.

  79. 79

    How does the described hash table handle collisions?

    Model answer

    It stores multiple key-value pairs in the same bucket and searches through that bucket when necessary.

    Explanation

    This approach is commonly called separate chaining.

  80. 80

    How does the simple hash function calculate an index?

    Model answer

    It adds the character codes of the key's characters and returns the sum modulo the number of buckets.

    Explanation

    The remainder is always a valid bucket index from zero through one less than the bucket count.

  81. 81

    Why does using the remainder modulo the bucket count produce a valid index?

    Model answer

    The remainder is always between zero and one less than the number of buckets.

    Explanation

    For example, with five buckets, possible remainders are zero through four.

  82. 82

    What happens when a key is inserted into an empty hash-table bucket?

    Model answer

    The bucket is assigned a new key-value pair.

    Explanation

    No collision search is needed when the bucket is undefined.

  83. 83

    What happens when an existing key is inserted again with a new value?

    Model answer

    The value associated with that key is updated rather than adding a duplicate key-value pair.

    Explanation

    The bucket is searched for the existing key before a new entry is appended.

  84. 84

    How does hash-table lookup find a value?

    Model answer

    It hashes the key to locate its bucket and then searches that bucket for the key's associated value.

    Explanation

    The same key produces the same bucket index used during insertion.

  85. 85

    How does hash-table removal handle a bucket containing one item?

    Model answer

    If the item has the requested key, the bucket entry is deleted.

    Explanation

    No additional search or rearrangement is needed when only one pair occupies the bucket.

  86. 86

    How does hash-table removal handle a bucket containing multiple items?

    Model answer

    It searches the bucket and deletes only the key-value pair whose key matches the requested key.

    Explanation

    Other entries involved in the collision must remain stored.

  87. 87

    What are the two pieces of information stored in a linked-list node?

    Model answer

    A node stores an element and a reference to the next node.

    Explanation

    The reference connects the nodes into a sequence.

  88. 88

    What are the main advantages of linked lists over arrays described in the content?

    Model answer

    Linked lists have dynamic size and efficient insertion and deletion, while arrays traditionally have fixed size and less efficient insertion and deletion.

    Explanation

    A linked list grows by adding nodes without resizing an underlying fixed block.

  89. 89

    What is the main random-access advantage of an array over a linked list?

    Model answer

    An array can access an item at a known index directly, whereas a linked list must follow links from the head to reach that position.

    Explanation

    Accessing the fifth linked-list item requires traversing the preceding nodes.

  90. 90

    Why can arrays waste memory compared with linked lists?

    Model answer

    Arrays may be allocated larger than currently necessary to allow future growth, while linked lists allocate nodes as needed.

    Explanation

    The dynamic size of a linked list avoids unused reserved array positions.

  91. 91

    What are the head and size of a linked list?

    Model answer

    The head points to the first node, and the size records the number of nodes.

    Explanation

    An empty linked list has a null head and size zero.

  92. 92

    How is an element added to the end of the described linked list?

    Model answer

    A new node is created, the list is traversed from the head to the final node, the final node's next reference is set to the new node, and the size is incremented.

    Explanation

    The new node initially has a null next reference because it becomes the last node.

  93. 93

    How is the head node removed from a linked list?

    Model answer

    The head is changed to the current head's next reference.

    Explanation

    This bypasses the old first node and makes the second node the new head.

  94. 94

    How is a middle node removed from a linked list?

    Model answer

    The previous node's next reference is changed to the node being removed's next reference.

    Explanation

    The previous node then points past the removed node.

  95. 95

    What does a linked list's isEmpty operation test?

    Model answer

    It tests whether the list's length is zero.

    Explanation

    A zero length means that the list contains no nodes.

  96. 96

    How does a linked list find the index of an element?

    Model answer

    It starts at the head, traverses one node at a time, increments an index, and returns the index when the element is found.

    Explanation

    If traversal reaches the end without finding the element, it returns negative one.

  97. 97

    How does a linked list retrieve an element at a given index?

    Model answer

    It starts at the head and follows next references until the traversal count equals the requested index, then returns that node's element.

    Explanation

    Unlike an array, the list cannot jump directly to the index.

  98. 98

    How does inserting at index zero differ from inserting at another linked-list index?

    Model answer

    At index zero, the new node points to the current head and becomes the new head; elsewhere, the previous and current nodes are linked around the new node.

    Explanation

    The special head case avoids needing a previous node.

  99. 99

    What index restrictions apply to the described linked-list insertion and removal operations?

    Model answer

    Insertion rejects an index greater than the list length, while removal rejects negative indices and indices greater than or equal to the list length.

    Explanation

    Insertion may occur at the position immediately after the final existing node, but removal requires an existing node.

  100. 100

    What is a trie?

    Model answer

    A trie, or prefix tree, is a tree used to store associative data such as words one character at a time.

    Explanation

    Words with common prefixes share the same initial path.

  101. 101

    How is a word represented in a trie?

    Model answer

    Each character is represented by a node or step in a path through the tree.

    Explanation

    A path from the root spells the sequence of characters in the word.

  102. 102

    When do branches form in a trie?

    Model answer

    Branches form when words differ at a character position or when one word ends while another continues.

    Explanation

    Shared prefixes use common nodes before diverging.

  103. 103

    How does a trie mark the end of a word?

    Model answer

    Each node has an end flag that is set to true when a complete word ends at that node.

    Explanation

    This distinguishes a complete word from a prefix of a longer word.

  104. 104

    What information does a trie node store in the described implementation?

    Model answer

    It stores a map of character keys to child nodes and an end-of-word flag.

    Explanation

    The map identifies possible next characters, while the flag identifies completed words.

  105. 105

    How does the trie add operation work recursively?

    Model answer

    It examines the first character, creates a child node if needed, and recursively processes the remaining substring from that child.

    Explanation

    When no characters remain, it marks the current node as the end of a word.

  106. 106

    How does a trie determine whether a word exists?

    Model answer

    It follows one child node for each character and returns true only if the final character is reached at a node marked as an ending.

    Explanation

    If any required character is missing or the final node is not marked as an ending, the input is not a stored word.

  107. 107

    Why can a trie validate words efficiently?

    Model answer

    It follows the characters of the target word directly rather than scanning every stored word.

    Explanation

    Shared prefixes are represented once and guide the search through the tree.

  108. 108

    How does the trie print all stored words?

    Model answer

    It recursively visits child nodes, concatenates each character to a prefix string, and records the string whenever it reaches an end-of-word node.

    Explanation

    The recursion explores every character path in the trie.

  109. 109

    What is a graph data structure?

    Model answer

    A graph is a collection of nodes, or vertices, and the edges or connections between them.

    Explanation

    Graphs model relationships such as friendships or links between web pages.

  110. 110

    What is the difference between a directed and an undirected graph?

    Model answer

    An undirected graph has edges without direction, while a directed graph has edges that point from one node to another.

    Explanation

    A friendship is typically undirected, whereas a web-page link may point only one way.

  111. 111

    What does an adjacency list represent?

    Model answer

    It associates each vertex with a collection of its neighboring vertices.

    Explanation

    For example, a node's list contains the nodes directly connected to it.

  112. 112

    What does an adjacency matrix represent?

    Model answer

    It is a two-dimensional array in which rows and columns represent nodes and entries indicate whether an edge exists.

    Explanation

    A zero indicates no relationship and a one indicates a relationship in the unweighted version.

  113. 113

    What shape must an adjacency matrix have?

    Model answer

    Each row must contain the same number of elements as there are nodes, producing a square matrix.

    Explanation

    A graph with three nodes is represented by a 3×33 \times 33×3 adjacency matrix.

  114. 114

    How does an adjacency matrix represent a directed graph?

    Model answer

    An entry is one only when the node represented by the row has an edge pointing to the node represented by the column.

    Explanation

    The matrix need not be symmetrical because an edge may point in only one direction.

  115. 115

    What does an incidence matrix represent?

    Model answer

    Its rows represent nodes and its columns represent individual edges.

    Explanation

    Unlike an adjacency matrix, its rows and columns can have different numbers of elements.

  116. 116

    How is an undirected edge represented in an incidence matrix?

    Model answer

    The column for that edge contains a one in the two rows corresponding to its connected nodes.

    Explanation

    Each edge connects two nodes, so exactly those two node rows identify it.

  117. 117

    How is a directed edge represented in an incidence matrix?

    Model answer

    A one marks the node where the edge leaves, and negative one marks the node where the edge enters.

    Explanation

    The signs encode the direction of the edge.

  118. 118

    What are weighted graph edges?

    Model answer

    Weighted edges store numerical values representing the cost, distance, strength, or another quantity associated with a connection.

    Explanation

    Unweighted graphs use only the presence or absence of an edge, commonly represented by zero and one.

  119. 119

    What is graph traversal?

    Model answer

    Graph traversal is the process of visiting nodes in a graph, often to determine distances or reachability.

    Explanation

    Breadth-first search and depth-first search are the main traversal types described.

  120. 120

    How does breadth-first search explore a graph?

    Model answer

    It visits the starting node's neighbors first, then visits their unvisited neighbors, proceeding outward one edge-distance at a time.

    Explanation

    This ordering makes breadth-first search useful for finding shortest unweighted distances from a starting node.

  121. 121

    What data structure does breadth-first search use to manage nodes to visit?

    Model answer

    It uses a queue.

    Explanation

    The queue ensures that nodes discovered earlier are processed before nodes discovered later.

  122. 122

    How does the described breadth-first search represent unreachable nodes?

    Model answer

    It initializes their distances to infinity and leaves that value unchanged if they cannot be reached from the start node.

    Explanation

    The root node starts with distance zero, and each newly discovered neighbor receives its current node's distance plus one.

  123. 123

    What does the output of the described breadth-first search contain?

    Model answer

    It returns an object mapping each node to its distance from the root node.

    Explanation

    The distance counts the number of edges or hops from the starting node.

  124. 124

    How does breadth-first search obtain a node's neighbors from an adjacency matrix?

    Model answer

    It examines the row associated with the current node and treats every column containing one as a connected neighbor.

    Explanation

    The row specifies which nodes are directly connected to that current node.

  125. 125

    How does the breadth-first search avoid assigning a longer distance to an already discovered node?

    Model answer

    It assigns a distance only when the node's current distance is still infinity.

    Explanation

    The first discovery in breadth-first order provides the shortest unweighted distance.

  126. 126

    Why is the starting node assigned distance zero in breadth-first search?

    Model answer

    It requires zero edges or hops to reach the starting node from itself.

    Explanation

    All other reachable nodes receive distances relative to this root distance.

1 of 126
Question 1 of 126

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent