Given we have a graph G = (V,E) and a subset F with only V in it, for each connected component S of F, add the minimum weight edge in the cut (S, V \ S) to F.
Why is it that every time the minimum weight edge is added to F, F remains acyclic?
To create cycle, you have to create edge which connects vertices which are already connected.
If you add edge between vertices that are not connected, you don't create new cycle. You connect two unconnected components. But graph remains acyclic.
To get better understanding how it works, you could represent connected component of graph as single vertex. And then, when your add edge between unconnected components, your just merge vertices.
By the way, this question is not related to weights (and MST algorithm). It's still valid without weights.
Related
The following is an example from my graph theory and algorithm course:
Let A be a minimal subset of edges of a weighted undirected graph G (distinct weight), such that if we remove A from G then G becomes disconnected. The lightest edge in A must be in any MST.
Why this is a correct fact? I couldn't understand it.
According to the definition of minimal edge cut:
A minimal edge cut is an edge cut such that if any edge is put back in the graph, the graph will be reconnected.
In the following figure:
The set, A = {a, b, c, d} is a minimal edge cut.
If A is removed from G, the graph becomes disconnected.
Now, the property: "lightest edge in A must be in any MST" can be explained by the Cut Property:
Therefore the statement is true, because,
The lightest edge of the set A = e = min(a, min(b, min(c,d))) will be the part of MST.
Here's how I think of it. It's given that A is a set of edges that when removed disconnects G. We could say that A connects (at least) two subsets of nodes in G, G1 and G2. There can be no edges linking G1 and G2 that don't go through A, because otherwise the connection of G1 and G2 would not depend on A. Also denote m as the minimum edge within A (not necessarily the minimum of G, although it could be!).
So the question is, can you make the minimum path from G1 to G2 through A without taking its minimum edge m? Well think of another subgraph which is all of the nodes which contain an edge within A. So at least one node from G1 and one node from G2 (such that they are connected through A). The MST of this subgraph must include m, because a MST must include the smallest edge weight. And the MST of G must include the MST of A, else we would be getting from G1 to G2 in a suboptimal way.
Let G = (V, E) be a strongly connected directed graph. Start with the graph G' = (V, {}). We are given a list L of edges in E such that every edge in L we add to G' (in order) connects two strongly connected components. What's a fast algorithm to keep track of the strongly connected components of G' as we add one edge at a time? Using Kosaraju's or Tarjan's algorithm at every step takes O(|E|(|V|+|E|)) time, which I'm guessing can be improved.
I want to check if my unidirected graph is a tree. Tree is an acyclic and connected graph. I have a function that checks if graph is connected. So it is enough to be a tree if graph is connected and |E|=|V|-1?
You are correct, E = V - 1 is sufficient to check that your graph is a tree.
The logic is that every tree begins with just a root note (V=1, E=0, so E=V-1), and from there, any time we add one node (V=V+1), we must also add exactly one edge (E=E+1). This makes the equation E=V-1 remain true for all trees.
A cycle occurs when we connect two existing nodes with a new edge (E=E+1 but V stays the same), rendering the equation E=V-1 false.
If it interests you, you may want to read about the more general formula v - e + f = 2, where f is the number of regions inside a graph, including the exterior region. (A tree only has an exterior region so f=1). This rule is called Euler's Formula, which you can read about on Wikipedia.
Connected: It means that for every pair of vertices you choose, there will always be a path between them.
|E|=|V|-1: if your graph has |V| vertices and you are given |E|=|V|-1 edges to connect them, then if you form a cycle, you won't be able to form a connected graph (some vertices will remain without edges). We can conclude that these conditions are enough.
How would I convert a simple directed graph to a simple undirected one?
Is this possible?
Assuming: (from here)
in a simple digraph loops are disallowed. (A loop is an arc that pairs a vertex to itself.)
and: (from here)
a simple [undirected] graph is an undirected graph that has no loops (edges connected at both ends to the same vertex) and no more than one edge between any two different vertices.
I'm assuming the edges are unweighted, otherwise this can't be done without specifying how this needs to be done.
AM = Adjacency matrix
AL = Adjacency list
Remove the direction of all edges.
AM: For each edge A->B, row A, column B would already be set. Also set row B, column A.
AL: For each edge A->B, the edge already appears in A's AL, also add the edge to B's AL.
Go through all edges, removing all edges for which we already found an edge between the two vertices that are the end-points of that edge (so if A-B has already been processed, if we find another A-B (or equivalently B-A), we must remove that edge). This needs to be done since there can be multiple edges between vertices in a simple directed graph, but this can't happen in a simple undirected graph.
AM: Nothing really needs to be done since an AM forces there to only be a single edge between vertices, so we'd just be overwriting the edges.
AL: For each vertex A, for each edge A-B or B-A in its adjacency list, remove all other edges A-B or B-A in the list.
Source and details...
For each directed edge e=(x,y), add new vertices ve1,…,ve5 and replace e with the edges xve1, ve1ve2, ve1ve3, ve3ve4, ve4ve5, ve3y.
To decode, every leaf (degree-1 vertex) whose neighbour has degree 2 must be ve5 for some edge e=(x,y); its neighbour is ve4 and the other neighbour of ve4 is ve3. ve3 has a unique neighbour that has both degree 3 and is adjacent to a leaf: the neighbour is ve1 and its leaf is ve2 (if ve1 has two leaf neighbours, pick one arbitrarily to be ve2). The other neighbour of ve1 is x and the other neighbour of ve3 is y. Restore the directed edge (x,y) and delete the vertices ve1,…,ve5.
G=(V,E) is a directed graph. (a,b) \in E is a directed edge.
G' = (V, E') is an equivalent undirected graph where (a,b) is transformed as (a,b) and (b,a).
Thus, edge directed edge becomes two edge (one in each direction).
I have the following problem on my homework:
Give an O(n+m) algorithm to find that whether an edge e would be a part of the MST of a graph
(We are allowed to get help from others on this assignment, so this isn't cheating.)
I think that I could do a BFS and find if this edge is a edge between two layers and if so whether this edge was the smallest across those layers. But what could I say when this edge is not a tree edge of the BFS tree?
As a hint, if an edge is not the heaviest edge in any cycle that contains it, there is some MST that contains that edge. To see this, consider any MST. If the MST already contains the edge, great! We're done. If not, then add the edge into the MST. This creates a cycle in the graph. Now, find the heaviest edge in that cycle and remove it from the graph. Everything is now still connected (because if two nodes used to be connected by a path that went across that edge, now they can be connected by just going around the cycle the other way). Moreover, since the cost of the edge was deleted wasn't any smaller than the cost of the edge in question (because the edge isn't the heaviest edge in the cycle), the cost of this tree can't be any greater than before. Since we started with an MST, we must therefore end with an MST.
Using this property, see if you can find whether the edge is the heaviest edge on any cycle in linear time.
We will solve this using MST cycle property, which says that, "For any cycle C in the graph, if the weight of an edge e of C is larger than the weights of all other edges of C, then this edge cannot belong to an MST."
Now, run the following O(E+V) algorithm to test if the edge E connecting vertices u and v will be a part of some MST or not.
Step 1
Run dfs from one of the end-points(either u or v) of the edge E considering only those edges that have weight less than that of E.
Step 2
Case 1
If at the end of this dfs, the vertices u and v get connected, then edge E cannot be a part of some MST. This is because in this case there definitely exists a cycle in the graph with the edge E having the maximum weight and it cannot be a part of the MST(from the cycle property).
Case 2
But if at the end of the dfs u and v stay disconnected, then edge E must be the part of some MST as in this case E is never the maximum weight edge of the cycles that it is a part of.
Find if there are any paths that are cheaper than the current one (u,v) that creates a cycle to u and v. If yes, then (u,v) is not on the mst. Otherwise it is. This can be proved by the cut property and the cycle property.