analysis of algorithms - hosei...depth-first search 深さ優先探索 algorithm アルゴリズム...
TRANSCRIPT
アルゴリズムの設計と解析
黄 潤和、佐藤 温(TA) 2012.4~
Contents (L8 – DFS and BFS) Basis of Graph Depth-First Search Breadth-First Search Weighted Graph
2
3
Properties 特性
Notation n number of vertices m number of edges deg(v) degree of vertex v
Property 1
Σv deg(v) = 2m Proof: each endpoint is counted twice
Property 2 In an undirected graph with no self-loops and no multiple edges m ≤ n (n − 1)/2
Proof: each vertex has degree at most (n − 1)
Example n = 4 m = 6 deg(v) = 3
4
Adjacency List 隣接リスト
An adjacency list is an array of lists. Each individual list shows what vertices a given vertex is adjacent to. 隣接リストは、リストの配列となります。個々のリストは、指定した頂点に隣接する頂点を示している。 An example: The graph
The adjacency list Vertex List containing adjacent vertices A B C D B A D C A D A B
B
D
C
A
Adjacency Matrix 隣接マトリクス
An adjacency matrix is a two-dimensional array in which the elements indicate whether an edge is present between two vertices. If a graph has n vertices, the adjacency matrix is an n-by-n matrix. An example: The graph
5
6
Subgraphs サブグラフ
A subgraph S of a graph G is a graph such that The edges of S are a
subset of the edges of G The vertices of S are a
subset of the vertices of G A spanning subgraph of G is a subgraph that contains all the vertices of G Gの全域部分木:全ての節を含む
Subgraph
Spanning subgraph
7
Connectivity 連結性
A graph is connected if there is a path between every pair of vertices 連結グラフ:全ての節がお互いに接続されている。
A connected component of a graph G is a maximal connected subgraph of G 連結部位はグラフGの最大のサブグラフである。
Connected graph
Non connected graph with two connected components
8
Spanning Trees and Forests 全域木と全域森
A spanning tree of a connected graph is a spanning subgraph that is a tree A spanning tree is not unique unless the graph is a tree グラフが木でない限り全域木は1つではない。 Spanning trees have applications to the design of communication networks コミュニケーションネットワークへの利用 A spanning forest of a graph is a spanning subgraph that is a forest
Graph
Spanning tree
9
Depth-First Search 深さ優先探索
D B
A
C
E
10
Outline Depth-first search 深さ優先探索
Algorithm アルゴリズム
Example 例
Properties 特性
Analysis 分析
Applications of DFS DFSのアプリケーション Path finding 経路調査結果
Cycle finding サイクル調査結果
11
Depth-First Search 深さ優先探索
Depth-first search (DFS) is a general technique for traversing a graph グラフ探索の一般的な手法の1つ A DFS traversal of a graph G Visits all the vertices and
edges of G 全ての節と枝を訪れる
Determines whether G is connected Gが連結しているかの判断
Computes the connected components of G Gの接続部位の計算
Computes a spanning forest of G 全域森の計算
DFS on a graph with n vertices and m edges takes O(n + m ) time n個の節とm個の枝の場合の時間: O(n + m ) DFS can be further extended to solve other graph problems Find and report a path
between two given vertices 与えられた2点間のパスの探索と表示
Find a cycle in the graph グラフ内のサイクルの発見
12
DFS Algorithm DFSアルゴリズム
The algorithm uses a mechanism for setting and getting “labels” of vertices and edges
Algorithm DFS(G, v) Input graph G and a start vertex v of G Output labeling of the edges of G in the connected component of v as discovery edges and back edges setLabel(v, VISITED)
for all e ∈ G.incidentEdges(v) if getLabel(e) = UNEXPLORED w ← opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) DFS(G, w) else setLabel(e, BACK)
Algorithm DFS(G) Input graph G Output labeling of the edges of G as discovery edges and back edges
for all u ∈ G.vertices() setLabel(u, UNEXPLORED) for all e ∈ G.edges() setLabel(e, UNEXPLORED) for all v ∈ G.vertices() if getLabel(v) = UNEXPLORED DFS(G, v)
13
Example 例 unexplored:未訪問 visited:訪問済
D B
A
C
E
D B
A
C
E
D B
A
C
E
discovery edge back edge
A visited vertex A unexplored vertex
unexplored edge
14
Example (cont.) 例
D B
A
C
E
D B
A
C
E
D B
A
C
E
D B
A
C
E
15
http://www.algolist.net/Algorithms/Graph/Undirected/Depth-first_search
16
Properties of DFS DFSの特性
Property 1 DFS(G, v) visits all the
vertices and edges in the connected component of v
全ての節と枝を訪れる。
Property 2 The discovery edges labeled
by DFS(G, v) form a spanning tree of the connected component of v
訪問済みの枝はラベルを貼られる。
D B
A
C
E
17
Analysis of DFS DFSの分析
Setting/getting a vertex/edge label takes O(1) time 節や枝のラベルの設定や取得: O(1) Each vertex is labeled twice once as UNEXPLORED (未訪問) once as VISITED (訪問済)
Each edge is labeled twice once as UNEXPLORED once as DISCOVERY or BACK (発見されたor戻る)
Method incidentEdges is called once for each vertex DFS runs in O(n + m) time provided the graph is represented by the adjacency list structure 実行時間: O(n + m) Recall that Σv deg(v) = 2m
18
Breadth-First Search 幅優先探索
C B
A
E
D
L0
L1
F L2
19
Outline 概要
Breadth-first search Algorithm アルゴリズム
Example 例
Properties 特性
Analysis 分析
Applications アプリケーション
DFS vs. BFS DFSとBFSの比較
Comparison of applications アプリケーションの比較
Comparison of edge labels エッジラベルの比較
20
Breadth-First Search 幅優先探索とは?
Breadth-first search (BFS) is a general technique for traversing a graph A BFS traversal of a graph G Visits all the vertices and
edges of G グラフGの全ての節と枝を訪れる
Determines whether G is connected 連結しているか判断する
Computes the connected components of G 接続部位の計算
Computes a spanning forest of G 全域森の計算
BFS on a graph with n vertices and m edges takes O(n + m ) time n個の節とm個の枝の場合の時間: O(n + m ) BFS can be further extended to solve other graph problems Find and report a path with
the minimum number of edges between two given vertices 2点間の最小の枝の数の探索とそのパスの表示
Find a simple cycle, if there is one 1つの単純なサイクルの発見
21
http://algorithms.blog55.fc2.com/blog-entry-24.html
22
Example 例 unexplored:未訪問 visited:訪問済
C B
A
E
D
discovery edge cross edge
A visited vertex A unexplored vertex
unexplored edge
L0
L1
F
C B
A
E
D
L0
L1
F
C B
A
E
D
L0
L1
F
23
Example (cont.) 例
C B
A
E
D
L0
L1
F
C B
A
E
D
L0
L1
F L2
C B
A
E
D
L0
L1
F L2
C B
A
E
D
L0
L1
F L2
12/6/5 17時36分 Breadth-First Search 24
Example (cont.) 例
C B
A
E
D
L0
L1
F L2
C B
A
E
D
L0
L1
F L2
C B
A
E
D
L0
L1
F L2
25
http://algorithms.blog55.fc2.com/blog-entry-24.html
26
Properties 特性
Notation Gs: connected component of s
Property 1 BFS(G, s) visits all the vertices and
edges of Gs Property 2 The discovery edges labeled by
BFS(G, s) form a spanning tree Ts of Gs
Property 3 For each vertex v in Li
The path of Ts from s to v has i edges
Every path from s to v in Gs has at least i edges
C B
A
E
D
L0
L1
F L2
C B
A
E
D
F
27
Analysis 解析 Setting/getting a vertex/edge label takes O(1) time 節や枝のラベルの設定や取得: O(1) Each vertex is labeled twice once as UNEXPLORED(未訪問) once as VISITED(訪問済)
Each edge is labeled twice once as UNEXPLORED once as DISCOVERY or CROSS(発見されたorクロスの)
Each vertex is inserted once into a sequence Li 各節は連結リストに1度挿入される Method incidentEdges is called once for each vertex BFS runs in O(n + m) time provided the graph is represented by the adjacency list structure 実行時間: O(n + m) Recall that Σv deg(v) = 2m
28
Applications アプリケーション
Using the template method pattern, we can specialize the BFS traversal of a graph G to solve the following problems in O(n + m) time Compute the connected components of G
Gの連結したコンポーネントの計算 Compute a spanning forest of G
Gの全域森の計算 Find a simple cycle in G, or report that G is a forest
Gの中のサイクルの発見、森であることの表示 Given two vertices of G, find a path in G between them with
the minimum number of edges, or report that no such path exists Gの中の2点間の最小の枝数のパスの発見、もしくはそんなパスは存在しないことの表示
29
DFS vs. BFS DFSとBFSの比較
C B
A
E
D
L0
L1
F L2
C B
A
E
D
F
DFS BFS
Applications DFS BFS Spanning forest, connected components, paths, cycles
√ √
Shortest paths √
Biconnected components √
30
DFS vs. BFS (cont.) DFSとBFSの比較
Back edge (v,w) w is an ancestor of v in
the tree of discovery edges
Cross edge (v,w) w is in the same level as
v or in the next level in the tree of discovery edges
C B
A
E
D
L0
L1
F L2
C B
A
E
D
F
DFS BFS
Weighted Graph
31
Shortest Path
32
Triangle inequality
33
Single source shortest paths
34
35
Exercise 7-1 Let G be a graph whose vertices are integers 1 to 7, and let the adjacent list be given by the table below: 節の集合が整数1から7までのグラフG また以下の隣接リストがある。 vertex adjacent vertices 1 2 4 6 2 1 5 3 4 7 4 1 3 7 5 2 7 6 1 7 7 3 4 5 6
1. Draw G グラフG を描きなさい 2. Show the adjacency matrix of G グラフGの隣接マトリクスを書きなさい 3. Show the spanning tree of G using a DFS traversal starting from vertex 1.
節の集合1からDFSを使ってグラフG の全域木を書きなさい。 4. Show the spanning tree of G using a BFS traversal starting from vertex 1.
節の集合1からBFSを使ってグラフG の全域木を書きなさい。
Exercise 8-1 Write an algorithm to check whether a graph G has cycles or not. What is the running time of your algorithm? (Hint: modify the DFS. Any back edge will create a cycle) グラフG にサイクルがあるかどうかをチェックするアルゴリズムを書いてください。 またそのアルゴリズムの実行時間は? (ヒント: DFSを変更してください)