使用graph-tool的BFS迭代器函数在图上,其中边指向根节点。

huangapple go评论76阅读模式
英文:

Using graph-tool's BFS iterator function on graph were edges point towards the root

问题

I can help with translating the code portion. Here it is:

# 创建图
g = Graph()
g.add_vertex(7)
elist=[(1,0),(2,0),(3,1),(4,1),(5,2),(6,2)]
g.add_edge_list(elist)
ind=g.new_vp('int',[0,1,2,3,4,5,6])
g.vp["label"] =g.new_vp("string",["a","b","c","d","e","f","g"])
lab1=g.vp["label"]
w=np.random.rand(len(elist))
g.ep["weight"] =g.new_ep("double",w)
epw=g.ep['weight']

# 绘制图
graph_draw(g,vertex_text=lab1)

# 定义一个翻转边的函数
def flip(G,vlabel,eps,eptypes):
  g=Graph()
  g.vp['label']=g.new_vp('string')
  lab=g.vp['label']
  N=G.num_vertices()
  g.add_vertex(N)
  Glabs=G.vp[vlabel]
  for k in range(g.num_vertices()):
    nG=G.vertex(k)
    ng=g.vertex(k)
    lab[ng]=Glabs[nG]
  for k in range(len(eps)):
    g.ep[eps[k]]=g.new_ep(eptypes[k])
  for e in G.edges(): 
    e1=[int(i) for i in e]
    e2=g.add_edge(e1[1],e1[0])
    for k in eps:
      propG=G.ep[k]
      propg=g.ep[k]
      propg[e2]=propG[e]
  return(g,lab)

# 使用函数
[g2,lab]=flip(g,'label',['weight'],['double'])

# 绘制结果图
graph_draw(g2,vertex_text=lab)

# 获取BFS迭代器
utree=bfs_iterator(g2,source=0,array=True)
print("utree:",utree)

Please note that the translated code retains its original structure and formatting.

英文:

I want to use graph-tool's bfs_iterator() on a graph where the edges point toward the root, like below:

# Create graph
g = Graph()
g.add_vertex(7)
elist=[(1,0),(2,0),(3,1),(4,1),(5,2),(6,2)]
g.add_edge_list(elist)
ind=g.new_vp('int',[0,1,2,3,4,5,6])
g.vp["label"] =g.new_vp("string",["a","b","c","d","e","f","g"])
lab1=g.vp["label"]
w=np.random.rand(len(elist))
g.ep["weight"] =g.new_ep("double",w)
epw=g.ep['weight']

# plot graph
graph_draw(g,vertex_text=lab1)

使用graph-tool的BFS迭代器函数在图上,其中边指向根节点。

The only way I could achieve this is by creating a copy of it where all edges are reversed, but I don't think this solution is scalable.

Is there a better (more efficient) way to achieve this using graph-tool?

This is the code I came up with:

# Define a function that flips edges
def flip(G,vlabel,eps,eptypes):
  g=Graph()
  g.vp['label']=g.new_vp('string')
  lab=g.vp['label']
  N=G.num_vertices()
  g.add_vertex(N)
  Glabs=G.vp[vlabel]
  for k in range(g.num_vertices()):
    nG=G.vertex(k)
    ng=g.vertex(k)
    lab[ng]=Glabs[nG]
  for k in range(len(eps)):
    g.ep[eps[k]]=g.new_ep(eptypes[k])
  for e in G.edges(): 
    e1=[int(i) for i in e]
    e2=g.add_edge(e1[1],e1[0])
    for k in eps:
      propG=G.ep[k]
      propg=g.ep[k]
      propg[e2]=propG[e]
  return(g,lab)

# use function
[g2,lab]=flip(g,'label',['weight'],['double'])

# Plot the resulting graph
graph_draw(g2,vertex_text=lab)

# get BFS iterator
utree=bfs_iterator(g2,source=0,array=True)
print("utree:",utree)

使用graph-tool的BFS迭代器函数在图上,其中边指向根节点。

utree: [[0 1]
 [0 2]
 [1 3]
 [1 4]
 [2 5]
 [2 6]]

答案1

得分: 1

有一个非常简单而极其高效的解决方案。只需调用:

g.set_reversed(True)

这将反转图中的所有边,而且是一个快速的“O(1)”操作。

或者,您可以创建一个图视图如下:

u = GraphView(g, reversed=True)

图“u”将指向图“g”,但所有边都被反转。这也是一个“O(1)”操作,它不会复制图。

英文:

There's a very simple and extremely efficient answer to your problem. Just call:

g.set_reversed(True)

This will reverse all the edges in the graph, and it's a fast O(1) operation.

Alternatively, you can create a graph view as follows:

u = GraphView(g, reversed=True)

The graph u will point to g but with all edges reversed. This is also a O(1) operation, and it will not copy the graph.

huangapple
  • 本文由 发表于 2023年5月18日 11:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定