-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd88000
commit cd20ac9
Showing
8 changed files
with
163 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
from nsdlib.algorithms.reconstruction.sbrp import sbrp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from networkx import Graph | ||
|
||
from nsdlib.algorithms.reconstruction.utils import init_extended_network, \ | ||
compute_neighbors_probability, NODE_INFECTION_PROBABILITY_ATTR, \ | ||
remove_invalid_nodes | ||
|
||
|
||
def sbrp(G: Graph, IG: Graph, | ||
reconstruction_threshold=0.5, | ||
max_iterations: int = 1) -> Graph: | ||
"""SbRP graph reconstruction algorithm. | ||
@param G: Network | ||
@param IG: Infected network | ||
@param reconstruction_threshold: Reconstruction threshold | ||
@return: Extended network | ||
References | ||
---------- | ||
- [1] W. Zang, P. Zhang, C. Zhou, i L. Guo, „Discovering Multiple Diffusion Source Nodes in Social Networks”, Procedia Comput. Sci., t. 29, s. 443–452, 2014, doi: 10.1016/j.procs.2014.05.040. | ||
- [2] W. Zang, P. Zhang, C. Zhou, i L. Guo, „Locating multiple sources in social networks under the SIR model: A divide-and-conquer approach”, J. Comput. Sci., t. 10, s. 278–287, wrz. 2015, doi: 10.1016/j.jocs.2015.05.002. | ||
""" | ||
EG = init_extended_network(G=G, IG=IG) | ||
iter = 1 | ||
nodes = [1] | ||
while iter < max_iterations and nodes: | ||
iter += 1 | ||
for node in IG: | ||
for neighbour in G.neighbors(node): | ||
if neighbour in IG: | ||
continue | ||
EG.nodes[neighbour][ | ||
NODE_INFECTION_PROBABILITY_ATTR] = compute_neighbors_probability( | ||
G=EG, node=neighbour) | ||
|
||
remove_invalid_nodes(EG, reconstruction_threshold) | ||
return EG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import operator | ||
from functools import reduce | ||
|
||
import networkx as nx | ||
from networkx import Graph | ||
|
||
from nsdlib.common.models import NODE_TYPE | ||
|
||
NODE_INFECTION_PROBABILITY_ATTR = "INFECTION_PROBABILITY" | ||
|
||
|
||
def init_extended_network(G: Graph, IG: Graph) -> Graph: | ||
""" | ||
Initialize extended network. | ||
@param G: Network | ||
@param IG: Infected network | ||
@return: Extended network | ||
""" | ||
EG = G.copy() | ||
nx.set_node_attributes( | ||
EG, | ||
{ | ||
node: {NODE_INFECTION_PROBABILITY_ATTR: 1.0 if node in IG else 0.0} | ||
for node in G | ||
}, | ||
) | ||
return EG | ||
|
||
|
||
def compute_neighbors_probability(node: NODE_TYPE, G: Graph) -> float: | ||
""" | ||
Compute probability of infection for a given node. | ||
@param node: Node | ||
@param G: Graph | ||
@return: Probability of infection for a given node | ||
""" | ||
neighbors_probability = [ | ||
G.nodes[node][NODE_INFECTION_PROBABILITY_ATTR] for node in | ||
nx.neighbors(G, node) | ||
] | ||
return reduce( | ||
operator.mul, | ||
neighbors_probability, | ||
1, | ||
) | ||
|
||
|
||
def remove_invalid_nodes(EG: Graph, threshold: float) -> Graph: | ||
""" | ||
Remove nodes with infection probability lower than threshold. | ||
@param EG: Extended network | ||
@param threshold: Infection probability threshold | ||
@return: Extended network with removed nodes that have infection probability lower than threshold | ||
""" | ||
nodes_to_remove = [] | ||
for node in EG.nodes(data=True): | ||
data = node[1] | ||
infection_probability = data[NODE_INFECTION_PROBABILITY_ATTR] | ||
if infection_probability < threshold: | ||
nodes_to_remove.append(node[0]) | ||
|
||
EG.remove_nodes_from(nodes_to_remove) | ||
EG.remove_nodes_from(list(nx.isolates(EG))) | ||
|
||
return EG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters