A stack is a useful structure for storing data because it has many useful applications. For example, it can be applied in implementing recursive computing method. A stack is a “last-in-first-out” structure — data items are stored linearly as one item is placed on top of the other. The only two operations are: pop and push. The pop operation retrieves the top data item on the stack, while the push operation puts a new data item on the top of the stack. The figure below shows how a stack works:
(a) Can you write up a computing procedure for DFS using a stack instead of using recursion?
(b) Can you suggest a practical situation where we must use a stack instead of using recursion?
Follow-Up:
(a) The first possible version is as follows.
Stack_DFS1(x):
- Mark x; Push(x);
- if stack is empty, then end of algorithm; otherwise, do the following:
- y = top of stack (without popping it);
- if there is a neighbor z of y that is not marked, then do:
- Mark z; Push(z);
- else
- Pop;
- go to Step 2.
Another possible version is as follows.
Stack_DFS2(x):
- Push(x);
- if stack is empty, then end of algorithm; otherwise, do the following:
- y <- Pop;
- if y is not marked, then Mark y;
- for every neighbor z of y that is not marked, do the following:
- Push(z);
- go to Step 2.
(b) In the Web crawling application, we need to use a stack. In fact, crawling is done by many computers running the “search” (or explore) simultaneously. Each computer takes the next node to be explored from the top of the stack, downloads the HTML file, and then scans it for further hyperlinks. But when a new HTML document is found (i.e., a “neighbor”), no recursive call is made; instead, the new node is pushed onto the stack.
Yet one interesting question remains: when we see a HTML document, how do we know it is indeed “new” (i.e., not marked) so that we want to push it on stack?
No comments:
Post a Comment