Final Exam Flashcards

1
Q

Descibe selection sort

A

At iteration 0, find the smallest element in the vect, swap it with the element at idx 0

At iteration 1, find the second smallest element, swap it with the element at idx 1

Repeat until sorted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

Describe bubble sort

A

run thru elements from smallest idx to largest

At each iteration, if the element that your inspecting is greater than the next, swap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe insertion sort

A

At iteration i, make sure that the first i+1 values in the vector are sorted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the run time of bubble sort

A

O(n^2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the run time of selection sort

A

O(n^2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the run time of insertion sort

A

O(n^2). However, with a “nearly” sorted vector, this can be O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the run time of sorting with the STL’s multiset

A

O(n log n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe heap sort

A

Uses a priority queue to sort in place

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the run time of heap sort

A

O(n log n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe merge sort

A

Split the vector into two equal parts, and recursively sort both parts. Merge the two sorted sub vectors as the recursion returns

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the run time of merge sort

A

O(n log n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe quick sort

A

Partition the vector. Keep a left and a right pointer. Increment the left pointer until you have a value greater than or equal to the pivot, decrement the right pointer until you have a value less than or equal to the pivot. Swap the two elements, then repeat until sorted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the run time of quick sort

A

O(n log n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe bucket sort

A

Approximate where we think each element should be in the vector. Once we have a vector that is almost sorted, use insertion sort to finish the job.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the run time of bucket sort

A

Average case: O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe DFS

A

Maintain a visited field for each node. For each node on n’s adjacency list, if the next node hasn’t been visited, check its children. Continue for all nodes that appear. In a connected graph, every node will be visited.

Can be done either recursively or with a queue

16
Q

What is the run time of a DFS

A

O(V+E)

However, if there are more vertices than edges, O(V)
If there are more edges than vertices, O(E)

17
Q

Describe BFS

A

Use a stack. Pop a node off of the stack, do some processing on the node, push all of the node’s children onto the stack in reverse order. Repeat until the stack is empty.

BFS visits all nodes and edges, but does so in order of distance from the starting node

Use for finding the shortest hop distance between nodes

18
Q

What is the run time of a BFS

A

O(V+E)

Depends on the number of vertices and edges in a graph

19
Q

Describe Dijkstra’s algorithm

A

A modification to BFS. Uses a multimap, keyed on the distance from the starting node.

For all nodes, set their back links to NULL, their distances to -1, and their visited field to false. Set node 0’s distance to 0, put it on the multimap. Repeat the following:

Remove a node n from the front of the multimap, set its visited field to true. For each edge from n to n2 that hasn’t been visited, push n2 onto the multimap, set its distance, set its back link.

20
Q

What is the run time of Dijkstra’s algorithm

A

O(E log V)

21
Q

List the steps of dynamic programming

A
  1. Implement a recursive solution
  2. Memoize recursive answers, cache them, recall when repeat recursive calls occur
  3. Eliminate recursive calls, usually do this by using for loops instead
  4. Eliminate or reduce the cache.
22
Q

Describe Prim’s algorithm

A

A modification of Dijkstra’s algorithm. Builds the minimum spanning tree node by node. Maintain a current spanning tree. Find the minimum edge weight from a node in the current spanning tree to a node not in it. Add that node and edge to the current tree. Keep doing this until all nodes are added to the tree.

Uses a multimap of edges, but keyed on weights of edges instead.

23
Q

What is the run time of Prim’s algorithm

A

O(E log V)

24
Q

What is the prerequisite for using topological sort

A

Graph must be a DAG

25
Q

What is the run time of Topological sort

A

O(V+E)

26
Q

Describe the halting problem

A

Write the procedure: string halt(char *program, char *input)

The first parameter is a C++ program which when compiled will execute, reading from stdin

Halt must return in a finite amount of time, and what it returns depends on what happens when program is compiled and executes on the contents of the file input as stdin. If program finishes in a finite amount of time, halt should return ‘halt’. Otherwise, halt should return ‘infinite loop’ and exit.

This is impossible.

27
Q

Describe Kruskal’s algorithm

A

Builds the spanning tree edge by edge instead of node by node.

Start with a disjoint sets with |V| nodes. Sort all edges from min weight to max weight. Process the edges; if the edge connects two disconnected components, then add the edge to the spanning tree. Repeat until there is only 1 connected component remaining. The edges you have added to that point makes up the min spanning tree

28
Q

What is the runtime of Kruskal’s algorithm

A

O(E log E)

29
Q

What data structure is used for Prim’s algorithm

A

A multimap that is keyed on edge weight

30
Q

What data structure is used for Kruskal’s algorithm

A

Disjoint sets, and a vector of the sorted edges

31
Q

Why is Dijkstra’s algorithm potentially faster than Topological sort

A

Topological sort will process ALL edges, where Dijkstra’s won’t necessarily process all edges

32
Q
A