MIT 6.006 - Algorithms Flashcards
(67 cards)
How many edges (given |V| number of vertices) are possible in a simple graph which is:
- Directed
- undirected
a simple graph doesn’t contain self loops, nor duplicate edges from v1 to v2 so:
- v*(v-1)
- v choose 2
for Both |E| = O(|v|^2)
What’s the out degree of a vertex?
How many edges are going out of that node.
What’s the sum of all outgoing degrees over all vertices in an undirected graph?
What about a directed graph?
undirected: 2*|E|
directed: |E|
If I solve the problem of “output all of the vertices |V| reachable from a specific vertex t, and for each its shortest path to t,
what would be the space complexity of a naive algorithm?
How can I do it better?
a naive output might be O(v^2).
Shortest paths tree will take only O(v)
How do I know that the shortest paths from t to all V is a tree?
- It has no cycles (otherwise, i would find a shorter path)
- It is connected (by definition,otherwise there would be no path…)
Why does BFS take O(|V|+|E|) time?
We allocate P() data structure of size V, which takes O(V) time, and then we run over all the edges of the graph which is O(E).
What’s wrong with the following BFS code if the graph is general? When it will work?
while len(nodesCurrentLevel)>0:
currentNode = nodesCurrentLevel.pop(0)
#Visit the node
currentLevelNodesVisited.append(currentNode.val)
if currentNode.left:
nodesNextLevel.append(currentNode.left)
if currentNode.right:
nodesNextLevel.append(currentNode.right)
ans.append(currentLevelNodesVisited)
In a general graph, there are loops, so this can create an infinite loop.
-> I need to check if a node was already encountered before adding it to the next level.
This will work on a tree since, by definition, a tree has no cycles.
What does DFS do better than BFS? (Why do I care about it if BFS already solved reachability and shortest path)
Time complexity of BFS is O(|V| +|E|).
Time complexity of DFS is O(|E|) (we consider the sum of out-degrees).
Notice that DFS solves reachability and not shortest path.
Since P(v) is saved for each node during DFS, and we can have up to V vertices on the stack, Its space requirement is O(v)
What does 6006 call a “full DFS”? What’s its output?
What’s its run time?
perform DFS for every node which its parent was not set yet.
This algo find the # of connected components in a graph.
Run time is O(|V| + |E|)
What’s a DAG? Give an example for a DAG
Directed acyclic graph.
A tree.
What’s a topological order
f(v) such that f(u)<f(v) if (u,v) is an edge.
What can I say about finishing order of DFS in a DAG? Why?
The reverse of the finishing order of DFS is a topological order.
If there’s an edge (u,v) I want to prove that f(u)<f(v). Either v was visited before u or not.
If u was visited before v, then u will call DFS on v which will finish first, and so reverse finish will mean that f(v)<f(u).
if u was not visited before we visit v, there must be a s’ node such that there’s a path from our source node s to v, without u.
That means that with u there’s a second path from s to v(just look at p(v)….up to s), which means that the graph is cyclic, but the graph is DAG…
In the lecture he claimed that since graph is acyclic, we can’t reach u from v and therefore visit(v) will finish before visit(u) will finish. It is correct and my claim wasn’t correct BECAUSE IT IS POSSIBLE FOR U TO BE NOT REACHABLE from S. DAG is acyclic but it’s not necessarily connected!
If it’s a DAG -> topological order from reverse time finished DSF
No such topological order -> not a DAG
Cycle detection in a directed graph- how?
If it’s a DAG -> topological order from reverse time finished DSF
No such topological order -> not a DAG!
Simply run DFS and check if reverse finishing time is a topological order or not.
DFS will traverse an edge from v to an ancestor of v: take a cycle (v0,v1…vk,v0) and let’s assume that v0 is the first node that DFS will visit (if not, then reorder the cycle such that it does - that is called without loss of generality).
DFS will visit after v0 v1,v2…vk and before finishing v0 will try to visit v0 from vk!
This will also tell me where’s the cycle in G.
What’s a weighted graph?
Along with V,E, there’s a weight function w:E->Z (notice that a weight can be negative,positive, or 0).
Why do we care about a weighted graph?
This models many things in real life: actual distances for routes, latency in a network, strength of connection in a social network etc etc
Why can we assume that we can query a weight in constant time?
We can save the weight of an edge in a hashset, or in the adjacency list of a vertex, which by itself can be a hashset
What’s the weight of a path
The sum of weight of its edges…
What’s a weighted shortest path
the path between u and v with minium weight (notice that if there’s a cycle with a negative weight this is minus infinity, if no path this is defined as infinity).
How can I solve the shortest path problem in a weighted graph with BFS?
Convert any u->v of weight, for example, 5, to a graph where there are 4 dummy nodes in between.
Of course, this will add many nodes and edges to the graph if the weights are big
We’re going to discuss finding the shortest path in a weight graph for the following algorithms:
- DAG-relaxation
- Bellman-Ford
- Dijkstra.
At what situation/complexity does each algorithm operate?
- DAG relaxation over DAGS at linear time (O(V+E)).
- Bellman-Ford - any graph, any weight function , O(V*E)
- Dijkstra, all weights are non-negative, O(VlogV+E)
Given a function delta(v) =delta(s,v) which outputs the minimum distance from s (the source node) to v, how can I create a data structure that will tell me P(v) (who is the parent of v) for all v?
Start with an empty data structure, where the parent of s is set to null.
For every v, look at its parents (incoming neighbors). if for a parent u, it is the case that delta(v)=delta(u)+w(v,u) then u is also a parent (there can possibly be more than one) of v in a shortest path from s. So i can assign p(v)=u.
In fact one iteration though all the nodes and their incoming edges will be sufficient.
Why doesn’t a simple DFS-like can’t calculate distances for any DAG?
Because a DAG, although acyclic, can still have multiple paths to the same node.
For example u->v->w
and u->v1->v2>-v3->w
Explain how DAG-relaxation works
Maintain estimates, which are upper bounds, d(s,v)>=delta(s,v).
Gradually lower those high estimates until d=delta (the estimates are the same as the actual distance).
Set d=infinite for all v,set d=0 for s.
If there’s a e,(u,v) such that d(s,v)= d(v)>d(u)+w(u,v) then set d(v)=d(u)+w(u,v) ->relaxation
Process each v in a topological order. For each outgoing neighbor, process its adjacent neighbors (relax any (u,v) that can be relaxed).
DAG relaxation: what is it? Why is it safe?
Relaxation is If there’s a e,(u,v) such that d(s,v)= d(v)>d(u)+w(u,v) then set d(v)=d(u)+w(u,v)
it’s safe as d(s,v) is either infinite, or the weight of some path from s to v, and so >= then delta(s,v).