(This question is a variation of the LeetCode question 1242. Web Crawler Multithreaded. If you haven't completed that question yet, it is recommended to solve it first.)
Given a URL startUrl and an interface htmlParser, implement a concurrent web crawler to discover and return all unique URLs that have the exact same hostname as startUrl.
The crawler must follow the rules below:
- Starting Point: Begin crawling from
startUrl. - Same Hostname: Only record and further crawl URLs whose hostname is exactly identical to the hostname of
startUrl.
All URLs use the http protocol without a port number.
The hostname is defined as the substring between "://" and the next '/' (or the end of the string if no further '/' exists).
- URL Uniqueness: Before checking uniqueness, remove the fragment part (everything after and including
'#'). Two URLs are considered the same if they match after this sanitization step.- For example:
"http://example.com/page#section1"and"http://example.com/page"should both be treated as"http://example.com/page"for uniqueness checking and for subsequent crawling.
- For example:
- No Duplicate Visits: A sanitized URL must not be crawled or added more than once.
- Fragment Sanitization Order: Fragment removal occurs before hostname comparison and before deduplication.
- Graph Characteristics: The hyperlink graph may contain cycles, and pages may reference previously visited pages.
You are provided with the implementation of HtmlParser:
/*
* Provided Html Parser implementation. You should NOT modify it.
*/
class HtmlParser {
// Returns all raw URLs from the webpage of the given URL.
List<String> getUrls(String url){...}
}
Each call to getUrls is subject to a certain latency to simulate the real-world network conditions, so your solution must use concurrency to fetch multiple pages in parallel.
For testing purposes, you will be given three variables urls, edges and startUrl to describe the underlying hyperlink graph. Only startUrl is accessible in your code; urls and edges are not directly available.
Return all discovered URLs (after sanitization) in any order.
Constraints:
- All URLs use the
httpprotocol and do not contain a port number. - 1 ≤
urls.length≤ 1000, - 1 ≤
edges.length≤ 1000 - Each
getUrlscall will return in ≤ 15 ms.
Example 1:
Input: urls = ["http://example.com/page1", "http://example.com/page2", "http://example.com/page3#sectionA", "http://example.net/page4#"], edges = [[0, 1], [0, 2], [1, 3], [2, 0]], startUrl = "http://example.com/page1"
Output: ["http://example.com/page1", "http://example.com/page2", "http://example.com/page3"]
Explanation:
- All three reachable pages share the hostname "example.com".
- The URL "http://example.com/page3#sectionA" is sanitized to "http://example.com/page3".
- The page "http://example.net/page4#" is ignored due to a different hostname.
Example 2:
Input: urls = ["http://news.yahoo.com/home", "http://news.google.com/top", "http://news.yahoo.com/news"], edges = [[1, 0], [0, 2]], startUrl = "http://news.google.com/top"
Output: ["http://news.google.com/top"]
Example 3:
Input: urls = ["http://site.com/a", "http://site.com/b#frag1", "http://site.com/b#frag2", "http://site.com/c", "http://other.com/x", "http://site.com/d", "http://site.com/e#", "http://site.com/f"], edges = [[0, 1], [0, 2], [1, 3], [2, 3], [3, 4], [3, 5], [5, 0], [5, 6], [6, 7], [7, 0]], startUrl = "http://site.com/a"
Output: ["http://site.com/a", "http://site.com/b", "http://site.com/c", "http://site.com/d", "http://site.com/e", "http://site.com/f"]
