1)分别用邻接矩阵和邻接表存储图,输入一定数量的顶点和边,建立一个图; 2)实现图的深度优先遍历并输出结果; 3)实现图的广度优先遍历并输出结果。
时间: 2023-12-05 12:23:52 浏览: 20
建立图的邻接矩阵或邻接表存储并在此基础上实现图的深度优先遍历和广度优先遍历.doc
5星 · 资源好评率100%
好的,下面是用Python实现题目要求的代码:
1. 用邻接矩阵和邻接表存储图
```python
class Graph:
def __init__(self, vertices):
self.V = vertices
self.adj_matrix = [[0 for column in range(vertices)] for row in range(vertices)]
self.adj_list = [[] for i in range(vertices)]
def add_edge(self, u, v):
self.adj_matrix[u][v] = 1
self.adj_matrix[v][u] = 1
self.adj_list[u].append(v)
self.adj_list[v].append(u)
```
2. 实现图的深度优先遍历
```python
def DFSUtil(self, v, visited):
visited.add(v)
print(v, end=' ')
for neighbour in self.adj_list[v]:
if neighbour not in visited:
self.DFSUtil(neighbour, visited)
def DFS(self, start):
visited = set()
self.DFSUtil(start, visited)
for vertex in range(self.V):
if vertex not in visited:
self.DFSUtil(vertex, visited)
```
3. 实现图的广度优先遍历
```python
from collections import deque
def BFS(self, start):
visited = set()
queue = deque([start])
visited.add(start)
while queue:
vertex = queue.popleft()
print(vertex, end=' ')
for neighbour in self.adj_list[vertex]:
if neighbour not in visited:
visited.add(neighbour)
queue.append(neighbour)
for vertex in range(self.V):
if vertex not in visited:
queue = deque([vertex])
visited.add(vertex)
while queue:
vertex = queue.popleft()
print(vertex, end=' ')
for neighbour in self.adj_list[vertex]:
if neighbour not in visited:
visited.add(neighbour)
queue.append(neighbour)
```
完整代码如下:
```python
from collections import deque
class Graph:
def __init__(self, vertices):
self.V = vertices
self.adj_matrix = [[0 for column in range(vertices)] for row in range(vertices)]
self.adj_list = [[] for i in range(vertices)]
def add_edge(self, u, v):
self.adj_matrix[u][v] = 1
self.adj_matrix[v][u] = 1
self.adj_list[u].append(v)
self.adj_list[v].append(u)
def DFSUtil(self, v, visited):
visited.add(v)
print(v, end=' ')
for neighbour in self.adj_list[v]:
if neighbour not in visited:
self.DFSUtil(neighbour, visited)
def DFS(self, start):
visited = set()
self.DFSUtil(start, visited)
for vertex in range(self.V):
if vertex not in visited:
self.DFSUtil(vertex, visited)
def BFS(self, start):
visited = set()
queue = deque([start])
visited.add(start)
while queue:
vertex = queue.popleft()
print(vertex, end=' ')
for neighbour in self.adj_list[vertex]:
if neighbour not in visited:
visited.add(neighbour)
queue.append(neighbour)
for vertex in range(self.V):
if vertex not in visited:
queue = deque([vertex])
visited.add(vertex)
while queue:
vertex = queue.popleft()
print(vertex, end=' ')
for neighbour in self.adj_list[vertex]:
if neighbour not in visited:
visited.add(neighbour)
queue.append(neighbour)
# 测试
g = Graph(5)
g.add_edge(0, 1)
g.add_edge(0, 4)
g.add_edge(1, 2)
g.add_edge(1, 3)
g.add_edge(1, 4)
g.add_edge(2, 3)
g.add_edge(3, 4)
print("邻接矩阵:")
for row in g.adj_matrix:
print(row)
print("邻接表:")
for i, row in enumerate(g.adj_list):
print(f"{i}: {row}")
print("深度优先遍历:")
g.DFS(0)
print()
print("广度优先遍历:")
g.BFS(0)
print()
```
输出结果如下:
```
邻接矩阵:
[0, 1, 0, 0, 1]
[1, 0, 1, 1, 1]
[0, 1, 0, 1, 0]
[0, 1, 1, 0, 1]
[1, 1, 0, 1, 0]
邻接表:
0: [1, 4]
1: [0, 2, 3, 4]
2: [1, 3]
3: [1, 2, 4]
4: [0, 1, 3]
深度优先遍历:
0 1 2 3 4
广度优先遍历:
0 1 4 2 3
```
阅读全文