DAT 305 Data Structures for Problem Solving

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Dictionary (map) Data structure - Hash table, binary search tree

ADT that associates (or maps) keys with values.

worst-case scenario

An algorithm is the runtime complexity for an input that results in the longest execution.

Recursive algorithm

An algorithm that breaks the problem into smaller subproblems and applies the algorithm itself to solve the smaller subproblems.

Recursive Binary Search

An algorithm that searches a sorted list for a key by first comparing the key to the middle element in the list and recursively searching half of the remaining list so long as the key is not found.

Constant time operation

An operation that, for a given processor, always operates in the same amount of time, regardless of input values.

Computational problem

Specifies an input, a question about the input that can be answered, using a computer, and the desired output.

11, 4, 11 11 is appended to an empty list, yielding: 114 is appended, yielding: 11, 47 is appended, yielding: 11, 4, 7

Starting with an empty list, what is the list contents after the following operations? Append(list, 11) Append(list, 4) Append(list, 7) a 4, 7, 11 b 7, 4, 11 c 11, 4, 7

6 - For an input size of 64, the number of recursive function calls is log2(64) = 6.

Suppose BinarySearch is used to search for a key within a list with 64 numbers. If the key is not found, how many recursive calls to BinarySearch are made?

1024

Suppose a list of 1024 elements is searched with linear search. How many distinct list elements are compared against a search key that is less than all elements in the list?

10

Suppose a sorted list of 1024 elements is searched with binary search. How many distinct list elements are compared against a search key that is less than all elements in the list?

True Division is often slower than multiplication on many processors. But each division operation takes the same amount of time regardless of operand values, so is still a constant time operation.

T/F - Certain hardware may execute division more slowly than multiplication, but both may still be constant time operations.

True Copying all characters in the string would require more operations for longer strings. But assignment of a pointer/reference is a constant time operation.

T/F - In the code below, suppose str1 is a pointer or reference to a string. The code only executes in constant time if the assignment copies the pointer/reference, and not all the characters in the string. str2 = str1

False The programming language also affects what is a constant time operation. Ex: A programming language could provide variable size floating point values as the only available numerical type and implement arithmetic operations in software. Since the values are not fixed size, arithmetic operations would not be constant time.

T/F - The hardware running the code is the only thing that affects what is and what is not a constant time operation.

False - A linked list stores an ordered list of item. the links in each node define the order in which terms are stored.

T/F A linked list stores items in an unspecified order.

False - Different underlying data structures will require different algorithms to perform same list ADT operation, which will have different runtimes. Ex: For prepending an item to a list, a linked list-based implementation is more efficient than an array-based implementation.

T/F A list ADT's underlying data structure has no impact on the program's execution.

A function f(N) that is ≥ the worst case T(N), for all values of N ≥ 1

Upper bound

False When the low and high indices are equal, the list has 1 item to search. If the 1 item doesn't match the key, then BinarySearch makes a recursive call with a low argument greater than the high argument.

When the low and high arguments are equal, BinarySearch does not make a recursive call.

Longest common substring

Which common algorithm can be used to solve: Do two students essays share a common phrase consisting of a sequence of more than 100 letters? a Longest common substring b Shortest path algorithm c Binary search

Binary search

Which common algorithm can be used to solve: Given a list of a company's employee records and an employee's first and last name, what is a specific employee's phone number? a Longest common substring b Shortest path algorithm c Binary search

Shortest path algorithm

Which common algorithm can be used to solve: Given the airports at which an airline operates and distances between those airports, what is the shortest total flight distance between two airports? a Longest common substring b Shortest path algorithm c Binary search

No - The algorithms for appending an item to an array and linked lists are shown in the animation above, and require different implementations.

Y/N Can the following algorithm be implemented with the same code for both an array and linked list? Append an item

T(N) = 6n + T(N/4) Correct T(N)=6N+T(N/4)has T on both sides of the equation

+ 6N +2Which function is a recurrence realation? T(N) = N2 + 6n + 2 T(N) = 6N + T(N/4) T(N) = log2N

Graph

A data structure for representing connections among items, and consists of vertices connected by edges. A vertex represents an item in a graph. An edge represents a connection between two vertices in a graph.

Binary tree

A data structure in which each node stores data and has up to two children, known as a left child and a right child.

Linked list

A data structure that stores an ordered list of items in nodes, where each node stores data and has a pointer to the next node.

Array

A data structure that stores an ordered list of items, where each item is directly accessible by a positional index.

Hash table

A data structure that stores unordered items by mapping (or hashing) each item to a location in an array.

Abstract Data Type (ADT)

A data type whose properties are specified independently of any particular programming language.

O(N) A linear search may need to search all N elements within a list.

A linear search has a _____ runtime complexity.

False A loop with a constant number of iterations can be a constant time operation. Ex: The value of variable y in the loop below does not affect the number of operations. for (i = 0; i < 10; i++) sum += y

A loop is never a constant time operation.

Big O Notation

A mathematical way of describing how a function (running time of an algorithm) generally behaves in relation to the input size.

Fibonacci sequence

A numerical sequence where each term is the sum of the previous 2 terms in the sequence, except the first 2 terms, which are 0 and 1.

Priority queue Data structure - Heap

A queue where each item has a priority, and items with higher priority are closer to the front of the queue than items with lower priority.

20, 30 Item 2 is removed from the list, yielding: 20, 30

A remove operation for a list ADT will remove the specified item. Given a list with contents: 2, 20, 30, what is the list contents after the following operation? Remove(list, item 2) a 2, 30 b 2, 20, 30 c 20, 30

O(N2) The total number of operations for selection sort is proportional to N · N.

A selection sort has a _____ runtime complexity

Algorithm

A sequence of steps to solve a computational problem or perform a calculation.

Heap

A tree that maintains the simple property that a node's key is greater than or equal to the node's children's keys.

Set Data structure - Binary search tree, hash table

ADT for a collection of distinct items.

List Data structure - Array, linked list

ADT for holding ordered data

Dynamic array Data structure - Array

ADT for holding ordered data and allowing indexed access

Bag Data structure - Array, Linked list

ADT for storing items in which the order does not matter and duplicate items are allowed.

Queue Data structure - Linked list

ADT in which items are inserted at the end of the queue and removed from the front of the queue.

Stack Data structure - Linked list

ADT in which items are only inserted on or removed from the top of a stack

Deque Data structure - Linked list

ADT in which items can be inserted and removed at both the front and back.

False - An algorithm can be described in English, pseudocode, a programming language, hardware, tec. as long as the algorithm precisely describes the steps of a computational procedure.

Consider the problem of determining the number of times (or frequency) a specific word appears in a list of words. T/F An algorithm to solve this computation problem must be written using a programming language?

Integer value for the frequency of specified word. The computational problem's question can be phrased as: How many times does the user-specified word appear on the list? The answer to that problem is an integer value for the frequency of the specified word.

Consider the problem of determining the number of times (or frequency) a specific word appears in a list of words. What is the problem output? a. Integer value for the frequency of most frequent word b. String value for the most frequent word in input array c. Integer value for the frequency of a specified word.

Array of all words and string for user-specified word The input must include a list of all words and the specific word for which determining the frequency is desired.

Consider the problem of determining the number of times (or frequency) a specific word appears in a list of words. Which can be used as the problem input? a. String for user-specified word b. Array of unique words and string for user-specified word c. Array of all words and string for user-specified word

5

Given an array with 32 elements, how many list elements will be checked if the key is less than all elements in the list, using binary search?

Space-complexity (of an algorithm)

Indicates how much memory an algorithm needs

999 - Inserting at the beginning requires making room for the new item. so every current item must be shifted once.

Inserting an item at the beginning of a 999 - item array requires how many items to be shifted?

0 - No shifting of other items is required, which is an advantage of using linked lists.

Inserting an item at the beginning of a 999 item linked list requires how many items to be shifted?

0 - Appending an item just places the new item at the end of the array. No shifting of exisitng items is necessary.

Inserting an item at the end of a 999 item array requires how many items to be shifted?

0 - The new item is simply added to the end.

Inserting an item at the end of a 999 item linked list requires how many items to be shifted?

A function f(N) that is ≤ the best case T(N), for all values of N ≥ 1

Lower bound Function

constant 5 does not change with the input size, so the runtime of O(5) remains constant.

O(5) has a _____ runtime complexity.

quadratic O(N + N2) reduces to O(N2). The highest power is 2, so the runtime complexity is quadratic.

O(N + N2) has a _____ runtime complexity.

linearithmic The first N indicates the runtime complexity is linear, then multiplying with log N results in a linearithmic runtime complexity.

O(N log N) has a _____ runtime complexity.

Ω notation

Provides a growth rate for an algorithm's lower bound.

O notation

Provides a growth rate for an algorithm's upper bound.

Θ notation

Provides a growth rate that is both an upper and lower bound.

True Python, C++, and Java all have built-in ADTs for deques, lists, sets, and dictionaries (or maps).

T/F Python, C++, and Java all provide built-in support for a deque ADT

False Although runtime and memory usage are the most common, computational complexity can include other factors, such as network communication.

T/F Runtime and memory usage are the only two resources making up computational complexity.

False he underlying data structures used to implement ADTs may vary between implementations. Some programming languages allow programmers to select a specific implementation. Ex: Java's list ADTs can be implemented using arrays or linked lists.

T/F The underlying data structure for a list data structure is the same for all programming languages.

False Two different algorithms can produce the same result in various ways and may have different computational complexities.

T/F Two different algorithms that produce the same result have the same computational complexity.

False Mathematically, a constant times a constant is still a constant. Therefore 3, or any other constant number of constant time operations, can collectively be considered 1 constant time operation.

The 3 constant time operations in the code below can collectively be considered 1 constant time operation. x = 26.5 y = 15.5 z = x + y

Computational complexity

The amount of resources used by the algorithm

Algorithm efficiency

The aspects of an algorithm that determine the amount of resources used by the algorithm (like CPU time and memory space).

Record

The data structure that stores subitems, often called fields, with a name associated with each subitem.

Yes - Both the array and linked list have a length subitem. The algorithm to return the size just returns that length subitem. GetCurrentSize(values) { return values⇢length }

Y/N Can the following algorithm be implemented with the same code for both an array and linked list? Return the current size

No - The first array element is accessed using array[0], but the first linked list item is accessed using list⇢head. So, different algorithms are needed for an array and linked list.

Y/N Can the following algorithm be implemented with the same code for both an array and linked list? Return the first item

NP-Complete

Set of problems for which no known efficient algorithm exists.

A visual diagram of an operation done by a recursive function, that separates operations done directly by the function and operations done by recursive calls.

Recursion Tree

True - The data stored in a list node can be a record with multiple subitems. Ex. A linked list storing employee data might use a record containing the employee's name, title, and salary. Also the list node itself can be implemented as a record, having subitems for the data and the pointer to the next node.

T/F A list node's data can store a record with multiple subitems.

True - A binary tree node can have no children, a single left or right, or both a left and right child.

T/F A node in binary tree can have zero, one or two children.

False - A programmer need not have knowledge of the underlying implementation to use a list ADT.

T/F A programmer must know the underlying implementation of the list ADT in order to use a list.

False Many third-party libraries, which are not built in to the programming language standard, implement ADTs.

T/F ADTs are only supported in standard libraries.

True - An efficient algorithm is generally one whose runtime increases no more than polynomially with respective to the input size. In contrast, an algorithm with an exponential runtime is not efficient.

T/F An algorithm with a polynomial runtime is considered efficient.

True - An algorithm can have the same best and worst case, in which case the algorithm always does the same number of operations regardless of input data.

T/F An algorithm's best and worst case scenarios are always different.

False - Many computational problems exist for which an efficient algorithm is unknown. Such problems are often encountered in real applications.

T/F An efficient algorithm exists for all computational problems.

True - Whether or not an efficient algorithm exists for NP-complete problems is an open research question. However, the current consensus is that such an algorithm is unlikely.

T/F An efficient algorithm to solve an NP-complete problem may exist.

True - Array elements are stored in sequential locations, so can be easily accessed using an index.

T/F Array elements are stored in sequential locations, so can be easily accessed using an index.

True Runtime and space complexity analysis, discussed below, provide a mathematical way to compare algorithm efficiency.

T/F Computational complexity analysis allows the efficiency of algorithms to be compared.

False - The variable value N cannot be replaced with a constant of 0 to describe a best case scenario. A best case scenario must describe the contents of the data being processed.

T/F Nearly every algorithm has a best case time complexity when N = 0

Worst-case scenario

The scenario where the algorithm does the maximum possible number of operations.

Best-case scenario

The scenario where the algorithm does the minimum possible number of operations.

Auxiliary space complexity (of an algorithm)

The space complexity not including the input data

True Assignment of a single, fixed size data value is a constant time operation.

The statement below that assigns x with y is a constant time operation. y = 10 x = y

Algorithm runtime

The time it takes an algorithm to execute

Linear Search

This search method starts at the beginning of the list and compares each element in turn with the required value until a match is found or the end of the list is reached.

Abstraction

To have a user interact with an item at a high-level, with lower-level internal details hidden from the user.


Kaugnay na mga set ng pag-aaral

Human Growth & Development: Test 2

View Set