Lekcja – Król maili / Najlepiej skomunikowane miasta
def add_node(V, node): if node in V.keys(): return else: V[node] = {} def add_edge(V, source, dest, weight): if not source in V.keys(): add_node(V, source) if not dest in V.keys(): add_node(V, dest) if not dest in V[source].keys(): V[source][dest] = weight else: V[source][dest] += weight # http://snap.stanford.edu/data/email-Eu-core.html file_path = r"D:/ALG-GRAPH/data/email-Eu-core.txt" V = {} with open(file_path, 'r') as f: for line in f: u,v = line.split(' ') u = int(u) v = int(v) add_edge(V, u, v, 1) print(f'Number of nodes: {len(V)}') print(f'Number of edges: {sum( [ len(V[n]) for n in V.keys() ] )}') print(f'Max outcoming emails: { max( [ len(V[n]) for n in V.keys() ] ) }') max_mails = max( [ len(V[n]) for n in V.keys() ] ) mail_king = -1 for n in V.keys(): if len(V[n]) == max_mails: print(f'King of the emails is {n}') mail_king = n in_mails = 0 for n in V.keys(): if mail_king in V[n]: in_mails += 1 print(f'Number of incoming emails for {mail_king} is: {in_mails}')
Lab
import csv from operator import itemgetter from cv2 import THRESH_TOZERO from defusedxml import ExternalReferenceForbidden from matplotlib.animation import FFMpegFileWriter from sqlalchemy import false network = {} select_month = 1 with open('data/usa-domestic-flight-2019.csv') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') line_count = 0 for row in csv_reader: if line_count == 0: print(f'Column names are {", ".join(row)}') line_count += 1 else: line_count += 1 origin = row[8] destination = row[11] month = int(row[12]) if month != select_month:# or passengers == 0: continue if origin not in network.keys(): network[origin] = [destination] else: if destination not in network[origin]: network[origin].append(destination) print(f'Processed {line_count} lines.') #print(network) out_degrees = [] for origin, destinations in network.items(): out_degrees.append({ 'name': origin, 'connections' : len(destinations) }) top10 = sorted(out_degrees, key=itemgetter('connections'), reverse=True) for x in top10[:10]: print(f"{x['name']}\t{x['connections']}")