本文整理汇总了Python中graph_tool.Graph.remove_vertex方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.remove_vertex方法的具体用法?Python Graph.remove_vertex怎么用?Python Graph.remove_vertex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graph_tool.Graph
的用法示例。
在下文中一共展示了Graph.remove_vertex方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _filter_short_branch
# 需要导入模块: from graph_tool import Graph [as 别名]
# 或者: from graph_tool.Graph import remove_vertex [as 别名]
def _filter_short_branch(self, filter=False, short=30):
"""
filter out very short branches: do this maybe not right for some models, for models with flat part, it is right
I will test how this effect the final matching results
need to delete nodes, switch with the last one then delete last
"""
if filter == False:
self.verts = self.verts_init
self.edges = self.edges_init
else:
init_graph = Graph(directed=False)
init_graph.add_vertex(len(self.verts_init))
for edge in self.edges_init:
init_graph.add_edge(init_graph.vertex(edge[0]), init_graph.vertex(edge[1]))
terminal_node = []
for v in init_graph.vertices():
if v.out_degree() == 1:
terminal_node.append(v)
visitor = DepthVisitor()
short_nodes = []
for tn in terminal_node:
search.dfs_search(init_graph, tn, visitor)
tmp_node = visitor.get_short_branch(min_length=short)
visitor.reset()
for n in tmp_node:
short_nodes.append(n)
## get edges on the short paths
short_nodes = list(set(short_nodes))
short_edges = []
temp_verts = self.verts_init[:]
v_num = len(self.verts_init)
if len(short_nodes):
for v in reversed(sorted(short_nodes)):
for ve in init_graph.vertex(v).out_edges():
short_edges.append(ve)
## delete edges first, then vertex
short_edges = list(set(short_edges))
for e in short_edges:
init_graph.remove_edge(e)
print 'deleting vertex',
for v in reversed(sorted(short_nodes)):
print v,
temp_verts[int(v)] = temp_verts[v_num-1]
init_graph.remove_vertex(v, fast=True)
v_num -= 1
print '\ndeleting related edges' # already done above, just info user
else:
print 'no short branches'
######## new vertices and edges ########
self.verts = temp_verts[:v_num]
self.edges = []
for e in init_graph.edges():
self.edges.append([int(e.source()), int(e.target())])
示例2: gen_fs
# 需要导入模块: from graph_tool import Graph [as 别名]
# 或者: from graph_tool.Graph import remove_vertex [as 别名]
def gen_fs(dicProperties):
np.random.seed()
graphFS = Graph()
# on définit la fraction des arcs à utiliser la réciprocité
f = dicProperties["Reciprocity"]
rFracRecip = f/(2.0-f)
# on définit toutes les grandeurs de base
rInDeg = dicProperties["InDeg"]
rOutDeg = dicProperties["OutDeg"]
nNodes = 0
nEdges = 0
rDens = 0.0
if "Nodes" in dicProperties.keys():
nNodes = dicProperties["Nodes"]
graphFS.add_vertex(nNodes)
if "Edges" in dicProperties.keys():
nEdges = dicProperties["Edges"]
rDens = nEdges / float(nNodes**2)
dicProperties["Density"] = rDens
else:
rDens = dicProperties["Density"]
nEdges = int(np.floor(rDens*nNodes**2))
dicProperties["Edges"] = nEdges
else:
nEdges = dicProperties["Edges"]
rDens = dicProperties["Density"]
nNodes = int(np.floor(np.sqrt(nEdges/rDens)))
graphFS.add_vertex(nNodes)
dicProperties["Nodes"] = nNodes
# on définit le nombre d'arcs à créer
nArcs = int(np.floor(rDens*nNodes**2)/(1+rFracRecip))
# on définit les paramètres fonctions de probabilité associées F(x) = A x^{-tau}
Ai = nArcs*(rInDeg-1)/(nNodes)
Ao = nArcs*(rOutDeg-1)/(nNodes)
# on définit les moyennes des distributions de pareto 2 = lomax
rMi = 1/(rInDeg-2.)
rMo = 1/(rOutDeg-2.)
# on définit les trois listes contenant les degrés sortant/entrant/bidirectionnels associés aux noeuds i in range(nNodes)
lstInDeg = np.random.pareto(rInDeg,nNodes)+1
lstOutDeg = np.random.pareto(rOutDeg,nNodes)+1
lstInDeg = np.floor(np.multiply(Ai/np.mean(lstInDeg), lstInDeg)).astype(int)
lstOutDeg = np.floor(np.multiply(Ao/np.mean(lstOutDeg), lstOutDeg)).astype(int)
# on génère les stubs qui vont être nécessaires et on les compte
nInStubs = int(np.sum(lstInDeg))
nOutStubs = int(np.sum(lstOutDeg))
lstInStubs = np.zeros(np.sum(lstInDeg))
lstOutStubs = np.zeros(np.sum(lstOutDeg))
nStartIn = 0
nStartOut = 0
for vert in range(nNodes):
nInDegVert = lstInDeg[vert]
nOutDegVert = lstOutDeg[vert]
for j in range(np.max([nInDegVert,nOutDegVert])):
if j < nInDegVert:
lstInStubs[nStartIn+j] += vert
if j < nOutDegVert:
lstOutStubs[nStartOut+j] += vert
nStartOut+=nOutDegVert
nStartIn+=nInDegVert
# on vérifie qu'on a à peu près le nombre voulu d'edges
while nInStubs*(1+rFracRecip)/float(nArcs) < 0.95 :
vert = np.random.randint(0,nNodes)
nAddInStubs = int(np.floor(Ai/rMi*(np.random.pareto(rInDeg)+1)))
lstInStubs = np.append(lstInStubs,np.repeat(vert,nAddInStubs)).astype(int)
nInStubs+=nAddInStubs
while nOutStubs*(1+rFracRecip)/float(nArcs) < 0.95 :
nAddOutStubs = int(np.floor(Ao/rMo*(np.random.pareto(rOutDeg)+1)))
lstOutStubs = np.append(lstOutStubs,np.repeat(vert,nAddOutStubs)).astype(int)
nOutStubs+=nAddOutStubs
# on s'assure d'avoir le même nombre de in et out stubs (1.13 is an experimental correction)
nMaxStubs = int(1.13*(2.0*nArcs)/(2*(1+rFracRecip)))
if nInStubs > nMaxStubs and nOutStubs > nMaxStubs:
np.random.shuffle(lstInStubs)
np.random.shuffle(lstOutStubs)
lstOutStubs.resize(nMaxStubs)
lstInStubs.resize(nMaxStubs)
nOutStubs = nInStubs = nMaxStubs
elif nInStubs < nOutStubs:
np.random.shuffle(lstOutStubs)
lstOutStubs.resize(nInStubs)
nOutStubs = nInStubs
else:
np.random.shuffle(lstInStubs)
lstInStubs.resize(nOutStubs)
nInStubs = nOutStubs
# on crée le graphe, les noeuds et les stubs
nRecip = int(np.floor(nInStubs*rFracRecip))
nEdges = nInStubs + nRecip +1
# les stubs réciproques
np.random.shuffle(lstInStubs)
np.random.shuffle(lstOutStubs)
lstInRecip = lstInStubs[0:nRecip]
lstOutRecip = lstOutStubs[0:nRecip]
lstEdges = np.array([np.concatenate((lstOutStubs,lstInRecip)),np.concatenate((lstInStubs,lstOutRecip))]).astype(int)
# add edges
graphFS.add_edge_list(np.transpose(lstEdges))
remove_self_loops(graphFS)
remove_parallel_edges(graphFS)
lstIsolatedVert = find_vertex(graphFS, graphFS.degree_property_map("total"), 0)
graphFS.remove_vertex(lstIsolatedVert)
#.........这里部分代码省略.........