Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# -*- coding: utf-8 -*- 

r""" 

Graphs with a given degree sequence 

 

The methods defined here appear in :mod:`sage.graphs.graph_generators`. 

""" 

 

########################################################################### 

# 

# Copyright (C) 2006 Robert L. Miller <rlmillster@gmail.com> 

# and Emily A. Kirkman 

# Copyright (C) 2009 Michael C. Yurko <myurko@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# http://www.gnu.org/licenses/ 

########################################################################### 

 

# import from Sage library 

from sage.graphs.graph import Graph 

from sage.misc.randstate import current_randstate 

 

 

def DegreeSequence(deg_sequence): 

""" 

Returns a graph with the given degree sequence. Raises a NetworkX 

error if the proposed degree sequence cannot be that of a graph. 

 

Graph returned is the one returned by the Havel-Hakimi algorithm, 

which constructs a simple graph by connecting vertices of highest 

degree to other vertices of highest degree, resorting the remaining 

vertices by degree and repeating the process. See Theorem 1.4 in 

[CharLes1996]_. 

 

INPUT: 

 

- ``deg_sequence`` - a list of integers with each 

entry corresponding to the degree of a different vertex. 

 

 

EXAMPLES:: 

 

sage: G = graphs.DegreeSequence([3,3,3,3]) 

sage: G.edges(labels=False) 

[(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)] 

sage: G.show() # long time 

 

:: 

 

sage: G = graphs.DegreeSequence([3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]) 

sage: G.show() # long time 

 

:: 

 

sage: G = graphs.DegreeSequence([4,4,4,4,4,4,4,4]) 

sage: G.show() # long time 

 

:: 

 

sage: G = graphs.DegreeSequence([1,2,3,4,3,4,3,2,3,2,1]) 

sage: G.show() # long time 

 

REFERENCE: 

 

.. [CharLes1996] Chartrand, G. and Lesniak, L.: Graphs and Digraphs. 

Chapman and Hall/CRC, 1996. 

""" 

import networkx 

return Graph(networkx.havel_hakimi_graph([int(i) for i in deg_sequence])) 

 

def DegreeSequenceBipartite(s1 ,s2 ): 

r""" 

Returns a bipartite graph whose two sets have the given 

degree sequences. 

 

Given two different sequences of degrees `s_1` and `s_2`, 

this functions returns ( if possible ) a bipartite graph 

on sets `A` and `B` such that the vertices in `A` have 

`s_1` as their degree sequence, while `s_2` is the degree 

sequence of the vertices in `B`. 

 

INPUT: 

 

- ``s_1`` -- list of integers corresponding to the degree 

sequence of the first set. 

- ``s_2`` -- list of integers corresponding to the degree 

sequence of the second set. 

 

ALGORITHM: 

 

This function works through the computation of the matrix 

given by the Gale-Ryser theorem, which is in this case 

the adjacency matrix of the bipartite graph. 

 

EXAMPLES: 

 

If we are given as sequences ``[2,2,2,2,2]`` and ``[5,5]`` 

we are given as expected the complete bipartite 

graph `K_{2,5}` :: 

 

sage: g = graphs.DegreeSequenceBipartite([2,2,2,2,2],[5,5]) 

sage: g.is_isomorphic(graphs.CompleteBipartiteGraph(5,2)) 

True 

 

Some sequences being incompatible if, for example, their sums 

are different, the functions raises a ``ValueError`` when no 

graph corresponding to the degree sequences exists. :: 

 

sage: g = graphs.DegreeSequenceBipartite([2,2,2,2,1],[5,5]) 

Traceback (most recent call last): 

... 

ValueError: There exists no bipartite graph corresponding to the given degree sequences 

 

TESTS: 

 

:trac:`12155`:: 

 

sage: graphs.DegreeSequenceBipartite([2,2,2,2,2],[5,5]).complement() 

Graph on 7 vertices 

""" 

 

from sage.combinat.integer_vector import gale_ryser_theorem 

from sage.graphs.bipartite_graph import BipartiteGraph 

 

s1 = sorted(s1, reverse = True) 

s2 = sorted(s2, reverse = True) 

 

m = gale_ryser_theorem(s1,s2) 

 

if m is False: 

raise ValueError("There exists no bipartite graph corresponding to the given degree sequences") 

else: 

return Graph(BipartiteGraph(m)) 

 

def DegreeSequenceConfigurationModel(deg_sequence, seed=None): 

""" 

Returns a random pseudograph with the given degree sequence. Raises 

a NetworkX error if the proposed degree sequence cannot be that of 

a graph with multiple edges and loops. 

 

One requirement is that the sum of the degrees must be even, since 

every edge must be incident with two vertices. 

 

INPUT: 

 

- ``deg_sequence`` - a list of integers with each 

entry corresponding to the expected degree of a different vertex. 

 

- ``seed`` - for the random number generator. 

 

 

EXAMPLES:: 

 

sage: G = graphs.DegreeSequenceConfigurationModel([1,1]) 

sage: G.adjacency_matrix() 

[0 1] 

[1 0] 

 

Note: as of this writing, plotting of loops and multiple edges is 

not supported, and the output is allowed to contain both types of 

edges. 

 

:: 

 

sage: G = graphs.DegreeSequenceConfigurationModel([3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]) 

sage: sorted(G.edges(labels=False)) 

[(0, 2), (0, 10), (0, 15), (1, 6), (1, 16), (1, 17), (2, 5), (2, 19), 

(3, 7), (3, 14), (3, 14), (4, 9), (4, 13), (4, 19), (5, 6), 

(5, 15), (6, 11), (7, 11), (7, 17), (8, 11), (8, 18), (8, 19), 

(9, 12), (9, 13), (10, 15), (10, 18), (12, 13), (12, 16), (14, 17), 

(16, 18)] 

sage: G.show() # long time 

 

REFERENCE: 

 

.. [Newman2003] Newman, M.E.J. The Structure and function of complex 

networks, SIAM Review vol. 45, no. 2 (2003), pp. 167-256. 

""" 

if seed is None: 

seed = current_randstate().long_seed() 

import networkx 

return Graph(networkx.configuration_model([int(i) for i in deg_sequence], seed=seed), loops=True, multiedges=True, sparse=True) 

 

 

def DegreeSequenceTree(deg_sequence): 

""" 

Returns a tree with the given degree sequence. Raises a NetworkX 

error if the proposed degree sequence cannot be that of a tree. 

 

Since every tree has one more vertex than edge, the degree sequence 

must satisfy len(deg_sequence) - sum(deg_sequence)/2 == 1. 

 

INPUT: 

 

- ``deg_sequence`` - a list of integers with each 

entry corresponding to the expected degree of a different vertex. 

 

 

EXAMPLES:: 

 

sage: G = graphs.DegreeSequenceTree([3,1,3,3,1,1,1,2,1]) 

sage: G.show() # long time 

""" 

import networkx 

return Graph(networkx.degree_sequence_tree([int(i) for i in deg_sequence])) 

 

def DegreeSequenceExpected(deg_sequence, seed=None): 

""" 

Returns a random graph with expected given degree sequence. Raises 

a NetworkX error if the proposed degree sequence cannot be that of 

a graph. 

 

One requirement is that the sum of the degrees must be even, since 

every edge must be incident with two vertices. 

 

INPUT: 

 

- ``deg_sequence`` - a list of integers with each 

entry corresponding to the expected degree of a different vertex. 

 

- ``seed`` - for the random number generator. 

 

 

EXAMPLES:: 

 

sage: G = graphs.DegreeSequenceExpected([1,2,3,2,3]) 

sage: G.edges(labels=False) 

[(0, 2), (0, 3), (1, 1), (1, 4), (2, 3), (2, 4), (3, 4), (4, 4)] 

sage: G.show() # long time 

 

REFERENCE: 

 

.. [ChungLu2002] Chung, Fan and Lu, L. Connected components in random 

graphs with given expected degree sequences. 

Ann. Combinatorics (6), 2002 pp. 125-145. 

""" 

if seed is None: 

seed = current_randstate().long_seed() 

import networkx 

return Graph(networkx.expected_degree_graph([int(i) for i in deg_sequence], seed=seed), loops=True)