Algorithms - Trees Flashcards

1
Q

What is the minimum spanning tree?

A

A minimum spanning tree is a subgraph of a graph that includes all the vertices of the original graph, but contains zero cycles.

Thus, the minimum spanning tree represents the lowest-cost way to connect all points.

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

What is the shortest path?

A

The shortest path is the lowest-cost path between two nodes.

We can find this using Dijkstra’s or A* algorithm.

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

What is meant by topological sort?

A

Topological sort is an ordering of the nodes in the graph such that if there is an edge (u, v) in the graph, then u appears before v in the ordering.

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

How can we get the topological sort of a graph?

A

We can use recursive DFS to get the reverse topological sort of the graph, and then reverse it to get the true topological sort.

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

What is the general method of Prim’s algorithm?

A

First, we find an initial edge, which is the minimum cost edge of the entire graph.

Then, we need to repeatedly find the smallest edge connected to our selected vertices, until our minimum spanning tree is found.

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

What is Prim’s algorithm used for?

A

Prim’s algorithm is a greedy method to get us the minimum spanning tree of a connected graph G.

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

What is Dijkstra’s algorithm used for?

A

Dijkstra’s algorithm is a greedy method to get the shortest possible path from a starting to a terminal vertex.

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

What is the general method of Dijkstra’s algorithm?

A

We start by giving every vertex in the graph a value based on their distance from the initial vertex. For vertices that are not directly connected, we give them a value of infinity.

Then, we recursively select the shortest unvisited path. From this new point, we perform a process called relaxation - we say that if the distance of our current vertex + the distance to the next vertex is shorter than the existing distance in our array, we update that value.

We repeat this until we have visited every vertex, and at this point we have an array consisting of the shortest path to each vertex. From here, we can generate a path between the initial and terminal vertex to get our shortest path.

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

What is the general method of a Breadth First Search algorithm?

A

First, we visit our initial vertex.

Then, we explore this vertex, placing all vertices around it into a queue. The order does not matter.

Then, we will visit the next vertex in the queue, and also explore that vertex, placing all around it into the queue.

We repeat this over and over again until we have a list of visited vertices in the queue.

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

What is a BFS/DFS Spanning Tree?

A

A BFS/DFS Spanning Tree is a tree created in the process of performing a breath-first or depth-first search, where we treat the initial vertex as the root of a rooted tree, and have all explored vertices be subtrees of that vertex.

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

What is the general method of a Depth First Search algorithm?

A

First, we visit our initial vertex.

Then, we explore this vertex, selecting the first vertex in sight. If we find a vertex, we place it onto our stack, “suspending” it for now.

If we do not find a vertex, then we push the vertex off the stack, returning to our previous vertex.

DFS is almost as if we are taking a mad dash to every vertex until we hit a dead end.

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

What is the general method for Heapsort?

A

First, we treat the given array as if it were a tree. We do this using a level-based definition.

Then, we turn that tree into a heap using a max-heapify function. We know that the first element heap[0] is the maximum value, so we swap it with the final value in the array, and call that sorted.

We then recursively do this for n - 1 elements, until we have a fully sorted array.

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

What are the benefits and downsides to Heapsort?

A

Heapsort is not stable, so Mergesort is a better option if you’re looking for pure stability.

However, Heapsort is much faster, operating at a guaranteed worst-case complexity of O(n log n), which is very fast compared to Mergesort’s O(n) complexity.

Furthermore, Heapsort is inplace, making it very memory efficient, with a space complexity of O(1).

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