예제 #1
0
 def test_parse_graph6(self):
     data = """DF{"""
     G = nx.parse_graph6(data)
     assert_equal(sorted(G.nodes()), [0, 1, 2, 3, 4])
     assert_equal([e for e in sorted(G.edges())], [(0, 3), (0, 4), (1, 3),
                                                   (1, 4), (2, 3), (2, 4),
                                                   (3, 4)])
예제 #2
0
 def test_read_graph6(self):
     data="""DF{"""
     G=nx.parse_graph6(data)
     fh = StringIO(data)
     Gin=nx.read_graph6(fh)
     assert_equal(sorted(G.nodes()),sorted(Gin.nodes()))
     assert_equal(sorted(G.edges()),sorted(Gin.edges()))
예제 #3
0
 def test_read_graph6(self):
     data = """DF{"""
     G = nx.parse_graph6(data)
     fh = StringIO(data)
     Gin = nx.read_graph6(fh)
     assert_nodes_equal(G.nodes(), Gin.nodes())
     assert_edges_equal(G.edges(), Gin.edges())
예제 #4
0
 def test_generate_and_parse_graph6(self):
     for i in list(range(13)) + [31, 47, 62, 63, 64, 72]:
         g = nx.random_graphs.gnm_random_graph(i, i * i // 4, seed=i)
         gstr = nx.generate_graph6(g, header=False)
         assert_equal(len(gstr),
                      ((i-1) * i // 2 + 5) // 6 + (1 if i < 63 else 4))
         g2 = nx.parse_graph6(gstr)
         assert_equal(g2.order(), g.order())
         assert_equal(sorted(g2.edges()), sorted(g.edges()))
예제 #5
0
 def test_generate_and_parse_graph6(self):
     for i in list(range(13)) + [31, 47, 62, 63, 64, 72]:
         g = nx.random_graphs.gnm_random_graph(i, i * i // 4, seed=i)
         gstr = nx.generate_graph6(g, header=False)
         assert_equal(len(gstr),
                      ((i - 1) * i // 2 + 5) // 6 + (1 if i < 63 else 4))
         g2 = nx.parse_graph6(gstr)
         assert_equal(g2.order(), g.order())
         assert_edges_equal(g2.edges(), g.edges())
예제 #6
0
파일: graph.py 프로젝트: nickspoon/part-ii
def load_graph(fn, n):
    G = None
    f = open(fn, "r")
    for i, line in enumerate(f):
        if i == n:
            G = nx.parse_graph6(line.strip())
            break
    if G is None:
        raise EOFError("Graph %d not found: EOF reached" % n)
    f.close()
    return G
예제 #7
0
 def test_read_graph6(self):
     data="""DF{"""
     G=nx.parse_graph6(data)
     (fd,fname)=tempfile.mkstemp()
     fh=open(fname,'w')
     b=fh.write(data)
     fh.close()
     Gin=nx.read_graph6(fname)
     assert_equal(sorted(G.nodes()),sorted(Gin.nodes()))
     assert_equal(sorted(G.edges()),sorted(Gin.edges()))
     os.close(fd)
     os.unlink(fname)
예제 #8
0
 def test_read_graph6(self):
     data="""DF{"""
     G=nx.parse_graph6(data)
     (fd,fname)=tempfile.mkstemp()
     fh=open(fname,'w')
     b=fh.write(data)
     fh.close()
     Gin=nx.read_graph6(fname)
     assert_equal(sorted(G.nodes()),sorted(Gin.nodes()))
     assert_equal(sorted(G.edges()),sorted(Gin.edges()))
     os.close(fd)
     os.unlink(fname)
예제 #9
0
def test_case_generator(full=False):
    '''
    Return a generator for all test cases.
    
    Each test case is a tuple (name, grfilestream, treewidth)
    where
      - name is a string indicating the name of the test case
      - grfilestream is a stream from which the grfile can be read
      - treewidth is the known treewidth of the graph (or None if we don't care)
    '''

    # This covers some corner cases (comments, empty graphs, etc)
    for grfile in glob.glob('test/valid/*.gr'):
        yield grfile, open(grfile, 'r'), None

    # Test cases where some tw-solvers were buggy in the past
    for grfile in glob.glob('test/tw-solver-bugs/*.gr'):
        treewidth = None
        with open(grfile[:-3] + '.td') as td_stream:
            treewidth = read_tw_from_td(td_stream)
        yield grfile, open(grfile, 'r'), treewidth

    # More test cases where some tw-solvers were buggy in the past
    tests = ['test/tw-solver-bugs.graph6']

    if full:
        tests.append('test/n_upto_8.graph6')

    for fname in tests:
        with open(fname) as tests:
            for line in tests:
                line = line.strip().split(' ')
                graph6 = line[0]
                treewidth = int(line[1])

                G = networkx.parse_graph6(graph6)
                n = G.order()
                m = G.size()

                with tempfile.TemporaryFile('w+') as tmp:
                    tmp.write("p tw {:d} {:d}\n".format(n, m))
                    for (u, v) in G.edges(data=False):
                        tmp.write("{:d} {:d}\n".format(u + 1, v + 1))
                    tmp.flush()
                    tmp.seek(0)
                    yield graph6 + ' from ' + fname, tmp, treewidth
예제 #10
0
def main():
    args = parse_args()

    graph_file = open(args.graphs, 'r')

    txt_graphs = graph_file.readlines()

    output_count = 0

    for txt_graph in txt_graphs:
        graph = nx.parse_graph6(txt_graph.rstrip())
        i = args.variations
        while i > 0:
            attk = AttakGenerator(graph.copy())
            attk.avg_attacker_deg = args.degree
            attk.calculate()

            if attk.is_valid():
                output_count += 1
                test_graph(attk.graph, args)
                i -= 1
예제 #11
0
 def test_parse_graph6(self):
     data = """DF{"""
     G = nx.parse_graph6(data)
     assert_nodes_equal(G.nodes(), [0, 1, 2, 3, 4])
     assert_edges_equal(G.edges(), [(0, 3), (0, 4), (1, 3), (1, 4), (2, 3),
                                    (2, 4), (3, 4)])
				VoltageVector=linalg.solve(ConductanceMatrix,InputVector)
				TotalImpedance=VoltageVector[0]
				if TotalImpedance>MaxTotalImpedance:
					MaxTotalImpedance=TotalImpedance
	
	return MaxTotalImpedance

################################################################################################################################################
################################################################################################################################################
################################################################################################################################################


max_impedance=0
#sentinel=0.0000000001

while True:
	current_line=fin.readline()
	current_graph_string=current_line.strip()
	if len(current_graph_string)==0:
		fin.close()
		NumericResultsFile=open('NumericResults.txt','a')
		NumericResultsFile.write(str(G.size()))
		NumericResultsFile.write('-')
		NumericResultsFile.write(str(max_impedance))
		NumericResultsFile.write(str('\n'))
		NumericResultsFile.close()
		break
	G=nx.parse_graph6(current_graph_string)
	test_impedance=MaxImpedanceComputation(G)
	if test_impedance>max_impedance:
		max_impedance=test_impedance
예제 #13
0
import networkx
import sys

graphs = []
for line in sys.stdin:
    graphs.append(networkx.parse_graph6(line.strip()))
예제 #14
0
파일: maxdeg.py 프로젝트: ixtel/graph-tools
#!/usr/bin/env python

"""
Usage:
    $ geng -qc 4 | maxdeg.py
    3
    2
    3
    2
    3
    3
"""

from sys import stdin

from networkx import parse_graph6

if __name__=="__main__":
    for line in stdin.readlines():
        stripped_line = line.rstrip()
        g = parse_graph6(stripped_line)
        print max(g.degree().values())
예제 #15
0
파일: plotgraph.py 프로젝트: Janno/kblocks
#!/usr/bin/python
from networkx import parse_graph6, draw
import pylab as P


if __name__=='__main__':
    from sys import argv
    g = parse_graph6(argv[1])
    draw(g)
    P.draw()
    P.show()
예제 #16
0
    'Gs@ipo',\
    'GtPHOk',\
    'Is?HGtcU?',\
    'Is@?xOXX?',\
    'It?IQGiDO',\
    'It?IQKWHG',\
    'It?IQOeDO',\
    ]
c2 = [\
    'Gv?IXW',\
    'Is@@WoXX?',\
    'It?GYDKKO',\
    'It?IQScDG',\
    ]

h = nx.parse_graph6(c3[0])


#import functools

basic_sym = ['.', 'o'...]
def is_planar(G):
    G = planarity.planarity_networkx.pgraph_graph(G)
    return planarity.planarity_functions.is_planar(G)
def _get_d2(pt, to, G):
    assert G.has_edge(pt, to)
    old = pt
    ls = []
    while 2 == len(G[to]):
        if to == old: break
        ls.append(to)
예제 #17
0
 def test_parse_graph6(self):
     data="""DF{"""
     G=nx.parse_graph6(data)
     assert_nodes_equal(G.nodes(),[0, 1, 2, 3, 4])
     assert_edges_equal(G.edges(),
                  [(0, 3), (0, 4), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)])
예제 #18
0
# with open("v4.g6") as f1:
#     for line1 in f1:
#         G = nx.parse_graph6(line1.strip())
#         with open("v4.g6") as f2:
#             for line2 in f2:
#                 if line2 != line1:
#                     H = nx.parse_graph6(line2.strip())
#                     homs = homsearch.find_homomorphisms(G, H)
#                     if len(homs) > 0:
#                         all_homs[line1.strip()+","+line2.strip()] = homs[0]
#                     else:
#                         all_homs[line1.strip() + "," + line2.strip()] = []
all_homs = {}
with open("v4.g6") as f1:
    for line1 in f1:
        G = nx.parse_graph6(line1.strip())
        with open("v4.g6") as f2:
            for line2 in f2:
                if line2 != line1:
                    H = nx.parse_graph6(line2.strip())
                    homs = homsearch.find_homomorphisms(G, H, only_count=True)
                    all_homs[line1.strip() + "-" + line2.strip()] = str(homs)

for gh in all_homs:
    if all_homs[gh] == "0":
        GH = gh.split("-")
        G = nx.parse_graph6(GH[0])
        H = nx.parse_graph6(GH[1])
        edges = G.edges()
        found = False
        for e in edges:
예제 #19
0
 def test_parse_graph6(self):
     data="""DF{"""
     G=nx.parse_graph6(data)
     assert_equal(sorted(G.nodes()),[0, 1, 2, 3, 4])
     assert_equal([e for e in sorted(G.edges())],
                  [(0, 3), (0, 4), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)])
                VoltageVector = linalg.solve(ConductanceMatrix, InputVector)
                TotalImpedance = VoltageVector[0]
                if TotalImpedance > MaxTotalImpedance:
                    MaxTotalImpedance = TotalImpedance

    return MaxTotalImpedance


################################################################################################################################################
################################################################################################################################################
################################################################################################################################################

max_impedance = 0
#sentinel=0.0000000001

while True:
    current_line = fin.readline()
    current_graph_string = current_line.strip()
    if len(current_graph_string) == 0:
        fin.close()
        NumericResultsFile = open('NumericResults.txt', 'a')
        NumericResultsFile.write(str(G.size()))
        NumericResultsFile.write('-')
        NumericResultsFile.write(str(max_impedance))
        NumericResultsFile.write(str('\n'))
        NumericResultsFile.close()
        break
    G = nx.parse_graph6(current_graph_string)
    test_impedance = MaxImpedanceComputation(G)
    if test_impedance > max_impedance:
        max_impedance = test_impedance
예제 #21
0
파일: mao.py 프로젝트: Janno/kblocks
        return mts[0]
    else:
        return maotree2_aux(g,mts,k=i-1)
        


def check_maos_corret(g):
    m1 = list(all_maos(g))
    m2 = list(all_maos_slow(g))
    m1.sort()
    m2.sort()
    if not set(m1) == set(m2):
        print l
        print len(m1)
        print len(m2)


if __name__=='__main__':
    from networkx import parse_graph6
    from sys import stdin

    for l_ in stdin:
        l = l_.strip()
        g = parse_graph6(l)
        # check_maos(g)
        m = mao(g,0)
        print m
        print maotree_old(g,m)
        print maotree(g,m)

예제 #22
0
import csv, json
import networkx as nx
from networkx.readwrite import json_graph

with open("data", "r") as f:
    reader = csv.DictReader(f)
    json_all_graphs = {}
    for row in reader:
        graphs = row['graphs'].split("-")
        G = nx.parse_graph6(graphs[0])
        d = json_graph.node_link_data(G)
        cy_json = []
        for n in d["nodes"]:
            cy_json.append({"group": "nodes", "data": {"id": n["id"]}})

        for e in d["links"]:
            cy_json.append({"group": "edges", "data": {"source": e["source"], "target": e["target"]}})

        json_all_graphs[graphs[0]] = cy_json

with open('force/graphs/all.json', 'w') as jsonfile:
    json.dump(json_all_graphs, jsonfile)