Python实现(1)实现图的邻接矩阵存储,包括建立、输出,输出指定顶点的边信息。 (2)实现图的邻接链表存储,包括建立、输出,输出指定顶点的边信息。 (3)实现将图的领接矩阵转换成邻接表,和将邻接表转换成邻接矩阵。 (4)在两种存储结构上,实现深度遍历算法。 (5)在两种存储结构上,实现广度遍历算法。
时间: 2024-02-27 17:53:24 浏览: 67
好的,下面是Python实现的代码:
1. 图的邻接矩阵存储
```python
class Graph:
def __init__(self, num_vertices):
self.adj_matrix = [[0] * num_vertices for _ in range(num_vertices)]
self.num_vertices = num_vertices
def add_edge(self, i, j, weight):
self.adj_matrix[i][j] = weight
self.adj_matrix[j][i] = weight
def get_edge(self, i, j):
return self.adj_matrix[i][j]
def display(self):
for i in range(self.num_vertices):
for j in range(self.num_vertices):
print(self.adj_matrix[i][j], end=' ')
print()
```
2. 图的邻接链表存储
```python
from collections import defaultdict
class Graph:
def __init__(self):
self.adj_list = defaultdict(list)
def add_edge(self, u, v, weight):
self.adj_list[u].append((v, weight))
self.adj_list[v].append((u, weight))
def get_edge(self, u, v):
for edge in self.adj_list[u]:
if edge[0] == v:
return edge[1]
return None
def display(self):
for u in self.adj_list:
print(u, end=' -> ')
for v, weight in self.adj_list[u]:
print(f'{v}({weight})', end=' ')
print()
```
3. 将图的领接矩阵转换成邻接表,和将邻接表转换成邻接矩阵
```python
class Graph:
def __init__(self, num_vertices):
self.adj_matrix = [[0] * num_vertices for _ in range(num_vertices)]
self.adj_list = defaultdict(list)
self.num_vertices = num_vertices
def add_edge(self, i, j, weight):
self.adj_matrix[i][j] = weight
self.adj_matrix[j][i] = weight
self.adj_list[i].append((j, weight))
self.adj_list[j].append((i, weight))
def matrix_to_list(self):
return self.adj_list
def list_to_matrix(self):
for u in self.adj_list:
for v, weight in self.adj_list[u]:
self.adj_matrix[u][v] = weight
self.adj_matrix[v][u] = weight
return self.adj_matrix
```
4. 图的深度遍历算法
```python
class Graph:
def __init__(self, num_vertices):
self.adj_matrix = [[0] * num_vertices for _ in range(num_vertices)]
self.adj_list = defaultdict(list)
self.num_vertices = num_vertices
def add_edge(self, i, j, weight):
self.adj_matrix[i][j] = weight
self.adj_matrix[j][i] = weight
self.adj_list[i].append((j, weight))
self.adj_list[j].append((i, weight))
def dfs(self, start):
visited = [False] * self.num_vertices
self._dfs(start, visited)
def _dfs(self, u, visited):
visited[u] = True
print(u, end=' ')
for v, weight in self.adj_list[u]:
if not visited[v]:
self._dfs(v, visited)
```
5. 图的广度遍历算法
```python
from queue import Queue
class Graph:
def __init__(self, num_vertices):
self.adj_matrix = [[0] * num_vertices for _ in range(num_vertices)]
self.adj_list = defaultdict(list)
self.num_vertices = num_vertices
def add_edge(self, i, j, weight):
self.adj_matrix[i][j] = weight
self.adj_matrix[j][i] = weight
self.adj_list[i].append((j, weight))
self.adj_list[j].append((i, weight))
def bfs(self, start):
visited = [False] * self.num_vertices
q = Queue()
q.put(start)
visited[start] = True
while not q.empty():
u = q.get()
print(u, end=' ')
for v, weight in self.adj_list[u]:
if not visited[v]:
visited[v] = True
q.put(v)
```
您可以根据需要调用这些方法来实现您的需求。
阅读全文