"""Leetcode 785. Is Graph Bipartite?
Medium

URL: https://leetcode.com/problems/is-graph-bipartite/

Given an undirected graph, return true if and only if it is bipartite.

Recall that a graph is bipartite if we can split it's set of nodes into two
independent subsets A and B such that every edge in the graph has one node in A
and another node in B.

The graph is given in the following form: graph[i] is a list of indexes j for
which the edge between nodes i and j exists.  Each node is an integer between 0
and graph.length - 1.  There are no self edges or parallel edges: graph[i] does
not contain i, and it doesn't contain any element twice.

Example 1:
Input: [[1,3], [0,2], [1,3], [0,2]]
Output: true
Explanation: 
The graph looks like this:
0----1
|    |
|    |
3----2
We can divide the vertices into two groups: {0, 2} and {1, 3}.

Example 2:
Input: [[1,2,3], [0,2], [0,1,3], [0,2]]
Output: false
Explanation: 
The graph looks like this:
0----1
| \  |
|  \ |
3----2
We cannot find a way to divide the set of nodes into two independent subsets.
 
Note:
- graph will have length in range [1, 100].
- graph[i] will contain integers in range [0, graph.length - 1].
- graph[i] will not contain i or duplicate values.
- The graph is undirected: if any element j is in graph[i], then i will be in 
  graph[j].
"""

class Solution(object):
    def isBipartite(self, graph):
        """
        :type graph: List[List[int]]
        :rtype: bool
        """
        pass


def main():
    pass


if __name__ == '__main__':
    main()

@@ -1329,17 +1329,612 @@
 tion
-(object):
+NodeSetidDictDFSRecur(object):%0A    def _dfs(self, i, graph, node_setid):%0A        for j in graph%5Bi%5D:%0A            if j in node_setid:%0A                # If connected nodes have the same set id.%0A                if node_setid%5Bj%5D == node_setid%5Bi%5D:%0A                    return False%0A            else:%0A                # If not, add j to the set with diff set id. %0A                node_setid%5Bj%5D = 1 - node_setid%5Bi%5D%0A%0A                # Continue DFS from node j.%0A                is_bipartite = self._dfs(j, graph, node_setid)%0A                if not is_bipartite:%0A                    return False%0A%0A        return True%0A
 %0A   
@@ -2012,16 +2012,37 @@
 st%5Bint%5D%5D
+; a adjacency matrix.
 %0A       
@@ -2059,55 +2059,1179 @@
 ool%0A
-        %22%22%22%0A        pass%0A%0A%0Adef main():%0A    pass
+%0A        Apply recursive DFS through connected nodes to add to different sets.%0A%0A        Time complexity: O(%7CV%7C+%7CE%7C), where%0A          - %7CV%7C is the number of nodes.%0A          - %7CE%7C is the number of edges.%0A        Space complexity: O(%7CV%7C).%0A        %22%22%22%0A        # Edge cases: if only one or two nodes.%0A        if len(graph) %3C= 2:%0A            return True%0A%0A        # Use dict: node-%3Esetid=%7B0,1%7D for denoting visits and binary setid.%0A        node_setid = dict()%0A%0A        for i in range(len(graph)):%0A            if i not in node_setid:%0A                node_setid%5Bi%5D = 0%0A%0A                # Start DFS from node i.%0A                is_bipartite = self._dfs(i, graph, node_setid)%0A%0A                if not is_bipartite:%0A                    # If one node cannot be added, return False.%0A                    return False%0A%0A        return True%0A%0A%0Adef main():%0A    # Input: %5B%5B1,3%5D, %5B0,2%5D, %5B1,3%5D, %5B0,2%5D%5D%0A    # Output: true%0A    graph = %5B%5B1,3%5D, %5B0,2%5D, %5B1,3%5D, %5B0,2%5D%5D%0A    print SolutionNodeSetidDictDFSRecur().isBipartite(graph)%0A%0A    # Input: %5B%5B1,2,3%5D, %5B0,2%5D, %5B0,1,3%5D, %5B0,2%5D%5D%0A    # Output: true%0A    graph = %5B%5B1,2,3%5D, %5B0,2%5D, %5B0,1,3%5D, %5B0,2%5D%5D%0A    print SolutionNodeSetidDictDFSRecur().isBipartite(graph)
 %0A%0A%0Ai

Complete recur dfs sol