All problems
Eulerian Path Existence
Problem
Decide whether an undirected graph with n nodes has an Eulerian path: a trail that visits every edge exactly once.
edges is a list of [u, v] pairs. Ignoring isolated nodes, the graph must be connected and have either 0 or 2 odd-degree nodes. Return a boolean.
Hint
Connectivity of all non-isolated nodes plus 0 or 2 odd degrees is necessary and sufficient.
Test cases
case 1
in: [4,[[0,1],[1,2],[2,3]]]
expected: true
case 2
in: [3,[[0,1],[1,2],[2,0]]]
expected: true
case 3
in: [4,[[0,1],[0,2],[0,3]]]
expected: false
case 4
in: [6,[[0,1],[1,2],[3,4],[4,5]]]
expected: false
case 5
in: [1,[]]
expected: true
Starter codePython
def has_eulerian_path(n, edges):
# Your code here
pass5 test cases · run your code against them in the editor