markdown
グラフ探索と無重み距離の演習md 54c2d69
exercise/information/algorithm/search/graph-traversal-and-unweighted-distance.exercise.n.md

グラフ探索たんさく無重むおも距離きょり演習えんしゅう

date2026-07-14document_iddoc_1e9a64bb47ce2f51653a45cd7cab0053descriptionグラフの隣接・道・距離とDFS/BFSの候補管理、訪問済み集合、BFS最短性を確認する。prerequisitesグラフの基本 / DFSとBFSの基本type問題演習content_typeexercisestatusactiverelateddata/lecture/information/graph/graph-basics.lecture.n.md / data/lecture/information/algorithm/search/dfs-and-bfs-basics.lecture.n.md
informationalgorithmgraphexercise

1問題もんだい1:みち距離きょり連結成分れんけつせいぶん

無向むこうグラフ G=(V,E)

V={a,b,c,d,e,f},E={{a,b},{a,c},{b,d},{c,d},{e,f}}

さだめる。

  1. a から d へのながさ2のみちをすべてげよ。
  2. d(a,d)d(a,f)もとめよ。
  3. 連結成分れんけつせいぶんをすべてもとめよ。

1.1解答かいとう

ながさ2のみちa,b,da,c,d である。したがって d(a,d)=2 である。afあいだみち存在そんざいしないので d(a,f)= である。連結成分れんけつせいぶん{a,b,c,d}{e,f} である。

2問題もんだい2:DFSとBFSの順序じゅんじょ

問題もんだい1のグラフで、各隣接かくりんせつリストはアルファベットじゅん調しらべるものとする。始点してんa とし、頂点ちょうてん候補こうほ追加ついかした時点じてん訪問済ほうもんずみにする。

  1. 再帰的さいきてきDFSではじめて訪問ほうもんする頂点ちょうてん順序じゅんじょもとめよ。
  2. BFSではじめて訪問ほうもんする頂点ちょうてん順序じゅんじょもとめよ。
  3. BFSが記録きろくする探索たんさくレベル もとめ、a から各到達可能頂点かくとうたつかのうちょうてんへの距離きょり一致いっちすることを確認かくにんせよ。

2.1解答かいとう

DFSは a,b,d,cじゅん訪問ほうもんする。a から bb から dすすみ、d から未訪問みほうもんcすすむためである。

BFSは a,b,c,dじゅん訪問ほうもんする。距離きょり

[a]=0,[b]=[c]=1,[d]=2

であり、問題もんだい1でもとめた最短さいたんみちながさと一致いっちする。e,fa から到達不能とうたつふのうである。

3問題もんだい3:訪問済ほうもんず記録きろく時点じてん

三角形さんかくけいグラフ

V={s,u,v},E={{s,u},{s,v},{u,v}}

でBFSをおこなう。頂点ちょうてんをキューからすまで訪問済ほうもんずみにしない実装じっそうでは、どの重複ちょうふく発生はっせいるか説明せつめいせよ。また、追加時ついかじ訪問済ほうもんずみにする修正しゅうせい重複ちょうふくふせ理由りゆうべよ。

3.1解答かいとう

s処理しょりu,v がキューへはいる。uした時点じてんv がまだ未訪問みほうもんなら、へん {u,v} により vふたた追加ついかされ、キューに vふた存在そんざいる。

追加ついか同時どうじ訪問済ほうもんずみにすれば、最初さいしょv追加ついかした時点じてんvS となる。そのべつへんから v調しらべても追加条件ついかじょうけん vSたさないため、各頂点かくちょうてん高々たかだか一度いちどしか候補こうほ追加ついかされない。

4問題もんだい4:表現ひょうげん計算量けいさんりょう手法しゅほう選択せんたく

|V|=n,|E|=m無向むこうグラフについてこたえよ。

  1. 隣接りんせつリストに格納かくのうされるへん項目数こうもくすう空間計算量くうかんけいさんりょうしめせ。
  2. 隣接行列りんせつぎょうれつ空間計算量くうかんけいさんりょうへん存在判定そんざいはんてい時間計算量じかんけいさんりょうしめし、グラフにてきした表現ひょうげんえらべ。
  3. 隣接りんせつリストをもちいるDFSとBFSの時間計算量じかんけいさんりょう説明せつめいせよ。
  4. 無重むおも最短距離さいたんきょり到達可能性とうたつかのうせいへんおもみがことなる最短距離さいたんきょりに、それぞれDFSまたはBFSを適用てきようできるかべよ。

4.1解答かいとう

無向辺むこうへん両端点りょうたんてんのリストに一度いちどずつあらわれるため、へん項目数こうもくすう2m頂点ちょうてんのリストもふくめた空間計算量くうかんけいさんりょうO(n+m) である。隣接行列りんせつぎょうれつO(n2) 空間くうかん使用しようするが、へん存在そんざいO(1) 時間じかん判定はんていできる。mn2 より十分小じゅうぶんちいさいグラフには隣接りんせつリストがてきしている。

隣接りんせつリストでは、各頂点かくちょうてん高々たかだか一度いちど各無向辺かくむこうへん両端りょうたんから高々たかだか一度いちどずつ調しらべるため、DFSとBFSは O(n+m) 時間じかん動作どうさする。無重むおも最短距離さいたんきょりにはBFSを使用しようする。到達可能性とうたつかのうせいにはDFSとBFSのどちらも使用しようできる。へんおもみがことなる場合ばあい通常つうじょうのDFSとBFSはいずれも最短距離さいたんきょり保証ほしょうしない。おもみが非負ひふならDijkstraほうなどを使用しようする。

Exercises on Graph Traversal and Unweighted Distance

1Problem 1: Paths, distance, and connected components

Let the undirected graph G=(V,E) be defined by

V={a,b,c,d,e,f},E={{a,b},{a,c},{b,d},{c,d},{e,f}}.
  1. List every path of length 2 from a to d.
  2. Find d(a,d) and d(a,f).
  3. Find all connected components.

1.1Solution

The length-2 paths are a,b,d and a,c,d. Therefore d(a,d)=2. No path joins a to f, so d(a,f)=. The connected components are {a,b,c,d} and {e,f}.

2Problem 2: DFS and BFS order

Use the graph from Problem 1 and examine every adjacency list in alphabetical order. Start at a and mark a vertex visited when it is added to the candidates.

  1. Determine the first-visit order of recursive DFS.
  2. Determine the first-visit order of BFS.
  3. Find the traversal levels recorded by BFS and verify that they equal the distances from a to every reachable vertex.

2.1Solution

DFS visits a,b,d,c. It proceeds from a to b, from b to d, and then from d to the unvisited vertex c.

BFS visits a,b,c,d. The recorded distances are

[a]=0,[b]=[c]=1,[d]=2.

These levels equal the lengths of the shortest paths found in Problem 1. Vertices e,f are unreachable from a.

3Problem 3: When to mark a vertex visited

Run BFS on the triangle graph

V={s,u,v},E={{s,u},{s,v},{u,v}}.

Explain what duplication may occur if a vertex is not marked visited until it is removed from the queue. Then explain why marking it when it is added prevents the duplication.

3.1Solution

Processing s adds u,v to the queue. When u is removed, v may still be unvisited, so edge {u,v} can add v again, leaving two copies of v in the queue.

If a vertex is marked at insertion, then vS immediately after its first insertion. A later examination through another edge fails the condition vS, so every vertex is added to the candidates at most once.

4Problem 4: Representation, complexity, and method selection

Answer the following questions for an undirected graph with |V|=n and |E|=m.

  1. Give the number of edge entries stored in adjacency lists and their space complexity.
  2. Give the space complexity and edge-query time of an adjacency matrix, and choose a representation for a sparse graph.
  3. Explain the running time of DFS and BFS with adjacency lists.
  4. State whether DFS or BFS applies to unweighted shortest distance, reachability, and shortest distance with unequal edge weights.

4.1Solution

Each undirected edge appears once in the list of each endpoint, so there are 2m edge entries and the lists, including the vertex headers, use O(n+m) space. An adjacency matrix uses O(n2) space but answers an edge-existence query in O(1) time. Adjacency lists are preferable for a sparse graph, where m is much smaller than n2.

With adjacency lists, each vertex is processed at most once and each undirected edge is examined at most once from each endpoint. Thus DFS and BFS run in O(n+m) time. Use BFS for unweighted shortest distance. Either DFS or BFS determines reachability. Neither ordinary DFS nor ordinary BFS guarantees shortest distance when edge weights differ; for nonnegative weights, use a method such as Dijkstra's algorithm.

raw .n.md をコピー
loc をコピー (filepath:line ~ line)
copy share link
copy encoded share link
path をコピー
copy share link
copy encoded share link
copy share link
copy encoded share link
タブを全て閉じる