forked from igraph/python-igraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
4807 lines (4006 loc) · 200 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# vim:ts=4:sw=4:sts=4:et
# -*- coding: utf-8 -*-
"""
IGraph library.
@undocumented: deprecated, _graphmethod, _add_proxy_methods, _layout_method_wrapper,
_3d_version_for
"""
from __future__ import with_statement
__license__ = u"""
Copyright (C) 2006-2012 Tamás Nepusz <ntamas@gmail.com>
Pázmány Péter sétány 1/a, 1117 Budapest, Hungary
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
"""
# pylint: disable-msg=W0401
# W0401: wildcard import
from igraph._igraph import *
from igraph.clustering import *
from igraph.cut import *
from igraph.configuration import Configuration
from igraph.drawing import *
from igraph.drawing.colors import *
from igraph.datatypes import *
from igraph.formula import *
from igraph.layout import *
from igraph.matching import *
from igraph.operators import *
from igraph.statistics import *
from igraph.summary import *
from igraph.utils import *
from igraph.version import __version__, __version_info__
import os
import math
import gzip
import sys
import operator
from collections import defaultdict
from itertools import izip
from shutil import copyfileobj
from warnings import warn
def deprecated(message):
"""Prints a warning message related to the deprecation of some igraph
feature."""
warn(message, DeprecationWarning, stacklevel=3)
# pylint: disable-msg=E1101
class Graph(GraphBase):
"""Generic graph.
This class is built on top of L{GraphBase}, so the order of the
methods in the Epydoc documentation is a little bit obscure:
inherited methods come after the ones implemented directly in the
subclass. L{Graph} provides many functions that L{GraphBase} does not,
mostly because these functions are not speed critical and they were
easier to implement in Python than in pure C. An example is the
attribute handling in the constructor: the constructor of L{Graph}
accepts three dictionaries corresponding to the graph, vertex and edge
attributes while the constructor of L{GraphBase} does not. This extension
was needed to make L{Graph} serializable through the C{pickle} module.
L{Graph} also overrides some functions from L{GraphBase} to provide a
more convenient interface; e.g., layout functions return a L{Layout}
instance from L{Graph} instead of a list of coordinate pairs.
Graphs can also be indexed by strings or pairs of vertex indices or vertex
names. When a graph is indexed by a string, the operation translates to
the retrieval, creation, modification or deletion of a graph attribute:
>>> g = Graph.Full(3)
>>> g["name"] = "Triangle graph"
>>> g["name"]
'Triangle graph'
>>> del g["name"]
When a graph is indexed by a pair of vertex indices or names, the graph
itself is treated as an adjacency matrix and the corresponding cell of
the matrix is returned:
>>> g = Graph.Full(3)
>>> g.vs["name"] = ["A", "B", "C"]
>>> g[1, 2]
1
>>> g["A", "B"]
1
>>> g["A", "B"] = 0
>>> g.ecount()
2
Assigning values different from zero or one to the adjacency matrix will
be translated to one, unless the graph is weighted, in which case the
numbers will be treated as weights:
>>> g.is_weighted()
False
>>> g["A", "B"] = 2
>>> g["A", "B"]
1
>>> g.es["weight"] = 1.0
>>> g.is_weighted()
True
>>> g["A", "B"] = 2
>>> g["A", "B"]
2
>>> g.es["weight"]
[1.0, 1.0, 2]
"""
# Some useful aliases
omega = GraphBase.clique_number
alpha = GraphBase.independence_number
shell_index = GraphBase.coreness
cut_vertices = GraphBase.articulation_points
blocks = GraphBase.biconnected_components
evcent = GraphBase.eigenvector_centrality
vertex_disjoint_paths = GraphBase.vertex_connectivity
edge_disjoint_paths = GraphBase.edge_connectivity
cohesion = GraphBase.vertex_connectivity
adhesion = GraphBase.edge_connectivity
# Compatibility aliases
shortest_paths_dijkstra = GraphBase.shortest_paths
subgraph = GraphBase.induced_subgraph
def __init__(self, *args, **kwds):
"""__init__(n=0, edges=None, directed=False, graph_attrs=None,
vertex_attrs=None, edge_attrs=None)
Constructs a graph from scratch.
@keyword n: the number of vertices. Can be omitted, the default is
zero. Note that if the edge list contains vertices with indexes
larger than or equal to M{m}, then the number of vertices will
be adjusted accordingly.
@keyword edges: the edge list where every list item is a pair of
integers. If any of the integers is larger than M{n-1}, the number
of vertices is adjusted accordingly. C{None} means no edges.
@keyword directed: whether the graph should be directed
@keyword graph_attrs: the attributes of the graph as a dictionary.
@keyword vertex_attrs: the attributes of the vertices as a dictionary.
Every dictionary value must be an iterable with exactly M{n} items.
@keyword edge_attrs: the attributes of the edges as a dictionary. Every
dictionary value must be an iterable with exactly M{m} items where
M{m} is the number of edges.
"""
# Pop the special __ptr keyword argument
ptr = kwds.pop("__ptr", None)
# Set up default values for the parameters. This should match the order
# in *args
kwd_order = (
"n", "edges", "directed", "graph_attrs", "vertex_attrs",
"edge_attrs"
)
params = [0, [], False, {}, {}, {}]
# Is there any keyword argument in kwds that we don't know? If so,
# freak out.
unknown_kwds = set(kwds.keys())
unknown_kwds.difference_update(kwd_order)
if unknown_kwds:
raise TypeError("{0}.__init__ got an unexpected keyword argument {1!r}".format(
self.__class__.__name__, unknown_kwds.pop()
))
# If the first argument is a list or any other iterable, assume that
# the number of vertices were omitted
args = list(args)
if len(args) > 0 and hasattr(args[0], "__iter__"):
args.insert(0, params[0])
# Override default parameters from args
params[:len(args)] = args
# Override default parameters from keywords
for idx, k in enumerate(kwd_order):
if k in kwds:
params[idx] = kwds[k]
# Now, translate the params list to argument names
nverts, edges, directed, graph_attrs, vertex_attrs, edge_attrs = params
# When the number of vertices is None, assume that the user meant zero
if nverts is None:
nverts = 0
# When 'edges' is None, assume that the user meant an empty list
if edges is None:
edges = []
# When 'edges' is a NumPy array or matrix, convert it into a memoryview
# as the lower-level C API works with memoryviews only
try:
from numpy import ndarray, matrix
if isinstance(edges, (ndarray, matrix)):
edges = numpy_to_contiguous_memoryview(edges)
except ImportError:
pass
# Initialize the graph
if ptr:
GraphBase.__init__(self, __ptr=ptr)
else:
GraphBase.__init__(self, nverts, edges, directed)
# Set the graph attributes
for key, value in graph_attrs.iteritems():
if isinstance(key, (int, long)):
key = str(key)
self[key] = value
# Set the vertex attributes
for key, value in vertex_attrs.iteritems():
if isinstance(key, (int, long)):
key = str(key)
self.vs[key] = value
# Set the edge attributes
for key, value in edge_attrs.iteritems():
if isinstance(key, (int, long)):
key = str(key)
self.es[key] = value
def add_edge(self, source, target, **kwds):
"""add_edge(source, target, **kwds)
Adds a single edge to the graph.
Keyword arguments (except the source and target arguments) will be
assigned to the edge as attributes.
@param source: the source vertex of the edge or its name.
@param target: the target vertex of the edge or its name.
@return: the newly added edge as an L{Edge} object. Use
C{add_edges([(source, target)])} if you don't need the L{Edge}
object and want to avoid the overhead of creating t.
"""
eid = self.ecount()
self.add_edges([(source, target)])
edge = self.es[eid]
for key, value in kwds.iteritems():
edge[key] = value
return edge
def add_edges(self, es, attributes=None):
"""add_edges(es, attributes=None)
Adds some edges to the graph.
@param es: the list of edges to be added. Every edge is represented
with a tuple containing the vertex IDs or names of the two
endpoints. Vertices are enumerated from zero.
@param attributes: dict of sequences, all of length equal to the
number of edges to be added, containing the attributes of the new
edges.
"""
eid = self.ecount()
res = GraphBase.add_edges(self, es)
n = self.ecount() - eid
if (attributes is not None) and (n > 0):
for key, val in attributes.items():
self.es[eid:][key] = val
return res
def add_vertex(self, name=None, **kwds):
"""add_vertex(name=None, **kwds)
Adds a single vertex to the graph. Keyword arguments will be assigned
as vertex attributes. Note that C{name} as a keyword argument is treated
specially; if a graph has C{name} as a vertex attribute, it allows one
to refer to vertices by their names in most places where igraph expects
a vertex ID.
@return: the newly added vertex as a L{Vertex} object. Use
C{add_vertices(1)} if you don't need the L{Vertex} object and want
to avoid the overhead of creating t.
"""
vid = self.vcount()
self.add_vertices(1)
vertex = self.vs[vid]
for key, value in kwds.iteritems():
vertex[key] = value
if name is not None:
vertex["name"] = name
return vertex
def add_vertices(self, n, attributes=None):
"""add_vertices(n, attributes=None)
Adds some vertices to the graph.
Note that if C{n} is a sequence of strings, indicating the names of the
new vertices, and attributes has a key C{name}, the two conflict. In
that case the attribute will be applied.
@param n: the number of vertices to be added, or the name of a single
vertex to be added, or a sequence of strings, each corresponding to the
name of a vertex to be added. Names will be assigned to the C{name}
vertex attribute.
@param attributes: dict of sequences, all of length equal to the
number of vertices to be added, containing the attributes of the new
vertices. If n is a string (so a single vertex is added), then the
values of this dict are the attributes themselves, but if n=1 then
they have to be lists of length 1.
"""
if isinstance(n, basestring):
# Adding a single vertex with a name
m = self.vcount()
result = GraphBase.add_vertices(self, 1)
self.vs[m]["name"] = n
if attributes is not None:
for key, val in attributes.items():
self.vs[m][key] = val
elif hasattr(n, "__iter__"):
m = self.vcount()
if not hasattr(n, "__len__"):
names = list(n)
else:
names = n
result = GraphBase.add_vertices(self, len(names))
if len(names) > 0:
self.vs[m:]["name"] = names
if attributes is not None:
for key, val in attributes.items():
self.vs[m:][key] = val
else:
result = GraphBase.add_vertices(self, n)
if (attributes is not None) and (n > 0):
m = self.vcount() - n
for key, val in attributes.items():
self.vs[m:][key] = val
return result
def adjacent(self, *args, **kwds):
"""adjacent(vertex, mode=OUT)
Returns the edges a given vertex is incident on.
@deprecated: replaced by L{Graph.incident()} since igraph 0.6
"""
deprecated("Graph.adjacent() is deprecated since igraph 0.6, please use "
"Graph.incident() instead")
return self.incident(*args, **kwds)
def as_directed(self, *args, **kwds):
"""as_directed(*args, **kwds)
Returns a directed copy of this graph. Arguments are passed on
to L{Graph.to_directed()} that is invoked on the copy.
"""
copy = self.copy()
copy.to_directed(*args, **kwds)
return copy
def as_undirected(self, *args, **kwds):
"""as_undirected(*args, **kwds)
Returns an undirected copy of this graph. Arguments are passed on
to L{Graph.to_undirected()} that is invoked on the copy.
"""
copy = self.copy()
copy.to_undirected(*args, **kwds)
return copy
def delete_edges(self, *args, **kwds):
"""Deletes some edges from the graph.
The set of edges to be deleted is determined by the positional and
keyword arguments. If the function is called without any arguments,
all edges are deleted. If any keyword argument is present, or the
first positional argument is callable, an edge sequence is derived by
calling L{EdgeSeq.select} with the same positional and keyword
arguments. Edges in the derived edge sequence will be removed.
Otherwise the first positional argument is considered as follows:
- C{None} - deletes all edges (deprecated since 0.8.3)
- a single integer - deletes the edge with the given ID
- a list of integers - deletes the edges denoted by the given IDs
- a list of 2-tuples - deletes the edges denoted by the given
source-target vertex pairs. When multiple edges are present
between a given source-target vertex pair, only one is removed.
@deprecated: L{Graph.delete_edges(None)} has been replaced by
L{Graph.delete_edges()} - with no arguments - since igraph 0.8.3.
"""
if len(args) == 0 and len(kwds) == 0:
return GraphBase.delete_edges(self)
if len(kwds) > 0 or (callable(args[0]) and not isinstance(args[0], EdgeSeq)):
edge_seq = self.es(*args, **kwds)
else:
edge_seq = args[0]
return GraphBase.delete_edges(self, edge_seq)
def indegree(self, *args, **kwds):
"""Returns the in-degrees in a list.
See L{degree} for possible arguments.
"""
kwds['mode'] = IN
return self.degree(*args, **kwds)
def outdegree(self, *args, **kwds):
"""Returns the out-degrees in a list.
See L{degree} for possible arguments.
"""
kwds['mode'] = OUT
return self.degree(*args, **kwds)
def all_st_cuts(self, source, target):
"""\
Returns all the cuts between the source and target vertices in a
directed graph.
This function lists all edge-cuts between a source and a target vertex.
Every cut is listed exactly once.
@param source: the source vertex ID
@param target: the target vertex ID
@return: a list of L{Cut} objects.
@newfield ref: Reference
@ref: JS Provan and DR Shier: A paradigm for listing (s,t)-cuts in
graphs. Algorithmica 15, 351--372, 1996.
"""
return [Cut(self, cut=cut, partition=part)
for cut, part in izip(*GraphBase.all_st_cuts(self, source, target))]
def all_st_mincuts(self, source, target, capacity=None):
"""\
Returns all the mincuts between the source and target vertices in a
directed graph.
This function lists all minimum edge-cuts between a source and a target
vertex.
@param source: the source vertex ID
@param target: the target vertex ID
@param capacity: the edge capacities (weights). If C{None}, all
edges have equal weight. May also be an attribute name.
@return: a list of L{Cut} objects.
@newfield ref: Reference
@ref: JS Provan and DR Shier: A paradigm for listing (s,t)-cuts in
graphs. Algorithmica 15, 351--372, 1996.
"""
value, cuts, parts = GraphBase.all_st_mincuts(self, source, target, capacity)
return [Cut(self, value, cut=cut, partition=part)
for cut, part in izip(cuts, parts)]
def biconnected_components(self, return_articulation_points=False):
"""\
Calculates the biconnected components of the graph.
@param return_articulation_points: whether to return the articulation
points as well
@return: a L{VertexCover} object describing the biconnected components,
and optionally the list of articulation points as well
"""
if return_articulation_points:
trees, aps = GraphBase.biconnected_components(self, True)
else:
trees = GraphBase.biconnected_components(self, False)
clusters = []
if trees:
edgelist = self.get_edgelist()
for tree in trees:
cluster = set()
for edge_id in tree:
cluster.update(edgelist[edge_id])
clusters.append(sorted(cluster))
clustering = VertexCover(self, clusters)
if return_articulation_points:
return clustering, aps
else:
return clustering
blocks = biconnected_components
def clear(self):
"""clear()
Clears the graph, deleting all vertices, edges, and attributes.
@see: L{Graph.delete_vertices} and L{Graph.delete_edges}.
"""
self.delete_vertices()
for attr in self.attributes():
del self[attr]
def cohesive_blocks(self):
"""cohesive_blocks()
Calculates the cohesive block structure of the graph.
Cohesive blocking is a method of determining hierarchical subsets of graph
vertices based on their structural cohesion (i.e. vertex connectivity).
For a given graph G, a subset of its vertices S is said to be maximally
k-cohesive if there is no superset of S with vertex connectivity greater
than or equal to k. Cohesive blocking is a process through which, given a
k-cohesive set of vertices, maximally l-cohesive subsets are recursively
identified with l > k. Thus a hierarchy of vertex subsets is obtained in
the end, with the entire graph G at its root.
@return: an instance of L{CohesiveBlocks}. See the documentation of
L{CohesiveBlocks} for more information.
@see: L{CohesiveBlocks}
"""
return CohesiveBlocks(self, *GraphBase.cohesive_blocks(self))
def clusters(self, mode=STRONG):
"""clusters(mode=STRONG)
Calculates the (strong or weak) clusters (connected components) for
a given graph.
@param mode: must be either C{STRONG} or C{WEAK}, depending on the
clusters being sought. Optional, defaults to C{STRONG}.
@return: a L{VertexClustering} object"""
return VertexClustering(self, GraphBase.clusters(self, mode))
components = clusters
def degree_distribution(self, bin_width = 1, *args, **kwds):
"""degree_distribution(bin_width=1, ...)
Calculates the degree distribution of the graph.
Unknown keyword arguments are directly passed to L{degree()}.
@param bin_width: the bin width of the histogram
@return: a histogram representing the degree distribution of the
graph.
"""
result = Histogram(bin_width, self.degree(*args, **kwds))
return result
def dyad_census(self, *args, **kwds):
"""dyad_census()
Calculates the dyad census of the graph.
Dyad census means classifying each pair of vertices of a directed
graph into three categories: mutual (there is an edge from I{a} to
I{b} and also from I{b} to I{a}), asymmetric (there is an edge
from I{a} to I{b} or from I{b} to I{a} but not the other way round)
and null (there is no connection between I{a} and I{b}).
@return: a L{DyadCensus} object.
@newfield ref: Reference
@ref: Holland, P.W. and Leinhardt, S. (1970). A Method for Detecting
Structure in Sociometric Data. American Journal of Sociology, 70,
492-513.
"""
return DyadCensus(GraphBase.dyad_census(self, *args, **kwds))
def get_adjacency(self, type=GET_ADJACENCY_BOTH, attribute=None, \
default=0, eids=False):
"""Returns the adjacency matrix of a graph.
@param type: either C{GET_ADJACENCY_LOWER} (uses the lower
triangle of the matrix) or C{GET_ADJACENCY_UPPER}
(uses the upper triangle) or C{GET_ADJACENCY_BOTH}
(uses both parts). Ignored for directed graphs.
@param attribute: if C{None}, returns the ordinary adjacency
matrix. When the name of a valid edge attribute is given
here, the matrix returned will contain the default value
at the places where there is no edge or the value of the
given attribute where there is an edge. Multiple edges are
not supported, the value written in the matrix in this case
will be unpredictable. This parameter is ignored if
I{eids} is C{True}
@param default: the default value written to the cells in the
case of adjacency matrices with attributes.
@param eids: specifies whether the edge IDs should be returned
in the adjacency matrix. Since zero is a valid edge ID, the
cells in the matrix that correspond to unconnected vertex
pairs will contain -1 instead of 0 if I{eids} is C{True}.
If I{eids} is C{False}, the number of edges will be returned
in the matrix for each vertex pair.
@return: the adjacency matrix as a L{Matrix}.
"""
if type != GET_ADJACENCY_LOWER and type != GET_ADJACENCY_UPPER and \
type != GET_ADJACENCY_BOTH:
# Maybe it was called with the first argument as the attribute name
type, attribute = attribute, type
if type is None:
type = GET_ADJACENCY_BOTH
if eids:
result = Matrix(GraphBase.get_adjacency(self, type, eids))
result -= 1
return result
if attribute is None:
return Matrix(GraphBase.get_adjacency(self, type))
if attribute not in self.es.attribute_names():
raise ValueError("Attribute does not exist")
data = [[default] * self.vcount() for _ in xrange(self.vcount())]
if self.is_directed():
for edge in self.es:
data[edge.source][edge.target] = edge[attribute]
return Matrix(data)
if type == GET_ADJACENCY_BOTH:
for edge in self.es:
source, target = edge.tuple
data[source][target] = edge[attribute]
data[target][source] = edge[attribute]
elif type == GET_ADJACENCY_UPPER:
for edge in self.es:
data[min(edge.tuple)][max(edge.tuple)] = edge[attribute]
else:
for edge in self.es:
data[max(edge.tuple)][min(edge.tuple)] = edge[attribute]
return Matrix(data)
def get_adjacency_sparse(self, attribute=None):
"""Returns the adjacency matrix of a graph as a SciPy CSR matrix.
@param attribute: if C{None}, returns the ordinary adjacency
matrix. When the name of a valid edge attribute is given
here, the matrix returned will contain the default value
at the places where there is no edge or the value of the
given attribute where there is an edge.
@return: the adjacency matrix as a C{scipy.sparse.csr_matrix}.
"""
try:
from scipy import sparse
except ImportError:
raise ImportError('You should install scipy package in order to use this function')
import numpy as np
edges = self.get_edgelist()
if attribute is None:
weights = [1] * len(edges)
else:
if attribute not in self.es.attribute_names():
raise ValueError("Attribute does not exist")
weights = self.es[attribute]
N = self.vcount()
mtx = sparse.csr_matrix((weights, zip(*edges)), shape=(N, N))
if not self.is_directed():
mtx = mtx + sparse.triu(mtx, 1).T + sparse.tril(mtx, -1).T
return mtx
def get_adjlist(self, mode=OUT):
"""get_adjlist(mode=OUT)
Returns the adjacency list representation of the graph.
The adjacency list representation is a list of lists. Each item of the
outer list belongs to a single vertex of the graph. The inner list
contains the neighbors of the given vertex.
@param mode: if L{OUT}, returns the successors of the vertex. If
L{IN}, returns the predecessors of the vertex. If L{ALL}, both
the predecessors and the successors will be returned. Ignored
for undirected graphs.
"""
return [self.neighbors(idx, mode) for idx in xrange(self.vcount())]
def get_adjedgelist(self, *args, **kwds):
"""get_adjedgelist(mode=OUT)
Returns the incidence list representation of the graph.
@deprecated: replaced by L{Graph.get_inclist()} since igraph 0.6
@see: Graph.get_inclist()
"""
deprecated("Graph.get_adjedgelist() is deprecated since igraph 0.6, "
"please use Graph.get_inclist() instead")
return self.get_inclist(*args, **kwds)
def get_all_simple_paths(self, v, to=None, cutoff=-1, mode=OUT):
"""get_all_simple_paths(v, to=None, cutoff=-1, mode=OUT)
Calculates all the simple paths from a given node to some other nodes
(or all of them) in a graph.
A path is simple if its vertices are unique, i.e. no vertex is visited
more than once.
Note that potentially there are exponentially many paths between two
vertices of a graph, especially if your graph is lattice-like. In this
case, you may run out of memory when using this function.
@param v: the source for the calculated paths
@param to: a vertex selector describing the destination for the calculated
paths. This can be a single vertex ID, a list of vertex IDs, a single
vertex name, a list of vertex names or a L{VertexSeq} object. C{None}
means all the vertices.
@param cutoff: maximum length of path that is considered. If negative,
paths of all lengths are considered.
@param mode: the directionality of the paths. L{IN} means to calculate
incoming paths, L{OUT} means to calculate outgoing paths, L{ALL} means
to calculate both ones.
@return: all of the simple paths from the given node to every other
reachable node in the graph in a list. Note that in case of mode=L{IN},
the vertices in a path are returned in reversed order!
"""
paths = self._get_all_simple_paths(v, to, cutoff, mode)
prev = 0
result = []
for index, item in enumerate(paths):
if item < 0:
result.append(paths[prev:index])
prev = index+1
return result
def get_inclist(self, mode=OUT):
"""get_inclist(mode=OUT)
Returns the incidence list representation of the graph.
The incidence list representation is a list of lists. Each
item of the outer list belongs to a single vertex of the graph.
The inner list contains the IDs of the incident edges of the
given vertex.
@param mode: if L{OUT}, returns the successors of the vertex. If
L{IN}, returns the predecessors of the vertex. If L{ALL}, both
the predecessors and the successors will be returned. Ignored
for undirected graphs.
"""
return [self.incident(idx, mode) for idx in xrange(self.vcount())]
def gomory_hu_tree(self, capacity=None, flow="flow"):
"""gomory_hu_tree(capacity=None, flow="flow")
Calculates the Gomory-Hu tree of an undirected graph with optional
edge capacities.
The Gomory-Hu tree is a concise representation of the value of all the
maximum flows (or minimum cuts) in a graph. The vertices of the tree
correspond exactly to the vertices of the original graph in the same order.
Edges of the Gomory-Hu tree are annotated by flow values. The value of
the maximum flow (or minimum cut) between an arbitrary (u,v) vertex
pair in the original graph is then given by the minimum flow value (i.e.
edge annotation) along the shortest path between u and v in the
Gomory-Hu tree.
@param capacity: the edge capacities (weights). If C{None}, all
edges have equal weight. May also be an attribute name.
@param flow: the name of the edge attribute in the returned graph
in which the flow values will be stored.
@return: the Gomory-Hu tree as a L{Graph} object.
"""
graph, flow_values = GraphBase.gomory_hu_tree(self, capacity)
graph.es[flow] = flow_values
return graph
def is_named(self):
"""is_named()
Returns whether the graph is named, i.e., whether it has a "name"
vertex attribute.
"""
return "name" in self.vertex_attributes()
def is_weighted(self):
"""is_weighted()
Returns whether the graph is weighted, i.e., whether it has a "weight"
edge attribute.
"""
return "weight" in self.edge_attributes()
def maxflow(self, source, target, capacity=None):
"""maxflow(source, target, capacity=None)
Returns a maximum flow between the given source and target vertices
in a graph.
A maximum flow from I{source} to I{target} is an assignment of
non-negative real numbers to the edges of the graph, satisfying
two properties:
1. For each edge, the flow (i.e. the assigned number) is not
more than the capacity of the edge (see the I{capacity}
argument)
2. For every vertex except the source and the target, the
incoming flow is the same as the outgoing flow.
The value of the flow is the incoming flow of the target or the
outgoing flow of the source (which are equal). The maximum flow
is the maximum possible such value.
@param capacity: the edge capacities (weights). If C{None}, all
edges have equal weight. May also be an attribute name.
@return: a L{Flow} object describing the maximum flow
"""
return Flow(self, *GraphBase.maxflow(self, source, target, capacity))
def mincut(self, source=None, target=None, capacity=None):
"""mincut(source=None, target=None, capacity=None)
Calculates the minimum cut between the given source and target vertices
or within the whole graph.
The minimum cut is the minimum set of edges that needs to be removed to
separate the source and the target (if they are given) or to disconnect the
graph (if neither the source nor the target are given). The minimum is
calculated using the weights (capacities) of the edges, so the cut with
the minimum total capacity is calculated.
For undirected graphs and no source and target, the method uses the
Stoer-Wagner algorithm. For a given source and target, the method uses the
push-relabel algorithm; see the references below.
@param source: the source vertex ID. If C{None}, the target must also be
C{None} and the calculation will be done for the entire graph (i.e.
all possible vertex pairs).
@param target: the target vertex ID. If C{None}, the source must also be
C{None} and the calculation will be done for the entire graph (i.e.
all possible vertex pairs).
@param capacity: the edge capacities (weights). If C{None}, all
edges have equal weight. May also be an attribute name.
@return: a L{Cut} object describing the minimum cut
"""
return Cut(self, *GraphBase.mincut(self, source, target, capacity))
def st_mincut(self, source, target, capacity=None):
"""st_mincut(source, target, capacity=None)
Calculates the minimum cut between the source and target vertices in a
graph.
@param source: the source vertex ID
@param target: the target vertex ID
@param capacity: the capacity of the edges. It must be a list or a valid
attribute name or C{None}. In the latter case, every edge will have the
same capacity.
@return: the value of the minimum cut, the IDs of vertices in the
first and second partition, and the IDs of edges in the cut,
packed in a 4-tuple
"""
return Cut(self, *GraphBase.st_mincut(self, source, target, capacity))
def modularity(self, membership, weights=None):
"""modularity(membership, weights=None)
Calculates the modularity score of the graph with respect to a given
clustering.
The modularity of a graph w.r.t. some division measures how good the
division is, or how separated are the different vertex types from each
other. It's defined as M{Q=1/(2m)*sum(Aij-ki*kj/(2m)delta(ci,cj),i,j)}.
M{m} is the number of edges, M{Aij} is the element of the M{A}
adjacency matrix in row M{i} and column M{j}, M{ki} is the degree of
node M{i}, M{kj} is the degree of node M{j}, and M{Ci} and C{cj} are
the types of the two vertices (M{i} and M{j}). M{delta(x,y)} is one iff
M{x=y}, 0 otherwise.
If edge weights are given, the definition of modularity is modified as
follows: M{Aij} becomes the weight of the corresponding edge, M{ki}
is the total weight of edges adjacent to vertex M{i}, M{kj} is the
total weight of edges adjacent to vertex M{j} and M{m} is the total
edge weight in the graph.
@param membership: a membership list or a L{VertexClustering} object
@param weights: optional edge weights or C{None} if all edges are
weighed equally. Attribute names are also allowed.
@return: the modularity score
@newfield ref: Reference
@ref: MEJ Newman and M Girvan: Finding and evaluating community
structure in networks. Phys Rev E 69 026113, 2004.
"""
if isinstance(membership, VertexClustering):
if membership.graph != self:
raise ValueError("clustering object belongs to another graph")
return GraphBase.modularity(self, membership.membership, weights)
else:
return GraphBase.modularity(self, membership, weights)
def path_length_hist(self, directed=True):
"""path_length_hist(directed=True)
Returns the path length histogram of the graph
@param directed: whether to consider directed paths. Ignored for
undirected graphs.
@return: a L{Histogram} object. The object will also have an
C{unconnected} attribute that stores the number of unconnected
vertex pairs (where the second vertex can not be reached from
the first one). The latter one will be of type long (and not
a simple integer), since this can be I{very} large.
"""
data, unconn = GraphBase.path_length_hist(self, directed)
hist = Histogram(bin_width=1)
for i, length in enumerate(data):
hist.add(i+1, length)
hist.unconnected = long(unconn)
return hist
def pagerank(self, vertices=None, directed=True, damping=0.85,
weights=None, arpack_options=None, implementation="prpack",
niter=1000, eps=0.001):
"""Calculates the Google PageRank values of a graph.
@param vertices: the indices of the vertices being queried.
C{None} means all of the vertices.
@param directed: whether to consider directed paths.
@param damping: the damping factor. M{1-damping} is the PageRank value
for nodes with no incoming links. It is also the probability of
resetting the random walk to a uniform distribution in each step.
@param weights: edge weights to be used. Can be a sequence or iterable
or even an edge attribute name.
@param arpack_options: an L{ARPACKOptions} object used to fine-tune
the ARPACK eigenvector calculation. If omitted, the module-level
variable called C{arpack_options} is used. This argument is
ignored if not the ARPACK implementation is used, see the
I{implementation} argument.
@param implementation: which implementation to use to solve the
PageRank eigenproblem. Possible values are:
- C{"prpack"}: use the PRPACK library. This is a new
implementation in igraph 0.7
- C{"arpack"}: use the ARPACK library. This implementation
was used from version 0.5, until version 0.7.
- C{"power"}: use a simple power method. This is the
implementation that was used before igraph version 0.5.
@param niter: The number of iterations to use in the power method
implementation. It is ignored in the other implementations
@param eps: The power method implementation will consider the
calculation as complete if the difference of PageRank values between
iterations change less than this value for every node. It is
ignored by the other implementations.
@return: a list with the Google PageRank values of the specified
vertices."""
if arpack_options is None:
arpack_options = _igraph.arpack_options
return self.personalized_pagerank(vertices, directed, damping, None,\
None, weights, arpack_options, \
implementation, niter, eps)
def spanning_tree(self, weights=None, return_tree=True):
"""Calculates a minimum spanning tree for a graph.
@param weights: a vector containing weights for every edge in
the graph. C{None} means that the graph is unweighted.
@param return_tree: whether to return the minimum spanning tree (when
C{return_tree} is C{True}) or to return the IDs of the edges in
the minimum spanning tree instead (when C{return_tree} is C{False}).
The default is C{True} for historical reasons as this argument was
introduced in igraph 0.6.
@return: the spanning tree as a L{Graph} object if C{return_tree}
is C{True} or the IDs of the edges that constitute the spanning
tree if C{return_tree} is C{False}.
@newfield ref: Reference
@ref: Prim, R.C.: I{Shortest connection networks and some
generalizations}. Bell System Technical Journal 36:1389-1401, 1957.
"""
result = GraphBase._spanning_tree(self, weights)
if return_tree:
return self.subgraph_edges(result, delete_vertices=False)
return result
def transitivity_avglocal_undirected(self, mode="nan", weights=None):
"""Calculates the average of the vertex transitivities of the graph.
In the unweighted case, the transitivity measures the probability that
two neighbors of a vertex are connected. In case of the average local
transitivity, this probability is calculated for each vertex and then
the average is taken. Vertices with less than two neighbors require
special treatment, they will either be left out from the calculation
or they will be considered as having zero transitivity, depending on
the I{mode} parameter. The calculation is slightly more involved for
weighted graphs; in this case, weights are taken into account according
to the formula of Barrat et al (see the references).
Note that this measure is different from the global transitivity