#
# <one line to give the library's name and an idea of what it does.>
# Copyright (C) 2014  <copyright holder> <email>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
#

import structuralsimilaritymeasure

class SameNodeSimilarityMeasure(
  structuralsimilaritymeasure.StructuralSimilarityMeasure):
  '''
  Sprawdza liczbe takich samych wezlow w dwoch zadanych grafach.
  Zwraca przeskalowany wspolczynnik z zakresu <0, 1> gdzie:
  0 oznacza zupelnie niepodobne grafy do siebie, 1 oznacza 
  dokladnie takie same grafy.
  '''

  def __init__(self, options, str_name = 'SameNodeSimilarityMeasure'):
    """!
    Constructor
    @param options: a dictionary containing additional options for this measure
    @para str_name: default value same as the class name 
                     "SameNodeSimilarityMeasure"
    """
    super(SameNodeSimilarityMeasure, self).__init__(options, str_name)

  def calculate(self, g1, g2):
    """!
    Implementation of abstract calculate function from interface. 

    Calculates the  number  of same  nodes into  two graphs.  This number is 
    normalized to range <0,..., 1>

    @param g1 first graph
    @type g1: networgx.DiGraph/networkx.MultiDiGraph

    @param g2 second graph
    @type g1: networgx.DiGraph/networkx.MultiDiGraph

    @return Count of same nodes normalized to <0,...,1>
    @rtype: double
    """
    if not g1 or not g2:
      return 0
    g1s = set(g1.nodes())
    g2s = set(g2.nodes())
    normf = 0.0 + len(g1s.union(g2s))
    return (len(g1s.intersection(g2s)) / normf) if normf else 0.0
