I have this python function for DFS, why is it throwing error?
<pre><code>##This function will return the edges for an undirected graph####
###Example: when edges = [[1,2],[3,2]]
### this will return 
{
    1: {'nbr': [2], 'id': 1, 'visited': False},
    2: {'nbr': [1, 3], 'id': 2, 'visited': False},
    3: {'nbr': [2], 'id': 3, 'visited': False}
}

def createEdges(edges):
    E = {}
    for y in edges:
        for x in edges:
            if x[0] not in E:
                E[x[0]] = {"id": x[0], "nbr": [x[1]], "visited": False}
            elif x[1] not in E:
                E[x[1]] = {"id": x[1], "nbr": [x[0]], "visited": False}
            elif x[0] in E and x[1] not in E[x[0]]['nbr']:
                E[x[0]]['nbr'].append(x[1])
            elif x[1] in E and x[0] not in E[x[1]]['nbr']:
                E[x[1]]['nbr'].append(x[0])
    return E

    ####A function to explore the a vertex of the graph with edges E

def explore(vertex, E):
    if vertex not in E:
        return
    E[vertex]['ccNum'] = cc
    E[vertex]['visited'] = True
    for x in E[vertex]['nbr']:
        if E[x]['visited'] == False:
            explore(x, E)
            ### this function will set the visited index to true for 
            ### all the connecting points of the vertex

            ###  A function for DFS

def DFS(E):
    cc = 1
    for x in E:
        if E[x]['visited'] == False:
            explore(x, E)
            cc += 1
</code></pre>

<p>Everything was running well before the introduction of "cc" which keep tracks
of total connecting sub graphs</p>

Its now throwing error that:

<h3>Global name cc is not defined</h3>

<p>although cc is defined expose is being called under DFS, which has cc defined in its scope</p>

<p>you need to define "cc" as parameter of explore  or define it as a global var.</p>

<pre><code> cc = 0
 def explore(vertex, E):
   global cc
   if vertex not in E:
      return
   E[vertex]['ccNum'] = cc
   E[vertex]['visited'] = True
   for x in E[vertex]['nbr']:
      if E[x]['visited'] == False:
           explore(x, E)
          ### this function will set the visited index to true for 
           ### all the connecting points of the vertex

          ###  A function for DFS

def DFS(E):
  cc = 1
  for x in E:
      if E[x]['visited'] == False:
        explore(x, E)
        cc += 1
</code></pre>

python graph