<p>You have an undirected, connected graph of <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. You are given an array <code>graph</code> where <code>graph[i]</code> is a list of all the nodes connected with node <code>i</code> by an edge.</p>

<p>Return <em>the length of the shortest path that visits every node</em>. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/12/shortest1-graph.jpg" style="width: 222px; height: 183px;" />
<pre>
<strong>Input:</strong> graph = [[1,2,3],[0],[0],[0]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible path is [1,0,2,0,3]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/12/shortest2-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> graph = [[1],[0,2,4],[1,3,4],[2],[1,2]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible path is [0,1,4,2,3]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == graph.length</code></li>
	<li><code>1 &lt;= n &lt;= 12</code></li>
	<li><code>0 &lt;= graph[i].length &lt;&nbsp;n</code></li>
	<li><code>graph[i]</code> does not contain <code>i</code>.</li>
	<li>If <code>graph[a]</code> contains <code>b</code>, then <code>graph[b]</code> contains <code>a</code>.</li>
	<li>The input graph is always connected.</li>
</ul>

from math import inf
from itertools import permutations


class Solution:
    def shortestPathLength(self, graph: List[List[int]]) -> int:
        n = len(graph)
        nodes = list(range(n))

        # Floyd-Warshall algorithm to compute the distances between
        # all pairs of nodes and track nodes visited in the shortest paths
        distances = [[inf] * n for _ in nodes]
        visited_nodes = [[0] * n for _ in nodes]  # to track nodes visited

        for u in nodes:
            distances[u][u] = 0
            for v in graph[u]:
                distances[u][v] = 1
                visited_nodes[u][v] = (1 << u) | (1 << v)

        for k in nodes:
            for u in nodes:
                for v in nodes:
                    if distances[u][v] > distances[u][k] + distances[k][v]:
                        distances[u][v] = distances[u][k] + distances[k][v]
                        visited_nodes[u][v] = visited_nodes[u][k] | visited_nodes[k][v]

        # Held-Karp algorithm to solve the TSP with bitmasking and DP
        dp = [[inf] * n for _ in range(1 << n)]

        for i in range(n):
            dp[1 << i][i] = 0  # we can start at any node

        for mask in range(1 << n):
            for i in range(n):
                if not (mask & (1 << i)):
                    continue

                for j in range(n):
                    if mask & (1 << j):
                        continue

                    next_mask = mask | (1 << j) | visited_nodes[i][j]  # include visited nodes
                    dp[next_mask][j] = min(dp[next_mask][j], dp[mask][i] + distances[i][j])

        final_mask = (1 << n) - 1
        min_distance = inf
        for i in range(n):
            min_distance = min(min_distance, dp[final_mask][i])

        return min_distance

17300