Given a starting URL start_url and a simulated web represented as a directed graph of URLs, implement a web crawler that returns all URLs reachable from start_url that share the same hostname as start_url.
The hostname of a URL is the substring between http:// and the next / (or the end of the string). For example, the hostname of http://news.example.com/page is news.example.com.
You are given the web as a map graph where graph[url] is the list of URLs directly linked from url. Starting from start_url, traverse the linked URLs using BFS or DFS, but only follow links whose hostname matches the hostname of start_url. Return the list of all visited URLs.
Function Signature
def crawl(start_url: str, graph: dict[str, list[str]]) -> list[str]:
Rules
-
start_url
is always part of the result.
-
A link is followed only when its hostname is exactly equal to the hostname of
start_url
. A subdomain such as
blog.shop.io
is a different hostname from
shop.io
.
-
A page reached through a link with a different hostname is not visited, even if it links back to the starting hostname.
-
A URL that appears in a link list but is not a key of
graph
has no outgoing links.
-
Each URL appears once in the result. Return the URLs sorted in ascending lexicographic (plain string) order.
Constraints
-
1 <= len(graph) <= 1000
-
Each URL starts with
http://
, and the rest of it contains only lowercase letters, digits,
.
and
/
.
-
All URLs in
graph
are unique.
-
start_url
is guaranteed to be a key in
graph
.
-
1 <= len(url) <= 300
Examples
Input: start_url = "http://news.example.com/", graph = {
"http://news.example.com/": ["http://news.example.com/sports", "http://other.com/home"],
"http://news.example.com/sports": ["http://news.example.com/tech", "http://news.example.com/"],
"http://news.example.com/tech": [],
"http://other.com/home": ["http://news.example.com/"]
}
Output: ["http://news.example.com/", "http://news.example.com/sports", "http://news.example.com/tech"]
The link to http://other.com/home is skipped because its hostname is other.com. The crawl reaches /sports from the start page and /tech from /sports, and the link from /sports back to the start page is already visited.
Input: start_url = "http://a.com/x", graph = {
"http://a.com/x": ["http://b.com/y"],
"http://b.com/y": ["http://a.com/z"],
"http://a.com/z": []
}
Output: ["http://a.com/x"]
The only link out of the start page goes to b.com, so the crawl stops there. http://a.com/z has the right hostname but can only be reached through b.com.
Input: start_url = "http://shop.io/z", graph = {
"http://shop.io/z": ["http://shop.io/b", "http://shop.io/a"],
"http://shop.io/b": ["http://shop.io", "http://blog.shop.io/"],
"http://shop.io/a": ["http://shop.io/z"],
"http://shop.io": [],
"http://blog.shop.io/": ["http://shop.io/c"],
"http://shop.io/c": []
}
Output: ["http://shop.io", "http://shop.io/a", "http://shop.io/b", "http://shop.io/z"]
http://shop.io has no slash after the host, so its hostname is the whole rest of the string, shop.io, and it is visited. blog.shop.io is a different hostname, so http://shop.io/c is never reached. The output is sorted, which is why the start page comes last.