# Path: clusters/tarjan.py
# def strongly_connected_components(graph):
#     """
#     Tarjan's Algorithm (named for its discoverer, Robert Tarjan) is a graph theory algorithm
#     for finding the strongly connected components of a graph.
#     
#     Based on: http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
#     """
# 
#     index_counter = [0]
#     stack = []
#     lowlinks = {}
#     index = {}
#     result = []
# 
#     def strongconnect(node):
#         # set the depth index for this node to the smallest unused index
#         index[node] = index_counter[0]
#         lowlinks[node] = index_counter[0]
#         index_counter[0] += 1
#         stack.append(node)
#     
#         # Consider successors of `node`
#         try:
#             successors = graph[node]
#         except:
#             successors = []
#         for successor in successors:
#             if successor not in lowlinks:
#                 # Successor has not yet been visited; recurse on it
#                 strongconnect(successor)
#                 lowlinks[node] = min(lowlinks[node],lowlinks[successor])
#             elif successor in stack:
#                 # the successor is in the stack and hence in the current strongly connected component (SCC)
#                 lowlinks[node] = min(lowlinks[node],index[successor])
#         
#         # If `node` is a root node, pop the stack and generate an SCC
#         if lowlinks[node] == index[node]:
#             connected_component = []
#             
#             while True:
#                 successor = stack.pop()
#                 connected_component.append(successor)
#                 if successor == node: break
#             component = tuple(connected_component)
#             # storing the result
#             result.append(component)
#     
#     for node in graph:
#         if node not in lowlinks:
#             strongconnect(node)
#     
#     return result


import re
from decimal import *
from django.db import models
from django.conf import settings
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from clusters.utils import *
from clusters.tarjan import strongly_connected_components
        for flow in flows:
            nodes.append(flow.from_function)
            nodes.append(flow.to_function)
            nodes.append(flow.resource_type)
        nodes = list(set(nodes))
        return nodes
        
    def regions(self):
        areas = {}
        agents = self.agents()
        for agent in agents:
            co = self.community
            ca = CommunityAgent.objects.get(
                community=co, agent=agent)
            key = ca.region_latitude + ca.region_longitude
            if key:
                if not key in areas:
                    areas[key] = Region(
                        ca.geographic_area,
                        ca.region_latitude,
                        ca.region_longitude,
                        {})
                area = areas[key]
                afs = agent.functions.filter(function__cluster=self)
                for af in afs:
                    if not af.function.id in area.function_dict:
                        area.function_dict[af.function.id] = AggregateFunction(
                            af.function, {})
                    fn = area.function_dict[af.function.id]
                    for afrt in af.function_resources.all():

# Path: clusters/tarjan.py
# def strongly_connected_components(graph):
#     """
#     Tarjan's Algorithm (named for its discoverer, Robert Tarjan) is a graph theory algorithm
#     for finding the strongly connected components of a graph.
#     
#     Based on: http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
#     """
# 
#     index_counter = [0]
#     stack = []
#     lowlinks = {}
#     index = {}
#     result = []
# 
#     def strongconnect(node):
#         # set the depth index for this node to the smallest unused index
#         index[node] = index_counter[0]
#         lowlinks[node] = index_counter[0]
#         index_counter[0] += 1
#         stack.append(node)
#     
#         # Consider successors of `node`
#         try:
#             successors = graph[node]
#         except:
#             successors = []
#         for successor in successors:
#             if successor not in lowlinks:
#                 # Successor has not yet been visited; recurse on it
#                 strongconnect(successor)
#                 lowlinks[node] = min(lowlinks[node],lowlinks[successor])
#             elif successor in stack:
#                 # the successor is in the stack and hence in the current strongly connected component (SCC)
#                 lowlinks[node] = min(lowlinks[node],index[successor])
#         
#         # If `node` is a root node, pop the stack and generate an SCC
#         if lowlinks[node] == index[node]:
#             connected_component = []
#             
#             while True:
#                 successor = stack.pop()
#                 connected_component.append(successor)
#                 if successor == node: break
#             component = tuple(connected_component)
#             # storing the result
#             result.append(component)
#     
#     for node in graph:
#         if node not in lowlinks:
#             strongconnect(node)
#     
#     return result


# Path: clusters/models.py
import re
from decimal import *
from django.db import models
from django.conf import settings
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from clusters.utils import *
from clusters.tarjan import strongly_connected_components

        for flow in flows:
            nodes.append(flow.from_function)
            nodes.append(flow.to_function)
            nodes.append(flow.resource_type)
        nodes = list(set(nodes))
        return nodes
        
    def regions(self):
        areas = {}
        agents = self.agents()
        for agent in agents:
            co = self.community
            ca = CommunityAgent.objects.get(
                community=co, agent=agent)
            key = ca.region_latitude + ca.region_longitude
            if key:
                if not key in areas:
                    areas[key] = Region(
                        ca.geographic_area,
                        ca.region_latitude,
                        ca.region_longitude,
                        {})
                area = areas[key]
                afs = agent.functions.filter(function__cluster=self)
                for af in afs:
                    if not af.function.id in area.function_dict:
                        area.function_dict[af.function.id] = AggregateFunction(
                            af.function, {})
                    fn = area.function_dict[af.function.id]
                    for afrt in af.function_resources.all():

                        reskey = "".join([str(afrt.resource_type.id), afrt.role])