Python 應用鄰接表實作圖的簡易教材
目標
- 了解圖的基本概念與應用場景。
- 使用鄰接表表示圖的結構。
- 學習圖的基本操作(如遍歷、最短路徑等)。
1. 圖的基本概念
- 圖是由節點(Node)和邊(Edge)組成的數據結構。
- 無向圖:節點間的邊沒有方向,如好友關係。
- 有向圖:節點間的邊有方向,如追隨關係。
- 加權圖:邊有數值權重,如距離或成本。
2. 使用鄰接表表示圖
鄰接表是用字典表示圖的方法,節點的鄰居存儲在列表中。
graph = {
1: [2, 3],
2: [1, 4],
3: [1],
4: [2]
}
print(graph)
from collections import defaultdict
graph = defaultdict(list)
n = int(input("輸入節點數量: "))
for _ in range(n - 1):
a, b = map(int, input("輸入一條邊: ").split())
graph[a].append(b)
graph[b].append(a)
print("鄰接表:", dict(graph))
3. 圖的基本操作
操作 1:深度優先搜尋(DFS)
- 深度優先搜尋是沿著一條路徑一直走到底,然後回頭找其他路徑。
def dfs(node, graph, visited):
print(node, end=" ")
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
dfs(neighbor, graph, visited)
visited = set()
dfs(1, graph, visited)
操作 2:廣度優先搜尋(BFS)
from collections import deque
def bfs(start, graph):
q = deque([start])
visited = set()
visited.add(start)
while q:
node = q.popleft()
print(node, end=" ")
for neighbor in graph[node]:
if neighbor not in visited:
visited.add(neighbor)
q.append(neighbor)
bfs(1, graph)
操作 3:判斷是否連通
def is_connected(graph, n):
visited = set()
def dfs(node):
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
dfs(neighbor)
dfs(1)
return len(visited) == n
print("圖是否連通:", is_connected(graph, n))
操作 4:尋找最短路徑
def shortest_path(graph, start, end):
from collections import deque
q = deque([(start, 0)])
visited = set()
visited.add(start)
while q:
node, dist = q.popleft()
if node == end:
return dist
for neighbor in graph[node]:
if neighbor not in visited:
visited.add(neighbor)
q.append((neighbor, dist + 1))
return -1
print("最短路徑長度:", shortest_path(graph, 1, 4))
4. 綜合應用:圖的問題解決
應用 1:找到所有連通分量
def connected_components(graph, n):
visited = set()
components = []
def dfs(node, component):
visited.add(node)
component.append(node)
for neighbor in graph[node]:
if neighbor not in visited:
dfs(neighbor, component)
for node in range(1, n + 1):
if node not in visited:
component = []
dfs(node, component)
components.append(component)
return components
print("圖的連通分量:", connected_components(graph, n))
總結
- 使用鄰接表表示圖:節省空間,結構清晰。
- 掌握遍歷方法:DFS 和 BFS 是解決圖問題的基礎。
- 圖的基本操作:
- 實用性強:這些操作能解決地圖導航、社交網絡等實際問題。