![]() |
VOOZH | about |
Given a undirected graph of n nodes and m edges. The task is to find minimum edges required to make Euler Circuit in the given graph. Examples:
Input : n = 3,
m = 2
Edges[] = {{1, 2}, {2, 3}}
Output : 1By connecting 1 to 3, we can create a Euler Circuit.
For a Euler Circuit to exist in the graph we require that every node should have even degree because then there exists an edge that can be used to exit the node after entering it. Now, there can be two cases:
1. There is one connected component in the graph In this case, if all the nodes in the graph is of even degree then we say that the graph already have a Euler Circuit and we don't need to add any edge in it. But if there is any node with odd degree we need to add edges. There can be even number of odd degree vertices in the graph. This can be easily proved by the fact that the sum of degrees from the even degrees node and degrees from odd degrees node should match the total degrees that is always even as every edge contributes two to this sum. Now, if we pair up random odd degree nodes in the graph and add an edge between them we can make all nodes to have even degree and thus make an Euler Circuit exist.
2. There are disconnected components in the graph We first mark components as odd and even. Odd components are those which have at least one odd degree node in them. Take all the even components and select a random vertex from every component and line them up linearly. Now we add an edge between adjacent vertices. So we have connected the even components and made an equivalent odd component that has two nodes with odd degree. Now to deal with odd components i.e components with at least one odd degree node. We can connect all these odd components using edges whose number is equal to the number of disconnected components. This can be done by placing the components in the cyclic order and picking two odd degree nodes from every component and using these to connect to the components on either side. Now we have a single connected component for which we have discussed. Below is implementation of this approach:
1