本文整理汇总了Python中igraph.Graph.get_eid方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.get_eid方法的具体用法?Python Graph.get_eid怎么用?Python Graph.get_eid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类igraph.Graph
的用法示例。
在下文中一共展示了Graph.get_eid方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Graph
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import get_eid [as 别名]
from igraph import Graph
foster = Graph().LCF(90, [17, -9, 37, -37, 9, -17], 15)
foster.to_directed()
print "Is Directed? " + str(foster.is_directed())
for start in range(0, 90):
for end in range(0, 90):
# Don't delete this. Delete opposite direction edge
if start + 1 == end:
opposite_ID = foster.get_eid(end, start, True, False)
if opposite_ID != 1:
foster.delete_edges([opposite_ID])
else:
opposite_ID = foster.get_eid(end, start, True, False)
if opposite_ID != -1:
current_ID = foster.get_eid(start, end, True, False)
if current_ID != -1:
foster.delete_edges([current_ID])
print "Number of Edges: " + str(len(foster.get_edgelist()))
print (foster.is_connected())
foster_list = foster.get_adjacency()
for sublist in foster_list:
sublist = map(str, sublist)
sublist_string = " ".join(sublist)
print (sublist_string)
示例2: TestDatabase
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import get_eid [as 别名]
class TestDatabase(unittest.TestCase):
def setUp(self):
"""
This will run prior to each test.
It produces a test graph of five nodes where a-c are all interconnected.
The nodes are named a-e.
"""
self.g = Graph()
self.g.add_vertices(5)
self.g.add_edges([(0,1), (0,2), (1,2)])
self.g.vs["name"] = ['a','b','c','d', 'e']
def test_AddNode(self):
# Makes sure novel node is actually added.
self.g, self.node_index = database.AddNode('f', 'symptom', graph=self.g)
self.assertEqual(self.g.vs[5]["name"], 'f')
self.assertEqual(self.g.vs[5]["type"], 'symptom')
self.assertEqual(self.node_index, 5)
#And it does nothing if node name already exists.
self.g, self_node_index = database.AddNode('a', 'symptom', graph=self.g)
self.assertEqual(self.g.vs["name"], ['a','b','c','d','e','f'])
self.assertEqual(self_node_index, 0)
def test_AddAllEdges(self):
# Add more nodes to test.
self.g, fnode_index = database.AddNode('f', 'symptom', graph=self.g)
self.g, gnode_index = database.AddNode('g', 'symptom', graph=self.g)
self.g, hnode_index = database.AddNode('h', 'symptom', graph=self.g)
# Add actual edges.
self.g = database.AddAllEdges(self.g, [5,6,7])
# There should now be six edges (three originally) and three just added.
self.assertEqual(len(self.g.es), 6)
self.assertEqual(self.g.get_eid(5,6), 3)
self.assertEqual(self.g.get_eid(5,7), 4)
self.assertEqual(self.g.get_eid(6,7), 5)
def test_IdentifySimilarNodes(self):
# Simple test that car and cart are most related and not racecars.
self.node_tuples = database.IdentifySimilarNodes(['car', 'cart', 'racecars'])
self.assertEqual(self.node_tuples, [('car', 'cart')])
def test_MergeNodes(self):
# After merging, e should remain and be connected to b and c.
database.MergeNodes(self.g, 'e', 'a')
self.assertEqual([node["name"] for node in self.g.vs.find(name='e').neighbors()], ['b', 'c'])
self.assertRaises(ValueError, self.g.vs.find, name='a')
def test_MergeWeightedNodes(self):
self.g.es["weight"] = 1.0
self.g['a', 'c'] = 2
self.g['b', 'c'] = 3
# If a (keep) is merged with b, then it should inherit 3 edge from c.
database.MergeWeightedNodes(self.g, 'a', 'b')
self.assertEqual(self.g['a', 'c'], 3)
self.assertEqual(self.g.es['weight'], [3,1.0])
# B should have been deleted.
self.assertRaises(ValueError, self.g.vs.find, name='b')
self.setUp()
self.g.es["weight"] = 1.0
self.g['a', 'c'] = 2
self.g['b', 'c'] = 3
# If b (keep) is merged with a, then it should then it should keep its 3 edge with c.
database.MergeWeightedNodes(self.g, 'b', 'a')
#self.assertEqual(self.g['b', 'c'], 3)
#self.assertEqual(self.g.es['weight'], None)
self.setUp()
self.g.es["weight"] = 1.0
self.g['a', 'c'] = 2
self.g['b', 'c'] = 3
self.g['b', 'e'] = 4
# Existent edge should be kept.
database.MergeWeightedNodes(self.g, 'a', 'b')
self.assertEqual(self.g['a', 'e'], 4)
def test_IsolateSubGraph(self):
self.g_sub = database.IsolateSubGraph(self.g, ['a', 'b', 'e'])
# There should be three vertices and 1 edge.
self.assertEqual(len(self.g_sub.vs), 3)
self.assertEqual(len(self.g_sub.es), 1)
self.assertEqual(self.g_sub.vs['name'], ['a', 'b', 'e'])
# There should be an edge between 'a' and 'b'.
self.assertEqual(self.g_sub.get_eid(0, 1), 0)
def test_NodesInOrderOfCentrality(self):
# Make a the most highly connected node.
database.AddNode('f', 'symptom', self.g)
database.AddNode('g', 'symptom', self.g)
#.........这里部分代码省略.........
示例3: Topo
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import get_eid [as 别名]
class Topo( object ):
def __init__( self, *args, **params ):
"""Topo object.
Optional named parameters:
hinfo: default host options
sopts: default switch options
lopts: default link options
calls build()"""
self.g = Graph()
self.hopts = params.pop( 'hopts', {} )
self.sopts = params.pop( 'sopts', {} )
self.lopts = params.pop( 'lopts', {} )
# ports[src][dst][sport] is port on dst that connects to src
self.ports = {}
self.build( *args, **params )
def build( self, *args, **params ):
"Override this method to build your topology."
pass
def addNode( self, name, **opts ):
"""Add Node to graph.
name: name
opts: node options
returns: node name"""
self.g.add_vertex( name, **opts )
return name
def addHost( self, name, **opts ):
"""Convenience method: Add host to graph.
name: host name
opts: host options
returns: host name"""
if not opts and self.hopts:
opts = self.hopts
return self.addNode( name, isSwitch=False, **opts )
def addSwitch( self, name, **opts ):
"""Convenience method: Add switch to graph.
name: switch name
opts: switch options
returns: switch name"""
if not opts and self.sopts:
opts = self.sopts
return self.addNode( name, isSwitch=True, **opts )
def addLink( self, node1, node2, port1=None, port2=None, key=None, **opts ):
"""node1, node2: nodes to link together
port1, port2: ports (optional)
opts: link options (optional)
key: useless, kept for compatibility with mininet"""
if not opts and self.lopts:
opts = self.lopts
port1, port2 = self.addPort( node1, node2, port1, port2 )
opts = dict( opts )
opts.update( node1=node1, node2=node2, port1=port1, port2=port2 )
self.g.add_edge(node1, node2, **opts)
def nodes( self, sort=True ):
"""Return a list of nodes in graph"""
nodes = self.g.vs["name"]
if sort:
nodes.sort()
return nodes
def isSwitch( self, n ):
"""Return true if node is a switch."""
return self.g.vs.find(name=n)['isSwitch']
def switches( self, sort=True ):
"""Return a list of switches."""
#return [ n for n in self.nodes() if self.isSwitch( n ) ]
switches = self.g.vs.select(isSwitch=True)["name"]
if sort:
switches.sort()
return switches
def hosts( self, sort=True ):
"""Return a list of hosts."""
hosts = self.g.vs.select(isSwitch=False)["name"]
if sort:
hosts.sort()
return hosts
def links( self, sort=False, withKeys=False, withInfo=False ):
"""Return a list of links
sort: sort links alphabetically, preserving (src, dst) order
withKeys: return link keys
withInfo: return link info
returns: list of ( src, dst [,key, info ] )"""
if withKeys:
if withInfo:
links = [(self.g.vs[e[0]]["name"], self.g.vs[e[1]]["name"], e, self.g.es[self.g.get_eid(e[0],e[1])].attributes()) for e in self.g.get_edgelist()]
else:
links = [(self.g.vs[e[0]]["name"], self.g.vs[e[1]]["name"], e) for e in self.g.get_edgelist()]
else:
if withInfo:
links = [(self.g.vs[e[0]]["name"], self.g.vs[e[1]]["name"], self.g.es[self.g.get_eid(e[0],e[1])].attributes()) for e in self.g.get_edgelist()]
#.........这里部分代码省略.........