Ejercicios Graphs, Paths & Components

Ejercicios básicos de Grafos.

Ejercicio - Número de Nodos y Enlaces

(resuelva en código propio y usando la librería NetworkX (python) o iGraph (R))

Cuente el número de nodos y enlaces con los siguientes links (asumiendo que el grafo puede ser dirigido Y no dirigido):


In [1]:
edges = set([(1, 2), (3, 1), (3, 2), (2, 4)])

In [2]:
edges = set([(1, 2), (3, 1), (3, 2), (2, 4)])
edges_list = [i[0] for i in edges] + [i[1] for i in edges]
nodes = set(edges_list)

edges_number = len(edges)
nodes_number = len(nodes)

print ("Número de nodos: " + str(nodes_number))
print ("Número de enlaces: " + str(edges_number))

"""Now using NetorkX"""
import networkx as nx

G = nx.Graph()
G.add_edges_from(edges)

print ("Número de nodos: " + str(G.number_of_nodes()))
print ("Número de aristas: " + str(G.number_of_edges()))


Número de nodos: 4
Número de enlaces: 4
Número de nodos: 4
Número de aristas: 4

Ejercicio - Matriz de Adyacencia

(resuelva en código propio y usando la librería NetworkX (python) o iGraph (R))

Cree la matriz de adyacencia del grafo del ejercicio anterior (para dirigido y no-dirigido)


In [3]:
"""Código propio"""
import numpy as np

edges = set([(1,2), (3, 1), (3, 2), (2, 4)])

def adj_matrix_dgraph(edges):
    edges_list = [i[0] for i in edges] + [i[1] for i in edges]
    nodes = set(edges_list)
    
    """create matrix"""
    matrix = np.zeros((len(nodes),len(nodes)))
    
    for edge in edges:
        matrix[edge[0] - 1,edge[1] -1] = 1
    
    return matrix

def adj_matrix(edges):
    edges_list = [i[0] for i in edges] + [i[1] for i in edges]
    nodes = set(edges_list)
    
    """create matrix"""
    matrix = np.zeros((len(nodes),len(nodes)))
    
    for edge in edges:
        i = edge[0]-1
        j = edge[1]-1
        matrix[i,j] = 1
        matrix[j,i] = 1
    
    return matrix

print ("matriz para grafo dirigido:\n" + str(adj_matrix_dgraph(edges)))
print ("\n")
print ("matriz para grafo no dirigido:\n" + str(adj_matrix(edges)))

"""Solución con NetworkX"""

import networkx as nx
G = nx.Graph()
G.add_edges_from(edges)

matrix = nx.adjacency_matrix(G)

print (matrix)

DG = nx.DiGraph()
DG.add_edges_from(edges)

print ("\n")
print (nx.adjacency_matrix(DG))


matriz para grafo dirigido:
[[ 0.  1.  0.  0.]
 [ 0.  0.  0.  1.]
 [ 1.  1.  0.  0.]
 [ 0.  0.  0.  0.]]


matriz para grafo no dirigido:
[[ 0.  1.  1.  0.]
 [ 1.  0.  1.  1.]
 [ 1.  1.  0.  0.]
 [ 0.  1.  0.  0.]]
  (0, 1)	1
  (0, 2)	1
  (1, 0)	1
  (1, 2)	1
  (1, 3)	1
  (2, 0)	1
  (2, 1)	1
  (3, 1)	1


  (0, 1)	1
  (1, 3)	1
  (2, 0)	1
  (2, 1)	1

D## Ejercicio - Sparseness

Calcule la proporción entre número de links existentes en 3 redes reales (http://snap.stanford.edu/data/index.html) contra el número de links posibles.


In [4]:
import numpy as np

""" The entered datasets correspond to non-directed graphs"""
""" information about the dataset can be found in the following link:
    http://snap.stanford.edu/data/egonets-Facebook.html """

edges1 = np.genfromtxt('0.edges', dtype="int", delimiter=" ")

edges2 = np.genfromtxt('348.edges', dtype="int", delimiter=" ")

edges3 = np.genfromtxt('414.edges', dtype="int", delimiter=" ")

def edges_to_nodes(edges):
    edges_list = [i[0] for i in edges] + [i[1] for i in edges]
    nodes = set(edges_list)
    
    return nodes


def edge_rate(edges):
    nodes = edges_to_nodes(edges)
    n = len(nodes)
    
    print ("len(n) = %d" %(n))
    
    """ For a non-directed graph and excluding reflexive relations"""
    possible_edges = (n*(n-1))/2
    
    print ("possible_edges=%d" % (possible_edges))
    result = float(len(edges))/possible_edges
    return result

def edge_rate_dgraph(edges):
    nodes = edges_to_nodes(edges)
    n = len(nodes)
    
    """ For a directed graph including reflexive relations"""
    possible_edges = n**2
    result = float(len(edges))/possible_edges
    return result

print (edge_rate(edges1))
print (edge_rate(edges2))
print (edge_rate(edges3))

""" With networkx """

import networkx as nx

G1 = nx.read_edgelist('0.edges', delimiter=" ")
G2 = nx.read_edgelist('348.edges', delimiter=" ")
G3 = nx.read_edgelist('414.edges', delimiter=" ")


def possible_edges(graph):
    nodes = graph.number_of_nodes()
    return (nodes*(nodes-1))/2

print ("possible_edges(G1)=%d" % (possible_edges(G1)))

def edge_rate_nx(graph):
    return float(graph.number_of_edges())/float(possible_edges(graph))

print ("\n")
print (edge_rate_nx(G1))
print (edge_rate_nx(G2))
print (edge_rate_nx(G3))


len(n) = 333
possible_edges=55278
0.09113933210318753
len(n) = 224
possible_edges=24976
0.2556053811659193
len(n) = 150
possible_edges=11175
0.3029977628635347
possible_edges(G1)=55278


0.045569666051593766
0.12780269058295965
0.15149888143176735

En la matriz de adyacencia de cada uno de las redes elegidas, cuantos ceros hay?


In [5]:
""" Without NetworkX """
import numpy as np

def edges_to_nodes(edges):
    edges_list = [i[0] for i in edges] + [i[1] for i in edges]
    nodes = set(edges_list)
    print ("len(nodes)=%d" %(len(nodes)))
    return nodes

""" The entered datasets correspond to non-directed graphs"""
""" information about the dataset can be found in the following link:
    http://snap.stanford.edu/data/egonets-Facebook.html """

edges1 = np.genfromtxt('0.edges', dtype="int", delimiter=" ")
print (len(edges1))

edges2 = np.genfromtxt('348.edges', dtype="int", delimiter=" ")
print (len(edges2))

edges3 = np.genfromtxt('414.edges', dtype="int", delimiter=" ")
print (len(edges3))

""" Asuming there aren't repeated elements in the dataset """

def number_of_zeroes(edges):
    n = len(edges_to_nodes(edges))
    zeroes = n**2 - len(edges)
    return zeroes

def number_of_zeroes_dgraph(edges):
    n = len(edges_to_nodes(edges))
    zeroes = n**2 - len(edges)
    return zeroes


print ("number_of_zeroes(edges1)=%d" %(number_of_zeroes(edges1)))
print ("number_of_zeroes(edges2)=%d" %(number_of_zeroes(edges2)))
print ("number_of_zeroes(edges3)=%d" %(number_of_zeroes(edges3)))


5038
6384
3386
len(nodes)=333
number_of_zeroes(edges1)=105851
len(nodes)=224
number_of_zeroes(edges2)=43792
len(nodes)=150
number_of_zeroes(edges3)=19114

In [6]:
""" With NetworkX """
import networkx as nx

""" The selected datasets are non-directed graphs. Therefore their adjacency matrix is simetrical """

""" For undirected graphs NetworkX stores only the edges of one of the matrix's triangles (upper or lower)"""

G1 = nx.read_edgelist('0.edges', delimiter=" ")
print (len(G1.edges()))

G2 = nx.read_edgelist('348.edges', delimiter=" ")
print (len(G2.edges()))

G3 = nx.read_edgelist('414.edges', delimiter=" ")
print (len(G3.edges()))

N1 = len(G1.nodes())
N2 = len(G2.nodes())
N3 = len(G3.nodes())

def zeroes(graph):
    N = len(graph.nodes())
    result = N**2 - 2*len(graph.edges())
    print ("zeroes=%d" %(result))
    return result

zeroes(G1)
zeroes(G2)
zeroes(G3)


2519
3192
1693
zeroes=105851
zeroes=43792
zeroes=19114
Out[6]:
19114

Ejercicio - Redes Bipartitas

Defina una red bipartita y genere ambas proyecciones, explique qué son los nodos y links tanto de la red original como de las proyeccciones


In [7]:
import numpy as np

network1 = set([(1,'a'),(3,'b'), (4,'d'),(5,'b'),(1,'b'), (2,'d'), (1,'d'), (3,'c')])

def projection_u(edges):
    edges_list = list(edges)
    result = []
    for i in range(0,len(edges_list)):
        for j in range(i+1, len(edges_list)):
            if edges_list[i][1] == edges_list[j][1]:
                tup = (edges_list[i][0], edges_list[j][0])
                result.append(tup)
    
    return set(result)

print (projection_u(network1))

def projection_v(edges):
    edges_list = list(edges)
    result = []
    for i in range(0,len(edges_list)):
        for j in range(i+1, len(edges_list)):
            if edges_list[i][0] == edges_list[j][0]:
                tup = (edges_list[i][1], edges_list[j][1])
                result.append(tup)
    
    return set(result)

print (projection_v(network1))


{(5, 1), (3, 1), (2, 1), (4, 2), (4, 1), (3, 5)}
{('d', 'a'), ('d', 'b'), ('a', 'b'), ('b', 'c')}

In [8]:
""" With NetworkX """
import numpy as np
import networkx as nx
from networkx.algorithms import bipartite

edges = set([(1,'a'),(3,'b'), (4,'d'),(5,'b'),(1,'b'), (2,'d'), (1,'d'), (3,'c')])
B = nx.Graph()
B.add_nodes_from([1,2,3,4,5], bipartite=0)
B.add_nodes_from(['a','b','c','d'], bipartite=1)
B.add_edges_from(edges)

projection_u = bipartite.projected_graph(B, [1,2,3,4,5])
print (projection_u.edges())

projection_v = bipartite.projected_graph(B, ['a','b','c','d'])
print (projection_v.edges())


[(1, 2), (1, 3), (1, 4), (1, 5), (2, 4), (3, 5)]
[('a', 'b'), ('a', 'd'), ('b', 'c'), ('b', 'd')]

Explicación

Los nodos de la red original corresponden a elementos pertenecientes a dos conjuntos U y V. Para este caso particular los conjuntos son disjuntos y tienen tipos diferentes de datos. los links de la red original representan relaciones entre ambos conjuntos.

En la red de proyecciones de U, los nodos representan elementos de un mismo conjunto y los enlaces representan relaciones entre elementos del mismo conjunto definidas a partir de elementos comunes del conjunto V con el cual un par de elementos x,y del conjunto U se relacionan. i.e. si 'x' y 'y', pertenecientes al conjunto, U se relacionan con un elemento 'w', perteneciente al conjunto V, entonces 'x' se relaciona con 'y', i.e. hay un link entre 'x' y 'y'.

Para el caso de las proyecciones de V, la definición varía en que si un par de elementos 'x','y' pertenecientes al conjunto V se relacionan, i.e. hay un link entre ellos, entonces 'x' y 'y' se relacionan en la proyección de V.

Ejercicio - Paths

Cree un grafo de 5 nodos con 5 enlaces. Elija dos nodos cualquiera e imprima:

  • 5 Paths diferentes entre los nodos
  • El camino mas corto entre los nodos
  • El diámetro de la red
  • Un self-avoiding path

In [9]:
import numpy as np

edges = set([(1,4),(2,3),(2,4),(2,5),(3,4),(4,1),(3,2),(4,2),(5,2),(4,3)])

graph = {1:[4],
         2:[3,4,5],
         3:[2,4],
         4:[3,2],
         5:[2]}

print (edges)
""" From 1 to 5 """
path1 = [1,4,3,2,5]
path2 = [1,4,2,5]
path3 = [1,4,2,3,2,5]
path4 = [1,4,3,4,2,5]
path5 = [1,4,2,4,2,5]

print(path1)
print(path2)
print(path3)
print(path4)
print(path5)

""" Shortest path"""
def find_shortest_path(graph, start, end, path=[]):
    path = path + [start]
    if start == end:
        return path
    if not start in graph:
        return None
    shortest = None
    for node in graph[start]:
        if node not in path:
            newpath = find_shortest_path(graph, node, end, path)
            if newpath:
                if not shortest or len(newpath) < len(shortest):
                    shortest = newpath
    return shortest
print ("Shortest path is: %s" %(str(find_shortest_path(graph,1,5))))


{(4, 1), (3, 2), (5, 2), (1, 4), (2, 3), (4, 3), (4, 2), (2, 5), (3, 4), (2, 4)}
[1, 4, 3, 2, 5]
[1, 4, 2, 5]
[1, 4, 2, 3, 2, 5]
[1, 4, 3, 4, 2, 5]
[1, 4, 2, 4, 2, 5]
Shortest path is: [1, 4, 2, 5]

In [10]:
import numpy as np
import networkx as nx

edges = set([(1,4),(2,3),(2,4),(2,5),(3,4),(4,1),(3,2),(4,2),(5,2),(4,3)])

G = nx.Graph()
G.add_edges_from(edges)

for path in nx.all_simple_paths(G, source=1,target=5):
    print (path)
    
print ("Shortest path is: %s" %(str(nx.shortest_path(G,source=1,target=5))))


[1, 4, 3, 2, 5]
[1, 4, 2, 5]
Shortest path is: [1, 4, 2, 5]

Ejercicio - Componentes

Baje una red real (http://snap.stanford.edu/data/index.html) y lea el archivo


In [11]:
import numpy as np

""" The entered datasets correspond to non-directed graphs"""
""" information about the dataset can be found in the following link:
    http://snap.stanford.edu/data/egonets-Facebook.html """

edges1 = np.genfromtxt('0.edges', dtype="int", delimiter=" ")

In [12]:
""" With NetworkX """
import numpy as np
import networkx as nx

G1 = nx.read_edgelist('0.edges', delimiter=" ")

Utilizando NetworkX o iGraph descubra el número de componentes


In [13]:
import numpy as np
import networkx as nx

G = nx.read_edgelist('0.edges', delimiter=" ")

print ("number_connected_components=%d" %(nx.number_connected_components(G)))


number_connected_components=5

Implemente el algorithmo Breadth First para encontrar el número de componentes (revise que el resultado es el mismo que utilizando la librería)


In [14]:
import numpy as np
import collections
from IPython.core.debugger import Tracer

""" The entered datasets correspond to non-directed graphs"""
""" information about the dataset can be found in the following link:
    http://snap.stanford.edu/data/egonets-Facebook.html """

edges1 = np.genfromtxt('0.edges', dtype="int", delimiter= " ")

edges2 = set([(1,2),(2,3),(2,4),(3,4),(4,5),(4,6),(7,)])


e = list(edges2)

print ("Tuple 5 is:" + str(e[4][0]))
print ("Tuple 5 is:" + str(e[4][0]))

""" This method is for undirected graphs """

def edges_to_graph(edges):
    edges = list(edges)
    graph = {}
    
    for i in range(0,len(edges)):
        
        if graph.get(edges[i][0], None):
            graph[edges[i][0]].add(edges[i][1])
        else:
            if len(edges[i]) == 2:
                graph[edges[i][0]] = set([edges[i][1]])
            else:
                graph[edges[i][0]] = set([])
        
        if len(edges[i]) == 2:
            if graph.get(edges[i][1], None):
                graph[edges[i][1]].add(edges[i][0])
            else:
                graph[edges[i][1]] = set([edges[i][0]])

    return graph

graph1 = edges_to_graph(edges1)
graph2 = edges_to_graph(edges2)
print (str(edges_to_graph(edges2)))

""" The following code snippet was taken from Mann, Edd. Depth-First Search and Breadth-First Search in Python.
    http://eddmann.com/posts/depth-first-search-and-breadth-first-search-in-python/ """

def bfs(graph, start):
    visited, queue = set(), collections.deque([start])
    while queue:
        vertex = queue.popleft()
        if vertex not in visited:
            visited.add(vertex)
            queue.extend(graph[vertex] - visited)
    return visited

def number_connected_components(graph):
    nodes = set(graph.keys())
    num = 0
    while len(nodes):
        root = next(iter(nodes))
        visited = bfs(graph, root)
        nodes = nodes - visited
        num += 1
        
    return num

print (str(number_connected_components(graph2)))

print (str(number_connected_components(graph1)))


Tuple 5 is:7
Tuple 5 is:7
{1: {2}, 2: {1, 3, 4}, 4: {2, 3, 5, 6}, 6: {4}, 5: {4}, 3: {2, 4}, 7: set()}
2
5

Ejercicio - Degree distribution

(resuelva en código propio y usando la librería NetworkX (python) o iGraph (R))

Haga un plot con la distribución de grados de la red real


In [30]:
import numpy as np
import networkx as nx
import seaborn as sns

%matplotlib inline

edges1 = np.genfromtxt('0.edges', dtype="int", delimiter=" ")

graph1 = edges_to_graph(edges1)

def degrees(graph):
    degrees = {}
    for node, links in graph.items():
        degrees[node] = len(links)
    return degrees

d = degrees(graph1)

ax = sns.distplot(list(d.values()))

""" With NetworkX """
G = nx.read_edgelist('0.edges', delimiter=" ")

print (G.degree())

bx = sns.distplot(list(G.degree().values()))


{'236': 36, '186': 43, '122': 62, '285': 46, '24': 15, '346': 26, '271': 72, '304': 54, '176': 13, '9': 56, '130': 15, '329': 29, '204': 21, '213': 38, '252': 64, '332': 42, '82': 33, '65': 11, '276': 17, '26': 67, '280': 42, '272': 44, '211': 29, '199': 46, '84': 12, '133': 17, '62': 25, '239': 58, '172': 40, '322': 71, '53': 30, '3': 16, '170': 45, '175': 16, '46': 4, '56': 77, '254': 16, '194': 18, '231': 20, '117': 5, '127': 15, '135': 9, '103': 15, '188': 47, '23': 16, '116': 16, '73': 9, '299': 19, '288': 3, '315': 55, '119': 61, '323': 38, '48': 21, '57': 14, '200': 56, '98': 48, '313': 36, '63': 5, '344': 8, '67': 75, '118': 35, '325': 38, '277': 64, '134': 18, '270': 3, '76': 2, '36': 10, '223': 26, '274': 13, '88': 19, '21': 64, '339': 26, '108': 12, '197': 15, '169': 37, '275': 9, '273': 8, '83': 6, '28': 12, '312': 25, '242': 23, '214': 16, '20': 14, '307': 3, '71': 2, '333': 7, '207': 2, '168': 10, '308': 23, '341': 11, '128': 27, '334': 27, '238': 22, '265': 26, '141': 27, '78': 8, '345': 15, '317': 6, '158': 24, '38': 8, '302': 19, '27': 4, '54': 7, '139': 8, '109': 36, '291': 35, '142': 42, '203': 56, '105': 13, '232': 24, '64': 6, '217': 7, '248': 20, '126': 6, '224': 27, '261': 37, '283': 4, '144': 14, '226': 13, '290': 13, '25': 68, '342': 33, '146': 9, '300': 6, '94': 21, '1': 16, '184': 17, '159': 13, '149': 13, '13': 30, '59': 18, '17': 12, '326': 18, '80': 22, '187': 15, '161': 24, '66': 14, '31': 22, '136': 21, '7': 19, '255': 1, '49': 3, '320': 20, '85': 13, '246': 13, '123': 17, '284': 15, '140': 10, '137': 15, '343': 17, '115': 20, '297': 24, '185': 25, '104': 31, '324': 25, '171': 7, '111': 13, '14': 14, '310': 12, '32': 5, '30': 16, '222': 10, '92': 20, '72': 23, '40': 43, '125': 3, '266': 17, '212': 17, '278': 9, '340': 5, '237': 6, '311': 6, '309': 9, '330': 15, '230': 8, '70': 1, '16': 8, '249': 23, '39': 14, '251': 13, '10': 9, '55': 16, '228': 2, '69': 9, '113': 39, '258': 14, '257': 17, '196': 12, '156': 11, '303': 20, '286': 1, '81': 2, '174': 3, '293': 2, '33': 1, '42': 1, '347': 6, '2': 9, '281': 15, '263': 6, '279': 1, '51': 6, '338': 6, '162': 7, '19': 15, '75': 13, '218': 8, '268': 10, '314': 12, '5': 12, '178': 12, '192': 4, '243': 7, '195': 8, '181': 9, '8': 7, '245': 4, '129': 6, '328': 8, '331': 19, '150': 10, '101': 18, '99': 12, '296': 6, '102': 5, '318': 10, '163': 5, '173': 5, '165': 10, '121': 11, '306': 8, '177': 10, '180': 19, '87': 12, '189': 6, '60': 7, '259': 7, '45': 11, '58': 3, '269': 5, '167': 6, '227': 14, '96': 8, '91': 7, '143': 11, '61': 2, '93': 7, '41': 23, '235': 4, '6': 5, '89': 7, '107': 2, '79': 11, '208': 6, '132': 15, '221': 7, '301': 2, '250': 4, '106': 7, '29': 12, '337': 8, '100': 8, '110': 4, '264': 4, '225': 9, '86': 5, '152': 4, '148': 19, '295': 9, '201': 3, '289': 3, '260': 7, '160': 1, '321': 2, '68': 8, '131': 6, '22': 10, '298': 10, '262': 3, '220': 3, '234': 1, '44': 5, '50': 10, '193': 4, '97': 2, '124': 3, '112': 2, '90': 1, '179': 2, '216': 1, '151': 6, '145': 1, '157': 2, '4': 9, '327': 3, '95': 5, '120': 3, '247': 2, '190': 3, '282': 1, '244': 1, '319': 7, '233': 1, '256': 1, '166': 3, '202': 3, '155': 2, '191': 2, '147': 5, '206': 3, '229': 5, '138': 1, '164': 2, '240': 2, '77': 5, '219': 5, '305': 1, '153': 1, '154': 1, '198': 1, '182': 2, '336': 2, '241': 1, '294': 2, '253': 2, '267': 1, '316': 1, '205': 1, '47': 1, '183': 1, '52': 1, '34': 1, '35': 1}
/home/sangeea/anaconda3/lib/python3.6/site-packages/statsmodels/nonparametric/kdetools.py:20: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  y = X[:m/2+1] + np.r_[0,X[m/2+1:],0]*1j

Calcule el grado promedio


In [33]:
""" Without NetworkX """

def k_mean(graph):
    d = degrees(graph)
    
    return (float(sum(d.values()))/len(d))

print (k_mean(graph1))

""" With NetworkX """
d_nx = G.degree()
k_mean_nx = sum(d_nx.values())/len(d_nx)
print (k_mean_nx)


15.12912912912913
15.12912912912913

Ejercicio - Diámetro


In [34]:
N = 5

Cree un grafo de N nodos con el máximo diámetro posible


In [36]:
""" This is a method for undirected graphs """
def max_k(number_of_nodes):
    edges = []
    for i in range(1, number_of_nodes):
        edges.append((i, i+1))
    
    graph = edges_to_graph(edges)
    return graph

G = max_k(100)
print(G)


{1: {2}, 2: {1, 3}, 3: {2, 4}, 4: {3, 5}, 5: {4, 6}, 6: {5, 7}, 7: {8, 6}, 8: {9, 7}, 9: {8, 10}, 10: {9, 11}, 11: {10, 12}, 12: {11, 13}, 13: {12, 14}, 14: {13, 15}, 15: {16, 14}, 16: {17, 15}, 17: {16, 18}, 18: {17, 19}, 19: {18, 20}, 20: {19, 21}, 21: {20, 22}, 22: {21, 23}, 23: {24, 22}, 24: {25, 23}, 25: {24, 26}, 26: {25, 27}, 27: {26, 28}, 28: {27, 29}, 29: {28, 30}, 30: {29, 31}, 31: {32, 30}, 32: {33, 31}, 33: {32, 34}, 34: {33, 35}, 35: {34, 36}, 36: {35, 37}, 37: {36, 38}, 38: {37, 39}, 39: {40, 38}, 40: {41, 39}, 41: {40, 42}, 42: {41, 43}, 43: {42, 44}, 44: {43, 45}, 45: {44, 46}, 46: {45, 47}, 47: {48, 46}, 48: {49, 47}, 49: {48, 50}, 50: {49, 51}, 51: {50, 52}, 52: {51, 53}, 53: {52, 54}, 54: {53, 55}, 55: {56, 54}, 56: {57, 55}, 57: {56, 58}, 58: {57, 59}, 59: {58, 60}, 60: {59, 61}, 61: {60, 62}, 62: {61, 63}, 63: {64, 62}, 64: {65, 63}, 65: {64, 66}, 66: {65, 67}, 67: {66, 68}, 68: {67, 69}, 69: {68, 70}, 70: {69, 71}, 71: {72, 70}, 72: {73, 71}, 73: {72, 74}, 74: {73, 75}, 75: {74, 76}, 76: {75, 77}, 77: {76, 78}, 78: {77, 79}, 79: {80, 78}, 80: {81, 79}, 81: {80, 82}, 82: {81, 83}, 83: {82, 84}, 84: {83, 85}, 85: {84, 86}, 86: {85, 87}, 87: {88, 86}, 88: {89, 87}, 89: {88, 90}, 90: {89, 91}, 91: {90, 92}, 92: {91, 93}, 93: {92, 94}, 94: {93, 95}, 95: {96, 94}, 96: {97, 95}, 97: {96, 98}, 98: {97, 99}, 99: {98, 100}, 100: {99}}
1.98

Cree un grafo de N nodos con el mínimo diámetro posible


In [38]:
""" This is a method for undirected graphs """
def clique(number_of_nodes):
    edges = []
    for i in range(1, number_of_nodes+1):
        for j in range (1, number_of_nodes+1):
            if (i != j):
                edges.append((i, j))
    
    graph = edges_to_graph(edges)
    return graph

G = clique(10)
print(G)


{1: {2, 3, 4, 5, 6, 7, 8, 9, 10}, 2: {1, 3, 4, 5, 6, 7, 8, 9, 10}, 3: {1, 2, 4, 5, 6, 7, 8, 9, 10}, 4: {1, 2, 3, 5, 6, 7, 8, 9, 10}, 5: {1, 2, 3, 4, 6, 7, 8, 9, 10}, 6: {1, 2, 3, 4, 5, 7, 8, 9, 10}, 7: {1, 2, 3, 4, 5, 6, 8, 9, 10}, 8: {1, 2, 3, 4, 5, 6, 7, 9, 10}, 9: {1, 2, 3, 4, 5, 6, 7, 8, 10}, 10: {1, 2, 3, 4, 5, 6, 7, 8, 9}}

Cree un grafo de N nodos que sea un ciclo simple


In [41]:
""" This is a method for undierected graphs """
def simple_loop(number_of_nodes):
    edges = []
    for i in range(1, number_of_nodes):
        edges.append((i,i+1))
    
    edges.append((number_of_nodes,1))
    graph = edges_to_graph(edges)
    
    return graph

G = simple_loop(5)
print(G)


{1: {2, 5}, 2: {1, 3}, 3: {2, 4}, 4: {3, 5}, 5: {1, 4}}

Ejercicio - Pregunta "real"

Una aerolínea tiene las siguientes rutas desde las ciudades a las que sirve (cada par tiene servicio en ambas direcciones).


In [45]:
routemap =  [('St. Louis', 'Miami'), 
             ('St. Louis', 'San Diego'), 
             ('St. Louis', 'Chicago'), 
             ('San Diego', 'Chicago'), 
             ('San Diego', 'San Francisco'), 
             ('San Diego', 'Minneapolis'), 
             ('San Diego', 'Boston'), 
             ('San Diego', 'Portland'), 
             ('San Diego', 'Seattle'), 
             ('Tulsa', 'New York'), 
             ('Tulsa', 'Dallas'), 
             ('Phoenix', 'Cleveland'), 
             ('Phoenix', 'Denver'), 
             ('Phoenix', 'Dallas'), 
             ('Chicago', 'New York'), 
             ('Chicago', 'Los Angeles'), 
             ('Miami', 'New York'), 
             ('Miami', 'Philadelphia'), 
             ('Miami', 'Denver'), 
             ('Boston', 'Atlanta'), 
             ('Dallas', 'Cleveland'), 
             ('Dallas', 'Albuquerque'), 
             ('Philadelphia', 'Atlanta'), 
             ('Denver', 'Minneapolis'), 
             ('Denver', 'Cleveland'), 
             ('Albuquerque', 'Atlanta'), 
             ('Minneapolis', 'Portland'), 
             ('Los Angeles', 'Seattle'), 
             ('San Francisco', 'Portland'), 
             ('San Francisco', 'Seattle'), 
             ('San Francisco', 'Cleveland'), 
             ('Seattle', 'Portland')]

Cuál es el máximo número de intercambios que tendría que hacer un pasajero en un solo viaje entre dos ciudades servidas? (suponiendo rutas óptimas)


In [46]:
def edges_to_graph(edges):
    edges = list(edges)
    graph = {}
    
    for i in range(0,len(edges)):
        
        if graph.get(edges[i][0], None):
            graph[edges[i][0]].add(edges[i][1])
        else:
            if len(edges[i]) == 2:
                graph[edges[i][0]] = set([edges[i][1]])
            else:
                graph[edges[i][0]] = set([])
        
        if len(edges[i]) == 2:
            if graph.get(edges[i][1], None):
                graph[edges[i][1]].add(edges[i][0])
            else:
                graph[edges[i][1]] = set([edges[i][0]])

    return graph


""" This function was taken from Python Software Foundation.
    Python Patterns - Implementing Graphs. https://www.python.org/doc/essays/graphs/ 
    (Visited in march 2017) """
def find_shortest_path(graph, start, end, path=[]):
    path = path + [start]
    if start == end:
        return path
    if not start in graph:
        return None
    shortest = None
    for next in graph[start]:
        if next not in path:
            newpath = find_shortest_path(graph, next, end, path)
            if newpath:
                if not shortest or len(newpath) < len(shortest):
                    shortest = newpath
    return shortest

graph1 = edges_to_graph(routemap)

def adjacency_matrix(graph):
    keys = list(graph.keys())
    keys.sort()
    
    adj_matrix = np.zeros((len(keys),len(keys)))
    
    for node, edges in graph.items():
        for edge in edges:
            adj_matrix[keys.index(node)][keys.index(edge)] = 1
    
    return (adj_matrix, keys)
            

adj_matrix = adjacency_matrix(graph1)

def distance_matrix(graph):
    keys = list(graph.keys())
    keys.sort()
    
    d_matrix = np.zeros((len(keys),len(keys)))
    
    for i in range(0, len(keys)):
        for j in range(0, len(keys)):
            start = keys[i]
            end = keys[j]
            path = find_shortest_path(graph, start, end)
            d_matrix[i][j] = len(path)-1
    
    return (d_matrix, keys)


def max_distance(graph):
    result = distance_matrix(graph)
    keys = result[1]
    d_matrix = result[0]
    
    result = 0
    result_i = -1
    result_j = -1
    for i in range(0, len(keys)):
        for j in range(0, len(keys)):
            if d_matrix[i][j] >= result:
                result = d_matrix[i][j]
                result_i = i
                result_j = j
    
    start = keys[result_i]
    end = keys[result_j]
    
    return (start, end, result)

print (distance_matrix(graph1)[0])
print (max_distance(graph1))


[[ 0.  1.  2.  4.  2.  1.  3.  5.  3.  4.  3.  2.  2.  4.  3.  3.  4.  4.
   2.]
 [ 1.  0.  1.  3.  3.  2.  3.  4.  2.  3.  3.  1.  3.  3.  2.  3.  3.  3.
   3.]
 [ 2.  1.  0.  2.  3.  3.  3.  3.  3.  2.  3.  2.  4.  2.  1.  2.  2.  2.
   4.]
 [ 4.  3.  2.  0.  3.  3.  3.  1.  2.  2.  1.  3.  4.  2.  1.  2.  2.  1.
   2.]
 [ 2.  3.  3.  3.  0.  1.  1.  3.  2.  2.  3.  3.  1.  2.  2.  1.  2.  3.
   2.]
 [ 1.  2.  3.  3.  1.  0.  2.  4.  3.  3.  2.  3.  1.  3.  3.  2.  3.  4.
   1.]
 [ 3.  3.  3.  3.  1.  2.  0.  4.  1.  1.  2.  2.  1.  2.  2.  2.  3.  2.
   3.]
 [ 5.  4.  3.  1.  3.  4.  4.  0.  3.  3.  2.  4.  4.  2.  2.  2.  1.  2.
   3.]
 [ 3.  2.  3.  2.  2.  3.  1.  3.  0.  2.  1.  1.  2.  3.  2.  3.  3.  1.
   2.]
 [ 4.  3.  2.  2.  2.  3.  1.  3.  2.  0.  3.  3.  2.  1.  1.  2.  2.  2.
   4.]
 [ 3.  3.  3.  1.  3.  2.  2.  2.  1.  3.  0.  2.  3.  3.  2.  3.  3.  2.
   1.]
 [ 2.  1.  2.  3.  3.  3.  2.  4.  1.  3.  2.  0.  3.  4.  3.  4.  4.  2.
   3.]
 [ 2.  3.  4.  4.  1.  1.  1.  4.  2.  2.  3.  3.  0.  3.  3.  2.  3.  3.
   2.]
 [ 4.  3.  2.  2.  2.  3.  2.  2.  3.  1.  3.  4.  3.  0.  1.  1.  1.  2.
   4.]
 [ 3.  2.  1.  1.  2.  3.  2.  2.  2.  1.  2.  3.  3.  1.  0.  1.  1.  1.
   3.]
 [ 3.  3.  2.  2.  1.  2.  2.  2.  3.  2.  3.  4.  2.  1.  1.  0.  1.  2.
   3.]
 [ 4.  3.  2.  2.  2.  3.  3.  1.  3.  2.  3.  4.  3.  1.  1.  1.  0.  2.
   4.]
 [ 4.  3.  2.  1.  3.  4.  2.  2.  1.  2.  2.  2.  3.  2.  1.  2.  2.  0.
   3.]
 [ 2.  3.  4.  2.  2.  1.  3.  3.  2.  4.  1.  3.  2.  4.  3.  3.  4.  3.
   0.]]
('Los Angeles', 'Albuquerque', 5.0)

Si usted necesitara viajar mucho en esta aerolínea, cual sería el lugar óptimo para vivir? (i.e. minimizar el número de intercambios para llegar a cualquier ciudad)


In [47]:
""" The for optimal place to live I understand the city with the least number of leaps """

def optimal_place(graph):
    result = distance_matrix(graph)
    keys = result[1]
    d_matrix = result[0]
    
    total_leaps = 3000000000
    result_i = -1
    
    for i in range(0, len(keys)):
        current_sum = sum(d_matrix[i])
        if current_sum < total_leaps:
            total_leaps = current_sum
            result_i = i
            
    city = keys[result_i]
    vector = d_matrix[result_i]
    
    return (city, total_leaps, vector)

print (optimal_place(graph1))


('San Diego', 34.0, array([ 3.,  2.,  1.,  1.,  2.,  3.,  2.,  2.,  2.,  1.,  2.,  3.,  3.,
        1.,  0.,  1.,  1.,  1.,  3.]))

Visualice la red


In [48]:
G = nx.Graph()
G.add_edges_from(routemap)

nx.draw(G)


/home/sangeea/anaconda3/lib/python3.6/site-packages/networkx/drawing/nx_pylab.py:126: MatplotlibDeprecationWarning: pyplot.hold is deprecated.
    Future behavior will be consistent with the long-time default:
    plot commands add elements without first clearing the
    Axes and/or Figure.
  b = plt.ishold()
/home/sangeea/anaconda3/lib/python3.6/site-packages/networkx/drawing/nx_pylab.py:138: MatplotlibDeprecationWarning: pyplot.hold is deprecated.
    Future behavior will be consistent with the long-time default:
    plot commands add elements without first clearing the
    Axes and/or Figure.
  plt.hold(b)
/home/sangeea/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py:917: UserWarning: axes.hold is deprecated. Please remove it from your matplotlibrc and/or style files.
  warnings.warn(self.msg_depr_set % key)
/home/sangeea/anaconda3/lib/python3.6/site-packages/matplotlib/rcsetup.py:152: UserWarning: axes.hold is deprecated, will be removed in 3.0
  warnings.warn("axes.hold is deprecated, will be removed in 3.0")