import * as $N from "@/core/base/BaseNode" ; import * as $G from "@/core/base/BaseGraph" ; import * as $SU from "@/utils/StructUtils" ; export enum DegreeMode { in , out , und , dir , all } todo export interface DegreeDistribution { in : Uint32Array ; out : Uint32Array ; dir : Uint32Array ; und : Uint32Array ; all : Uint32Array ; } class DegreeCentrality { constructor ( ) { } getCentralityMap ( graph , weighted ? , conf ? ) { weighted = weighted != null ? ! ! weighted : true ; conf = conf == null ? DegreeMode . all : conf ; let ret = { } ; switch ( conf ) { case DegreeMode . in : for ( let key in graph . getNodes ( ) ) { let node = graph . getNodeById ( key ) ; if ( node != null ) { if ( ! weighted ) { ret [ key ] = node . in_deg ; } else { ret [ key ] = ret [ key ] || 0 ; for ( let k in node . inEdges ( ) ) { ret [ key ] += node . inEdges ( ) [ k ] . getWeight ( ) ; } } } } break ; case DegreeMode . out : for ( let key in graph . getNodes ( ) ) { let node = graph . getNodeById ( key ) ; if ( node != null ) { if ( ! weighted ) { ret [ key ] = node . out_deg ; } else { ret [ key ] = ret [ key ] || 0 ; for ( let k in node . outEdges ( ) ) { ret [ key ] += node . outEdges ( ) [ k ] . getWeight ( ) ; } } } } break ; case DegreeMode . und : for ( let key in graph . getNodes ( ) ) { let node = graph . getNodeById ( key ) ; if ( node != null ) { if ( ! weighted ) { ret [ key ] = node . deg ; } else { ret [ key ] = ret [ key ] || 0 ; for ( let k in node . undEdges ( ) ) { ret [ key ] += node . undEdges ( ) [ k ] . getWeight ( ) ; } } } } break ; case DegreeMode . dir : for ( let key in graph . getNodes ( ) ) { let node = graph . getNodeById ( key ) ; if ( node != null ) { if ( ! weighted ) { ret [ key ] = node . in_deg + node . out_deg ; } else { ret [ key ] = ret [ key ] || 0 ; let comb = $SU . mergeObjects ( [ node . inEdges ( ) , node . outEdges ( ) ] ) ; for ( let k in comb ) { ret [ key ] += comb [ k ] . getWeight ( ) ; } } } } break ; case DegreeMode . all : for ( let key in graph . getNodes ( ) ) { let node = graph . getNodeById ( key ) ; if ( node != null ) { if ( ! weighted ) { ret [ key ] = node . deg + node . in_deg + node . out_deg ; } else { ret [ key ] = ret [ key ] || 0 ; let comb = $SU . mergeObjects ( [ node . inEdges ( ) , node . outEdges ( ) , node . undEdges ( ) , ] ) ; for ( let k in comb ) { ret [ key ] += comb [ k ] . getWeight ( ) ; } } } } break ; } return ret ; } TODO TODO degreeDistribution ( graph ) { let max_deg = 0 , key , nodes = graph . getNodes ( ) , node , all_deg ; for ( key in nodes ) { node = nodes [ key ] ; all_deg = node . in_deg + node . out_deg + node . deg + 1 ; max_deg = all_deg > max_deg ? all_deg : max_deg ; } let deg_dist = { in : new Uint32Array ( max_deg ) , out : new Uint32Array ( max_deg ) , dir : new Uint32Array ( max_deg ) , und : new Uint32Array ( max_deg ) , all : new Uint32Array ( max_deg ) , } ; for ( key in nodes ) { node = nodes [ key ] ; deg_dist . in [ node . in_deg ] ++ ; deg_dist . out [ node . out_deg ] ++ ; deg_dist . dir [ node . in_deg + node . out_deg ] ++ ; deg_dist . und [ node . deg ] ++ ; deg_dist . all [ node . in_deg + node . out_deg + node . deg ] ++ ; } return deg_dist ; } } export { DegreeCentrality } ;