def test_random_directed_1(self):
     CC = nx.from_numpy_matrix(self.C, create_using=nx.DiGraph)
     lenC = len(max(nx.kosaraju_strongly_connected_components(CC), key=len))
     DD = nx.from_numpy_matrix(self.D, create_using=nx.DiGraph)
     lenD = len(max(nx.kosaraju_strongly_connected_components(DD), key=len))
     self.assertEqual(lenC, (self.C).shape[0])
     self.assertEqual(lenD, (self.D).shape[0])
def DONEnolongerneeded():
    """extracting a part of the metabolic network
       dataset from Stanford University
       output: a connected graph (contains loops, I verifyed)
       of 932 nodes, saved into json file. The BASE.
    """
    f_ll = open("PP-Pathways_ppi.csv", "rt").readlines()
    allEdges = set()
    for line in f_ll:
        tup = tuple(int(i) for i in line.replace("\n", "").split(","))
        if tup[0] > 2500 or tup[1] > 2500:
            continue
        else:
            allEdges.add(tup)
    ALLMETAB = nx.DiGraph()
    ALLMETAB.add_edges_from(list(allEdges))
    print(len(list((ALLMETAB.edges()))))
    print(len(list((ALLMETAB.nodes()))))
    #extract the nodes making part of the largest connected component:
    ho_l = max(nx.kosaraju_strongly_connected_components(ALLMETAB), key=len)
    print(type(ho_l))
    SG = ALLMETAB.subgraph(ho_l)
    print(len(SG.nodes()))
    print(len(SG.edges()))
    print(nx.is_connected(SG.to_undirected()))  #True, ok
    print(nx.is_directed(SG))  # , True ok
    EDGEStxt = ", ".join(list(str(list(tu)) for tu in SG.edges()))
    textjson = '{"nodes": ' + str(len(
        SG.nodes())) + ', "edges": [' + EDGEStxt + ']}'
    with open("METABO.jsonlike", "w") as p:
        p.write(textjson)
        p.close()
    return "done"
 def test_null_graph(self):
     G = nx.DiGraph()
     assert_equal(list(nx.strongly_connected_components(G)), [])
     assert_equal(list(nx.kosaraju_strongly_connected_components(G)), [])
     assert_equal(list(nx.strongly_connected_components_recursive(G)), [])
     assert_equal(len(nx.condensation(G)), 0)
     assert_raises(nx.NetworkXPointlessConcept, nx.is_strongly_connected, nx.DiGraph())
Example #4
0
 def test_null_graph(self):
     G = nx.DiGraph()
     assert list(nx.strongly_connected_components(G)) == []
     assert list(nx.kosaraju_strongly_connected_components(G)) == []
     assert list(nx.strongly_connected_components_recursive(G)) == []
     assert len(nx.condensation(G)) == 0
     pytest.raises(nx.NetworkXPointlessConcept, nx.is_strongly_connected, nx.DiGraph())
    def test_algo_time_nx(self):
        """
        this test check the run time of connected_components and shortest path
        in networkx
        :return:None
        """
        ga1 = GraphAlgo.GraphAlgo()
        filename = '../data/G_100_800_1.json'
        ga1.load_from_json(filename)
        ga = self.graph_nx(ga1.get_graph())

        with self.elapsed_timer() as elapsed:
            star = elapsed()

            l = nx.single_source_dijkstra(ga, 0, 2)
            end = elapsed()
            res = end - star
            try:
                with open("res2.txt", "a") as f:
                    f.write(f"\ngraph:{filename},shortest_path:{res}\n{l}")
                    star = elapsed()
                    # print(list(nx.strongly_connected_components(ga)))
                    print(list(nx.kosaraju_strongly_connected_components(ga)))

                    end = elapsed()
                    res = end - star

                    f.write(f"\ngraph:{filename},connected_component:{res}\n")

            except IOError as e:
                print(e)
Example #6
0
 def find_SCCS(self):
     """
         (1) It first finds the SCCs of the transaction graph.
         verified
     """
     # scc requires a digraph
     graph = self.create_graph(_type='di')
     self.SCCs = list(kosaraju_strongly_connected_components(graph))
     self.SCCs.sort()
Example #7
0
def city_graph_generator(city):
    """
    Generate an OSMnx graph of **city**.

    Graph modified to be undirected, weighted, and meeting the triangle
    inequality.

    Parameters
    ----------
    city: str
        The city to query road network from. *'City, Country'*.

    Returns
    -------
    G: graph
        OSMnx generated graph of **city**.
        Graph is weighted, undirected, and satisfies the triangle inequality.

    Raises
    ------
    AssertionError
        If **city** is not a string.
    """

    # Assert city input is string.
    assert type(city) is str, "City name not valid, must be string."

    # Query and generate OSMNX graph from city.
    O = ox.graph_from_place(city,
                            network_type='drive',
                            simplify=True,
                            retain_all=False,
                            timeout=60)

    # Keep only the largest strongly connected component.
    keep = max(nx.kosaraju_strongly_connected_components(O), key=len)
    G_sub = O.subgraph(keep).copy()
    G_dir = nx.DiGraph(G_sub)
    G = nx.Graph()
    G.graph.update(G_dir.graph)

    # Iterate over edges to keep maximum weight edges
    # in the case where there are parallel edges.
    for u, v in G_dir.edges():
        G.add_nodes_from([(u, G_dir.nodes[u]), (v, G_dir.nodes[v])])
        if (v, u) in G_dir.edges():
            wgt = max(G_dir.edges[u, v]['length'], G_dir.edges[v, u]['length'])
        else:
            wgt = G_dir.edges[u, v]['length']
        G.add_edges_from([(u, v, G_dir.edges[u, v])])
        G.edges[u, v]['length'] = wgt
        G.edges[u, v]['weight'] = wgt

    return G
Example #8
0
    def stronglyConnectedComponents(self) -> list:
        """
        Gets strongly connected components of graph.

        Returns
        -------
        list
            Strongly connected component of specified node
        """
        return list(nodeSet for nodeSet in
                    nx.kosaraju_strongly_connected_components(self.graph))
Example #9
0
 def test_connected_raise(self):
     G = nx.Graph()
     with pytest.raises(NetworkXNotImplemented):
         next(nx.strongly_connected_components(G))
     with pytest.raises(NetworkXNotImplemented):
         next(nx.kosaraju_strongly_connected_components(G))
     with pytest.raises(NetworkXNotImplemented):
         next(nx.strongly_connected_components_recursive(G))
     pytest.raises(NetworkXNotImplemented, nx.is_strongly_connected, G)
     pytest.raises(nx.NetworkXPointlessConcept, nx.is_strongly_connected,
                   nx.DiGraph())
     pytest.raises(NetworkXNotImplemented, nx.condensation, G)
Example #10
0
    def build(self):
        """Finalise the graph, after adding all input files to it."""

        assert not self.final, 'Trying to mutate a final graph.'

        # Replace each strongly connected component with a single node `NodeSet`
        for scc in sorted(nx.kosaraju_strongly_connected_components(self.graph),
                          key=len, reverse=True):
            if len(scc) == 1:
                break

            self.shrink_to_node(NodeSet(scc))

        self.final = True
def createGraphs2(subdata, deg):
    avg = 0
    for j in range(len(subdata)):
        matrix = subdata[j]
        threshold = alzTest.findThresh(matrix, deg)
        for k in range(len(subdata[j])):
            for l in range(len(subdata[j][k])):
                if subdata[j][k][l] > threshold:
                    subdata[j][k][l] = 1
                else:
                    subdata[j][k][l] = 0
        G = nx.DiGraph(subdata[j])
        listoflists = nx.kosaraju_strongly_connected_components(G)
        avg += len(listoflists[0])
    return float(avg / len(subdata))
Example #12
0
def createGraphs2(subdata, deg):
  avg = 0
  for j in range(len(subdata)):
    matrix = subdata[j]
    threshold = alzTest.findThresh(matrix, deg)
    for k in range(len(subdata[j])):
      for l in range(len(subdata[j][k])):
        if subdata[j][k][l] > threshold:
          subdata[j][k][l] = 1
        else:
          subdata[j][k][l] = 0
    G = nx.DiGraph(subdata[j])
    listoflists = nx.kosaraju_strongly_connected_components(G)
    avg += len(listoflists[0])
  return float(avg/len(subdata))
Example #13
0
def print_results(nx_g, cm_g):
	""" Print out result by each algorithm.
	    For testing/debuging purposes only.
	"""
	print('Tarjan class:', [sorted(component) for component in tarjan.Tarjan(cm_g).get_scc()])
	print()
	print('Tarjan func: ', [sorted(component) for component in tarjan.strongly_connected_components(cm_g)])
	print()
	print('Gabow class:', [sorted(component) for component in gabow.Gabow(cm_g).get_scc()])
	print()
	print('Gabow func: ', [sorted(component) for component in gabow.strongly_connected_components(cm_g)])
	print()
	print('Tarjan NX:  ', [sorted(component) for component in nx.strongly_connected_components(nx_g)])
	print()
	print('Kosaraju NX:', [sorted(component) for component in nx.kosaraju_strongly_connected_components(nx_g)])
Example #14
0
def degvssizeAvg(data):
  degreedict = {}
  for deg in range(1, 100, 5):
    subdata = copy.deepcopy(data[('NL', 'corr')])
    threshold = determineThreshold(subdata, deg)
    print "degree: " + str(deg) + " threshold: " + str(threshold)     
    myList = createGraphs(subdata, threshold)
    avg = 0
    for graph in myList:
      listoflists = nx.kosaraju_strongly_connected_components(graph)
      avg += len(listoflists[0])
    avg = float(avg/len(myList))
    degreedict[deg] = avg
    
  plt.plot(degreedict.keys(), degreedict.values(), 'bo')
  plt.show()
def degvssizeAvg(data):
    degreedict = {}
    for deg in range(1, 100, 5):
        subdata = copy.deepcopy(data[('NL', 'corr')])
        threshold = determineThreshold(subdata, deg)
        print "degree: " + str(deg) + " threshold: " + str(threshold)
        myList = createGraphs(subdata, threshold)
        avg = 0
        for graph in myList:
            listoflists = nx.kosaraju_strongly_connected_components(graph)
            avg += len(listoflists[0])
        avg = float(avg / len(myList))
        degreedict[deg] = avg

    plt.plot(degreedict.keys(), degreedict.values(), 'bo')
    plt.show()
Example #16
0
    def stronglyConnectedSubgraphs(self) -> list[Graph[V, E]]:
        """
        Gets strongly connected subgraphs of graph.

        Returns
        -------
        list[Graph[V, E]]
            Graph strongly connected components
        """
        subgraphs: list[Graph[V, E]] = list()
        for nodeSet in nx.kosaraju_strongly_connected_components(self.graph):
            subgraph: Graph[V, E] = Graph(is_directed=self.isDirected,
                                          is_weighted=self.isWeighted)
            subgraph.addNodes(nodeSet)
            for edge in self.graph.edges:
                if edge[0] in nodeSet and edge[1] in nodeSet:
                    subgraph.addEdge(edge[0], edge[1])
            subgraphs.append(subgraph)
        return subgraphs
    def directed(self):
        D = self.Initial_Laplacian(self.size, self.prob)
        for i in range(self.size):
            if D[i][i] == 1.:
                D[i][i] = 0.

        """ Until here, we have the adjacency matrix of arandom directed graph,
            but we are not sure whether it is stringly connected or not! """
        graph = nx.from_numpy_matrix(D, create_using=nx.DiGraph)
        largest = max(nx.kosaraju_strongly_connected_components(graph), key=len)

        adj = np.zeros((len(largest), len(largest)))
        v = 0
        w = 0
        for i in largest:
            for j in largest:
                adj[v][w] = D[i][j]
                w +=1
            w = 0
            v +=1
        return adj
def topological_order_value_iteration(P, states, actions, gamma, eps, max_iteration):
    '''
    A function that performs topological value iteration on the MDP input.
    INPUT:
    - P the transition table
        P[s][a] == [(probability, nextstate, reward, done), ...] 
    - states, the discrete sets of states in the environment
    - actions, the discrete set of actions in the environment
    - gamma, the discount factor for rewards
    OUTPUT:
    value function values for discrete states
    '''
    g, P = build_graph(P, actions, states)
    V = defaultdict(int)
    # returned in reversed topological order
    comps = list(nx.kosaraju_strongly_connected_components(g))
    comps.reverse()
    for comp in comps:
        print("component is: {}".format(comp))
        # update the VI values when you add each component
        V.update(hacky_value_iteration(P, comp, actions, V, gamma, eps, max_iteration))
    return V
Example #19
0
File: test.py Project: TomLex/GAL
#!/usr/bin/env python2

import networkx as nx
import graph, tarjan, gabow

if __name__ == '__main__':
	for i in range(3):
		# generate random graph and convert to custom representation
		nx_graph = nx.gnm_random_graph(15, 25, directed=True)
		custom_graph = graph.nx2custom(nx_graph)

		# get strong connected components
		print "Strongly connected components by Tarjan's algorithm:"
		print [ sorted(component) for component in tarjan.strongly_connected_components(custom_graph) ]
		print
		print "Strongly connected components by Gabow's algorithm:"
		print [ sorted(component) for component in gabow.strongly_connected_components(custom_graph) ]
		print
		print "Strongly connected components by Tarjan's networkx library algorithm:"
		print [ sorted(component) for component in nx.strongly_connected_components(nx_graph) ]
		print
		print "Strongly connected components by Kosaraju's networkx library algorithm:"
		print [ sorted(component) for component in nx.kosaraju_strongly_connected_components(nx_graph) ]
		print '-----------------------------------------------------------------------'
 def test_Expo_directed_2(self):
     C = Exponential(10).directed()
     CC = nx.from_numpy_matrix(C, create_using=nx.DiGraph)
     lenC = len(max(nx.kosaraju_strongly_connected_components(CC), key=len))
     self.assertEqual(lenC, C.shape[0])
Example #21
0
    print("nx: ", f)

    start = time()
    algo.shortest_path(1, 2)
    print(f'shortest_path-Python: {time() - start}')
    print("algo: ", algo.shortest_path(1, 2))

    """---------------connected components compare---------------"""

    print("test 1-SCC")
    g = compare_nx.load_from_json('../data/G_10_80_0.json')
    algo = GraphAlgo()
    algo.load_from_json('../data/G_10_80_0.json')

    start = time()
    f = nx.kosaraju_strongly_connected_components(g)
    print(f'scc-nx: {time() - start}')

    start = time()
    algo.connected_components()
    print(f'scc-Python: {time() - start}')

    print("test 2-SCC")

    g = compare_nx.load_from_json('../data/G_100_800_0.json')
    algo = GraphAlgo()
    algo.load_from_json('../data/G_100_800_0.json')
    start = time()
    f = nx.kosaraju_strongly_connected_components(g)

    print(f'scc-nx: {time() - start}')
Example #22
0
def run_simulation():
    nx_results = [
    ]  # contains the result (in sec) of all the functions in networkx (Short|Component|Components)
    py_results = [
    ]  # contains the result (in sec) of all the functions in python (Short|Component|Components)
    # contains the result (in sec) of all the functions in java (Short|Component|Components)
    java_results = [
        0.0024046, 0.0020393, 0.0286995, 0.3990609, 0.5003517, 0.2987889,
        0.0001906, 0.000615, 0.0005584, 0.0255793, 0.0330737, 0.0009482,
        0.0001255, 0.00016747, 0.0255405, 0.2244528, 0.757112, 0.9485512
    ]
    """-------------------- Shortest_path comparison --------------------"""

    json_graph = [
        '../data/G_10_80_1.json', '../data/G_100_800_1.json',
        '../data/G_1000_8000_1.json', '../data/G_10000_80000_1.json',
        '../data/G_20000_160000_1.json', '../data/G_30000_240000_1.json'
    ]
    i = 1
    for f in json_graph:
        print('----- test', i, ': Shortest_path comparison -----')
        i = i + 1
        graph_nx = compare_nx.load_from_json(f)
        graph_algo = GraphAlgo()
        graph_algo.load_from_json(f)

        time_start = time.time()
        src = int(uniform(0, graph_algo.graph.v_size()))
        dest = int(uniform(0, graph_algo.graph.v_size()))
        list_nx = nx.shortest_path(graph_nx, src, dest, weight='weight')
        time_end = time.time()
        delta = time_end - time_start
        nx_results.append(delta)
        print('Time_nx:', delta)
        print('graph_nx: ', list_nx)

        time_start = time.time()
        graph_algo.shortest_path(src, dest)
        time_end = time.time()
        delta = time_end - time_start
        py_results.append(delta)
        print('Time_python:', delta)
        print('graph_algo:', graph_algo.shortest_path(src, dest), '\n')
    """-------------------- Connected_component comparison --------------------"""

    i = 1
    for f in json_graph:
        print('----- test', i, ': Connected_component (Id_scc) -----')
        i = i + 1
        graph_algo = GraphAlgo()
        graph_algo.load_from_json(f)

        time_start = time.time()
        random = int(uniform(0, graph_algo.graph.v_size()))
        component_list = graph_algo.connected_component(random)
        time_end = time.time()
        delta = time_end - time_start
        py_results.append(delta)
        # print("component_list:", component_list)
        print('Time_python:', delta, '\n')

        nx_results.append(
            0)  # Connected_component does not exist in nx -> always zero
    """-------------------- Connected_components comparison --------------------"""

    i = 1
    for f in json_graph:
        print('----- test', i, ': Connected_componentS (ALL_scc) -----')
        i = i + 1
        graph_nx = compare_nx.load_from_json(f)
        graph_algo = GraphAlgo()
        graph_algo.load_from_json(f)

        time_start = time.time()
        list_nx = nx.kosaraju_strongly_connected_components(graph_nx)
        time_end = time.time()
        delta = time_end - time_start
        nx_results.append(delta)
        print('Time_nx:', delta)

        time_start = time.time()
        graph_algo.connected_components()
        time_end = time.time()
        delta = time_end - time_start
        py_results.append(delta)
        print('Time_python:', delta, '\n')

    # print('----- results -----')
    # print("nx_results:", nx_results)
    # print("py_results:", py_results)
    # print("java_results:", java_results)

    return nx_results, py_results, java_results
def graph_check(graph_matrix, size):
    graph = nx.from_numpy_matrix(graph_matrix, create_using=nx.DiGraph)
    largest = max(nx.kosaraju_strongly_connected_components(graph), key=len)
    assert size == len(largest)
Example #24
0
def compute_SCC_graphs(p):
    graph = nx.DiGraph()
    t_triples, t_cardinality = hdt.search_triples("", p, "")
    print("amount of triples: ", t_cardinality)
    for (l, p, r) in t_triples:
        graph.add_edge(l, r)
    # compute SCC
    start = time.time()
    mydict = {}
    for n in graph.nodes:
        collect_succssor = []
        for s in graph.successors(n):
            collect_succssor.append(s)
        mydict[n] = collect_succssor

    sccs = tarjan(mydict)
    filter_scc = [x for x in sccs if len(x) > 1]
    # compute the counter for ths filtered SCC
    ct = Counter()
    for f in filter_scc:
        ct[len(f)] += 1
    print('SCC info by tarjan : ', ct)

    end = time.time()
    hours, rem = divmod(end - start, 3600)
    minutes, seconds = divmod(rem, 60)
    print("Time taken Tarjan: {:0>2}:{:0>2}:{:05.2f}".format(
        int(hours), int(minutes), seconds))

    start = time.time()

    nx_sccs = nx.strongly_connected_components(graph)
    filter_nx_sccs = [x for x in nx_sccs if len(x) > 1]
    nx_ct = Counter()
    for f in filter_nx_sccs:
        nx_ct[len(f)] += 1

    print('SCC info by original nx : ', nx_ct)
    end = time.time()
    hours, rem = divmod(end - start, 3600)
    minutes, seconds = divmod(rem, 60)
    print("Time taken original: {:0>2}:{:0>2}:{:05.2f}".format(
        int(hours), int(minutes), seconds))

    start = time.time()

    nx_sccs = nx.kosaraju_strongly_connected_components(graph)
    filter_nx_sccs = [x for x in nx_sccs if len(x) > 1]
    nx_ct = Counter()
    for f in filter_nx_sccs:
        nx_ct[len(f)] += 1

    print('SCC info by original nx : ', nx_ct)
    end = time.time()
    hours, rem = divmod(end - start, 3600)
    minutes, seconds = divmod(rem, 60)
    print("Time taken kosaraju: {:0>2}:{:0>2}:{:05.2f}".format(
        int(hours), int(minutes), seconds))

    # obtain the corresponding scc graphs
    scc_graphs = []
    for s in filter_scc:
        g = graph.subgraph(s).copy()
        scc_graphs.append(g)
    return filter_scc, scc_graphs