linear time algorithm for mst given |E|=99+|V| - algorithm

I need to make a linear algorithm for finding minimal spanning tree given an undirected weighted and connected graph(with no isolated verities) the has |V| vertices and |V|+99 edges
I think the solution should be based on Kruskal and divide an concur but no luck till now, any ideas?

First remove all vertexes of degree 1, and add their adjacent edges to the MST until there are no more vertexes of degree 1.
Then consider each chain of degree-2 vertices. If a chain is a cycle, then add all but the most expensive edge to the MST.
Otherwise, the chain must run between two vertexes of degree >= 3. Replace the chain with a single edge. The cost of this edge will be the cost of the most expensive edge in the chain. That is the difference in cost between connecting the two ends via the chain instead of just connecting the vertexes to the ends.
Now you're left with vertexes of degree >=3. There must be less than 200 of those, and less than 300 edges remaining. Run Kruskal's to determine which edges go in the MST.

Related

Convert undirected graph to directed graph such that the indegree of each vertex is at least 2

For an undirected graph G if possible I want to convert G to a directed graph such that in the directed graph each vertex must have an indegree of at least 2.
I worked out that for it to be possible the amount of edges will need be at least 2|V| therefore will only be possible from at least 5 vertexs.
I also worked out that each vertex will be in a cycle with at least two other vertex,
but I tried using a modification of N/F but can't seem to think of an algorithm that can actually do the conversion (if possible).
Any help / guidance will be appreciated
Given an undirected, unweighted graph with n vertices and m edges, you can decide whether an orientation with minimum indegree at least 2 exists (and find it, if so) in O(m^3) time. If there are no vertices of degree 3, you can sometimes do this fairly simply in O(m) time.
If v is a vertex of degree 1, the task is impossible. If v has degree 2, both its edges must be out-edges, so we can delete 'v' from our graph for now. We do this operation repeatedly based on the new degrees; it's possible that we end up with new degree-3 vertices, but we can now assume all vertices have degree >= 3.
If all vertices are degree 4 or greater, the orientation must exist, and you can use a standard Eulerian circuit algorithm (available in many standard graph libraries) as described in this post. You may need to modify the graph first: Eulerian circuits only exist if all vertex degrees are even. There are always an even number of odd degree vertices: match them up in pairs in any way, adding an edge for each pair. Orienting the edges on the original graph based on the circuit gives a solution for your problem.
For graphs with degree-3 vertices, you can combine a standard algorithm with a nonstandard one. The standard algorithm is maximum-cardinality matching for general graphs, usually the 'Blossom algorithm', which can be found in standard graph libraries. The non-standard (i.e., probably not found in your graph library) algorithm is found in the paper On finding orientations with the fewest number of vertices with small out-degree, which reduces the problem of 'Find an orientation with the fewest outdegree-less-than-2 vertices' to the problem of finding a maximum matching in a graph with O(m) vertices. The algorithm is quite short, and simpler since our graph has minimum degree 3. This solves your problem for fewest-indegrees-less-than-2 after you flip all edge directions.

Algorithm for Finding Graph Connectivity

I'm tackling an interesting question in programming. It is this: we keep adding undirected edges to a graph, until the graph (or subgraph) is connected (i.e. we can use some path to get from each vertex to any other vertex in that subgraph). We stop as soon as the graph is connected.
For example if we have vertices 1,2,3 and 4 and we want the subgraph 1,2,3 to be connected.
Let's say we have edges (3,4), then (2,3), then (1,4), then (1,3). We only need to add in the first 3 edges for the subgraph to be connected, then we stop (edge 1,3 isn't needed).
Obviously I can run a BFS every time an edge is added to see if we can reach the required vertices, but if there are say m edges then we would potentially have to run BFS m times which seems too slow. Any better options? Thanks.
You should research the marvelous "Disjoint-set data structure" and the corresponding union - find algorithm. It can seem magical, but the worst case time and space complexity are tiny, O(α(n)) and O(n) respectively, where α is the inverse Ackerman function.
You can run just one time the BFS to find connected components. Then, each time you add an edge, if it is between vertices of two different components, you can merge them by a reference. So, the complexity of this algorithm is |V| + |E|.
Notice that the implementation of this method should be done by some reference techniques, especially to update the component number of the vertices.
I would normally do this using a disjoint set structure, as Doug suggests. It would be like Kruskal's algorithm for finding the minimum spanning tree, except you process edges in the given order.
If you don't need a spanning tree as output, though, then you can do this with an incremental BFS or DFS:
Pick any vertex, and find the vertices connected to it with BFS or DFS. Color these vertices red. If you start with no edges, of course, then there will be only one red vertex at this stage.
As you add edges, don't do anything else until you add an edge that connects a red vertex to a non-red vertex. Then run BFS or DFS, excluding the new edge, to find all the new vertices that will connect to the red set. Color them all red.
Stop when all vertices are red.
This is a little simpler in practice than using disjoint set, and takes O(|V|+|E|) time, since each vertex will be traversed by exactly one BFS/DFS search.
It does the work in chunks, though, so if you need each edge test to be fast individually, then disjoint set is better.

Necessary to MST edges in a graph

Given a G(V,E) weighted(on edges) graph i need to find the number of edges that belong in every MST as well as the number of the edges that belong in at least one but not all and the ones that belong in none.
The graph is given as input in the following form(example):
3 3
1 2 1
1 3 1
2 3 2
First 3 is the number of nodes,second 3 is the number of edges.The following three lines are the edges where first number is where they start,second is where they end and third is the value.
I've thought about running kruskal once to find an MST and then for each edge belonging in G check in(linear?) time if it can be replace an edge in this MST without altering it's overall weight.If it can't it belongs in none.If it can it belongs in one but not all.I can also mark the edges in the first MST(maybe with a second value 1 or 0)and in the end check how many of them could not be replaced.These are the ones belonging in every possible MST. This algorithm is probably O(V^2) and i am not quite sure how to write it in C++.My questions are,could we somehow reduce it's complexity? If i add an edge to the MST how can i check(and implement in C++) if the cycle formed contains an edge that has less weight?
You can do this by adding some extra work to Kruskal's algorithm.
In Kruskal's algorithm, edges with the same weight can be in any order, and the order in which they are actually checked determines which MST you get out of all possible MSTs. For every MST, there is a sort-consistent order of the edges that will produce that tree.
No matter what sort-consistent order is used, the state of the union-find structure will be the same between weights.
If there is only one edge of a specific weight, then it is in every MST if Kruskal's algorithm selects it, because it would be selected with any sort-consistent edge order, and otherwise it is in no MSTs.
If there are multiple edges with the same weight, and Kruskal's algorithm would select at least one of them, then you can pause Kruskal's at that point and make a new (small) graph with just those edges that connect different sets, using the sets that they connect as vertices.
All of those edges are in at least one MST, because Kruskal's might pick them first. Any of those edges that are bridges in the new graph are in every MST, because Kruskal's would select them no matter what. See: https://en.wikipedia.org/wiki/Bridge_(graph_theory)
So, modify Kruskal's algorithm as follows:
When Kruskal's would select an edge, before doing the union, gather and process ALL the non-redundant edges with the same weight.
Make a small graph with those edges using the find() sets they connect as vertices
Use Tarjan's algorithm or equivalent (see the link) to find all the bridges in the graph. Those edges are in ALL MSTs.
The remaining edges in the small graph are in some, but not all, MSTs.
perform all the unions for edges in the small graph and move on to the next weight.
Since bridge-finding can be done in linear time, and every edge is in at most one small graph, the whole algorithm is still O(|V| + |E| log |E|).

How can we prove there will be at least a vertex with in-degree of zero in Directed Acyclic graph?

I can see the hypothesis true intuitively but, mathematically I am not able to prove. Any help would be appreciated.
Let this graph have n vertices.
Suppose we reverse all the edges, then we are trying to prove there is a vertex with an out-degree of zero.
If not, then simply start anywhere and travel along n edges (always possible as every vertex as non-zero out-degree). Therefore we have visited n+1 vertices - so at least 2 of them must be the same (pigeon hole principle), and therefore we have found a cycle in your acyclic graph.

Graph Algorithms

What is the maximum and minimum number of edges to be considered in krushkal's algorithm with an example for both cases.
What I thought was since the Krushkal's algorithm is for finding minimum spanning tree the maximum number of edges is (V-1) where V is the number of vertices. Adding one more edge would result in a cycle in the graph. How can we obtain at a minimum value ?
Kruskal's algorithm stops when you've added V - 1 edges to your MST, so this is the minimum that have to be considered. This happens when the lowest value V - 1 edges of your graph do not form a cycle, and they will be added one after the other by the algorithm, after which it will stop.
For example, consider a complete graph with edges with cost 1, which is minimum in the graph, between node 1 and every other node. Make all the other edges have cost 2.
The worst case is when you must inspect every edge (of which there are O(V^2)) until you finally select V - 1. This means that you have to force a lot of cycles to be created before the last edge is added.
Consider a complete graph again. Have the V - 2 edges between node 1 and V - 2 nodes have cost 1, which is minimum in the graph. These will be selected first. Now let node k be the one that is not part of a selected edge, so that is left out of the graph. Have the edge between node k and node 1 have the largest cost. This will cause it to be inspected and added to the MST last, forcing the algorithm to inspect all O(V^2) edges before building the MST.
Remember the Kruskal's algorithm processes edges in increasing order of their cost, rejecting edges that would form a cycle if added to the MST we are building.
A tree of N vertices always has N-1 edges. Consequently you have to consider at least N-1 edges during Kruskal's algorithm. An example may be a graph which is a tree.

Resources