Hikmat Flashcards

1
Q

What is a graph?

A

A set of vertices and a set of edges connecting the vertices

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

What’s usually associated with each edge?

A

A weight or cost

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

What makes a path simple?

A

If all vertices, except possibly the first and last, are distinct

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

What are the two ways to represent a graph?

A
  • Adjacency matrix
  • Adjacency list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the memory requirement of an adjacency list?

A

O(|E| + |V|)

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

What is the memory requirement of an adjacency matrix?

A

O(|V^2|)

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

What is Breadth First Search (BFS)?

A

An algorithm that systematically “discovers” all vertices that can be reached from S (source node)

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

What is the total cost of BFS?

A

O(|V| + |E|)

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

Why is BFS not Θ(|V| +|E|)?

A

Some vertices may not be reachable from s

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

What is Depth First Search (DFS)?

A

Edges are explored out of the most recently discovered node, going deeper whenever possible

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

How do BFS and DFS differ?

A

A single run of DFS is guaranteed to visit all nodes even if the graph has more than one connected component

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

What is the total cost of DFS?

A

Θ(|E| + |V|)

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

What is topological sorting?

A

A linear ordering of the nodes of directed graph such that every edge from node u to node v, u comes before v in the ordering

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

What is a connected component of an undirected graph?

A

A subgraph in which there is a path between every pair of vertices

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

What is Relaxation?

A

Relaxing an edge (u, v) means testing if we can improve the shortest path cost of v by using the edge (u, v)

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

What is the complexity of Kruskal’s Algorithm?

A

It is O(mlogn) with n = |V| and m = |E|

17
Q

What is the difference between Kruskal’s algorithm and Prim’s algorithm?

A

Kruskal’s is edge based while Prim’s is vertex based

18
Q

How does Prim’s algorithm work?

A
  • Maintains a set S of vertices, initially containing a single vertex s which could be any vertex in V
  • At each iteration we consider the sets S and V - S
  • Find the edge (u, v) with minimum weight such that u ∈ S and v ∈ V - S
  • Add v to S and (u, v) to T
19
Q

What is the time complexity of Prim’s algorithm?

A

O(m log n)

20
Q

What is a cut?

A

A cut (S, V - S) of the graph G is a partition of V

22
Q

How does Bellman-Ford Algorithm work?

A
  • Computes the shortest path from a given source to all other nodes in the graph
  • IT works with negative weights and can detect negative cycles
  • It uses the function RELAX to compute the shortest path
  • RELAX is applied to all edges in the graph |V | - 1 times
23
Q

What is the time complexity of Bellman-Ford?

A

O(|V| x |E|)

24
Q

How does Dijkstra’s algorithm work?

A
  • Works when all weights are positive
  • Faster than Bellman-Ford
  • Maintains a set S of nodes whose shortest paths have been determined
  • All other nodes are kept in a min-priority queue to keep track of the next node to process
25
Q

When does Dynamic Programming apply?

A
  • Optimal substructure
  • Overlapping subproblems
26
Q

What are the two ways it can solved?

A

With memorization or bottom-up solutions