Path search
path finds a chain of links between two articles. --strategy picks what “best” means:
| Strategy | Finds | Search |
|---|---|---|
followed | The links readers click most (default) | Followed paths |
shortest | The fewest clicks, all links counting the same | Bidirectional BFS |
popular | A way through pages readers click into most | Page costs |
niche | A way through pages few others link to | Page costs |
niche-clicks | A way through pages readers rarely click into | Page costs |
niche-views | A way through pages readers rarely visit | Page costs |
followed, popular, niche-clicks and niche-views read the click_count and view_count
columns. A link or a page without a count counts 0. On a graph built without a clickstream,
every count is 0: followed then returns a path with the fewest clicks, and the command warns
you. strategies prints how each one scores a path.
Bidirectional breadth-first search
A plain breadth-first search (BFS) starts from the first page and visits its links, then the links of those links, and so on. With about 85 links per page, the number of pages grows very fast at each level.
path searches from both ends at once:
- The forward side starts at the source page and follows links forward.
- The backward side starts at the goal page and follows links backward: “which pages link to this one?”
At each step, the search grows one whole level of one side. It stops as soon as a page is reached by both sides. Because each side is complete up to its depth, that page closes a shortest path.
Each side keeps, for each page it reached, the page it came from. The path is rebuilt by walking these links back from the meeting page to both ends.
The backward side without a reverse index
A WGRF file stores only outgoing links. To find “pages that link to X”, you need a reverse index, and the format does not have one yet (see “Open / deferred” in the spec). Building one when the file loads takes several seconds.
Instead, a backward step scans every link in the graph once, in parallel, and keeps the links that point into the backward frontier. This costs the same whatever the frontier size: about 235 million links read in order.
A forward step reads only the links of the frontier pages, but in random order.
So the search picks the side to grow by cost:
- If the forward frontier has fewer than 5 links per node in the graph (about 14 million links for French Wikipedia), it takes a forward step.
- Otherwise, it takes a backward step with a full scan.
For example, Tour Eiffel to Pikachu visits about 100,000 pages and takes about 30 ms.
Parallel levels with stable results
explore and the forward steps of path expand a level in parallel. Several threads can reach
the same new page at the same time. To keep the result the same on every run, each new page keeps
the parent with the lowest position in the frontier, as a single-threaded search would. The
output does not depend on thread timing.
Followed paths
path --strategy followed looks for the path readers are most likely to click through, from the
clickstream.
Link cost
A link’s cost is about how unlikely its click is, in bits. It compares the link’s clicks with the clicks on all the links of its page:
cost = 1 + bits(clicks out of the page) - bits(clicks on the link)
| Share of the page’s clicks | Cost |
|---|---|
| more than half | 1 |
| about a quarter | 2 or 3 |
| about a thousandth | 11 |
| no clicks | 33 |
A link without clicks costs 33, more than any clicked link. So the search takes a long chain of clicked links before one unclicked link. The counts are shares of each page’s own clicks: a link that gets most of the clicks of a small page costs as little as the main link of a big one.
Dijkstra with buckets
The cost belongs to the link, so a page can be reached again later at a lower cost. The search is Dijkstra’s algorithm: it fixes a page’s distance only when it takes the page out of the queue. Costs are integers from 1 to 33, so the queue is one bucket per distance (Dial’s algorithm).
The search runs forward only, on one thread, and takes pages in the order they entered their bucket, so the result is the same on every run. The clicks out of each page come from one parallel scan when the command starts.
Page costs
popular, niche, niche-clicks and niche-views put the cost on the page you enter instead
of on the link. They differ by what they count for each page and whether a high count is cheap or
expensive.
| Strategy | Count | Cheap pages |
|---|---|---|
popular | clicks on links into the page | high counts |
niche | links into the page | low counts |
niche-clicks | clicks on links into the page | low counts |
niche-views | the page’s view_count | low counts |
The in-link and click counts come from one parallel scan of every link when the command starts, about 100 ms on French Wikipedia.
Niche: cost from rarity
The niche strategies make entering a page cost the number of bits of its count, at least 1:
| Count | Cost |
|---|---|
| 0 or 1 | 1 |
| 2 to 3 | 2 |
| 4 to 7 | 3 |
| 1,024 to 2,047 | 11 |
| about 100,000 | 17 |
Every page costs at least 1, so a path does not get long just to use small pages: three pages with 10 in-links (cost 4 each, 12 in total) lose to one page with 1,000 (cost 10). Doubling a page’s count adds 1 to its cost, so a hub costs about as much as a few obscure pages.
Popular: cost from popularity
popular turns this around. A page with the most-clicked count costs 1, and each bit less adds
1:
cost = 1 + bits(largest count in the graph) - bits(count of the page)
A page nobody clicked into costs the most, 1 plus the bits of the largest count. As with the niche strategies, a short path through less popular pages can still beat a long detour through popular ones.
The goal and the total
The search returns a path with the lowest total cost over the pages after the source. The goal’s own cost is the same for every path, so it changes nothing.
Search by cost buckets
This is a shortest-path search with a cost on each page instead of on each link: every way into a page costs the same. So the search can grow pages by increasing distance and fix a page’s distance the first time it reaches it. Any later way in comes from a page at least as far.
Costs are small integers (1 to 33), so pages wait in one bucket per distance instead of a priority queue. The search takes the lowest non-empty bucket, expands it in parallel like one level of a breadth-first search, and puts each new page into the bucket for its distance plus its cost. It stops when it reaches the goal. When several paths share the lowest cost, the result is the same on every run, for the same reason as a breadth-first level.
The search goes forward only. A backward side would need a full scan of every link for each
bucket, and there are dozens of buckets. For example, niche from Tour Eiffel to Pikachu
visits about 180,000 pages and takes about 40 ms after the in-link count.
Explore with the clickstream
explore walks level by level like a breadth-first search, so each page shows at its shortest
distance from the start. --strategy picks which page of the level above becomes its parent:
followed(default): the page whose link to it readers click most. Ties, and pages with no clicked link from the level above, keep the first-found parent.first-found: the first page that reached it, see Parallel levels.
With a clickstream, each page shows the clicks of the link from its parent. --order clicks
(default) lists the most clicked links first.
Adding a strategy
Each strategy is one PathStrategy object listed in PathStrategies.All. path --strategy, its
help, the missing-column warnings and the strategies command all read that list, so
PathCommand has no per-strategy code.
The code lives in WikipediaPath.GraphBuilder/Analyze/Paths/:
| Folder | Contents |
|---|---|
Paths/ | The path and strategies commands |
Strategies/ | PathStrategy, IPathSearch, PageCostStrategy, each strategy and the PathStrategies list |
Algorithms/ | The searches (PathSearch, FollowedPathSearch, PageCostPathSearch), PathResult and PageCounts |
A strategy picks an algorithm and the counts it scores with. An algorithm knows nothing about strategies or the command line.
A strategy defines:
| Member | Role |
|---|---|
Name | The --strategy value, lowercase with dashes |
Summary | One line for path --help: what the path goes through |
Details | A few sentences for strategies: how it scores, what each page shows |
Reads | click_count, view_count or none. path warns when the graph lacks one |
Prepare | Counts what the search needs once, and returns an IPathSearch |
IPathSearch.Find runs the search. IPathSearch.Score returns the text printed next to each page
of the result, or null.
Pick the shape that fits:
- A cost per page from one count. Add a
PageCostStrategyentry. It takes a function that returns one count per node (PageCountshas in-links, clicks in, clicks out and views), whether low or high counts are cheap, and the unit printed after each count. It reuses the page costs search, and needs no new class. - Another cost or another search. Subclass
PathStrategyand return your ownIPathSearch.ShortestStrategyandFollowedStrategyare the examples.
Then add the new name to the tables above and in the command reference. The tests check that every strategy has a unique name, a summary and details.
Excluded pages
With --exclude-date-page or --exclude-country, the search never goes through a page of that
kind. The source and the goal can still be one. This works with every strategy, and with
explore.