Finds an isolated vertex in a graph using only a restricted threshold oracle — a
black box that answers "does this graph have more than k edges?" — rather than
direct access to the adjacency list. The goal is to do this in as few oracle calls
as possible, since each query is treated as the expensive operation.
Query wraps a graph and exposes one operation:
oracle.query(threshold) # -> True if edge_count > threshold, else FalseNo other information about the graph is available. In particular, you can't ask
"what are vertex v's neighbors?" — only "is the total edge count above this
number?", optionally after removing some vertices from consideration.
1. Recover the edge count — binary search over the threshold.
find_num_edges binary-searches the range [0, n(n-1)/2] (the maximum possible
edges on n vertices) for the exact edge count, using query(mid) to halve the
range each step. This takes O(log(n²)) = O(log n) queries.
2. Localize an isolated vertex — binary search over the vertex list.
find_isolated_vertex then binary-searches the vertex list itself. At each step
it removes a candidate prefix of vertices from a scratch copy of the graph and
asks the oracle whether the edge count is unchanged (query(total_edges - 1)
returning True means removing that prefix removed zero edges). If the prefix
turns out to be edge-free, every vertex in it is isolated, so the search recurses
into that half; otherwise it recurses into the other half. This narrows n
vertices down to one candidate in O(log n) queries.
3. Verify. The candidate vertex is removed on its own and the oracle is queried once more to confirm the edge count didn't change — the actual proof that the vertex is isolated, not just a byproduct of the search.
Total cost: O(log n) oracle queries, versus the O(n) queries a linear scan
over vertices (or an O(m) read of the full edge list) would need.
$ python3 isolatedvertex.py
Total queries made: 9
total edges: 12
total vertices: 8
Isolated vertex found: 7
On the 8-vertex, 12-edge sample graph in isolatedvertex.py, the algorithm finds
isolated vertex 7 in 9 total queries — 5 to recover the edge count via the
threshold search, 3 to binary-search the vertex list down to one candidate, and
1 to verify it.
pip install networkx
python3 isolatedvertex.pyEdit the graph construction at the bottom of isolatedvertex.py to try other
inputs.