Graphs Flashcards
(57 cards)
[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.
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
[Graph]
In a directed graph, how to detect a back edge?
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)
[Graph]
How to find a back edge in an undirected graph?
Check the following:
prev[u] != v
prev[v] != u
[Graph property]
Algorithmic way to find cross edge
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
[Graph property]
Algorithmic way to find back edge
Given vertices v and w:
1. pre/post of v must be in between pre/post of w
[SCC]
What is the relationship between cycles in a graph G and back edges in its DFS tree?
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
[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
1) True - but doesn’t affect whether cycles exist in G
2) True - this is definition of a back edge
[SCC]
How to topologically sort a DAG
- Run DFS
- Sort postorder number in descending order
[SCC]
Topological sorting runtime
O(n+m)
[SCC]
What are the two guaranteed vertex types in every DAG?
1) At least one source vertex (no incoming edges)
2) At least one sink vertex (no outgoing edges)
[SCC]
Using DAG’s DFS postorder numbering, how can you identify 1) source and 2) sink vertices?
Source has highest postorder #
Sink has lowest postorder #
[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.
1) False - every DAG must have at least one source vertex
2) True - these would be the last explored in DFS
[SCC]
Minimum # of edges you must add to a directed, connected graph to make it strongly connected
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)
[SAT]
When is a 2-SAT formula guaranteed to be satisfiable based on its SCCs?
if for all i, x_i and x_i-bar are in different SCCs
[SAT]
When is a 2-SAT formula NOT guaranteed to be satisfiable based on its SCCs?
if for some i, x_i and x_i-bar are in the same SCC
[SAT]
6 steps of 2-SAT algo
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
[SAT]
Key fact needed before running 2-SAT
for all i, x_i and x_i-bar are in different SCCs
[Paths]
Modifying graph by offsetting negative values is not valid for Dijkstra’s . Why?
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
[Paths]
How to detect cycle in an DIRECTED graph
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)
[Paths]
How to detect cycle in an UNDIRECTED graph
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.
[MST]
Input & Output
Input: Undirected weighted graph
Output: Spanning tree where sum of edge weights is the smallest possible
[MST]
Why is any bridge edge guaranteed to be in an MST?
Given a cut between S and V-S along that edge, that edge would be the only one that exists
[MST]
Explain greedy approach in plain words
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.
[Kruskal]
Algorithm
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