Data Structures and Algorithms (23-30)

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

NP-hard class problem

Class of problem for which on one has developed an algorithm that will solve the problem in O(n^k) time for k is any constant.

Time complexity of using LPT scheduling

O(mn) where n is the number of jobs, m is the number of workers. [O(n log n) to sort jobs into decreasing time, O(m) to assign jobs and find the minimum of m finish times]

Time complexity of sorting via priority queue

O(n log n)

For (respectively) a min/max heap, how can you change the key?

decrease/increase key of an element

Leftist Tree Operations

empty, size, top, push, pop, meld, initialize

priority queue operations

empty; size; insert an element (push); get an element with min/max priority (top); remove element with min/max priority (pop)

AVL Operation Runtimes

find, insert, and delete are all O(log n)

Height of a winner tree

log2(n) [log base 2 of n]

Heap height

log2(n+1) (log base 2 of (n+1)) where n is the number of nodes

DFS time complexity

same as BFS

What operation do push and pop use in leftist trees?

the meld operation

Formula for LPT efficiency

(LPT Finish Time)/(Actual Minimum Finish Time) = (4/3)-[1*(3m)] where m is the number of workers.

Bin packing

-NP-Hard problem -n items to be packed, each has a different size -bins have a capacity c -find minimum number of bins to fit all items

Knapsack Greedy Solutions

-be greedy on capacity, select items in increasing order of weight (no good) -be greedy on profit, select items in decreasing order of profit (no good) -be greedy on profit density, divide profit by weight of item. -will work if allowed to select a fraction of an item to fit -select items in decreasing density

Best Fit (bin packing)

-determine set of bins that can fit a given item -pack item into bin that has least capacity -if set of bins is empty, put item into a new bin

BST Operation Runtimes

-find(): O(number of nodes) -insert(): O(height) -delete(): O(height)

First Fit (bin packing)

-items are packed into bins in arbitrary order, putting items in first available bin -once a bin cannot hold anymore items, start new bin -run-time of O(n log n) when using a max tournament tree to solve -greedy

Best Fit Decreasing (bin packing)

-items first sorted in decreasing order -best fit is applied afterwards

First Fit Decreasing (bin packing)

-items sorted in decreasing order -first fit is then applied

OP examples

-machine scheduling: schedule with min finishing time -bin packing: use fewest number of bins to pack items -min cost spanning tree: find spanning tree with min cost.

Adjacency Matrix

-take n^2 bits of space -for undirected graph, only need to store lower/upper triangle of graph due to symmetry -O(n) time to find vertex degree/vertices adjacent to a vertex

Properties of Leftist Trees

1) The right most path is a shortest root to external node path and the length of this path is s(root) 2) The number of internal nodes is at least [2^(s(root)]-1 As levels 1 through s(root) have no external nodes, s(root) <= log(n+1) 3) From properties 1 and 2, the traversal of the rightmost path takes O(log n) where n is the number of nodes in a leftist tree.

Time complexity for LPT scheduling with a min priority queue

O(n log (mn))

Time complexity to sort via a winner tree

O(n log n) more precisely, Theta(n log n) O(n) to initialize, O/theta(n log n) to remove the winner and replay the tree n times.

Process to meld two (respectively) min/max leftist trees

Traverse the rightmost paths to evaluate performance. Meld the right side of whichever tree has the (respectively) smaller/larger root with the other tree. If s(left)<s(right) between the roots, swap the left and right sides of the tree.

Min/max trees

Tree in which each node has a value and acts as a heap - no descendant of a node will be (respectively) smaller/larger than its parent (though they can be equal)

Process to push in a min/max leftist tree,

Create a single node (min/max tree) and meld it into the existing min/max leftist tree.

Process to initialize a leftist tree

Create n single nodes into a queue and repeatedly remove two nodes from the queue and meld them, putting the resulting min/max leftist tree back into the queue. The process terminates when only one leftist tree remains.

Greedy method

Decision is made one by one, and is not changed once made. -makes a locally optimal choice that may/may not be globally optimal -usually fail to find optimal solution, but are fast, simple, and provide good approximations

In what order will a max priority queue extract its elements?

Descending order of priority/key (right to left)

two types of priority queues

Min and Max Priority Queues

Time complexity to initialize a heap.

O(n) Each subtree has a time complexity of O(h-j+1) where h is the height of the heap, j is the level number at which there are a number of subtrees from their root. j<= 2^(j-1)

feasible solution

Solution that satisfies constraints.

Initializing a heap

Taking a binary tree with each node containing a value and sorting the elements until the tree exhibits heap properties (min or max). (Basically starting at the leaves of the tree and moving up, heapifying each subtree as needed.

Breadth-First Search (BFS)

Visit a starting vertex, add it to a queue. Remove the vertex and visit its unvisited adjacent vertices, putting each of them into the queue.

Depth-First Search (DFS)

Visit starting vertex, add it to stack. While the stack has vertices, remove the top vertex, label it as discovered, and add to stack the adjacent vertices. This can also be done recursively, where starting at the starting vertex, label it as visited, and for each of its adjacent vertices, run it through the same procedure.

Knapsack Problem

Wish to carry n items, all having a weight and having a value. Find the highest total value of items that can fit into a knapsack that has a weight capacity.

Can leftist trees be represent min/max priority queues?

Yes, by being a min/max tree that also displays leftist properties.

Can a heap be efficiently represented as an array?

Yes, via a level order traversal, the 0 index is left blank and the value of the heap's nodes are inserted beginning at index 1.

Time complexity to initialize a leftist tree

O(n) - the same to initialize a heap.

Knapsack Greedy Profit Density Runtime

O(n^(k+1)), since there are n^k subsets, and each subset takes O(n) time to evaluate

BFS time complexity

O(vertices + edges) with adjacency list O(v^2) with adjacency matrix

Optimization Problem

Problem in which some function requires optimization subject to constraints.

Process to pop in a min/max leftist tree,

Remove the root, and meld the two remaining subtrees.

AVL Tree

Self-balancing BST in which every node has a balancing factor: for node x, balance x = (height of x left subtree) - (height of x right subtree) -balance factor must be 1,0,-1, otherwise the tree rebalances as necessary

Application of a priority queue

Sorting - using the element key to determine priority, inserted elements are sorted and popped in priority order

Application of a Winner tree

Sorting, where the winners are deposited into a array in order and the tree is then replayed, without the former winner. k-way merging of runs during an external merge sort e.g. truck loading.

Which has a better merge time complexity, a leftist tree or a binary heap?

The leftist tree O(log n) rather than the binary heap O(n)

LPT scheduling with a min priority queue

The min priority queue holds the finish times of the m workers, initialized to zero. Scheduling a job means popping from the queue (selecting the lowest finish time working) and adding the time to that worker and inserting it back into the queue.

extended binary tree

The result of adding an external node wherever there is an empty subtree in a binary tree. The number of external nodes will be (n+1)

What complexity does a loser tree's operations have?

The same as the winner tree's.

What happens with you remove the min/max element of a min/max heap?

The tree needs to be heapified so it retains its heap property. The min/max of the former children of the removed element will become the new root (this is a top down approach).

Binary Search Tree (BST)

Binary tree in which each node has a key and value. -for each node, the left children's keys are smaller than the parent node. -the right children have larger keys.

How do we compensate for NP-hard problems?

By using fast heuristics, algorithms that consistently perform close to the best possible solution and run in an acceptable amount of time e.g. the LPT scheduling.

Loser trees

Complete binary tree with n external nodes and n-1 internal nodes. External nodes represent all contestants, and each internal node represents the loser between two external nodes. The root, therefore, will contain the overall loser. If the smaller valued node loses, it's a min loser tree, and if the larger valued node loses, it's a max loser tree.

Winner trees

Complete binary tree with n external nodes and n-1 internal nodes. External nodes represent all contestants, and each internal node represents the winner between two external nodes. The root, therefore, will contain the overall winner. If the smaller valued node wins, it's a min winner tree, and if the larger valued node wins, it's a max winner tree.

priority queue

FIFO abstract data type in which each element has a priority - when one retrieves the next element, the highest/lowest-priority element (according to a key) is retrieved first

optimal solution

Feasible solution that also optimizes objective function.

The function s()

For any node x in an extended binary tree, s(x) is the length of a shortest path from x to an external node from the subtree(s) generated from x. If x is an external node s(x) = 0. else, s(x) = min[s(leftChild(x)), s(rightChild(x))]+1

Graph Representation

Graphs are represented as matrices or lists. As a list, they can be linked adjacency lists, or array adjacency lists

Winner tree operations

Initialize, get winner of one round, remove winner and replay tree.

Longest Processing Time (LPT) Scheduling

Jobs sorted and assigned to the worker that will finish first at the time the job is being popped.

BST erase()

Must consider 3 cases for the element to delete: -element is leaf: simply delete -element is degree 1 node: replace element with child -element is degree 2 node: replace element with largest key found in left subtree, smallest key in right subtree.

Minimum finish time is what kind of difficulty problem?

NP-hard

Does LPT scheduling guarantee minimum finish time scheduling?

No - but it does get close every time.

Time complexity of push and pop for a priority queue

O(log n) where n is the size of the priority queue

Height Based Leftist Tree

A binary tress in which every node x satisfies s(leftChild(x)) >= s(rightChild(x))

Leftist trees

A linked binary tree that performs just like a heap (with the same asymptotic complexity) however, two leftist tree priority queues can meld in O(log n) time.

Heap sort

An in-place and not stable sort using a max priority queue implemented as a heap, meaning initial push operations are replaced with a heap initialization step.

In what order will a min priority queue extract its elements?

Ascending order of priority/key (left to right)

Indexed BST

BST in which nodes also have an additional field. ex: leftSize, number of nodes in left subtree

Time complexity of empty, size and top for a priority queue

O(1)

Time complexity to heapify a heap.

O(log n)

Time complexity to merge for a leftist tree

O(log n)

Complexity of inserting an element into a min/max heap tree

O(log n) where n is the heap size

Time complexity of Heap Sort

O(n)

Time complexity to initialize an n player winner tree

O(n)

Time complexity to merge for a binary heap

O(n)


संबंधित स्टडी सेट्स

WGU C211 Practice Tests 11/12/20

View Set

Chemistry Unit 4- Electrons in atoms

View Set

Urbanization (unit 8) reading guide

View Set