Test 3 Flashcards
(38 cards)
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|)
Time complexity of BFS, DFS and topological sorting?
O(|V|+|E|)
What is the time complexity of prim’s MST?
O(|V|+|E|log|V|)
What is the time complexity of Kruskal’s MST?
O(|V|+|E|log|E|)
What is the complexity of a graph? (How does m relate to n)
M= O(n^2)
Where m equals number of edges and n equals number of vertices
What is adjacent mean in reference to a graph?
Two vertices with an edge between them
What does degree mean in reference to a graph?
The number of vertices adjacent to a given vertex
Describe an adjacency matrix
It’s an n by n matrix where:
A of IJ is one if v of I and v of j are adjacent otherwise it is zero.
What is a walk in reference to a graph?
A sequence of vertices joined by edges
What is a path in reference to a graph?
A walk with no repeated vertices or edges