|
| 1 | +public class Main { |
| 2 | + |
| 3 | + public static void main(String[] args) { |
| 4 | + |
| 5 | + int noOfVertices=5; |
| 6 | + |
| 7 | + int[][] adjMatrix=new int[][]{ |
| 8 | + {0,1,0,0,1}, |
| 9 | + {1,0,1,1,0}, |
| 10 | + {0,1,0,1,0}, |
| 11 | + {0,1,1,0,1}, |
| 12 | + {1,0,0,1,0} |
| 13 | + }; |
| 14 | + isEuler(adjMatrix,noOfVertices); |
| 15 | + |
| 16 | + System.out.println(""); |
| 17 | + adjMatrix=new int[][]{ |
| 18 | + {0,1,0,0,1}, |
| 19 | + {1,0,1,0,0}, |
| 20 | + {0,1,0,1,0}, |
| 21 | + {0,0,1,0,1}, |
| 22 | + {1,0,0,1,0} |
| 23 | + }; |
| 24 | + isEuler(adjMatrix,noOfVertices); |
| 25 | + |
| 26 | + System.out.println(""); |
| 27 | + adjMatrix=new int[][]{ |
| 28 | + {0,1,1,0,1}, |
| 29 | + {1,0,1,1,0}, |
| 30 | + {1,1,0,1,0}, |
| 31 | + {0,1,1,0,1}, |
| 32 | + {1,0,0,1,0} |
| 33 | + }; |
| 34 | + isEuler(adjMatrix,noOfVertices); |
| 35 | + } |
| 36 | + |
| 37 | + static void isEuler(int[][] adjMatrix,int noOfVertices){ |
| 38 | + //if 0, then it is not a euler graph |
| 39 | + //if 1, then it is a Euler path |
| 40 | + //if 2, then it is a Euler Cycle |
| 41 | + int flag = isEulerianGraph(adjMatrix,noOfVertices); |
| 42 | + if(flag==0){ |
| 43 | + System.out.println("Graph is not Eulerian"); |
| 44 | + } |
| 45 | + else if(flag==1){ |
| 46 | + System.out.println("Graph has Eulerian Path"); |
| 47 | + findEulerianPath(adjMatrix,noOfVertices); |
| 48 | + } |
| 49 | + else{ |
| 50 | + System.out.println("Graph has Eulerian Cycle"); |
| 51 | + findEulerianPath(adjMatrix,noOfVertices); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + static int isEulerianGraph(int[][] adjMatrix,int noOfVertices){ |
| 56 | + |
| 57 | + int[] degreeOfVertices=findDegree(adjMatrix,noOfVertices); |
| 58 | + boolean connected=isConnected(adjMatrix,noOfVertices,degreeOfVertices); |
| 59 | + |
| 60 | + if(!connected){ |
| 61 | + return 0; |
| 62 | + } |
| 63 | + |
| 64 | + int noOfOddVertices=0; |
| 65 | + |
| 66 | + for(int degree:degreeOfVertices){ |
| 67 | + if(degree%2!=0){ |
| 68 | + noOfOddVertices++; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if(noOfOddVertices>2){ |
| 73 | + return 0; |
| 74 | + } |
| 75 | + |
| 76 | + return noOfOddVertices == 2 ? 1 : 2; |
| 77 | + } |
| 78 | + |
| 79 | + static int[] findDegree(int[][] adjMatrix,int noOfVertices){ |
| 80 | + int[] degree=new int[noOfVertices]; |
| 81 | + |
| 82 | + for(int i=0;i<noOfVertices;i++){ |
| 83 | + for(int j=0;j<noOfVertices;j++){ |
| 84 | + if(i!=j && adjMatrix[i][j]==1){ |
| 85 | + degree[i]++; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return degree; |
| 91 | + } |
| 92 | + |
| 93 | + static boolean isConnected(int[][] adjMatrix,int noOfVertices,int[] degreeOfVertices){ |
| 94 | + boolean[] visited=new boolean[noOfVertices]; |
| 95 | + |
| 96 | + int i=0; |
| 97 | + for(;i<noOfVertices;i++){ |
| 98 | + if(degreeOfVertices[i]>0){ |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if(i==noOfVertices){ |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + dfs(adjMatrix,visited,i,noOfVertices); |
| 108 | + |
| 109 | + for(int vertex=0;vertex<noOfVertices;vertex++){ |
| 110 | + if(!visited[vertex] && degreeOfVertices[vertex]>0){ |
| 111 | + return false; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + static void dfs(int[][] adjMatrix,boolean[] visited, int vertex,int noOfVertices){ |
| 119 | + visited[vertex]=true; |
| 120 | + |
| 121 | + for(int i=0;i<noOfVertices;i++){ |
| 122 | + if(i!=vertex && adjMatrix[vertex][i]==1 && !visited[i]){ |
| 123 | + dfs(adjMatrix,visited,i,noOfVertices); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + static void findEulerianPath(int[][] adjMatrix,int noOfVertices){ |
| 129 | + int[] degreeOfVertices=findDegree(adjMatrix,noOfVertices); |
| 130 | + int curr=0; |
| 131 | + |
| 132 | + for(int i=0;i<noOfVertices;i++){ |
| 133 | + if(degreeOfVertices[i]%2!=0){ |
| 134 | + curr=i; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + Stack<Integer> st=new Stack<>(); |
| 139 | + List<Integer> path=new ArrayList<>(); |
| 140 | + |
| 141 | + while(!st.isEmpty() || hasNeighbour(adjMatrix[curr])){ |
| 142 | + if(!hasNeighbour(adjMatrix[curr])){ |
| 143 | + path.add(curr); |
| 144 | + curr = st.pop(); |
| 145 | + } |
| 146 | + else{ |
| 147 | + for(int i=0;i<noOfVertices;i++){ |
| 148 | + if(adjMatrix[curr][i]==1){ |
| 149 | + st.add(curr); |
| 150 | + adjMatrix[curr][i]=0; |
| 151 | + adjMatrix[i][curr]=0; |
| 152 | + curr=i; |
| 153 | + break; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + for(int vertex:path){ |
| 160 | + System.out.print(vertex + " -> "); |
| 161 | + } |
| 162 | + System.out.println(curr); |
| 163 | + } |
| 164 | + |
| 165 | + static boolean hasNeighbour(int[] adjacent){ |
| 166 | + for(int i:adjacent){ |
| 167 | + if(i!=0){ |
| 168 | + return true; |
| 169 | + } |
| 170 | + } |
| 171 | + return false; |
| 172 | + } |
| 173 | +} |
0 commit comments