Final Flashcards
(54 cards)
What is a sparse vs dense graph?
|E| ~ |V^2| = dense
|E| < |V^2| = sparse
*E = O(|V^2|)
What are 2 ways of representation of a graph?
Adjacency List:
- Array “Adjacency” of |V| lists → in the slot of the vertex a, linked list of all vertices a connects to (edges)
- 1 list/vertex
- Can store weights in the adjacency lists
- Good for sparse graphs
- Good when frequently add/remove vertices or need to traverse the graph
- Not good in determinig if an edge(u,v) exists
Adjacency Matrix:
- |V| x |V| matrix → 1 is exists an edge, 0 if not (can also store the weight of the edge directly)
- For undirected graphs, A = A^T
- Good for adding and removing edges, NOT vertices
- Good to check presence/absence of an edge
- NOT sparse-efficient
Both have a running time of O(V+E)
What are the main features of the BFS algorithm for graph traversal?
(input, ouput, DS)
Input: graph G = (V, E) (directed or not) and a source node
Output:
d[v] = distance from s to all vertices v, v = inf. if not reachable
pi[v] = u such that (u, v) is the last edge on the shortes path s → v
Algorithm:
- Uses a queue to determine next vertex to visite (radial, priority = distance from the source)
- Node is gray when put into the priority queue
- Node is black when poped out of pq + visited
- Node is white if not visited yet (inf distance in d[v])
What are the main features of the DFS algorithm for graph traversal?
(input, ouput, DS)
Input: graph G = (V, E) (directed or not), NO source vertex
Output:
- d[v] = discovery time (v turns from white to gray)
- f[v] = finishing time (v turns from gray to black when added to f[v])
- pi[r] = predecessor o v = u, such that v was discovered during the scan of u’s adcacency list
Algorithm:
- Uses a stack
*Often subroutine of another algorithm
In DFS, what are Tree, Back, Forward and Cross edges?
Tree edge → in the depth-first forest. Found by exploring (u, v), discovers a new/white node
Back edge → (u, v), where u is a descendant of v (in the depth-first tree).
Forward edge → (u, v), where v is a descendant of u, but not a tree edge. (finds closed node)
Cross edge → any other edge. Can go between vertices in same depth- first tree or in different depth-first trees.
*Based on the color when being discovered
What type of graph traversal is used in the 3 following cases?
1. Topological Sort
2. Shortest Path
3. Minimum Spanning Tree
- Topological Sort → Depth-First (Stack)
- Shortest Path → Breadth-First (Queue)
- Minimum Spanning Tree → Best-First (Priority Queue)
What is a directed acyclic graph (DAG)?
What lemma characterizes a DAG?
- Can be used to encode precedence relations or dependencies in a natural way
- Source with no incoming edges
- Sink with no outgoing edges
*May have more than 1 of each
Lemma 1 → A directed graph G is acyclic iff a DFS of G yields no back edges (proof shows that a back edge leads to a cycle + a cycle implies a back edge, prove both ways)
What is a BFS-based approach to the Topological Sort algorithm?
What is its running time?
*Kahn’s algorithm
*Made on DAG → sorts elements in a natural way
*Version of BFS
- Start with source node (inDegree = 0), 1st in ordering / Compute all inDegrees and add all the inDegrees=0 into the queue (v)
- Delete v from G → safe since can’t create any cycles
- Decrease in-degree by 1 of all its neighbouring nodes, if inDegree=0, then add to the queue - Recursively compute topological ordering of G - {v}
- Repeat step 2 until the queue is empty
Running time:
Computing in-degree → O(V+E)
Rest of algortihm → O(V+E)
What is a DFS-based approach of Topological Sort?
What is the running time?
- Call DFS(g0 to compute finishing times f[v] for all v e V
- As each vertex is finishes, insert it onto the front of a linked list (representing the topological sort graph)
- Return the linked list of vertices
Time = O(V+E)
What edges are found in DFS of a connected undirected graph?
Tree edges and Back edges
- No forward or cross edges
What is the definition of a Strongly Connected Component?
What is a Component graph (what type of graph is it always)?
A strongly connected component (SCC) of G is a maximal set of vertices C within V such that for all u, v e C, both u → v and v → u exist.
*In directed graph
A component graph is a graph where each vertex represents a strongly connected component → Always a DAG (otherwise the 2 vertices are in the same SCC)
What are common features of G and its transpose? How is the transpose of G made?
What is the running time of creating G^T from G when using an adjacency lists vs adjacency matrix?
G^T has all the same vertices, but the edges are reversed
- G and G^T have the same SCCs
Can create G^T in O(V+E) using adjacency lists
*Adjancency matrix would be V^2 as you need to scan the VxV slots, not efficient for sparse graphs
What algorithm allows to determine SCCs?
Kosaraju’s Algorithm:
1. call DFS(G) to compute finishing times f [u] for all u
2. compute GT
3. call DFS(GT), but in the main loop, consider vertices in decreasing order of finishing time (f [u] as computed in first DFS)
4. Each DFS tree in step 3 is a SCC
Running time = O(V+E)
*2 DFS, 1 Transpose
Idea:
- By considering vertices in second DFS in decreasing order of finishing times from first DFS, we are visiting vertices of the component graph in topologically sorted order.
- Because we are running DFS on GT, we will not be visiting any v from a u, where v and u are in different components.
How does SCCs relate to DFS finishing times?
Let U and V be distinct SCC’s in G = (V, E).
Suppose there is an edge (u → v) e E such that u e U and v e V. Then f (U) > f (V).
i.e. The finishing time of any node in U is greater than the fishing time of any node in V.
*Close a node only after all descendants are closed
Consider U and V as single nodes of the SC graph (DAG)
What is the Ford Flukerson Algorithm?
What is the running time?
Ford-Fulkerson algorithm → method for finding the maximum flow in a flow network. Works by iteratively finding paths (augmenting paths) in the network with available capacity and pushing flow along these paths. The algorithm continues until no more augmenting paths can be found, at which point the maximum flow is reached.
*Augmenting path is a path from s → t in the residual graph
- Flow added is the smallest capacity in the residual graph along that path
Running Time = O (C*|E|) where C is the sum of capacities of all outgoing edges from the source (max network capacity) as the flow increase by at least 1 at every iteration
What are some variants of the Shortest path problem?
**Based on BFS
Single source (SSSP) → Find shortest paths froma given source vertex
Single-destination → Find shortest paths to a given destination vertex
- SSSP, but reversing the direction of each edge
Single-pair → Find shortest path from u to v
- SSSP solves this
- MTL → Cancun shortest path
All-pairs → find shortest path from u to v for all u, v e V
- By running a SSSP algorithm once, each vertex, but we can solve it faster
- Get to Cancun by shortest path, can start anywhere
What are conditions to the graph on which we perform the shortest path algorithm?
- No negative weight cycles → will go around until -inf.
*Shortest path contains NO cycles
- negative-weights → ruled out
- positive-weights → get shorter path by omitting the cycle
- Zero-weights → no reason to use them, assume don’t use them
Shortest path has at most |V| - 1 edges (DAG)
What is the SSSP algorithm?
Is it based on BFS or DFS?
Single-Path shortest path allows to find all shortest paths from a given source node to all other nodes
*Based on BFS
*Relax edges
What are properties of SSSP for DAGs?
What is the trick to solve this easily?
DAG implies no negative-weight cycles
- DAGs have minimum 1 source and 1 sink because if not, would have cycles (can have more)
- Can have negative-weights because no cycles
- Topological sorting allows to find SSSP easily
Run time = O(V + E)
What is Dijkstra’s algorithm?
It is an algorithm to find the Shortest Path form a single source to all vertices (SSSP)
*No negative weighted cycles
- Add all edges to the PQ (most set to inf. if not adjacent to source)
- While the PQ is not empty, extract the min → add it to S
*Greedy choice: at each step, choose the light edge - For each vertex v adjacent to u, relax the edge if tensed
Ouputs d[u] → an array of shortest paths from source to every vertex
*weighted version of BFS
What is the run time of Dijkstra’s algorithm?
Depends on implementation of the priority queue
For binary heap, each operation takes O(log v) time → O(E log V)
What type of graph does Topological search, Dijkstra’s, Minimum Spanning Tree, Ford-Fulkerson, Kurskal and Prim’s algorithms apply to?
Topological Sort → DAG
Dijkstra’s → Directed or undirected, no negative weight edges
Minimum Spanning Tree → Undirected (can have neg weight cycles)
Ford-Fulkerson → Directed graph
Kurskal’s Algorithm → Undirected weighted edges
Prim’s Algorithm → Undirected weighted edges
What is Kurskal’s Algorithm?
It is an algorithm for solving MST for sparse graphs
- Start with each nodes as separated disjoint set
- Chose lightest edge
- Check that they are in different disjoint sets (Union-Find)
Run Time = O(O(Ea(V)) + O(E log E) = O (E log V)
What data structure and running time correspond to Prim’s vs Kurskal’s algorithm?
When do we use each of these?
Kurskal → Disjoint Sets
- O( E log E) ~ O(E log V)
- For sparse graphs
Prim’s → Priority Queue
- O (E log V)
- For dense graphs