|
| 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 | + adjMatrix=new int[][]{ |
| 17 | + {0,1,0,0,1}, |
| 18 | + {1,0,1,0,0}, |
| 19 | + {0,1,0,1,0}, |
| 20 | + {0,0,1,0,1}, |
| 21 | + {1,0,0,1,0} |
| 22 | + }; |
| 23 | + isEuler(adjMatrix,noOfVertices); |
| 24 | + |
| 25 | + adjMatrix=new int[][]{ |
| 26 | + {0,1,1,0,1}, |
| 27 | + {1,0,1,1,0}, |
| 28 | + {1,1,0,1,0}, |
| 29 | + {0,1,1,0,1}, |
| 30 | + {1,0,0,1,0} |
| 31 | + }; |
| 32 | + isEuler(adjMatrix,noOfVertices); |
| 33 | + } |
| 34 | + |
| 35 | + static void isEuler(int[][] adjMatrix,int noOfVertices){ |
| 36 | + //if 0, then it is not a euler graph |
| 37 | + //if 1, then it is a Euler path |
| 38 | + //if 2, then it is a Euler Cycle |
| 39 | + int flag = isEulerianGraph(adjMatrix,noOfVertices); |
| 40 | + if(flag==0){ |
| 41 | + System.out.println("Graph is not Eulerian"); |
| 42 | + } |
| 43 | + else if(flag==1){ |
| 44 | + System.out.println("Graph has Eulerian Path"); |
| 45 | + } |
| 46 | + else{ |
| 47 | + System.out.println("Graph has Eulerian Cycle"); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + static int isEulerianGraph(int[][] adjMatrix,int noOfVertices){ |
| 52 | + |
| 53 | + int[] degreeOfVertices=findDegree(adjMatrix,noOfVertices); |
| 54 | + boolean connected=isConnected(adjMatrix,noOfVertices,degreeOfVertices); |
| 55 | + |
| 56 | + if(!connected){ |
| 57 | + return 0; |
| 58 | + } |
| 59 | + |
| 60 | + int noOfOddVertices=0; |
| 61 | + |
| 62 | + for(int degree:degreeOfVertices){ |
| 63 | + if(degree%2!=0){ |
| 64 | + noOfOddVertices++; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if(noOfOddVertices>2){ |
| 69 | + return 0; |
| 70 | + } |
| 71 | + |
| 72 | + return noOfOddVertices == 2 ? 1 : 2; |
| 73 | + } |
| 74 | + |
| 75 | + static int[] findDegree(int[][] adjMatrix,int noOfVertices){ |
| 76 | + int[] degree=new int[noOfVertices]; |
| 77 | + |
| 78 | + for(int i=0;i<noOfVertices;i++){ |
| 79 | + for(int j=0;j<noOfVertices;j++){ |
| 80 | + if(i!=j && adjMatrix[i][j]==1){ |
| 81 | + degree[i]++; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return degree; |
| 87 | + } |
| 88 | + |
| 89 | + static boolean isConnected(int[][] adjMatrix,int noOfVertices,int[] degreeOfVertices){ |
| 90 | + boolean[] visited=new boolean[noOfVertices]; |
| 91 | + |
| 92 | + int i=0; |
| 93 | + for(;i<noOfVertices;i++){ |
| 94 | + if(degreeOfVertices[i]>0){ |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if(i==noOfVertices){ |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + dfs(adjMatrix,visited,i,noOfVertices); |
| 104 | + |
| 105 | + for(int vertex=0;vertex<noOfVertices;vertex++){ |
| 106 | + if(!visited[vertex] && degreeOfVertices[vertex]>0){ |
| 107 | + return false; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return true; |
| 112 | + } |
| 113 | + |
| 114 | + static void dfs(int[][] adjMatrix,boolean[] visited, int vertex,int noOfVertices){ |
| 115 | + visited[vertex]=true; |
| 116 | + |
| 117 | + for(int i=0;i<noOfVertices;i++){ |
| 118 | + if(i!=vertex && adjMatrix[vertex][i]==1 && !visited[i]){ |
| 119 | + dfs(adjMatrix,visited,i,noOfVertices); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments