Test 3 Flashcards
Attributes of greedy algorithms?
Always chooses best option at time, not always correct, proof by contradiction and disprove by counterexample
Similarities between greedy algorithms and dynamic programming?
Makes a choice at each step
Differences between greedy and dynamic programming?
Dynamic finds optimal solutions to subproblems solves bottom up, greedy makes choice before solving some problems. Solves top down
What is the complexity to add an edge, remove an edge, is adjacent, for an adjacency matrix?
O(1)
What is the complexity to add an edge, remove an edge, and isadjacent for an adjacency list?
O(1)
What is the complexity difference between an adjacency list and an adjacency matrix to make?
An adjacency matrix takes O (n^2) vs adjacency list takes O(n)
What is the complexity difference between an adjacency list and an adjacency matrix to GetNeighbors(v)?
For adjacency matrix it is O(n), and for an adjacency list it is O(deg(v)) which equates to? O(m). (M is the number of edges).
What is the space difference between an adjacency list and an adjacency matrix?
O(n^2) for the adjacency matrix and O(m+n) for the adjacency list.
Main difference in structure between BFS and DFS?
BFS is queue-based and DFS is recursively stack based
Explain a BFS traversal tree
All non-tree edges are cross edges, they’re the same level or next level down
Explain a DFS traversal tree
All non-tree edges are back edges, they connect to ancestors
What are the characteristics of Dijkstra’s algorithm?
Weighted graph distance.
- Choose vertex with minimum distance
- Update Distance of neighbors with distance plus weight(u,v) if shorter.
- Repeat until destination or all vertices explored
Characteristics of prim’s algorithm?
Its a Minimum spanning tree
- Choose closest vertex.
- Update distance of neighbors with w(u,v) If shorter.
- Repeat until all vertices explored.
Describe Kruskal’s algorithm
- Sort edges
- Choose minimum edge
- Add to MST if inpoints are not already joined
What is the time complexity for Dijkstra’s algorithm?
O(|V|+|E|log|V|)