示例#1
0
文件: testgraph.py 项目: altoid/graph
    def test_kruskal(self):
        # https://en.wikipedia.org/wiki/Kruskal%27s_algorithm
        gr = graph.UGraph()
        a = graph.Node('a')
        b = graph.Node('b')
        c = graph.Node('c')
        d = graph.Node('d')
        e = graph.Node('e')
        f = graph.Node('f')
        g = graph.Node('g')

        gr.addnodes(a, b, c, d, e, f, g)

        gr.addedge(a, d, 5)
        gr.addedge(a, b, 7)
        gr.addedge(b, c, 8)
        gr.addedge(b, d, 9)
        gr.addedge(b, e, 7)
        gr.addedge(c, e, 5)
        gr.addedge(d, e, 15)
        gr.addedge(d, f, 6)
        gr.addedge(f, e, 8)
        gr.addedge(e, g, 9)
        gr.addedge(f, g, 11)

        mst = graph.kruskal(gr)
        for ex in mst.edges():
            print ex
示例#2
0
文件: floc.py 项目: tsokawe/fragnew
def set_mst(coords_sph, a0):
    """ Computes the mst, using graph objects, from the transformed coordinates
    """
    vertices_sph = [graph.Vertex(coord) for coord in coords_sph]
    mst_sph = graph.kruskal(vertices_sph)
    return mst_sph
示例#3
0
force_heads = force_heads[0::skips]

#keep = np.random.choice(len(force_heads), 320)
#force_tails = force_tails[keep]
#force_heads = force_heads[keep]

# rescale the force_heads so the biggest one is length 1 for plotting
M = np.max(np.linalg.norm(force_heads, axis = 1))
force_heads *= 1/M

# get the COM
com = myfloc.coords_sph * myfloc.a0

# get the mst (floc functions keep it as a sphere)
vertices = [graph.Vertex(coord) for coord in com]
mst = graph.kruskal(vertices)

# write the data
import csv


ofile = open("3d_com.csv", "w")
writer = csv.writer(ofile)
for row in com:
    writer.writerow([row[0],row[1],row[2]])
ofile.close()

ofile = open("3d_mst.csv","w")
writer = csv.writer(ofile)
for edge in mst:
    writer.writerow( [ edge.u.coords[0] , edge.u.coords[1] , edge.u.coords[2] ] )