KonkurStudy
همه کارت‌ها

BFS Scans Each Undirected Edge Exactly Twice

On adjacency lists, BFS touches every undirected edge twice (once per endpoint), giving total work Θ(V+E)\Theta(V+E).

کارت مفهوم کنکور ارشد علوم کامپیوتر

اهمیت
۰/۵

Intuition

BFS dequeues each vertex once and then scans that vertex's adjacency list. In an undirected graph the edge {u,w}\{u,w\} is stored in both uu's list and ww's list. So while processing uu you scan it once, and while processing ww you scan it again: each edge is examined exactly twice.

Result / Method

Summing the scan cost over all vertices gives ∑udeg⁡(u)=2∣E∣\sum_u \deg(u) = 2|E| edge inspections. Adding the O(V)O(V) cost of enqueueing/dequeueing each vertex once yields the textbook bound: T(n)=Θ(V+E).T(n) = \Theta(V+E). Note O(V)O(V) alone is wrong: for a connected graph EE can reach (n2)=Θ(n2)\binom{n}{2}=\Theta(n^2).

Worked example

Graph with edges {a,b},{a,c},{b,c}\{a,b\},\{a,c\},\{b,c\} (V=3,E=3V=3,E=3):

adj[a] = [b, c]
adj[b] = [a, c]
adj[c] = [a, b]

Edge {a,b}\{a,b\} is seen in adj[a] and in adj[b] -> 2 times. Same for the other two edges. Total scans =6=2E= 6 = 2E.

Exam trick: "Vertices are visited in order of smallest distance from vv" sounds true (BFS is level-order), but the order within a level is arbitrary, so it does not pin down the visiting order. The clean, unambiguous fact is that each undirected edge is scanned exactly twice — the very accounting behind Θ(V+E)\Theta(V+E).

فرمول‌های کلیدی

BFS running time
Θ(V+E)\Theta(V+E)

Each vertex dequeued once (O(V)) plus each edge scanned twice (O(E)).

Handshake / degree sum
∑udeg⁡(u)=2∣E∣\sum_{u} \deg(u) = 2|E|

Total adjacency-list scanning equals twice the edge count, so every edge is touched twice.

#bfs#graph-traversal#adjacency-list#running-time#complexity

سوالات تمرینی