> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/graphs-bfs-and-dfs/eulerian-path-and-circuit-for-directed-graph.md).

# Eulerian path and circuit for directed graph

[Eulerian Path](http://en.wikipedia.org/wiki/Eulerian_path) is a path in graph that visits every edge exactly once. Eulerian Circuit is an Eulerian Path which starts and ends on the same vertex.

A graph is said to be eulerian if it has a eulerian cycle. We have discussed [eulerian circuit for an undirected graph](https://www.geeksforgeeks.org/eulerian-path-and-circuit/). In this post, the same is discussed for a directed graph.

For example, the following graph has eulerian cycle as {1, 0, 3, 4, 0, 2, 1}

```java
class Solution {
    public static boolean isCircuit(List<Integer>[] graph) {
        int[] indegree = new int[graph.length];
        int[] outdegree = new int[graph.length];
        for (int i = 0; i < graph.length; i++) {
            for (int x : graph[i]) {
                indegree[x]++;
                outdegree[i]++;
            }
        }
        boolean smallerIndegreeFound = false;
        boolean biggerIndegreeFound = false;
        for (int i = 0; i < graph.length; i++) {
            if (indegree[i] != outdegree[i]) {
                if (indegree[i] + 1 == outdegree[i] && !biggerIndegreeFound)
                    biggerIndegreeFound = true;
                else if (outdegree[i] + 1 == indegree[i] && !smallerIndegreeFound)
                    smallerIndegreeFound = true;
                else
                    return false;
            }
        }
        if ((!smallerIndegreeFound && !biggerIndegreeFound) || (smallerIndegreeFound && biggerIndegreeFound))
            return true;
        return false;
    }

    public static boolean isPath(List<Integer>[] graph) {
        int[] indegree = new int[graph.length];
        int[] outdegree = new int[graph.length];
        for (int i = 0; i < graph.length; i++) {
            for (int x : graph[i]) {
                indegree[x]++;
                outdegree[i]++;
            }
        }
        for (int i = 0; i < graph.length; i++) {
            if (indegree[i] != outdegree[i])
                return false;
        }
        return true;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/graphs-bfs-and-dfs/eulerian-path-and-circuit-for-directed-graph.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
