markdown
Exercises on Graph Traversal and Unweighted Distancemd 54c2d69
exercise/information/algorithm/search/graph-traversal-and-unweighted-distance.exercise.n.md

Exercises on Graph Traversal and Unweighted Distance

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

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
タブを全て閉じる