or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

file-operations.mdgraph-management.mdgraph-utilities.mdindex.mdsubgraphs-clusters.md

graph-management.mddocs/

0

# Graph Creation and Management

1

2

Core graph objects and operations that form the foundation for building and manipulating graph structures in pydot. These classes provide a Python interface to Graphviz's graph model.

3

4

## Capabilities

5

6

### Graph Container (Dot Class)

7

8

The primary container for representing complete graphs. Supports directed graphs (digraph), undirected graphs (graph), and strict mode for eliminating duplicate edges.

9

10

```python { .api }

11

class Dot:

12

def __init__(self, graph_name='G', obj_dict=None, graph_type='digraph',

13

strict=False, suppress_disconnected=False, simplify=False, **attrs):

14

"""

15

Create a new graph container.

16

17

Parameters:

18

- graph_name (str): Name of the graph

19

- obj_dict (dict): Object dictionary for internal use

20

- graph_type (str): 'digraph' for directed, 'graph' for undirected

21

- strict (bool): Eliminate multiple edges between same nodes

22

- suppress_disconnected (bool): Hide disconnected nodes in output

23

- simplify (bool): Remove redundant elements

24

- **attrs: Additional graph attributes

25

"""

26

27

def add_node(self, node):

28

"""Add a node to the graph."""

29

30

def add_edge(self, edge):

31

"""Add an edge to the graph."""

32

33

def add_subgraph(self, subgraph):

34

"""Add a subgraph to the graph."""

35

36

def get_node(self, name):

37

"""

38

Get node by name.

39

40

Returns:

41

list: List of matching nodes (usually one element)

42

"""

43

44

def get_edge(self, src_or_list, dst=None):

45

"""

46

Get edge by source and destination nodes.

47

48

Parameters:

49

- src_or_list: Source node or list/tuple of (src, dst)

50

- dst: Destination node (if src_or_list is not a list/tuple)

51

52

Returns:

53

list: List of matching edges

54

"""

55

56

def get_nodes(self):

57

"""Get all nodes in the graph."""

58

59

def get_edges(self):

60

"""Get all edges in the graph."""

61

62

def get_subgraphs(self):

63

"""Get all subgraphs in the graph."""

64

65

def del_node(self, name, index=None):

66

"""

67

Delete node by name.

68

69

Parameters:

70

- name (str|Node): Node name or Node object

71

- index (int, optional): Specific index if multiple nodes with same name

72

73

Returns:

74

bool: True if node was deleted

75

"""

76

77

def del_edge(self, src_or_list, dst=None, index=None):

78

"""

79

Delete edge by source and destination.

80

81

Parameters:

82

- src_or_list: Source node or list/tuple of (src, dst)

83

- dst: Destination node (if src_or_list is not a list/tuple)

84

- index (int, optional): Specific index if multiple edges exist

85

86

Returns:

87

bool: True if edge was deleted

88

"""

89

90

def to_string(self):

91

"""

92

Generate raw DOT string representation.

93

94

Returns:

95

str: DOT language representation of the graph

96

"""

97

98

def get_graph_type(self):

99

"""Get the graph's type ('digraph' or 'graph')."""

100

101

def set_graph_defaults(self, **attrs):

102

"""Set default graph attributes."""

103

104

def get_graph_defaults(self):

105

"""Get default graph attributes."""

106

107

def set_node_defaults(self, **attrs):

108

"""Set default node attributes."""

109

110

def get_node_defaults(self):

111

"""Get default node attributes."""

112

113

def set_edge_defaults(self, **attrs):

114

"""Set default edge attributes."""

115

116

def get_edge_defaults(self):

117

"""Get default edge attributes."""

118

```

119

120

### Node Creation and Management

121

122

Individual graph vertices with customizable visual and semantic attributes.

123

124

```python { .api }

125

class Node:

126

def __init__(self, name='', obj_dict=None, **attrs):

127

"""

128

Create a new node.

129

130

Parameters:

131

- name (str): Unique identifier for the node

132

- obj_dict (dict): Object dictionary for internal use

133

- **attrs: Node attributes (shape, color, label, etc.)

134

"""

135

136

def get_name(self):

137

"""Get the node's name."""

138

139

def set_name(self, name):

140

"""Set the node's name."""

141

142

def to_string(self):

143

"""Generate DOT string representation of the node."""

144

```

145

146

### Edge Creation and Management

147

148

Connections between nodes with directional and styling properties.

149

150

```python { .api }

151

class Edge:

152

def __init__(self, src='', dst='', obj_dict=None, **attrs):

153

"""

154

Create a new edge.

155

156

Parameters:

157

- src (str|Node|list|tuple): Source node identifier, Node object, or

158

sequence of edge definitions for multiple edges

159

- dst (str|Node): Destination node identifier or Node object

160

- obj_dict (dict): Object dictionary for internal use

161

- **attrs: Edge attributes (color, style, label, etc.)

162

"""

163

164

def get_source(self):

165

"""Get the source node."""

166

167

def get_destination(self):

168

"""Get the destination node."""

169

170

def set_source(self, src):

171

"""Set the source node."""

172

173

def set_destination(self, dst):

174

"""Set the destination node."""

175

176

def to_string(self):

177

"""Generate DOT string representation of the edge."""

178

```

179

180

### Base Graph Class

181

182

Foundation class providing common functionality for all graph-like objects.

183

184

```python { .api }

185

class Graph:

186

def __init__(self, graph_name='G', obj_dict=None, graph_type='digraph',

187

strict=False, suppress_disconnected=False, simplify=False, **attrs):

188

"""Base graph class - use Dot class instead for main graphs."""

189

```

190

191

### Dynamic Attribute Methods

192

193

All graph objects support dynamically generated getter/setter methods for Graphviz attributes:

194

195

**For Dot/Graph objects** - 73 attributes including:

196

- `get_bgcolor()` / `set_bgcolor(value)` - Background color

197

- `get_rankdir()` / `set_rankdir(value)` - Layout direction ('TB', 'LR', etc.)

198

- `get_fontsize()` / `set_fontsize(value)` - Default font size

199

- `get_label()` / `set_label(value)` - Graph title/label

200

201

**For Node objects** - 38 attributes including:

202

- `get_shape()` / `set_shape(value)` - Node shape ('box', 'circle', 'ellipse', etc.)

203

- `get_color()` / `set_color(value)` - Node border color

204

- `get_fillcolor()` / `set_fillcolor(value)` - Node fill color

205

- `get_label()` / `set_label(value)` - Node display text

206

207

**For Edge objects** - 61 attributes including:

208

- `get_color()` / `set_color(value)` - Edge color

209

- `get_arrowhead()` / `set_arrowhead(value)` - Arrow style ('normal', 'dot', 'diamond', etc.)

210

- `get_style()` / `set_style(value)` - Line style ('solid', 'dashed', 'dotted', etc.)

211

- `get_label()` / `set_label(value)` - Edge label text

212

213

## Usage Examples

214

215

### Creating a Simple Graph

216

217

```python

218

import pydot

219

220

# Create directed graph

221

graph = pydot.Dot("example", graph_type="digraph")

222

223

# Add nodes with attributes

224

start = pydot.Node("start", shape="ellipse", style="filled", fillcolor="lightgreen")

225

process = pydot.Node("process", shape="box", label="Process Data")

226

end = pydot.Node("end", shape="ellipse", style="filled", fillcolor="lightcoral")

227

228

graph.add_node(start)

229

graph.add_node(process)

230

graph.add_node(end)

231

232

# Add edges

233

graph.add_edge(pydot.Edge("start", "process", label="input"))

234

graph.add_edge(pydot.Edge("process", "end", label="output"))

235

236

# Set graph properties

237

graph.set_rankdir("LR") # Left to right layout

238

graph.set_bgcolor("white")

239

```

240

241

### Working with Node and Edge Collections

242

243

```python

244

# Find nodes by name

245

nodes = graph.get_node("process")

246

if nodes:

247

process_node = nodes[0]

248

process_node.set_color("blue")

249

250

# Iterate through all nodes

251

for node in graph.get_nodes():

252

print(f"Node: {node.get_name()}")

253

254

# Iterate through all edges

255

for edge in graph.get_edges():

256

print(f"Edge: {edge.get_source()} -> {edge.get_destination()}")

257

```