minimum spanning tree + shortest path Flashcards

1
Q

what is Dijkstra’s algorithm

A

single source shortest path

Find the shortest path from one node to all nodes, no negative edges allowed

greedy algorithm

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

what is bellman-ford

A

find the shortest path from one node to all nodes, negative edges are allowed

dynamic programming alg

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

what is floyd-warshall

A

find the shortest path between all nodes. negative edges are allowed

dynamic programming alg

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

what is kruskals alg

A

finds a minimum spanning tree

order the edges by least weight to greatest. then iterate through the list, always picking the next shortest edge that doesn’t create a cycle

greedy alg

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

what is prims alg

A

finds a minimum spanning tree

uses the cut method, pick a random node and pick the next shortest edge connected to a connected node that doesn’t create a cycle

greedy alg

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

time complexity of prims alg

A

O (E log V), that is to say O(edges * log (vertices))

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

time complexity of kruskals alg

A

O (E log E), that is to say O(edges * log(edges))

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

when is a graph sparse, and when is it complete

A
  • In a sparse graph: E≈V
  • In a dense graph: E can be as large as V2
    ^^^^That seems like a really hard target to hit…..

A graph is complete when every two vertices/nodes are directly connected by an edge. Near-complete is similar, just that almost all are directly connected instead of requiring that every single pair be directly connected.

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

when should prims be used and when should kruskals be used

A
  • If the graph is complete or nearly complete, Prim performs
    faster O(ElogV)
  • In Kruskal: If the graph is sparse (few edges), Kruskal gives
    O(ElogE)

so, the more edges the more of an advantage prims has

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

how to implement dijkstras

A

Use a priority queue to repeatedly select the closest unvisited node, update its neighbors’ distances, and mark it as visited. Best for shortest paths in non-negative weight graphs.

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

hows to implement bellman-ford

A

For each edge, relax (update) distances V−1 times. Detects negative cycles and works with negative weights. Finds shortest path.

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

how to implement prims

A

Start from any node and add the shortest edge connecting the growing tree to a new node until all nodes are included. This creates Minimum Spanning Trees (MSTs).

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

how to implement kruskals

A

Sort all edges by weight, and use a union-find structure to add the shortest edge to the MST without creating cycles, until all nodes are connected.

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

how to implement floyd-warshall

A

Use a 2D matrix to update all pairs’ shortest paths by considering each node as an intermediate, one-by-one. Good for finding shortest paths in dense graphs.

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

time complexity of bellman-ford

A

O(V * E)

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

time complexity of dijkstras

A

O((V + E) * log (V))

17
Q

time and space complexity of floyd-warshall

A

time: O(n^3)
space: O(n^2)

18
Q

when should dijkstras be used, when should bellman-ford be used, and when should floyd-warshall be used

A

Dijkstra’s:
Use it when you need the shortest path from a single source, the graph has non-negative weights, and you want efficient performance (works best with sparse graphs).

Bellman-Ford:
Use it when you need the shortest path from a single source and the graph has negative weights or may have negative cycles. It’s slower but can handle scenarios Dijkstra’s can’t.

Floyd-Warshall:
Use it when you need shortest paths between all pairs of nodes, regardless of edge weights (though no negative cycles). It’s most suitable for dense graphs or smaller graphs, as it has a high time complexity.