Two BFS produce the same set of edges - algorithm

Under what kind of constraints, will two BFS (could start from different vertex) on a simple undirected graph produce the same set of edges?

If the graph is a minimum spanning tree, then it will produce all the edges in the breadth-first search which can start from any vertex.
Reason:
In BFS, all nodes are going to be visited.
The minimum spanning tree contains a minimal number of edges to connect all the nodes. So, the BFS traversal will traverse all the edges in order to visit all the nodes.
So, all the edges of the graph are in the set. That means if you start BFS from any vertex, all the edges are going to be included in the set. Thus, the same set of edges for any node.

Say you run BFS on graph G with starting vertex v (BFS(G, v)) and there is some edge e = (u,w) that isn't traversed. Running BFS(G, u) guarantees that (u,w) is traversed. Thus BFS will only produce a unique set of edges when it produces all edges. I.e. in acyclic graphs.

Related

Find minimum spanning tree of undirected weighed graph with root node having exactly k edges

Having an undirected graph with v vertices and e edges with each edges having a non-negative weight of at most w, the task is to find(if possible!) a subset of edges that connects all vertices with minimum cost. Also this graph has a root node which in the sub-graph should only be directly connected to exactly k other vertices(so it most have exactly k edges connected to it in the sub-graph). The algorithm should work in O(e⋅log(v)⋅log(w)) time.
So after some thinking I thought that we need to find a minimum spanning tree since it gives us the Minimum-cost subgraph which takes O(e.log(v)) time.However for insuring that the root node has exactly k edges in the resulting tree I couldn't come up with any working idea.
I think that maybe changing the weights of k edges that are connect to the root may enforce their selection,since in the Kruskal's algorithm we first sort the edges, if we pick exactly k edges that are already connected to the edge root in the graph and change their weights to some minimum amount then in the tree construction phase, chances are that this k edges make it to the MST. There are still problems here:
K edges should be picked so that it would be possible for them to make it to an MST.

Does a DFS produce a MST for an unweighted directed graph?

I'm getting confused from reading online posts. I know that a BFS traversal on an unweighted directed graph will produce a minimum spanning tree and shortest path. Can a DFS traversal on an unweighted directed graph do the same?
Yes, Breadth-First and Depth-First both yield spanning trees. It doesn't make much sense to discuss "minimum spanning tree" for an unweighted graph, because all spanning trees on a given graph with n vertices have the same number of vertices (n) and the same number of edges (n-1).
No, Depth-First does not guarantee shortest path. You really need Breadth-First for that. Consider a cyclic graph:
a - h - g - f
| |
b - c - d - e
Starting from vertex a, there are two possible results to depth-first search: a->b->c->d->e->f->g->h, and a->h->g->f->e->d->c->b. The former returns a very long path from a to h, and the latter returns a very long path from a to b.
Note that in this example, the graph is undirected. But undirected graphs are a special case of directed graphs; you can replace every undirected edge by two directed edges in opposing directions.

Determining whether or not a directed or undirected graph is a tree

I would like to know of a fast algorithm to determine if a directed or undirected graph is a tree.
This post seems to deal with it, but it is not very clear; according to this link, if the graph is acyclic, then it is a tree. But if you consider the directed and undirected graphs below: in my opinion, only graphs 1 and 4 are trees. I suppose 3 is neither cyclic, nor a tree.
What needs to be checked to see if a directed or undirected graph is a tree or not, in an efficient way? And taking it one step ahead: if a tree exists then is it a binary tree or not?
For a directed graph:
Find the vertex with no incoming edges (if there is more than one or no such vertex, fail).
Do a breadth-first or depth-first search from that vertex. If you encounter an already visited vertex, it's not a tree.
If you're done and there are unexplored vertices, it's not a tree - the graph is not connected.
Otherwise, it's a tree.
To check for a binary tree, additionally check if each vertex has at most 2 outgoing edges.
For an undirected graph:
Check for a cycle with a simple depth-first search (starting from any vertex) - "If an unexplored edge leads to a node visited before, then the graph contains a cycle." If there's a cycle, it's not a tree.
If the above process leaves some vertices unexplored, it's not a tree, because it's not connected.
Otherwise, it's a tree.
To check for a binary tree, if the graph has more than one vertex, additionally check that all vertices have 1-3 edges (1 to the parent and 2 to the children).
Checking for the root, i.e. whether one vertex contains 1-2 edges, is not necessary as there has to be vertices with 1-2 edges in an acyclic connected undirected graph.
Note that identifying the root is not generically possible (it may be possible in special cases) as, in many undirected graphs, more than one of the nodes can be made the root if we were to make it a binary tree.
If an undirected given graph is a tree:
the graph is connected
the number of edges equals the number of nodes - 1.
An undirected graph is a tree when the following two conditions are true:
The graph is a connected graph.
The graph does not have a cycle.
A directed graph is a tree when the following three conditions are true:
The graph is a connected graph.
The graph does not have a cycle.
Each node except root should have exactly one parent.

Visiting edges, vertices in an undirected graph

Problem: You have an Undirected graph G = (V, E) (V = vertices, E = edges) and you must visit each vertex and pass each edge in both directions.
The only algorithms I know for graphs are DFS, BFS, and a few MST's (Kruskal, etc.) My friend and I were discussing this problem, if it were directed I would simply DFS, and then DFS the transpose but the graph is unfortunately undirected. My friend proposed we perform an MST and DFS the MST and then find the remaining edges by iterating through those that arent in MST. I sort of see what he means but I am not sure this is a good approach? opinions? Also, how would I be able to pass by an edge in both directions if it is undirected?
It doesn't matter if the graph is directed or undirected. You could just replace every undirected edge with two directed edges and perform whatever algorithm you have for a directed graph. Both DFS and BFS will traverse the whole vertices and edges.
I think what you are looking for is called Graph Traversal. BFS and DFS are two graph traversal algorithms and they do not require the graph to be directed. MST on the other hand, is not a graph traversal algorithm.

How to find maximum spanning tree?

Does the opposite of Kruskal's algorithm for minimum spanning tree work for it? I mean, choosing the max weight (edge) every step?
Any other idea to find maximum spanning tree?
Yes, it does.
One method for computing the maximum weight spanning tree of a network G –
due to Kruskal – can be summarized as follows.
Sort the edges of G into decreasing order by weight. Let T be the set of edges comprising the maximum weight spanning tree. Set T = ∅.
Add the first edge to T.
Add the next edge to T if and only if it does not form a cycle in T. If
there are no remaining edges exit and report G to be disconnected.
If T has n−1 edges (where n is the number of vertices in G) stop and
output T . Otherwise go to step 3.
Source: https://web.archive.org/web/20141114045919/http://www.stats.ox.ac.uk/~konis/Rcourse/exercise1.pdf.
From Maximum Spanning Tree at Wolfram MathWorld:
"A maximum spanning tree is a spanning tree of a weighted graph having maximum weight. It can be computed by negating the weights for each edge and applying Kruskal's algorithm (Pemmaraju and Skiena, 2003, p. 336)."
If you invert the weight on every edge and minimize, do you get the maximum spanning tree? If that works you can use the same algorithm. Zero weights will be a problem, of course.
Although this thread is too old, I have another approach for finding the maximum spanning tree (MST) in a graph G=(V,E)
We can apply some sort Prim's algorithm for finding the MST. For that I have to define Cut Property for the maximum weighted edge.
Cut property: Let say at any point we have a set S which contains the vertices that are in MST( for now assume it is calculated somehow ). Now consider the set S/V ( vertices not in MST ):
Claim: The edge from S to S/V which has the maximum weight will always be in every MST.
Proof: Let's say that at a point when we are adding the vertices to our set S the maximum weighted edge from S to S/V is e=(u,v) where u is in S and v is in S/V. Now consider an MST which does not contain e. Add the edge e to the MST. It will create a cycle in the original MST. Traverse the cycle and find the vertices u' in S and v' in S/V such that u' is the last vertex in S after which we enter S/V and v' is the first vertex in S/V on the path in cycle from u to v.
Remove the edge e'=(u',v') and the resultant graph is still connected but the weight of e is greater than e' [ as e is the maximum weighted edge from S to S/V at this point] so this results in an MST which has sum of weights greater than original MST. So this is a contradiction. This means that edge e must be in every MST.
Algorithm to find MST:
Start from S={s} //s is the start vertex
while S does not contain all vertices
do
{
for each vertex s in S
add a vertex v from S/V such that weight of edge e=(s,v) is maximum
}
end while
Implementation:
we can implement using Max Heap/Priority Queue where the key is the maximum weight of the edge from a vertex in S to a vertex in S/V and value is the vertex itself. Adding a vertex in S is equal to Extract_Max from the Heap and at every Extract_Max change the key of the vertices adjacent to the vertex just added.
So it takes m Change_Key operations and n Extract_Max operations.
Extract_Min and Change_Key both can be implemented in O(log n). n is the number of vertices.
So This takes O(m log n) time. m is the number of edges in the graph.
Let me provide an improvement algorithm:
first construct an arbitrary tree (using BFS or DFS)
then pick an edge outside the tree, add to the tree, it will form a cycle, drop the smallest weight edge in the cycle.
continue doing this util all the rest edges are considered
Thus, we'll get the maximum spanning tree.
This tree satisfies any edge outside the tree, if added will form a cycle and the edge outside <= any edge weights in the cycle
In fact, this is a necessary and sufficient condition for a spanning tree to be maximum spanning tree.
Pf.
Necessary: It's obvious that this is necessary, or we could swap edge to make a tree with a larger sum of edge weights.
Sufficient: Suppose tree T1 satisfies this condition, and T2 is the maximum spanning tree.
Then for the edges T1 ∪ T2, there're T1-only edges, T2-only edges, T1 ∩ T2 edges, if we add a T1-only edge(x1, xk) to T2, we know it will form a cycle, and we claim, in this cycle there must exist one T2-only edge that has the same edge weights as (x1, xk). Then we can exchange these edges will produce a tree with one more edge in common with T2 and has the same sum of edge weights, repeating doing this we'll get T2. so T1 is also a maximum spanning tree.
Prove the claim:
suppose it's not true, in the cycle we must have a T2-only edge since T1 is a tree. If none of the T2-only edges has a value equal to that of (x1, xk), then each of T2-only edges makes a loop with tree T1, then T1 has a loop leads to a contradiction.
This algorithm taken from UTD professor R. Chandrasekaran's notes. You can refer here: Single Commodity Multi-terminal Flows
Negate the weight of original graph and compute minimum spanning tree on the negated graph will give the right answer. Here is why: For the same spanning tree in both graphs, the weighted sum of one graph is the negation of the other. So the minimum spanning tree of the negated graph should give the maximum spanning tree of the original one.
Only reversing the sorting order, and choosing a heavy edge in a vertex cut does not guarantee a Maximum Spanning Forest (Kruskal's algorithm generates forest, not tree). In case all edges have negative weights, the Max Spanning Forest obtained from reverse of kruskal, would still be a negative weight path. However the ideal answer is a forest of disconnected vertices. i.e. a forest of |V| singleton trees, or |V| components having total weight of 0 (not the least negative).
Change the weight in a reserved order(You can achieve this by taking a negative weight value and add a large number, whose purpose is to ensure non-negative) Then run your family geedy-based algorithm on the minimum spanning tree.

Resources