×

# EDGEDIR - Editorial(Unofficial)

Contest

## PREREQUISITES:

Graph theory, Basic graph algorithms

Difficulty: Easy-medium

## PROBLEM:

Given an undirected(connected) graph with N vertices and M edges, direct the edges(make a directed graph consisting of the same edges directed in either forward or backward direction) in such a manner that indegree of all the vertices is even or print that it is not possible.

## EXPLANATION:

Given N, we know that there are N vertices. So we take N disjoint sets, and while taking the edges as input, we check using union-find algorithm whether inclusion of this particular edge causes the formation of cycle. If yes, we'll not include this edge in our tree for now. We'll store every such edge in a different list, say L.

After the above step, we now have one spanning tree of the given graph. Now, perform Depth First Search on any node(since it is a connected graph) and orient the edges in the direction of DFS. This operation gives us a Directed Acyclic Graph(DAG).

Now, insert the extra edges from list L into the graph obtained after the above step in such a manner that it remains a DAG after inclusion of these extra edges. For that, we need to topologically sort the vertices. Thus, if we have an extra edge between u and v, and in topological ordering, u comes before v, then we direct the edge from u to v, otherwise from v to u. In this way, we have now included all the edges while retaining the properties of the DAG.

Now, traverse each node from the adjacency list and store its parents in a set. [vector<set<ll>> parents(n)]. Also, store the indegree of every vertex in an array, say indegree[].

Now, begin from the node(s) which has no children. If its indegree is not even, reverse the direction of one incoming edge to this node, without caring about its parent. Once its indegree is even, traverse all of its parents and repeat the same procedure for every node.

In this way, we reach the root node of our DAG which has no parents. Here, if its indegree is not even, we can be sure that the given graph cannot be converted to a directed graph in which every vertex has an even indegree. If the indegree of the root is even, then the graph has been converted to the desired graph. Hence we compare its edges with the given graph's edges and print the desired output.

This gives us a linear time complexity to reach the desired output!

1. Make a spanning tree.
2. Perform DFS on this tree and orient edges to obtain a DAG.
3. Topologically sort this DAG and insert the extra edges which were not in the spanning tree while maintaining the DAG property.
4. Backtrack from the leaves up towards the root making indegree of every vertex even.

EDIT: Nima came up with a point which I would like to bring into everyone's notice: If number of edges is odd, the solution is impossible. Else, solution is always possible! Thanks Nima! \/

O(V+E)=O(N+M)

## SOLUTION:

Author

166
accept rate: 100%

1

Performing a DFS will treat it like a tree isn't it ?
Then why don't we do a solution using only DFS :D...
One of my friend did... I think it's a correct approach...

(18 Dec '18, 07:29)
1

i did using simple bfs :)

(18 Dec '18, 11:22)

I too tried bfs method @shivam_g1470 , but my code wasn't working on the sample cases. Maybe I didn't cover all the cases. However, I wasn't convinced with that solution, hence I came up with this intuition and it worked well! ;-))

(18 Dec '18, 14:20)

@l_returns yes we're treating the given graph like a tree. And yes, I went through the solutions of several people and found out that we can do it using DFS only too. Peace!

(18 Dec '18, 14:24)

Okay great :)

(20 Dec '18, 08:50)

 1 Using Simple DFS: Make any directed graph with the given edges. If Nodes having odd indegree are odd, exit [Not possible]. Simple Idea: "Flip all edges between 2 odd indegree vertices so they both would have even indegrees." Start DFS from any node. DFS Mark Visited. Invert ^= Indegree of this vertex v. For all unvisited u, connected to this vertex v, Edge Between v and u ^= Invert Call DFS for vertex u Edge Between v and u ^= Invert This DFS is simple enough to code under 10 Lines neatly. Working: Initialize Invert as 0. Indegree(var) of a vertex is 0 if it's indegree is even, else Indegree(var) is 1. XOR with 1 inverts, else it remains the same. So, if indegree of some vertex is 1, Invert will flip. If Invert is 1 it means we need to flip the edges on our way. If Invert is 0 we don't need to flip. Example: There is one graph whose only 4 vertices has odd indegree. Let's name them 1 to 4. Also, there are two branches from vertex 2, one has vertex 1 and other has vertices 3 and 4. If we start DFS from vertex 1, all edges from 1 to 2 would be flipped and all edges from 3 to 4 would be flipped. Now Let's try DFS from vertex 2. Edges from vertex 2 to vertex 3 would be flipped. Now when it goes to vertex 4, it won't be able to find its pair so until it reaches vertex 1, all the edges would be flipped in the path. So we can see that edge from vertex 2 to vertex 3 gets flipped twice, resulting in flipping only from vertex 1 to vertex 2 and vertex 3 to vertex 4. Solution Link answered 20 Dec '18, 13:33 129●7 accept rate: 27% 1 @black_truce I went through your solution. Time complexity is pretty much similar to that of my solution. But my solution is more complex indeed! Good work!! (21 Dec '18, 12:05)
 1 @pandey_ji in your code while adding the extra edge after the topological sort you have the below code if(pehlekaun[pehla]>pehlekaun[dusra]) adj[dusra].push_back(pehla); else adj[pehla].push_back(pehla); Shouldn't the else condition be adj[dusra].push_back(pehla); Anyhow I ran the code with both the conditions and both seem to give correct answer. Am I missing something or are the test cases week ? answered 21 Dec '18, 00:39 11●1 accept rate: 0% 1 @vikram987 thanks for pointing out the error! That was kind of a severe error! I can't believe how this solution got accepted with such error. The code actually got accepted at once. Maybe the test cases were weak enough because I'm adding self loop there(by mistake though) and it's still passing! Thanks anyway for the correction!! <3 (21 Dec '18, 12:01)
 0 Access Denied on the Solution answered 18 Dec '18, 02:06 3★rds_98 45●6 accept rate: 8% The solutions haven't been made public yet by the host! They won't take much time hopefully. (18 Dec '18, 02:11) can u post using ideone (18 Dec '18, 02:16) rds_983★ Link to my solution on ideone with sample test cases: https://ideone.com/PvaHdF (18 Dec '18, 02:22)
 0 Interesting approach. I did it a bit differently: if num(edges) is odd -> impossible (because you have odd indegrees in total) if num(edges) is even -> you can always do it You can prove (2) with induction and use it to also direct the edges. each step of induction is: find two edges that share a vertex (a-b and b-c) such that removing them doesn't disconnect the graph (note*) direct them as (a->b and b<-c) remove them from the graph and go to step 1. note*: connectivity is in terms of edges. we don't want edges of the graph to be split into two separate components but it is ok to leave vertex "b" disconnected from the graph after removing the two edges. So, we start with a connected graph with even edges. Each step removes two edges and keeps the graph connected. (also note that removing those edges doesn't have any impact on the remaining graph because we don't care about outgoing degrees) So the induction invariant is maintained (connected graph & even edges). The base case is num(edges) = 0, which is when we are done. Now, the main challenge in implementing this is to remove pairs of edges that share a vertex and don't disconnect the graph. To do that, I picked a random vertex as seed, did BFS from that vertex and labeled vertices with their BFS level (imagine turning the graph into an onion with layers). Now, for each vertex in the outermost layer, as long as their degree is greater than 1, remove pairs of adjacent edges, prioritizing edges that have the other end in a farther layer. if you end up with a remaining degree of 1, just leave it. later when you process the other end of the edge (from an inner layer, you'll remove that edge first). my implementation: https://www.codechef.com/viewsolution/21869407 answered 18 Dec '18, 03:52 5★nima101 32●2 accept rate: 0% @nima101 your solution is a better and smarter approach! _/_ (18 Dec '18, 22:57)
 0 @pandey_ji can u explain the intuition behind your approach , I mean why this works!! answered 18 Dec '18, 16:49 1 accept rate: 0% @sudhanshu6324 first I'm converting the graph to a DAG so that there won't be any cycle which would make it tough to update the indegree of a vertex belonging to that cycle. Then, I'm simply updating the indegree of every vertex starting from leaves in bottom-up fashion till the root! That's it! (18 Dec '18, 23:00)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,499
×710
×398
×146
×121
×44
×25
×15

question asked: 18 Dec '18, 01:39

question was seen: 1,729 times

last updated: 21 Dec '18, 17:57