Graph Theory : Will it stop or not? - algorithm

I cannot find out how to start with this question:
A graph has n vertices and m edges.No two pairs of vertices can be connected by more than one edge.Rahul starts to play a game:
He changes the edges in following manner -
He selects one vertex, and adds an edge from that vertex towards all other vertex where the edges do not exist.
and at the same time, deletes all pre-existing edges from that vertex.
This game will stop only when their exist a direct edge between every two Vertices. You need to determine whether it is possible to finish this game, or whether this will never happen, no matter what moves he make.
Input : Initial state of graph will be given.
Output :"yes" or "no"
Can someone give a hint on how to start??

1) Order of moves doesn't matter (since you can exchange any two subsequent moves to the same result);
2) Two subsequent changes with the same vertex have zero effect;
3) You can get to final state iff. you can get there changing any vertex no more than once;
4) Any two connected vertices must be both changed or both unchanged, of any two vertices not connected exactly one should be changed.
Take a connected component in the graph. Vertices there should all change or all remain unchanged. If the component isn't fully connected, finishing the game is impossible. If there are at least three connected components, finishing the game is impossible. If there are exactly two fully connected components, all vertices in exactly one of them should be changed.
Answer: the game can be finished if and only if the graph either is already fully connected or consists of two fully connected components. (It's easy to see that when the graph consists of two fully connected components, changing a vertex effectively moves it from one component to another.)
Algorithm to check for the answer: suppose we are given a number of vertices n and then a list of edges in form (a,b) where a and b are vertex numbers from [1,n]. Let vertices be an array of records (num_edges, connected, sample), initialized with (0,k,k) (k is vertex number). Then for each edge (A,B):
increase num_edges for A and B by 1;
if A.sample equals B.sample, go to next edge;
exchange A.connected and B.connected, starting from A.connected vertex set sample to A.sample and follow connected until reaching B; got to next edge.
Finally check that all vertices starting from 1 and following connected have the same num_edges equal to (their number - 1) and all remaining vertices for similar loop. Time should be O(max(n log(n), m)), memory O(n).

A solved graph with n vertices will be a complete graph kn with ½n(n-1) edges.
Flipping the state of a vertex will mean that the vertex becomes disconnected from the graph and there are two disconnected complete sub-graphs K1 and K(n-1) which have contain 0 and ½(n-1)(n-2) edges, respectively.
Flipping the state of other vertices will disconnect each of them from the complete sub-graph containing it and connect it to all the vertices of the other complete sub-graph. So, generally, if there are x vertices that have flipped state then there will be a two complete sub-graphs Kx and K(n-x) with ½x(x-1) and ½(n-x)(n-1-x) edges respectively for a total of m = ½n(n-1) - nx +x(x-1) edges.
If we know m and n then we can solve the quadratic equation to find the number of moves x required to solve the problem:
x = ( n - SQRT( 4m + 2n - n² ) ) / 2
If x is a non-integer then the puzzle is not solvable.
If x is an integer then the puzzle may be solvable in exactly x moves but an additional check is needed to see if there is are two disconnected complete sub-graphs Kx and K(n-x).
Algorithm:
Calculate x; if it is not an integer then the graph is not solvable. [Complexity: O(1)]
Pick a random vertex:
its degree should be either (x-1) or (n-x-1); if it is not then the graph is not solvable.
generate a list of all its adjacent vertices. [Complexity: O(n)]
perform a depth-first (or breadth-first) search from that vertex. [Complexity: O(n+m)]
if the number of edges visited is ½x(x-1) or ½(n-x)(n-1-x) (corresponding to the degree of the original vertex) and no vertices are visited that were not adjacent to the original then the sub-graph is complete and you know the graph is solvable; if either condition is not true then the graph is not solvable.
To be certain you could do the same for the other sub-graph but it is unnecessary.
Examples:
The graph where n=4,m=2 with edges (1,2) and (3,4) is solvable since x = ( 4 - SQRT( 0 ) ) / 2 = 2, an integer, and there are two K2 disconnected sub-graphs.
The graph where n=4,m=3 with edges (1,2), (2,3), (3,4) has x = ( 4 - SQRT( 4 ) ) / 2 = 1, an integer, but there is only one, connected non-complete graph when there should be two disconnected K1 and K3 sub-graphs.

Related

How to split an undirected graph into minimum number of subpaths?

I need to split an undirected graph into minimum number of subpaths (vertices can be repeated, but not edges).
Is there any algorithm? How can it be implemented?
For example, for the given graph below, it could be:
BACI, CDEGFC
or BACDEGFC, CI
Let's say your graph is connected and has k vertices of odd degree. Then the answer is the max of k/2 and 1. If your graph isn't connected then the same applies to each connected component.
Let's first take the case where there are no vertices of odd degree. Start at any vertex, and keep proceeding to an arbitrary neighbor (along an unused edge) until you get back to the starting vertex. You know you can leave any vertex you can reach because they all have even degree.
If this uses up all edges then you're done. If not, pick a vertex adjacent to an unused edge and repeat. Keep doing this. When done you'll have a number of cycles, say r, and each one will share at least one vertex with another cycle. Next, merge adjacent cycles in the natural way. E.g. in a very simple case we have two triangles that share a vertex: abc and cde, and we finish our procedure with these two small cycles: abca and cdec. We merge these where the meet: abcdeca
a---b
\ /
c
/ \
d---e
Now, the issue with vertices of odd degree is that any trail will use up 2 edges adjacent to each vertex in it except the start and end (if different) where it only uses 1. That means that if there are odd vertices, there will be a separate trail for each pair of the same. You can apply the same procedure as before, but always start at an odd-degree vertex. You'll be forced to end at an odd-degree vertex. Merge where you can, but you'll always end up with one trail per pair of odd vertices.

Minimum Hop Count in Directed Graph based on Conditional Statement

A directed graph G is given with Vertices V and Edges E, representing train stations and unidirectional train routes respectively.
Trains of different train numbers move in between pairs of Vertices in a single direction.
Vertices of G are connected with one another through trains with allotted train numbers.
A hop is defined when a passenger needs to shift trains while moving through the graph. The passenger needs to shift trains only if the train-number changes.
Given two Vertices V1 and V2, how would one go about calculating the minimum number of hops needed to reach V2 starting from V1?
In the above example, the minimum number of hops between Vertices 0 and 3 is 1.
There are two paths from 0 to 3, these are
0 -> 1 -> 2 -> 7-> 3
Hop Count 4
Hop Count is 4 as the passenger has to shift from Train A to B then C and B again.
and
0 -> 5 -> 6 -> 8 -> 7 -> 3
Hop Count 1
Hop Count is 1 as the passenger needs only one train route, B to get from Vertices 0 to 3
Thus the minimum hop count is 1.
Input Examples
Input Graph Creation
Input To be solved
Output Example
Output - Solved with Hop Counts
0 in the Hop Count column implies that the destination can't be reached
Assuming number of different trainIDs is relatively small (like 4 in your example), then I suggest using layered graph approach.
Let number of vertices be N, number of edges M, and number of different trainIDs K.
Let's divide our graph to K graphs. (graphA, graphB, ...)
graphA contains only edges labeled with A, and so on.
Weight of each edge in each of the graphs is 0.
Now create edges between these graphs.
Edge between graphs is representing a 'hop'
grapha[i] connects to graphB[i], graphC[i], ...
Each of these edges has weight 1.
Now for every graph run Dijkstra's shortest path algorithm from V1 in that graph, and read results from V2 in all graphs, take minimal value.
This way minimum of results for running dijkstra's for every graph will be your minimum number of hops.
Memory complexity is O(K*(N+M))
And time complexity is O(K*(((2^K)*N+M)*log(KV)))
(2^K)*N comes from fact that for every 1<=i<=N, vertices graphA[i],graphB[i],... must be connected to each other, and this gives 2^K connections for every i, and (2^K)*N connections in total.
For cases where K is relatively small, like 4 in your example, but N and M are quite big, this algorithm works like a charm. It isn't suitable for situation where K is big though.
I'm not sure if that's clear. Tell me if you need more detailed explanation.
EDIT:
Hope this makes my algorithm more clear.
Black edges have weight 0, and red edges have weight 1.
By using layered graph approach, we translated our special graph into plain weighted graph, so we can just run Dijkstra's algorithm on it.
Sorry for ugly image.
EDIT:
Since max K = 10, we would like to remove 2^K from our time complexity. I believe this can be done by making edges that represent possible hops virtual, instead of physically storing them on adjacency list.

Impossible DFS of Directed graph question

Consider the following directed graph. For a given n, the vertices of the graph correspond to the
integers 1 through n. There is a directed edge from vertex i to vertex j if i divides j.
Draw the graph for n = 12. Perform a DFS of the above graph with n = 12. Record the discovery
and finish times of each vertex according to your DFS and classify all the edges of the graph into tree, back, forward, and cross edges. You can pick any start vertex (vertices) and any order of visiting the vertices.
I do not see how it is possible to traverse this graph because of the included stipulations. It is not possible to get a back edge because a dividing a smaller number by a larger number does not produce an integer and will never be valid.
Say we go by this logic and create a directed graph with the given instructions. Vertex 1 is able to travel to vertex 2, because 2 / 1 is a whole number. However, it is impossible to get to vertex 3 as vertex 2 can only travel to vertex 4, 6, 8, or 10. Since you cannot divide by a bigger number it will never be possible to visit a lower vertex once taking one of these paths and therefore not possible to reach vertex 3.
Your assumption about the back tracks is correct. You cannot have back tracks because you don't have any circles. Consider the following example:
When we start at 2 we will find the edges 2->4, 4->8, 4->12, 2->6 and 2->10. The edges 2->8 and 2->12 are forward edges, they are like shortcuts to get forward much faster. The edge 6->12 is a cross edge because you are switching from one branch to another branch. And since we have no circles to somehow get back to a previous node, we don't have any backward edges.

Write Algorithm for changing non-directional graph to directional

I need help to solve this question :
you have undirected graph G=(V,E) you want to write an algorithm to adjust all
One of the edges, so that in the directed graph obtained, he number of incoming edges into the node be always greater than zero.
for all edge {u,v} ∈ E you should chose one direction (u,v) or (v,u).
When the answer is positive, the algorithm must return the intention of the edges - which fulfills the requirement
As was pointed out, this problem clearly does not always have a solution. Since every vertex must have at least one incoming edge, if we have E < V, the problem is impossible.
So, let's assume that we have E >= V. Here is one way to approach the algorithm: First, count the number of edges attached to each vertex in O(E) time. Note, my solution assumes appropriate storage like an adjacency list. Can you see why an adjacency matrix would have a worse complexity?
Second, we will make a binary minheap of the vertices, according to their corresponding edge count, in O(V). Some intuition: if we have a vertex with only one edge, we must convert that into an incoming edge! When we assign the direction of that edge, we need to update the edge count of the vertex on the other side. If that other vertex goes from 2 edges to 1, we now are forced to assign the direction of its one edge left. Visually:
1 - 2 - 1
Arbitrarily choose a 1 edge count to make directed
1 - 2 -> 0
2 just lost an edge, so update to 1!
1 - 1 -> 0
Since it only has 1 edge now, convert its edge to be incoming!
0 -> 0 -> 0
Obviously this graph doesn't work since V > E, but you get the idea.
So, V times, we extract the minimum from the heap and fix in O(logV). Each time, decrement the edge count of the neighbor. Assuming the adjacency list, we can find a neighbor (first element) and update the count in O(1), and we can fix the heap again in O(logV). Overall, this step takes O(V logV).
Note, if all of our remaining vertices have more than one possible edge, this approach will arbitrarily select one of the vertices with a smallest edge count and arbitrarily select one of its edges. I will let you think about why this works (or try to provide a counter-example if you think it doesn't!) Finally, if E > V, then when we finish, there may be extra unnecessary edges left over. In O(E) time, we can arbitrarily assign directions to those edges.
Overall, we are looking at V + V*logV + E aka O(E + VlogV). Hope this helps!

unique maximum matching

I am trying to use bipartite graph to code my program with following properties:
in each side, there is N vertices
the graph is connected
Now, I wanna add a condition in my code which check if the number of edges is bigger than M, do not allow user to more activities(in a simple sentence print something in that condition) where M is maximum number edges such that it still has a unique maximum matching.
The question is how can I find M?
Any idea will be appreciated
Thanks
if you mean to find maximum m such that there is at least one graph with n vertices and m edges with a unique maximum matching, the answer is (n + 1) * n / 2.
to show that there is at least one graph with this number of edge, consider a graph with vertices x1, x2, .., xn in one part and vertices y1, .. yn in another part. draw an edge between vertex xi and yj iff (i <= j).
to show there can be no more edges, use induction on the number of vertices. first of all we can show if every vertex in the graph is connected to at least two vertex, the graph has at least two different maximum matching. (consider one maximum matching, follow a path from a vertex whose edges alternates between matching edges and non-matching edges, make a circle and reverse all the edges.)
so we know there is one vertex with degree equal to one. remove this vertex and it's neighbor and use induction on the remaining graph.
sorry for bad English.

Resources