Given a graph G in which every edge connects an even degree node with an odd degree node. How can i prove that the graph is bipartite?
Thanks in advance
This is the Welsh-Powell graph colouring algorithm:
All vertices are sorted according to the decreasing value of their degree in a list V
Colours are ordered in a list C
The first non-coloured vertex v in V is coloured with the first available colour in C. "Available" means a colour that has not previously used by this algorithm
The remaining part of the ordered list V is traversed and the same colour is allocated to every vertex for which no adjacent vertex has the same colour
Steps 3 and 4 are applied iteratively until all the vertices have been coloured
A graph is bipartite if it is 2-colourable. This intuitive fact is proven in Cambridge Tracts in Mathematics 131.
This is, of course, the cannon with which to shoot a mosquito. A graph is bipartite iff its vertices can be divided into two sets, such that every edge connects a vertex from set 1 to one in set 2. You already have such a division: each edge connects a vertex from the set of odd-degree vertices, to a vertex in the set of even-degree vertices.
Because by definition you already have two disjoint sets of vertices such that the only edges go between a vertex in one set and a vertex in the other set.
The even degree nodes are one set, and the odd degree nodes are the other set.
Pick any node, put it in set A. Take all the nodes that link to it, put them in set B. Now for every node added, add all it's neighbors to the opposite set, and check for the ones that already belong to one of the sets that they are in the right set. If you get a contradiction then the graph is not bipartite.
If you run out of neighbors but there are still nodes left, pick again any node and continue the algorithm until you either have no nodes or you found a contradiction.
If by "prove", you mean "find out", the complete solution is here
Bipartite.java
It works by keeping two boolean arrays. A marked array to check if a node has been visited, and another colored array. It then does a depth first search, marking neighbors with alternate colors. If a neighbor is marked with same color, graph is not bipartite.
Related
You're given an non directed graph with integer node weights and edge weights.
A node is "mark-able" if its node weight is non negative, and marking a node will cause all its neighbors node weights to decrease by the edge weight of the edge connecting the two.
If a marked node's node weight goes below 0, it's automatically unmarked (and the decrease of its neighbors edge weights is also undone).
Solve for a largest possible set of marked nodes.
Potentially easier problems:
Limit the edge weights to be positive.
Limit the edge weights to all be 1.
Solve for the size of the solution set instead of the set itself.
Is this problem solvable in polynomial time? What's the best solution?
Actually, this sounds like a generalization of the minimum vertex cover problem, which is known to be NP-complete.
Indeed, consider a graph where weights of vertices are 0 and weights of edges are 1.
For each edge, we can mark at most one of its endpoints: otherwise, both their weights will become negative.
This property also goes backwards, meaning that any set of markings that follows the "single marked endpoint" property is also a solution.
This means that we want to mark the largest possible set of vertices so that each edge is connected to at most one marked vertex.
In turn, this means that we want to find the smallest possible set of unmarked vertices so that each edge is connected to at least one unmarked vertex.
Considering there are no isolated vertices, it sounds like the unmarked vertices are the minimum vertex cover.
I've received a task to find an algorithm which divides a graph G(V,E) into pairs of neighboring edges (colors the graph, such that every pair of neighboring edges has the same color).
I've tried to tackle this problem by drawing out some random graphs and came to a few conclusions:
If a vertex is connected to 2(4,6,8...) vertices of degree 1, these make a pair of edges.
If a vertex of degree 1 is directly connected to a cycle, it doesn't matter which edge of the cycle is paired with the lone edge.
However, I couldn't come up with any other conclusions, so I tried a different approach. I thought about using DFS, finding articulation points and dividing graph into subgraphs with an even number of edges, because those should be dividable by this rule as well, and so on until I end up with only subgraphs of |E(G')| = 2.
Another thing I've come up with is to create a graph G', where E(G) = V(G') and V(G) = E(G'). That way I could get a graph, where I could remove pairs of vertices (former edges) either via DFS or always starting with leaf vertices along with their adjacent vertices.
The last technique is most appealing to me, but it seems to be the slowest one. Any feedback or tips on which of these methods would be the best is much appreciated.
EDIT: In other words, imagine the graph as a layout of a town. Vertices being crossroads, edges being the roads. We want to decorate (sweep, color) each road exactly once, but we can only decorate two connected roads at the same time. I hope this helps for clarification.
For example, having graph G with E={ab,bd,cd,ac,ae,be,bf,fd}, one of possible pair combinations is P={{ab,bf},{ac,cd},{ae,eb},{bd,df}}.
One approach is to construct a new graph G where:
A vertex in G corresponds to an edge in the original graph
An edge in G connects vertices a and b in G where a and b represent edges in the original graph that meet at a vertex in the original graph
Then, if I have understood the original problem correctly, the objective for G is to find the maximum matching, which can be done, for example, with the Blossom algorithm.
I have a directed graph with colored edges (red & blue) that may contain cycles. The question is to write an algorithm given two vertices (s,t) that finds the path with the minimal number of color changes between s and t (if such path exists).
I have found a solution using a variation of Dijkstra (I created a new graph where each vertex correspond to an edge of the previous graph, and contains the color of the edge. For example: if (1,2) is an edge in the old graph, then (1/2) is a vertex in the new one. I connected "adjacent edges" vertices, and edges in the new graph that change color got a weight of 1, where same color transition is 0).
I am looking for a solution in linear time (of V and E). The above one uses VxE edges in the new graph.
Is there such solution to find the minimal path?
First phase: Reduction to the shortest path problem.
For every node i we create two nodes i_red and i_blue.
For every blue edge i->j we create two edges i_red->j_blue with weight 1 and i_blue->j_blue with weight 0.
We handle red edges in similar fashion.
We also need a start node which is connected with start_red and start_blue with connection weight of 0.
Similar to the target node, which is connected with target_red and target_blue with weight 0-connections.
Now, search for the shortest path from newly created start node to the newly created target node. There are twice as many nodes and twice as many edges as in the original graph, so the reduction is linear.
After you reduced the problem to the shortest path search, you could do the following:
Step: use only edges with weight 0, treat the graph as undirected one and with help of bfs you can find all components in this 0-edge-graph in linear time.
Step: run bfs on the graph where the components from the prior step are glued together as super-nodes, so all edges have weight 1 and bfs will find the shortest path.
Obviously all three parts of this algorithm (bfs in 0-edge-graph, glueing the components to super-nodes and the bfs in the resulting graph) run in linear time.
Given a undirected graph. How to find the size of maximum subset of vertices of the graph in which each vertex has at degree atleast p, where the degree in subset is find among the vertices in subset only.
Vertices of degree less than p can never be part of the solution. Remove them entirely, including their edges. Look at the new graph and repeat, etc.
When this process stops, all vertices have degree at least p.
Then, look at the connected components of that graph and pick the largest one! (As Evgeny Kluev correctly points out, this is unnecessary of course. In my head, the remaining subgraph should have been connected, but of course the original problem makes no such demands.)
I have a disconnected bipartite undirected graph. I want to completely disconnect the graph. Only operation that I can perform is to remove a node. Removing a node will automatically delete its edges. Task is to minimize the number of nodes to be removed. Each node in the graph has atmost 4 edges.
By completely disconnecting a graph, I mean that no two nodes should be connected through a link. Basically an empty edge set.
I think, you cannot prove your algorithm is optimal because, in fact, it is not optimal.
To completely disconnect your graph minimizing the number of nodes to be removed, you have to remove all the nodes belonging to the minimal vertex cover of your graph. Searching the minimal vertex cover is usually NP-complete, but for bipartite graphs there is a polynomial-time solution.
Find maximum matching in the graph (probably with Hopcroft–Karp algorithm). Then use König's theorem to get the minimal vertex cover:
Consider a bipartite graph where the vertices are partitioned into left (L) and right (R) sets. Suppose there is a maximum matching which partitions the edges into those used in the matching (E_m) and those not (E_0). Let T consist of all unmatched vertices from L, as well as all vertices reachable from those by going left-to-right along edges from E_0 and right-to-left along edges from E_m. This essentially means that for each unmatched vertex in L, we add into T all vertices that occur in a path alternating between edges from E_0 and E_m.
Then (L \ T) OR (R AND T) is a minimum vertex cover.
Here's a counter-example to your suggested algorithm.
The best solution is to remove both nodes A and B, even though they are different colors.
Since all the edges are from one set to another, find these two sets using say BFS and coloring using 2 colours. Then remove the nodes in smaller set.
Since there are no edges among themselves the rest of the nodes are disconnected as well.
[As a pre-processing step you can leave out nodes with 0 edges first.]
I have thought of an algorithm for it but am not able to prove if its optimal.
My algorithm: On each disconnected subgraph, I run a BFS and color it accordingly. Then I identify the number of nodes colored with each color and take the minimum of the two and store. I repeat the procedure for each subgraph and add up to get the required minimum. Help me prove the algorithm if it's correct.
EDIT: The above algorithm is not optimal. The accepted answer has been verified to be correct.