Graphs Flashcards

1
Q

[SCC]
T/F
1) If a DFS tree has no back edges, the graph could still contain a cycle
2) A graph with a back edge must have exactly one cycle.
3) If you run DFS from a different starting vertex, some cycles in the graph might disappear.

A

1) False - cycles and back edges are equivalent

2) False - graph can have multiple back edges

3) False - cycles are a property of a graph, independent of DFS starting point

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

[Graph]
In a directed graph, how to detect a back edge?

A

Run DFS. If the postorder number of the “tail” vertex (the source of the edge) is greater than the postorder number of the “head” vertex (the destination of the edge)

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

[Graph]

How to find a back edge in an undirected graph?

A

Check the following:
prev[u] != v
prev[v] != u

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

[Graph property]

Algorithmic way to find cross edge

A

Given vertex u and v,
1. pre value for u must be less than its post value.
2. Both values of u are less than pre/post values of v

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

[Graph property]

Algorithmic way to find back edge

A

Given vertices v and w:
1. pre/post of v must be in between pre/post of w

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

[SCC]
What is the relationship between cycles in a graph G and back edges in its DFS tree?

A

Graph G has a cycle if and only if its DFS tree has a back edge.

This property holds true:
- Regardless of which vertex DFS starts from
- Regardless of the order vertices are explored in the adjacency list

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

[SCC]
T/F
1) If you change the order vertices are explored in the adjacency list, back edges could become tree edges
2) A graph has a cycle if and only if DFS finds a descendant with an edge to one of its ancestors

A

1) True - but doesn’t affect whether cycles exist in G
2) True - this is definition of a back edge

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

[SCC]

How to topologically sort a DAG

A
  1. Run DFS
  2. Sort postorder number in descending order
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

[SCC]

Topological sorting runtime

A

O(n+m)

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

[SCC]

What are the two guaranteed vertex types in every DAG?

A

1) At least one source vertex (no incoming edges)
2) At least one sink vertex (no outgoing edges)

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

[SCC]

Using DAG’s DFS postorder numbering, how can you identify 1) source and 2) sink vertices?

A

Source has highest postorder #

Sink has lowest postorder #

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

[SCC]

T/F

1) A DAG could exist without any source vertices if every vertex has at least one incoming edge.

2) In a DAG’s DFS, a vertex with no outgoing edges must have the lowest postorder number among its neighbors.

A

1) False - every DAG must have at least one source vertex

2) True - these would be the last explored in DFS

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

[SCC]

Minimum # of edges you must add to a directed, connected graph to make it strongly connected

A

Create an edge from any vertex in each sink SCC back to any vertex in source SCC

(By connecting any vertex from the sink SCC back to the source SCC, we can create the entire graph into a cycle)

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

[SAT]
When is a 2-SAT formula guaranteed to be satisfiable based on its SCCs?

A

if for all i, x_i and x_i-bar are in different SCCs

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

[SAT]
When is a 2-SAT formula NOT guaranteed to be satisfiable based on its SCCs?

A

if for some i, x_i and x_i-bar are in the same SCC

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

[SAT]
6 steps of 2-SAT algo

A

1) Simplify formula by removing unit clauses
2) Build implication graph
3) Run SCC algorithm to get topological ordering
4) Set sink SCC to true, source SCC to false
5) Remove satisfied sink/source SCC pair
6) Repeat steps 4-5 until graph is empty

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

[SAT]
Key fact needed before running 2-SAT

A

for all i, x_i and x_i-bar are in different SCCs

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

[Paths]

Modifying graph by offsetting negative values is not valid for Dijkstra’s . Why?

A

A-B-C
A-B=-1, B-C, -1, A-C=-1
If we offset negative by one, best path changes from A-B-C to A-C

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

[Paths]
How to detect cycle in an DIRECTED graph

A

Run SCC algorithm and check if number of vertices is equal to the number of SCCs

(if any SCC contains more than one vertex, then the graph has a cycle)

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

[Paths]
How to detect cycle in an UNDIRECTED graph

A

prev[u] != v AND prev[v] != u
for any (u,v)

If each were reached by a different path, then it doesn’t have a cycle.

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

[MST]

Input & Output

A

Input: Undirected weighted graph

Output: Spanning tree where sum of edge weights is the smallest possible

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

[MST]
Why is any bridge edge guaranteed to be in an MST?

A

Given a cut between S and V-S along that edge, that edge would be the only one that exists

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

[MST]
Explain greedy approach in plain words

A

Start from smallest weight, and take edge. Continue incrementing weight and selecting same weights until hitting edge that would create a cycle. Step up weight, and continue taking edges until you can’t without creating a cycle.

24
Q

[Kruskal]

Algorithm

A

Input: Connected, weighted graph

Algo:
* Sort weight using mergesort
* Initialize X with empty set
* Go through edges in order (by lowest weight)
* If adding edge doesn’t create a cycle add to X
* Return X

25
Q

[Kruskals]
Runtime

A

O(m log n)

  1. sort weight = O(m log n)
  2. check cycle = O(m log n) using union-find over all edges
26
Q

[Cut Property]

Define “cut”

A

Set of edges that divides vertices into 2 disjoint subsets: S and its complement S bar.

Requires an undirected graph.

27
Q

[Cut Property]

Key proof of cut property

A

Adding e* to T forms cycle, removing any cycle edge yields new tree T‘

28
Q

[Cut Property]

Key statement of cut property

A

It is always safe to add the lightest edge across any cut (ie. between vertex in S and one in V-S) provided X (edges that are part of MST) has no edges across the cut.

29
Q

[MST]

T/F
1) There can be more than one MST
2) MST won’t exist for a disjoint graph

A

1) True

2) False. “minimally spanning forest” denotes multiple MSTs for a disjoint graph.

30
Q

[Bellman Ford]

BF will always find a negative weight cycle in a graph

A

False

It will only find negative weight cycles reachable from start vertex.

31
Q

[Bellman Ford]

How to find negative weight cycle

A

Check DP Table D(i, n-1) and D(i, n) and see if any v in V is different.

32
Q

[Bellman Ford]

Subproblem

A

Let D(i,z) be the length of shortest path from s to z using at most i edges

33
Q

[Bellman Ford]

Recurrence Relation

Runtime

Return logic

A

Recurrence:
~~~
D(0,s) = 0
D(0,z) = inf if z != s

D(i,z) = min{
D(i-1, z),
min{D(i-1,y) + w(y,z), ∀(y,z)∈E}
}
for 1 <= i < n
~~~

Runtime: O(nm), because 2d table

Return logic: Return D(n-1, *)
(1D table, shortest path from s to all v in V)

34
Q

[Floyd Warshall - All Pairs]

Subproblem

A

Let D(i,s,t) be the length of the shortest path from s to t, using a subset of vertices V[1…i] as intermediate vertices

35
Q

[Floyd Warshall - All Pairs]

Recurrence

A
D(0,s,t) = w(s,t) if (s,t) in E
            inf otherwise

Recurrence
D(i,s,t) = min{
    D(i-1, s, t),
    D(i-1, s, i) + D(i-1, i, t)
}
for 0 <= i <= n, 1 <= s, t <= n
36
Q

[Floyd Warshall - All Pairs]

Runtime

A

O(n^3)

Triple nest loop (i,s,t)

37
Q

[Floyd Warshall - All Pairs]

Return logic

A

D(n, *, *)

2D table, shortest path for all pairs

38
Q

[Floyd Warshall]

FW will always find a negative weight cycle in a graph

A

True

It checks all pairs in the graph (unlike BF) so it will always find a cycle if it exists.

39
Q

[Floyd Warshall]

How to find a negative weight cycle

A

Check 2D table of D(n,,) to see if any of its diagonal entries are negative.

40
Q

[Bellman Ford]

Runtime to get all pairs shortest paths using BF

A

Worst case O(n^4)

  • BF is O(nm)
  • Running BF on all V is O(n^2m)
  • In case of dense graph where |E| = |V|, becomes O(n^2n^2), simplifies to O(n^4)
41
Q

[MST]

Given graph G=(V, E) give a formal statement of a “spanning tree”

ST != MST

A
  • S is subset of G
  • S = (V’, E’)
  • where V’ = V (# vertices equal)
  • where |E’| = |V| - 1 (n-1 edges)
42
Q

[MST]

Given G=(V,E) how many spanning trees are possible

A

|E| C_{|V|-1} - number of cycles

Ex G=(6,10) 6 vertices and 10 edges.
10 C_5 = “the number of ways to select 5 edges from 10 total edges”, and then subtract number of cycles

43
Q

[Runtime]

Big-O of O(n+m) operation on a clique and m >= n-1

A

O(n^2) because number of edges >= vertices

44
Q

[Runtime]

Big-O of O(n+m) operation on a tree

n=vertices, m=edges

A

O(n) because tree property means m = n - 1 so O(n + n-1) which simplifies to O(n).

45
Q

[Runtime]

Big-O of O(n+m) runtime of graph that is connected and m >= n-1

A

O(m)

O(n+m) = O(m+m) = O(2m) = O(m)

46
Q

[Runtime]

Traversing a graph

A

O(n+m)

(Includes reversing, copying, subgraphing, or anything that requires working with all vertices and edges in a graph)

47
Q

[Runtime]

Operating on a single vertex v

A

O(1)

(Includes find, read, removal)

48
Q

[Runtime]

Operating on all (or a subset of) vertices V

A

O(n)

(Includes find, read, removal)

49
Q

[Runtime]

Operating on a single edge e=(u,v)

A

O(n)

This is due to the need to traverse up to n vertices in the adjacency list for vertex u, to find and operate on adjacency v. (Includes find, read, removal)
50
Q

[Runtime]

Operating on all edges |E|

A

O(n + m)

(Includes find, read, removal)

51
Q

[Runtime]

Accessing properties of an edge or vertex

A

O(1)

This assumes you already have access to the vertex or edge itself - that is, you have paid the runtime costs above for finding that vertex/edge,

(e.g. weight, color, cost, capacity, etc.)

52
Q

[MST]

Why does Prim’s algorithm work with negative edge weights while Dijkstra’s algorithm doesn’t?

A

Dijkstra’s might get trapped in negative weight cycles.

Prim’s builds an MST. Since MST is a tree, it avoids cycles. Cut property also shows that crossing a cut is part of some MST. This property holds regardless of negative edge weight.

53
Q

[MST]

T/F

One can find a MST in a disconnected graph

A

False

  • Spanning tree by definition must “span” or reach all vertices in the graph.
  • Since a disconnected graph has vertices that cannot be reached from each other, it’s impossible to construct a single spanning tree, minimum or otherwise.
54
Q

For a directed graph G, denote SCC(G) the metagraph of strongly connected components. Denote by REV(G) the graph resulting from reversing the directions of all edges in G.

Please select all statements which are always true.

1) For all directed graphs G, SCC(G) is acyclic.
2) For all directed acyclic graphs G, SCC(G)=G.
3) There is an algorithm to find SCC(G) that runs in linear time on the size of G.
4) Every directed graph is the SCC(G) for some other graph G.
5) For all directed graphs G, SCC(REV(G))=REV(SCC(G)).

A

1) True. Covered in lectures

2) True. DAG = SCC

3) True. SCC algo

4) False. G with a cycle can’t be a SCC

5) True. We get the same graph

55
Q

After running DFS on a directed graph G=(V,E), an edge e=(uv) satisfies post(u)>post(v).

Please select all statements which are always true.

1) e is a back edge.

2) if there is a directed path from u to v (different from the edge e itself), then e is a forward edge.

3) if u is the parent of v, then e is a tree edge.

4) If pre(u)>pre(v), then e is a cross edge.

A

1) False

2) False

3) True

4) True

56
Q

Let G = (V,E) be a connected, undirected, weighted graph. You are told that all edges in G have distinct weights, and that an edge e of maximum weight is part of the MST.

1) All the weights are positive.

2) The edge e is the edge of minimum weight in some cut of G.

3) The edge e is not part of any cycle.

4) If we remove e from the graph, the resulting graph is disconnected.

A

1) False

2) True.

3) True. If there is a cycle, the cut e would never be chosen as minimum weight edge

4) True. The cut with edge e would only be chosen if edge e is a bridge edge.

57
Q

[2SAT]
Create implication of this conditional:
a v b

A

a-bar -> b
b-bar -> a