"Fossies" - the Fresh Open Source Software Archive

Member "radialnet/core/Graph.py" (17 Feb 2008, 7218 Bytes) of package /linux/privat/old/radialnet-0.44.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "Graph.py" see the Fossies "Dox" file reference documentation.

    1 # vim: set fileencoding=utf-8 :
    2 
    3 # Copyright (C) 2007 Insecure.Com LLC.
    4 #
    5 # Author: João Paulo de Souza Medeiros <ignotus21@gmail.com>
    6 #
    7 # This program is free software; you can redistribute it and/or modify
    8 # it under the terms of the GNU General Public License as published by
    9 # the Free Software Foundation; either version 2 of the License, or
   10 # (at your option) any later version.
   11 #
   12 # This program is distributed in the hope that it will be useful,
   13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15 # GNU General Public License for more details.
   16 #
   17 # You should have received a copy of the GNU General Public License
   18 # along with this program; if not, write to the Free Software
   19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   20 
   21 
   22 def is_a_new_connection(connection, conection_set):
   23     """
   24     """
   25     (i, j) = connection
   26 
   27     for edge in conection_set:
   28 
   29         (a, b) = edge.get_nodes()
   30 
   31         if (a == i and b == j) or (a == j and b == i):
   32             return False
   33 
   34     return True
   35 
   36 
   37 
   38 class Node(object):
   39     """
   40     Node class
   41     """
   42     def __init__(self, id=None):
   43         """
   44         Constructor method of Node class
   45         @type  : integer
   46         @param : Node identifier
   47         """
   48         self.__id = id
   49         """Node identifier"""
   50         self.__information = {}
   51         """Hash with general information"""
   52 
   53 
   54     def get_id(self):
   55         """
   56         Get node ID
   57         @rtype: number
   58         @return: Node identifier
   59         """
   60         return self.__id
   61 
   62 
   63     def set_id(self, id):
   64         """
   65         Set node ID
   66         @type  : number
   67         @param : Node identifier
   68         """
   69         self.__id = id
   70 
   71 
   72     def get_info(self, info=None):
   73         """
   74         Get general information about node
   75         @type  : string
   76         @param : Information name
   77         @rtype: mixed
   78         @return: The requested information
   79         """
   80         if info == None:
   81             return self.__information
   82 
   83         if self.__information.has_key(info):
   84             return self.__information[info]
   85             
   86         return None
   87 
   88 
   89     def set_info(self, info):
   90         """
   91         Set general information
   92         @type  : dict
   93         @param : General information dictionary
   94         """
   95         for key in info:
   96             self.__information[key] = info[key]
   97 
   98 
   99 
  100 class Edge:
  101     """
  102     """
  103     def __init__(self, nodes):
  104         """
  105         """
  106         self.__weigths = []
  107         self.__nodes = nodes
  108         self.__weigths_mean = None
  109 
  110 
  111     def get_nodes(self):
  112         """
  113         """
  114         return self.__nodes
  115 
  116 
  117     def get_weigths(self):
  118         """
  119         """
  120         return self.__weigths
  121 
  122 
  123     def set_weigths(self, weigths):
  124         """
  125         """
  126         self.__weigths = weigths
  127 
  128 
  129     def add_weigth(self, weigth):
  130         """
  131         """
  132         self.__weigths.append(weigth)
  133 
  134 
  135     def get_weigths_mean(self):
  136         """
  137         """
  138         return self.__weigths_mean
  139 
  140 
  141     def calc_weigths_mean(self):
  142         """
  143         """
  144         if len(self.__weigths) > 0:
  145             self.__weigths_mean = sum(self.__weigths) / len(self.__weigths)
  146 
  147         else:
  148             self.__weigths_mean = None
  149 
  150 
  151 
  152 class Graph:
  153     """
  154     Network Graph class
  155     """
  156 
  157     def __init__(self):
  158         """
  159         Constructor method of Graph class
  160         @type  : list
  161         @param : List of nodes
  162         """
  163         self.__main_node = None
  164         self.__nodes = []
  165         self.__edges = []
  166         self.__max_edge_mean_value = None
  167         self.__min_edge_mean_value = None
  168 
  169         self.calc_max_edge_mean_weight()
  170         self.calc_max_edge_mean_weight()
  171 
  172 
  173     def set_nodes(self, nodes):
  174         """
  175         """
  176         self.__nodes = nodes
  177 
  178 
  179     def get_nodes(self):
  180         """
  181         """
  182         return self.__nodes
  183 
  184 
  185     def get_number_of_nodes(self):
  186         """
  187         Get the number of nodes in graph
  188         @rtype: number
  189         @return: The number of nodes in the graph
  190         """
  191         return len(self.__nodes)
  192 
  193 
  194     def set_main_node(self, node):
  195         """
  196         Set the main node by ID
  197         @type  : number
  198         @param : The node ID
  199         """
  200         self.__main_node = node
  201 
  202 
  203     def set_main_node_by_id(self, id):
  204         """
  205         Set the main node by ID
  206         @type  : number
  207         @param : The node ID
  208         """
  209         self.__main_node = self.get_node_by_id(id)
  210 
  211 
  212     def get_node_by_id(self, id):
  213         """
  214         Get one node of graph by your ID
  215         @type  : number
  216         @param : The node ID
  217         @rtype: Node
  218         @return: The node
  219         """
  220         for node in self.__nodes:
  221 
  222             if node.get_id() == id:
  223                 return node
  224 
  225         return None
  226 
  227 
  228     def get_main_node(self):
  229         """
  230         Get the main node
  231         @rtype: Node
  232         @return: The main node
  233         """
  234         return self.__main_node
  235 
  236 
  237     def get_main_node_id(self):
  238         """
  239         Get the main node ID
  240         @rtype: number
  241         @return: The main node ID
  242         """
  243         return self.__main_node.get_id()
  244 
  245 
  246     def set_connection(self, a, b, weigth=None):
  247         """
  248         Set node connections
  249         @type  : list
  250         @param : List of connections
  251         """
  252         connection = (a, b)
  253 
  254         # if is a new connection make it
  255         if is_a_new_connection(connection, self.__edges):
  256             self.__edges.append(Edge(connection))
  257 
  258         # then add new weigth value
  259         if weigth != None:
  260 
  261             edge = self.get_connection(a, b)
  262             edge.add_weigth(weigth)
  263 
  264             edge.calc_weigths_mean()
  265 
  266             self.calc_min_edge_mean_weight()
  267             self.calc_max_edge_mean_weight()
  268 
  269 
  270     def get_connection(self, a, b):
  271         """
  272         """
  273         for edge in self.__edges:
  274 
  275             if a in edge.get_nodes() and b in edge.get_nodes():
  276                 return edge
  277 
  278 
  279     def get_edges(self):
  280         """
  281         """
  282         return self.__edges
  283 
  284 
  285     def get_node_connections(self, node):
  286         """
  287         """
  288         connections = []
  289 
  290         for edge in self.__edges:
  291 
  292             (a, b) = edge.get_nodes()
  293 
  294             if a == node:
  295                 connections.append(b)
  296             if b == node:
  297                 connections.append(a)
  298 
  299         return connections
  300 
  301 
  302     def get_max_edge_mean_weight(self):
  303         """
  304         """
  305         return self.__max_edge_mean_value
  306 
  307 
  308     def get_min_edge_mean_weight(self):
  309         """
  310         """
  311         return self.__min_edge_mean_value
  312 
  313 
  314     def calc_max_edge_mean_weight(self):
  315         """
  316         """
  317         max_value = None
  318 
  319         for edge in self.__edges:
  320 
  321             mean = edge.get_weigths_mean()
  322 
  323             if mean != None:
  324                 if mean > max_value or max_value == None:
  325                     max_value = mean
  326 
  327         self.__max_edge_mean_value = max_value
  328 
  329 
  330     def calc_min_edge_mean_weight(self):
  331         """
  332         """
  333         min_value = None
  334 
  335         for edge in self.__edges:
  336 
  337             mean = edge.get_weigths_mean()
  338 
  339             if mean != None:
  340                 if mean < min_value or min_value == None:
  341                     min_value = mean
  342 
  343         self.__min_edge_mean_value = min_value
  344