You are given a starting URL and an HtmlParser API. The parser returns every URL found on a page:
Design a multithreaded web crawler that visits every URL reachable from startUrl that has the same hostname as startUrl.
A hostname is the part of a URL between the scheme and the next /. For example, the hostname of http://news.yahoo.com/news/topics/ is news.yahoo.com.
Your crawler must:
startUrl in the result.startUrl.The result may be returned in any order.
The judge supplies the HtmlParser implementation and calls your WebCrawler class. The small HtmlParser declaration in the starter code describes that API; do not modify its method signature.
The judge also preloads the standard concurrency, collection, and callback APIs for every supported language. You do not need to add import, include, using, or package statements.
Input:
Output:
Explanation: http://news.google.com/news is reachable, but its hostname differs from the hostname of startUrl, so it is not crawled.
Input:
Output:
Explanation: The duplicate link and cycle do not cause either page to be crawled more than once.
1 <= number of URLs <= 10001 <= url.length <= 300startUrl is a valid URL in the hidden web graph.http:// or https:// scheme.htmlParser.getUrls(url) is safe to call concurrently.The URLs form a directed graph. Each page is a vertex, and every link returned by getUrls is an outgoing edge. The crawl is therefore a graph traversal with three extra requirements:
Checking the visited set and then adding a URL must be one atomic operation. If those steps are separate, two workers can both observe that a URL is absent and both crawl it.
An empty queue alone does not mean the crawl is complete. Another worker may still be parsing a page and may discover more URLs a moment later.
Use a fixed worker pool and shared crawl state:
pending count covering every queued or currently processing URL.Initialize the state by marking and enqueueing startUrl, with pending = 1.
Each worker repeatedly:
getUrls outside the shared-state lock so slow parsing does not serialize the workers.pending before it is placed on the queue.pending after the current URL has been fully processed.When pending becomes zero, no URL is queued and no worker can still discover more work. The workers can then be stopped and joined safely.
startUrl is inserted into the visited set before any worker begins, so it always appears in the result.
Before a discovered URL is enqueued, its hostname is compared with the starting hostname. Therefore, no different-host URL is crawled or returned.
The visited-set insertion is atomic. Exactly one worker can successfully claim a particular URL, so every same-host URL is enqueued and parsed at most once, even with duplicate edges or cycles.
Every claimed URL increments pending before becoming available to workers. A worker decrements pending only after it has inspected all outgoing links. Therefore, pending counts all work that could still reveal another URL. It can reach zero only after the entire reachable same-host graph has been explored.
Finally, the main thread joins all workers before copying the visited set. The returned result is complete and no crawl work remains in the background.
Let V be the number of reachable URLs with the starting hostname and E be the total number of links returned while crawling those URLs.
O(V + E) total work, excluding parser latency.O(V) for the visited set and work queue.With W workers, independent parser calls can overlap. The wall-clock time depends on graph shape and parser latency, but the total graph-processing work remains O(V + E).