CSE 2050 Final Exam

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

Downheap

Running time: O(logn) there is a downheap operation that will repeatedly swap an entry with its child until it's heapordered.

Heap (2)

-binary trees -big items on bottom (min heap) definition of complete heap( every level but the bottom is full and bottom level is filled from left to right) The index of element k is i. Find the index of: k.left_child 2i + 1 k.right_child 2i + 2 k.parent (i-1) // 2

Consider the following BST. How many nodes are in the subtree rooted at node 3 after rotating node 3 left? Include node 3 in this count - your answer should be at least 1.

1

Give the nodes visited in the correct order by the following traversal generator in a BST Node class, originally called with the root (e.g. root.traversal()): def traversal(self): if self.left is not None: yield from self.left if self.right is not None: yield from self.right yield self

1,3,5,4,7,6,2

Running time of build heap

O(n)

Breadth first search

Shortest Paths in unweighted graphs

Consider a binary tree with height k and n nodes. What is the maximum number of leaves in the tree?

2^k

How many iterator objects are created by the following code snippet: L = ["a", "b", "c", "d"] for i in L: print("hello") for j in L: print("goodbye")

5 iterator objects

Minimum Spanning Tree (Say we have an undirected weighted graph)

A minimum spanning tree is a tree of minimal cost that connects all of the vertices.

Which of the following problems would best be solved with a priority queue?

Calling passengers to board an airplane in order of status (e.g. first-class, then preferred, then economy)

Running time of Build-heap

Each call to _downheap costs O(log n) time. Build heap makes O(n) such calls Therefore, the running time is O(n log n) Can we get a tighter bound. Time for downheap to run at a node varies with the height of the node in the tree An n-element heap has height [log 𝑛] and at most [n/(2)^n+1] nodes of any height h Time required by downheap when called on a node of height h is O(h)

What problem does Dijkstra's algorithm solve?

Finding the shortest path in a weighted graph from one node to all the others

More notation

If (u, v) ∊ E , then v is a neighbor of u (i.e., v is adjacent to u) For an edge e = (u, v), we say that the vertices u and v are incident to the edge e simple graphs: Graphs without self-loops and multiple edges.

Weighted Graphs

In a weighted graph, each edge has a weight or cost Typically numeric (ints, decimals, doubles, etc.) Some graphs allow negative weights; many do not

Binary Search Trees

It is a binary tree so that every left descendant of a node has key less than that node and every right descendant of a node has a key greater than that node

Consider the heap represented by the following list: self._L = [a,b,c,d,e,f,g] Which nodes are the children of b? Give the letters representing those nodes, not their indices.

Left: d, Right: e

Consider a binary search tree with n nodes. What is the asymptotic worst-case running time of one rotate operation for a given node in the tree?

O(1)

Consider an undirected, unweighted graph. We want to visit all the vertices connected to a given start vertex, by order of non-decreasing distance from the start vertex. Which one or ones of the following algorithms solve the problem?

Only breadth first search

Which one or ones of the following algorithms can easily be implemented recursively?

Only depth first search

Prim's algorithm

Prim's Algorithm is used to find the minimum spanning tree from a graph. Prim's algorithm finds the subset of edges that includes every vertex of the graph such that the sum of the weights of the edges can be minimized. Prim's algorithm starts with the single node and explore all the adjacent nodes with all the connecting edges at every step. The edges with the minimal weights causing no cycles in the graph got selected. Implemented using priority queue

Insert(heap)

Running time O(logn) We will maintain the invariant that after each operation, the list of entries is heap-ordered. To insert a new node, we append it to the list and then repeated swap it with its parent until it is heap-ordered.

SImple Graph

Self-loops, edges that start and end at the same vertex. (Example: node 1) Multiple edges between the same pair of vertices. (Example: between nodes 2 and 5) Graphs without self-loops and multiple edges are called simple graphs

Depth first search

Strongly Connected Components

Priority queue

The Priority Queue ADT is a data type that stores a collection of items with priorities (not necessarily unique) that supports the following operations removemin() - Remove and return the item with the minimum priority. Ties are broken arbitrarily. findmin() - Return the item with the minimum priority. If there are multiple items with the minimum priority, ties may be broken arbitrarily. insert(item, priority) - Add item with the given priority.

Which one of the following is a reason that the adjacency set implementation of a graph is generally preferred to the edge set implementation?

The adjacency set implementation provides faster access to the neighbors of a given vertex.

Consider a graph class implemented using adjacency lists. We would like to use the following method to test whether two vertices are connected. def connected(self, a, b): if a == b: return True for n in self.neighbors(a): if self.connected(n, b): return True return False Which one of the following cases could cause the method to raise an error?

The graph contains cycles

Heap

The heap is the data structure used to implement the priority queue The root of max heap (min heap) contains the largest (smallest).

Both depth first search and breadth first search

Useful for exploring graphs, finding connected components, etc

Prim algorithm differences with Dijkstra's algorithm

Very similar to Dijkstra's algorithm! Differences: Instead of d[v] which we update by d[v] = min( d[v], d[u] + weight(u,v) ) we keep k[v] which we update by k[v] = min( k[v], weight(u,v) )

Graphs

What is graph Formally, a graph is G = (V,E) V is the set of vertices E is the set of edges (set and E is a set of pairs of elements of V) Undirected Graphs: the ordering of the vertices in an edge does not matter use sets to represent the edge Degree of a vertex: the number of adjacent vertices Directed Graphs or digraph: the ordering of the vertices in an edge does matter Use tuples to represent the edge In-degree and out-degree

Give the nodes in order for the list representation of the following heap:

[1,4,7,6,5]

__iter__ and __next__

__iter__ = called with iter() and produces an iterator __next__ called with next() and produces the next item in a series raises a stop iteration at the end

Which of the following is most likely the correct put method for a BSTNode class?

def put(self, key, value): if key == self.key: self.value = value elif key > self.key: if self.left is not None: self.right.put(key, value) else: self.right = BSTNode(key, value) elif key < self.key: if self.right is not None: self.left.put(key, value) else: self.left = BSTNode(key, value)

Consider a priority queue implemented with an unsorted list data structure and the following initialization: class UnsortedListPQ: def __init__(self): self._entries = []

def remove_min(self): entry = min(self._entries) self._entries.remove(entry) return entry

Nodes at height h

h = 0 < ceiling(n/2) h = 1 < ceiling(n/4) h = 2 < ceiling(n/8) h < ceiling(n/(2)^n+1) nodes

building a heap from scratch

heapify is a process of arranging the nodes in correct order so that they follow heap property

Which of the following operations have a worst case asymptotic running time of O(logn) (no better, no worse) in a heap?

insert, remove min, upheap, downheap

How many levels in a complete binary tree with n nodes

log(n)

Postorder Traversal

process the left subtree and process the right subtree and process the root

Inorder Traversal

process the left subtree, process the root and process the right subtree

Preorder Traversal

process the root and then all the subtrees from left to right

Level order Traversal

process the root of a tree, process all the nodes at the same depth from left to right and then proceed ti the next depth

Self balancing binary search trees

we get O(log(N)) time for insert, search, and delete


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

Amazon Operations Analyst Interview

View Set

Scrum Team Development Test Prep

View Set

5.2 - Translating and Starting a Program

View Set

FIN 357 Chapter 10: Making Capital Investment Decisions

View Set

management test #3(Chapter 11)- ECU

View Set