<jupyter_start><jupyter_text>MINCUT parserBy finding the max s-t flow of G, we also simultaneously find the min s-t cut of G, i.e. the set of edges with minimum weight that have to be removed from G so that there is no path from s to t in G.<jupyter_code>import csv
import os

# establish dictionaries
edges = {}
stops = {}

# variable precision in order to cluster nodes
ci=13

# directory where static data is located
directory = '../../data/static/'

# Load data about all stops (nodes) in the Uppsala Network (UL)
for mapp in os.listdir(directory):
    print("Reading directory",mapp)
    
    try:
        with open(directory + mapp + '/stops.txt') as file:
            reader2 = csv.DictReader(file)
            # Create dictionary key for each row in stops.csv with values on name, latitude and longitude
            
            for row in reader2:
                if (row['stop_id'][0:ci] not in stops):
                    print("\t","Stop",row['stop_id'][0:ci],"added to stops dictionary")
                    stops[row['stop_id'][0:ci]] = {'stop_id': row['stop_id'][0:ci], 'stop_name': row['stop_name'],
                                             'stop_lat': row['stop_lat'], 'stop_lon': row['stop_lon']}
    except NotADirectoryError:
        continue
        
# Load data about each edge in Uppsala Network (UL)
with open('./data/ul_edge_flows.csv') as file:
    reader1 = csv.DictReader(file)
    # Create dictionary key for each row in the csv with values on name, latitude and longitude
    for row in reader1:
        edges[row['edge_id']] = {'source_id': row['source_id'], 'sink_id': row['sink_id'],'source_name': row['source_name'], 'sink_name': row['sink_name'],
                                 'index': row['flow']}
        print(row['edge_id'])
import matplotlib.pyplot as plt
import networkx as nx
import math

# Build graph network from dictionary of edges
H = nx.DiGraph()
for i,edge in enumerate(edges.values()):
    H.add_edge(int(edge['source_id']),int(edge['sink_id']),a=round(float(edge['index']),3))
        
pos = nx.spring_layout(H)

nx.draw_networkx_nodes(H, pos, node_size=5)
nx.draw_networkx_edges(H, pos, edge_color='r')<jupyter_output>/Users/x/opt/anaconda3/lib/python3.7/site-packages/networkx/drawing/nx_pylab.py:579: MatplotlibDeprecationWarning: 
The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
  if not cb.iterable(width):
/Users/x/opt/anaconda3/lib/python3.7/site-packages/networkx/drawing/nx_pylab.py:676: MatplotlibDeprecationWarning: 
The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
  if cb.iterable(node_size):  # many node sizes<jupyter_text>The function minimum_cut() returns cut_value that is the total capacity of all cutting edges, as well as 2 sets of nodes each of them is a partition that contains the "s" or the "t" from which you can easily compute the edge_cut.<jupyter_code>import numpy as np
import pandas as pd

def hasPath(s,t):
    return nx.has_path(H,s,t)

def shortestPathLength(s,t):
    return nx.shortest_path_length(H,s,t)

def mincut(s,t):
    cut_value, [reachable, non_reachable] = nx.minimum_cut(H,s,t, capacity='a')
    return [cut_value, len(reachable), len(non_reachable)]

def cardinality(s,t):
    return nx.minimum_node_cut(H,s,t)

def mincutToAllNodes(source_node_id):
    vals = []
    source_id=stops[str(source_node_id)]['stop_id']
    source_name=stops[str(source_node_id)]['stop_name']
    source_lon=stops[str(source_node_id)]['stop_lon']
    source_lat=stops[str(source_node_id)]['stop_lat']
    for node in H.nodes():
        if node != source_node:
            sink_id=stops[str(node)]['stop_id']
            sink_name=stops[str(node)]['stop_name']
            sink_lon=stops[str(node)]['stop_lon']
            sink_lat=stops[str(node)]['stop_lat']
            
            print(round(mincut(source_node_id,node)[0],4),"\t",mincut(source_node_id,node)[1],"\t",mincut(source_node_id,node)[2],"\t",source_id,source_name,sink_id,sink_name)
            vals.append((mincut(source_node_id,node)[0],mincut(source_node_id,node)[1],mincut(source_node_id,node)[2],source_id,source_name,source_lon,source_lat,sink_id,sink_name,sink_lon,sink_lat))

    output = np.array(vals, dtype=str)
    pd.DataFrame(output).to_csv("./mincut_values_from_" + source_id +".csv",index=False,header=['cut_value','reachable','non_reachable','source_id','source_name','source_lon','source_lat','sink_id','sink_name','sink_lon','sink_lat'])

nx.minimum_cut(H,9022003700600,9022003191139,capacity='a')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13344887348353554, 'fraction_numerical': 0.014298093587521665, 'mean_word_length': 4.124306326304106, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '466510', 'license': 'MIT', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1585, 'n_words': 356, 'path': 'ul/notebooks/graph/ul-graph-with-data.ipynb', 'repo_name': 'eriknson/koda', 'size': '4616'}