Java Program to Check Whether an Undirected Graph Contains a Eulerian Path

This is a java program to check whether graph contains Eulerian Cycle. The criteran Euler suggested,
1. If graph has no odd degree vertex, there is at least one Eulerian Circuit.
2. If graph as two vertices with odd degree, there is no Eulerian Circuit but at least one Eulerian Path.
3. If graph has more than two vertices with odd degree, there is no Eulerian Circuit or Eulerian Path.

Here is the source code of the Java Program to Check Whether an Undirected Graph Contains a Eulerian Path. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

package com.maixuanviet.graph;
 
import java.util.InputMismatchException;
import java.util.Scanner;
 
public class UndirectedEulerPath
{
    private int[][] adjacencyMatrix;
    private int     numberOfNodes;
 
    public UndirectedEulerPath(int numberOfNodes, int[][] adjacencyMatrix)
    {
        this.numberOfNodes = numberOfNodes;
        this.adjacencyMatrix = new int[numberOfNodes + 1][numberOfNodes + 1];
        for (int sourceVertex = 1; sourceVertex <= numberOfNodes; sourceVertex++)
        {
            for (int destinationVertex = 1; destinationVertex <= numberOfNodes; destinationVertex++)
            {
                this.adjacencyMatrix[sourceVertex][destinationVertex] = adjacencyMatrix[sourceVertex][destinationVertex];
            }
        }
    }
 
    public int degree(int vertex)
    {
        int degree = 0;
        for (int destinationvertex = 1; destinationvertex <= numberOfNodes; destinationvertex++)
        {
            if (adjacencyMatrix[vertex][destinationvertex] == 1
                    || adjacencyMatrix[destinationvertex][vertex] == 1)
            {
                degree++;
            }
        }
        return degree;
    }
 
    public int countOddDegreeVertex()
    {
        int count = 0;
        for (int node = 1; node <= numberOfNodes; node++)
        {
            if ((degree(node) % 2) != 0)
            {
                count++;
            }
        }
        return count;
    }
 
    public static void main(String... arg)
    {
        int number_of_nodes;
        Scanner scanner = null;
        try
        {
            System.out.println("Enter the number of nodes in the graph");
            scanner = new Scanner(System.in);
            number_of_nodes = scanner.nextInt();
            int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes + 1];
            System.out.println("Enter the adjacency matrix");
            for (int i = 1; i <= number_of_nodes; i++)
            {
                for (int j = 1; j <= number_of_nodes; j++)
                {
                    adjacency_matrix[i][j] = scanner.nextInt();
                }
            }
            // make the graph undirected
            for (int i = 1; i <= number_of_nodes; i++)
            {
                for (int j = 1; j <= number_of_nodes; j++)
                {
                    if (adjacency_matrix[i][j] == 1
                            && adjacency_matrix[j][i] == 0)
                    {
                        adjacency_matrix[j][i] = 1;
                    }
                }
            }
            DirectedEulerianCircuit path = new DirectedEulerianCircuit(number_of_nodes,
                    adjacency_matrix);
            int count = path.countOddDegreeVertex();
            if (count == 0)
            {
                System.out
                        .println("As the graph has no odd degree vertex, there is at least one Eulerian Circuit.");
            }
            else if (count == 2)
            {
                System.out
                        .println("As the graph as two vertices with odd degree, there is no Eulerian Circuit but at least one Eulerian Path.");
            }
            else
            {
                System.out
                        .println("As the graph has more than two vertices with odd degree, there is no Eulerian Circuit or Eulerian Path.");
            }
        }
        catch (InputMismatchException inputMismatch)
        {
            System.out.println("Wrong Input format");
        }
        scanner.close();
    }
}

Output:

$ javac UndirectedEulerPath.java
$ java UndirectedEulerPath
 
Enter the number of nodes in the graph
4
Enter the adjacency matrix
0 1 1 1
1 0 1 0
1 1 0 1
1 0 1 0
As the graph as two vertices with odd degree, there is no Eulerian Circuit but at least one Eulerian Path.

Related posts:

Map to String Conversion in Java
Java Program to Implement Knight’s Tour Problem
Comparing Strings in Java
Introduction to the Java NIO2 File API
Hướng dẫn Java Design Pattern – Visitor
Using JWT with Spring Security OAuth (legacy stack)
Introduction to Netflix Archaius with Spring Cloud
Service Registration with Eureka
Spring’s RequestBody and ResponseBody Annotations
Java Program to Perform Partition of an Integer in All Possible Ways
Jackson – Marshall String to JsonNode
Interface trong Java 8 – Default method và Static method
Check If Two Lists are Equal in Java
Java Program to Implement Floyd-Warshall Algorithm
Disable Spring Data Auto Configuration
Java Program to Implement Heap Sort Using Library Functions
Using the Map.Entry Java Class
Java Program to Implement Bucket Sort
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Pagination and Sorting using Spring Data JPA
Java Program to Implement the String Search Algorithm for Short Text Sizes
Giới thiệu SOAP UI và thực hiện test Web Service
Servlet 3 Async Support with Spring MVC and Spring Security
Remove HTML tags from a file to extract only the TEXT
Java Streams vs Vavr Streams
Java Optional as Return Type
Java Program to Implement Miller Rabin Primality Test Algorithm
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Java Program to Implement Merge Sort Algorithm on Linked List
Java Program to Implement Hopcroft Algorithm
Java Program to Implement Weight Balanced Tree
Sử dụng CyclicBarrier trong Java