Test 3 Flashcards

1
Q

Attributes of greedy algorithms?

A

Always chooses best option at time, not always correct, proof by contradiction and disprove by counterexample

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

Similarities between greedy algorithms and dynamic programming?

A

Makes a choice at each step

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

Differences between greedy and dynamic programming?

A

Dynamic finds optimal solutions to subproblems solves bottom up, greedy makes choice before solving some problems. Solves top down

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

What is the complexity to add an edge, remove an edge, is adjacent, for an adjacency matrix?

A

O(1)

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

What is the complexity to add an edge, remove an edge, and isadjacent for an adjacency list?

A

O(1)

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

What is the complexity difference between an adjacency list and an adjacency matrix to make?

A

An adjacency matrix takes O (n^2) vs adjacency list takes O(n)

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

What is the complexity difference between an adjacency list and an adjacency matrix to GetNeighbors(v)?

A

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).

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

What is the space difference between an adjacency list and an adjacency matrix?

A

O(n^2) for the adjacency matrix and O(m+n) for the adjacency list.

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

Main difference in structure between BFS and DFS?

A

BFS is queue-based and DFS is recursively stack based

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

Explain a BFS traversal tree

A

All non-tree edges are cross edges, they’re the same level or next level down

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

Explain a DFS traversal tree

A

All non-tree edges are back edges, they connect to ancestors

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

What are the characteristics of Dijkstra’s algorithm?

A

Weighted graph distance.

  1. Choose vertex with minimum distance
  2. Update Distance of neighbors with distance plus weight(u,v) if shorter.
  3. Repeat until destination or all vertices explored
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Characteristics of prim’s algorithm?

A

Its a Minimum spanning tree

  1. Choose closest vertex.
  2. Update distance of neighbors with w(u,v) If shorter.
  3. Repeat until all vertices explored.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Describe Kruskal’s algorithm

A
  1. Sort edges
  2. Choose minimum edge
  3. Add to MST if inpoints are not already joined
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the time complexity for Dijkstra’s algorithm?

A

O(|V|+|E|log|V|)

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

Time complexity of BFS, DFS and topological sorting?

A

O(|V|+|E|)

17
Q

What is the time complexity of prim’s MST?

A

O(|V|+|E|log|V|)

18
Q

What is the time complexity of Kruskal’s MST?

A

O(|V|+|E|log|E|)

19
Q

What is the complexity of a graph? (How does m relate to n)

A

M= O(n^2)

Where m equals number of edges and n equals number of vertices

20
Q

What is adjacent mean in reference to a graph?

A

Two vertices with an edge between them

21
Q

What does degree mean in reference to a graph?

A

The number of vertices adjacent to a given vertex

22
Q

Describe an adjacency matrix

A

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.

23
Q

What is a walk in reference to a graph?

A

A sequence of vertices joined by edges

24
Q

What is a path in reference to a graph?

A

A walk with no repeated vertices or edges

25
Q

What is a circuit in reference to a graph?

A

A walk that begins and ends at the same vertex

26
Q

What is a cycle in reference to a graph?

A

A non-trivial path that begins and ends the same vertex, ie does not repeat edges or vertices

27
Q

What is connected mean in reference to a graph?

A

Two vertices that have a path between them

28
Q

Explain the process of BFS

A
  1. Mark all vertices as undiscovered
  2. Add v(0) to queue
  3. Mark v(0) as discovered
  4. While q is not empty:
    Dequeue vertex v
    Enqueue children of v that are undiscovered
    Mark v as explored
    Mark children as discovered
  5. If any vertex still undiscovered choose one and restart process
29
Q

Explain DFS process

A
DFS recursive:
    1. Mark v as discovered
    2. For c in N(v):
         If C is undiscovered call DFS recursive on C
     3. Mark v as explored
30
Q

What are the similarities of BFS and DFS?

A
  1. Both traverse one component at a time
  2. Both take O(n+m) time
  3. Both traversal trees have special properties
31
Q

What are the differences between DFS and BFS?

A
  1. DFS is somewhat easier to implement

2. The usage depends on the application

32
Q

What can BFS and DFS both be used for?

A

Component detection and cycle detection

33
Q

What applications are better for BFS?

A

On way to distance, radius and center of unweighted graph, and between this centrality

34
Q

What applications are best for DFS?

A

Cut edge or cut vertex detection, sorting (on a BST), topological sorting and backtracing

35
Q

What does a traditional algorithm for topological sorting use?

A

DFS

36
Q

What is a spanning tree?

A

Given an undirected and connected graph, a spanning tree spans the entirety of the graph

37
Q

What is a minimum spanning tree?

A

Given an undirected weighted graph, a minimum spanning tree cost the least to span the whole tree

38
Q

Main difference between Prim’s algorithm and Kruskal’s algorithm?

A

Prim’s runs faster and dense graphs with more edges and vertices, where? K is found to run faster and sparse graphs
(Think about it, K is reliant on log (M) so less edges = better)