• 显示图形顶点
• 显示图形边缘
• 添加一个顶点
• 添加边缘
• 创建一个图

## 在上面的图中

```V = {a, b, c, d, e}
E = {ab, ac, bd, cd, de}
```

```# Create the dictionary with graph elements
graph = { "a" : ["b","c"],
"b" : ["a", "d"],
"c" : ["a", "d"],
"d" : ["e"],
"e" : ["d"]
}

# Print the graph
print(graph)
```

```{'c': ['a', 'd'], 'a': ['b', 'c'], 'e': ['d'], 'd': ['e'], 'b': ['a', 'd']}
```

## 显示图形顶点

```class graph:
def __init__(self,gdict=None):
if gdict is None:
gdict = []
self.gdict = gdict

# Get the keys of the dictionary
def getVertices(self):
return list(self.gdict.keys())

# Create the dictionary with graph elements
graph_elements = { "a" : ["b","c"],
"b" : ["a", "d"],
"c" : ["a", "d"],
"d" : ["e"],
"e" : ["d"]
}

g = graph(graph_elements)

print(g.getVertices())
```

```['d', 'b', 'e', 'c', 'a']
```

## 显示图形边缘

```class graph:

def __init__(self,gdict=None):
if gdict is None:
gdict = {}
self.gdict = gdict

def edges(self):
return self.findedges()
# Find the distinct list of edges

def findedges(self):
edgename = []
for vrtx in self.gdict:
for nxtvrtx in self.gdict[vrtx]:
if {nxtvrtx, vrtx} not in edgename:
edgename.append({vrtx, nxtvrtx})
return edgename

# Create the dictionary with graph elements
graph_elements = { "a" : ["b","c"],
"b" : ["a", "d"],
"c" : ["a", "d"],
"d" : ["e"],
"e" : ["d"]
}

g = graph(graph_elements)

print(g.edges())
```

```[{'b', 'a'}, {'b', 'd'}, {'e', 'd'}, {'a', 'c'}, {'c', 'd'}]
```

## 添加一个顶点

```class graph:

def __init__(self,gdict=None):
if gdict is None:
gdict = {}
self.gdict = gdict

def getVertices(self):
return list(self.gdict.keys())

# Add the vertex as a key
if vrtx not in self.gdict:
self.gdict[vrtx] = []

# Create the dictionary with graph elements
graph_elements = { "a" : ["b","c"],
"b" : ["a", "d"],
"c" : ["a", "d"],
"d" : ["e"],
"e" : ["d"]
}

g = graph(graph_elements)

print(g.getVertices())
```

```['f', 'e', 'b', 'a', 'c','d']
```

## 添加边缘

```class graph:

def __init__(self,gdict=None):
if gdict is None:
gdict = {}
self.gdict = gdict

def edges(self):
return self.findedges()

edge = set(edge)
(vrtx1, vrtx2) = tuple(edge)
if vrtx1 in self.gdict:
self.gdict[vrtx1].append(vrtx2)
else:
self.gdict[vrtx1] = [vrtx2]

# List the edge names
def findedges(self):
edgename = []
for vrtx in self.gdict:
for nxtvrtx in self.gdict[vrtx]:
if {nxtvrtx, vrtx} not in edgename:
edgename.append({vrtx, nxtvrtx})
return edgename

# Create the dictionary with graph elements
graph_elements = { "a" : ["b","c"],
"b" : ["a", "d"],
"c" : ["a", "d"],
"d" : ["e"],
"e" : ["d"]
}

g = graph(graph_elements)
```[{'e', 'd'}, {'b', 'a'}, {'b', 'd'}, {'a', 'c'}, {'a', 'e'}, {'c', 'd'}]