Prerrequisito: Introducción a las Redes Sociales , Conceptos Básicos de Python
Cuando las personas están conectadas en redes entre sí, pueden influir en el comportamiento y las decisiones de los demás. Esto se llama comportamiento en cascada en las redes.
Consideremos un ejemplo, supongamos que todas las personas en una sociedad han adoptado una tendencia X. Ahora viene una nueva tendencia Y y un pequeño grupo acepta esta nueva tendencia y después de esto, sus vecinos también aceptan esta tendencia Y y así sucesivamente.
Entonces, hay 4 ideas principales en Comportamientos en cascada:
- Incrementando el pago.
- Gente clave.
- Impacto de las comunidades en las Cascadas.
- Cascada y Clusters.
A continuación se muestra el código para cada idea.
1. Aumentar el pago.
Python3
# cascade pay off import networkx as nx import matplotlib.pyplot as plt def set_all_B(G): for i in G.nodes(): G.nodes[i]['action'] = 'B' return G def set_A(G, list1): for i in list1: G.nodes[i]['action'] = 'A' return G def get_colors(G): color = [] for i in G.nodes(): if (G.nodes[i]['action'] == 'B'): color.append('red') else: color.append('blue') return color def recalculate(G): dict1 = {} # payoff(A)=a=4 # payoff(B)=b=3 a = 15 b = 5 for i in G.nodes(): neigh = G.neighbors(i) count_A = 0 count_B = 0 for j in neigh: if (G.nodes[j]['action'] == 'A'): count_A += 1 else: count_B += 1 payoff_A = a * count_A payoff_B = b * count_B if (payoff_A >= payoff_B): dict1[i] = 'A' else: dict1[i] = 'B' return dict1 def reset_node_attributes(G, action_dict): for i in action_dict: G.nodes[i]['action'] = action_dict[i] return G def Calculate(G): terminate = True count = 0 c = 0 while (terminate and count < 10): count += 1 # action_dict will hold a dictionary action_dict = recalculate(G) G = reset_node_attributes(G, action_dict) colors = get_colors(G) if (colors.count('red') == len(colors) or colors.count('green') == len(colors)): terminate = False if (colors.count('green') == len(colors)): c = 1 nx.draw(G, with_labels=1, node_color=colors, node_size=800) plt.show() if (c == 1): print('cascade complete') else: print('cascade incomplete') G = nx.erdos_renyi_graph(10, 0.5) nx.write_gml(G, "erdos_graph.gml") G = nx.read_gml('erdos_graph.gml') print(G.nodes()) G = set_all_B(G) # initial adopters list1 = ['2', '1', '3'] G = set_A(G, list1) colors = get_colors(G) nx.draw(G, with_labels=1, node_color=colors, node_size=800) plt.show() Calculate(G)
Producción:
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] cascade complete
2. Personas clave.
Python3
# cascade key people import networkx as nx import matplotlib.pyplot as plt G = nx.erdos_renyi_graph(10, 0.5) nx.write_gml(G, "erdos_graph.gml") def set_all_B(G): for i in G.nodes(): G.nodes[i]['action'] = 'B' return G def set_A(G, list1): for i in list1: G.nodes[i]['action'] = 'A' return G def get_colors(G): color = [] for i in G.nodes(): if (G.nodes[i]['action'] == 'B'): color.append('red') else: color.append('green') return color def recalculate(G): dict1 = {} # payoff(A)=a=4 # payoff(B)=b=3 a = 10 b = 5 for i in G.nodes(): neigh = G.neighbors(i) count_A = 0 count_B = 0 for j in neigh: if (G.nodes[j]['action'] == 'A'): count_A += 1 else: count_B += 1 payoff_A = a * count_A payoff_B = b * count_B if (payoff_A >= payoff_B): dict1[i] = 'A' else: dict1[i] = 'B' return dict1 def reset_node_attributes(G, action_dict): for i in action_dict: G.nodes[i]['action'] = action_dict[i] return G def Calculate(G): continuee = True count = 0 c = 0 while (continuee and count < 100): count += 1 # action_dict will hold a dictionary action_dict = recalculate(G) G = reset_node_attributes(G, action_dict) colors = get_colors(G) if (colors.count('red') == len(colors) or colors.count('green') == len(colors)): continuee = False if (colors.count('green') == len(colors)): c = 1 if (c == 1): print('cascade complete') else: print('cascade incomplete') G = nx.read_gml('erdos_graph.gml') for i in G.nodes(): for j in G.nodes(): if (i < j): list1 = [] list1.append(i) list1.append(j) print(list1, ':', end="") G = set_all_B(G) G = set_A(G, list1) colors = get_colors(G) Calculate(G)
Producción:
['0', '1'] :cascade complete ['0', '2'] :cascade incomplete ['0', '3'] :cascade complete ['0', '4'] :cascade complete ['0', '5'] :cascade incomplete ['0', '6'] :cascade complete ['0', '7'] :cascade complete ['0', '8'] :cascade complete ['0', '9'] :cascade complete ['1', '2'] :cascade complete ['1', '3'] :cascade complete ['1', '4'] :cascade complete ['1', '5'] :cascade complete ['1', '6'] :cascade complete ['1', '7'] :cascade complete ['1', '8'] :cascade complete ['1', '9'] :cascade complete ['2', '3'] :cascade incomplete ['2', '4'] :cascade incomplete ['2', '5'] :cascade incomplete ['2', '6'] :cascade incomplete ['2', '7'] :cascade incomplete ['2', '8'] :cascade incomplete ['2', '9'] :cascade complete ['3', '4'] :cascade complete ['3', '5'] :cascade incomplete ['3', '6'] :cascade complete ['3', '7'] :cascade complete ['3', '8'] :cascade complete ['3', '9'] :cascade complete ['4', '5'] :cascade incomplete ['4', '6'] :cascade complete ['4', '7'] :cascade complete ['4', '8'] :cascade complete ['4', '9'] :cascade incomplete ['5', '6'] :cascade incomplete ['5', '7'] :cascade incomplete ['5', '8'] :cascade incomplete ['5', '9'] :cascade complete ['6', '7'] :cascade complete ['6', '8'] :cascade complete ['6', '9'] :cascade complete ['7', '8'] :cascade complete ['7', '9'] :cascade complete ['8', '9'] :cascade complete
3. Impacto de las comunidades en las Cascadas.
Python3
import networkx as nx import random import matplotlib.pyplot as plt def first_community(G): for i in range(1, 11): G.add_node(i) for i in range(1, 11): for j in range(1, 11): if (i < j): r = random.random() if (r < 0.5): G.add_edge(i, j) return G def second_community(G): for i in range(11, 21): G.add_node(i) for i in range(11, 21): for j in range(11, 21): if (i < j): r = random.random() if (r < 0.5): G.add_edge(i, j) return G G = nx.Graph() G = first_community(G) G = second_community(G) G.add_edge(5, 15) nx.draw(G, with_labels=1) plt.show() nx.write_gml(G, "community.gml")
Producción:
4. Cascada en clústeres.
Python3
import networkx as nx import matplotlib.pyplot as plt def set_all_B(G): for i in G.nodes(): G.nodes[i]['action'] = 'B' return G def set_A(G, list1): for i in list1: G.nodes[i]['action'] = 'A' return G def get_colors(G): color = [] for i in G.nodes(): if (G.nodes[i]['action'] == 'B'): color.append('red') else: color.append('green') return color def recalculate(G): dict1 = {} a = 3 b = 2 for i in G.nodes(): neigh = G.neighbors(i) count_A = 0 count_B = 0 for j in neigh: if (G.nodes[j]['action'] == 'A'): count_A += 1 else: count_B += 1 payoff_A = a * count_A payoff_B = b * count_B if (payoff_A >= payoff_B): dict1[i] = 'A' else: dict1[i] = 'B' return dict1 def reset_node_attributes(G, action_dict): for i in action_dict: G.nodes[i]['action'] = action_dict[i] return G def Calculate(G): terminate = True count = 0 c = 0 while (terminate and count < 100): count += 1 # action_dict will hold a dictionary action_dict = recalculate(G) G = reset_node_attributes(G, action_dict) colors = get_colors(G) if (colors.count('red') == len(colors) or colors.count('green') == len(colors)): terminate = False if (colors.count('green') == len(colors)): c = 1 if (c == 1): print('cascade complete') else: print('cascade incomplete') nx.draw(G, with_labels=1, node_color=colors, node_size=800) plt.show() G = nx.Graph() G.add_nodes_from(range(13)) G.add_edges_from( [(0, 1), (0, 6), (1, 2), (1, 8), (1, 12), (2, 9), (2, 12), (3, 4), (3, 9), (3, 12), (4, 5), (4, 12), (5, 6), (5, 10), (6, 8), (7, 8), (7, 9), (7, 10), (7, 11), (8, 9), (8, 10), (8, 11), (9, 10), (9, 11), (10, 11)]) list2 = [[0, 1, 2, 3], [0, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 12], [2, 3, 4, 12], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6, 12]] for list1 in list2: print(list1) G = set_all_B(G) G = set_A(G, list1) colors = get_colors(G) nx.draw(G, with_labels=1, node_color=colors, node_size=800) plt.show() Calculate(G)
Producción:
[0, 1, 2, 3] cascade incomplete [0, 2, 3, 4] cascade incomplete [1, 2, 3, 4] cascade incomplete [2, 3, 4, 5] cascade incomplete [3, 4, 5, 6] cascade incomplete [4, 5, 6, 12] cascade incomplete [2, 3, 4, 12] cascade incomplete [0, 1, 2, 3, 4, 5] cascade incomplete [0, 1, 2, 3, 4, 5, 6, 12] cascade complete
Publicación traducida automáticamente
Artículo escrito por sankalpsharma424 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA