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

""" 

Examples of graphs 

""" 

#***************************************************************************** 

# Copyright (C) 2015 Travis Scrimshaw <tscrim at ucdavis.edu> 

# 

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

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

#****************************************************************************** 

 

from sage.structure.parent import Parent 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.structure.element_wrapper import ElementWrapper 

from sage.categories.graphs import Graphs 

from sage.rings.all import QQ 

 

class Cycle(UniqueRepresentation, Parent): 

r""" 

An example of a graph: the cycle of length `n`. 

 

This class illustrates a minimal implementation of a graph. 

 

EXAMPLES:: 

 

sage: from sage.categories.graphs import Graphs 

sage: C = Graphs().example(); C 

An example of a graph: the 5-cycle 

 

sage: C.category() 

Category of graphs 

 

We conclude by running systematic tests on this graph:: 

 

sage: TestSuite(C).run() 

""" 

def __init__(self, n=5): 

r""" 

EXAMPLES:: 

 

sage: from sage.categories.graphs import Graphs 

sage: C = Graphs().example(6); C 

An example of a graph: the 6-cycle 

 

TESTS:: 

 

sage: TestSuite(C).run() 

""" 

self._n = n 

Parent.__init__(self, category=Graphs()) 

 

def _repr_(self): 

r""" 

TESTS:: 

 

sage: from sage.categories.graphs import Graphs 

sage: Graphs().example() 

An example of a graph: the 5-cycle 

""" 

return "An example of a graph: the {}-cycle".format(self._n) 

 

def an_element(self): 

r""" 

Return an element of the graph, as per 

:meth:`Sets.ParentMethods.an_element`. 

 

EXAMPLES:: 

 

sage: from sage.categories.graphs import Graphs 

sage: C = Graphs().example() 

sage: C.an_element() 

0 

""" 

return self(0) 

 

def vertices(self): 

""" 

Return the vertices of ``self``. 

 

EXAMPLES:: 

 

sage: from sage.categories.graphs import Graphs 

sage: C = Graphs().example() 

sage: C.vertices() 

[0, 1, 2, 3, 4] 

""" 

return [self(i) for i in range(self._n)] 

 

def edges(self): 

""" 

Return the edges of ``self``. 

 

EXAMPLES:: 

 

sage: from sage.categories.graphs import Graphs 

sage: C = Graphs().example() 

sage: C.edges() 

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

""" 

return [self( (i, (i+1) % self._n) ) for i in range(self._n)] 

 

class Element(ElementWrapper): 

def dimension(self): 

""" 

Return the dimension of ``self``. 

 

EXAMPLES:: 

 

sage: from sage.categories.graphs import Graphs 

sage: C = Graphs().example() 

sage: e = C.edges()[0] 

sage: e.dimension() 

2 

sage: v = C.vertices()[0] 

sage: v.dimension() 

1 

""" 

if isinstance(self.value, tuple): 

return 2 

return 1 

 

Example = Cycle