Example #1
0
 def random(self):
     s = self.tE.get()
     try:
         if ',' in s:
             G = graph.biclique(*map(int, s.split(',')))
         else:
             G = graph.simplize(graph.random_graph(int(s)))
         self.draw(G)
     except:
         return
Example #2
0
 def vc(self):
     '''minimum vertex-cover problem'''
     import random
     random.seed(2)
     np.random.seed(2)
     num_bit, prob = 8, 0.3  # 30% of edges are connected.
     p = 3
     graph = random_graph(num_bit, prob)
     solve_graph(graph,
                 task='vertex-cover',
                 runner='scipy',
                 depth=p,
                 x0=np.random.random(2 * p - 1))
def main():
    for i in xrange(100):
        m_graph = random_graph(5, 3)

        k = kruskal(m_graph)
        p = prims(m_graph)

        print k
        print p

        k_total_weight = reduce(lambda s, edge: edge.weight + s, k, 0)
        p_total_weight = reduce(lambda s, edge: edge.weight + s, p, 0)

        assert k_total_weight == p_total_weight
Example #4
0
    def mc(self, runner='scipy'):
        '''max-cut problem, runner = ('scipy'|'projectq')'''
        import random
        random.seed(2)
        np.random.seed(2)
        num_bit, prob = 8, 0.3  # 30% of edges are connected.

        # define loss function and circuit
        graph = random_graph(num_bit, prob, random_weight=False)
        solve_graph(graph,
                    task='max-cut',
                    runner=runner,
                    depth=10,
                    optimizer='COBYLA',
                    max_iter=200)
Example #5
0
import graph

g = graph.random_graph(6,0.3,directed=True,min_weight=1,max_weight=10)
graph.draw(g)
Example #6
0
import graph as gp

print("PROJEKCIK 1 GRAFY")
graph_in_file = gp.read_graph_from_file('projekt1/graph_examples.txt')
graph_type = type(graph_in_file)

if graph_type == gp.AdjacencyMatrix:
    print(gp.convert(graph_in_file, gp.AdjacencyList))
    print(gp.convert(graph_in_file, gp.IncidenceMatrix))
elif graph_type == gp.IncidenceMatrix:
    print(gp.convert(graph_in_file, gp.AdjacencyMatrix))
    print(gp.convert(graph_in_file, gp.AdjacencyList))
elif graph_type == gp.AdjacencyList:
    print(gp.convert(graph_in_file, gp.AdjacencyMatrix))
    print(gp.convert(graph_in_file, gp.IncidenceMatrix))

gp.draw_graph(graph_in_file)

rnd_graph = gp.random_graph(7, 10)
gp.draw_graph(rnd_graph)
rnd1_graph = gp.random_graph(7, edge_probability=0.5)
gp.draw_graph(rnd1_graph)
Example #7
0
def get_force_tree():
    G = random_graph(15)
    graph_dict = graph_to_dict(G)
    d3_graph_dict = d3_format(graph_dict)
    export_json(d3_graph_dict)
    return render_template("force-tree.html")
import graph

g1 = graph.random_graph(10, 0.15, directed=False, seed=2018)
g2 = graph.random_graph(10, 0.20, directed=False, seed=2018)
g3 = graph.random_graph(10, 0.22, directed=False, seed=2018)

# Note for homework 8
# g1 is the graph shown in the image rg_10_15_2018.png
# g2 is the graph shown in the image rg_10_20_2018.png
# g3 is the graph shown in the image rg_10_22_2018.png

# check if (3,9) is an edge in g1.  It is!
e = (3, 9)
if e in g1:
    print("Yes, the edge", e, "is in g1.")
else:
    print("No, the edge", e, "is not in g1.")

# check if (3,0) is an edge in g1.  It is not.
e = (3, 0)
if e in g1:
    print("Yes, the edge", e, "is in g1.")
else:
    print("No, the edge", e, "is not in g1.")

# traverse the neighbors of a given node. May be needed for last problem.
for v in g1.Neighbors[8]:
    print(v, "is a neighbor of 8")

#print(g1.Neighbors[0])
#print(g1.Vertices)
Example #9
0
# Add results folder
data.make_folder(results_folder)
data.make_folder(graphs_folder)

# Import the data file
filename = 'tgraph_real_wikiedithyperlinks.txt'

# If no file was selected, exit
if filename == '':
    output.error('No data file selected, exiting...')
    exit()

if TEST:
    # Create a random graph
    output.important('Creating a random connected graph with 100 nodes')
    graph = random_graph()
else:
    # Read the graph
    output.important('Reading graph data from "' + filename + '"...')
    graph = datafile_to_graph(filename)

# Output graph info
output.success('\nSuccessfully read graph. Info:')
output.dim(str(graph.num_edges()) + "  edges")
output.dim(str(graph.num_vertices()) + "  vertices")


# Returns the filename for a file with a given timestamp
def get_timestamp_path(path, timestamp=None, postfix='.csv'):
    if timestamp is None:
        return path + '.static' + postfix