Java Program to Check Whether a Directed 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 Directed 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 DirectedEulerPath
{
    private int[][] adjacencyMatrix;
    private int     numberOfNodes;
 
    public DirectedEulerPath(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();
                }
            }
            DirectedEulerPath path = new DirectedEulerPath(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 DirectedEulerPath.java
$ java DirectedEulerPath
 
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:

Java Program to Implement Floyd-Warshall Algorithm
Setting a Request Timeout for a Spring REST API
Java Program to Represent Linear Equations in Matrix Form
Java Program to Implement Selection Sort
Spring Boot - Tomcat Port Number
Debug a HttpURLConnection problem
Lập trình đa luồng trong Java (Java Multi-threading)
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Practical Java Examples of the Big O Notation
Introduction to Spring Data MongoDB
Spring Boot - Introduction
Spring REST with a Zuul Proxy
Guide to the Java ArrayList
Java Program to Implement Insertion Sort
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Spring Boot - Google Cloud Platform
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Java Program to Implement Quick Sort with Given Complexity Constraint
Spring Boot Security Auto-Configuration
Converting a Stack Trace to a String in Java
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Java Program to Encode a Message Using Playfair Cipher
How to Add a Single Element to a Stream
Giới thiệu Design Patterns
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Java Program to Implement Circular Doubly Linked List
Java – Create a File
Kết hợp Java Reflection và Java Annotations
Transaction Propagation and Isolation in Spring @Transactional
How to Get a Name of a Method Being Executed?
Model, ModelMap, and ModelAndView in Spring MVC