DPP 1: Complexity and data structures
Asymptotic analysis, stacks, queues and trees
An algorithm performs n + n/2 + n/4 + ... operations. What is its asymptotic running time?
Answer: The geometric sum is less than 2n, so the running time is Θ(n).
Which structure directly supports breadth-first traversal of an unweighted graph?
Answer: A queue. Vertices are processed in the same order in which they are discovered.
What is the worst-case search time in an unbalanced binary search tree containing n distinct keys?
Answer: Θ(n), because the tree can degenerate into a chain.
A full binary tree has 15 internal nodes. How many leaves does it have?
Answer: 16. In a full binary tree every internal node has two children, so the number of leaves equals the number of internal nodes plus one.
Run breadth-first search from a source in an unweighted graph. What does the level of a discovered vertex represent?
Answer: Its shortest-path distance from the source measured in number of edges.
Why is inserting at the front of a singly linked list Θ(1)?
Answer: Only the new node's next pointer and the head pointer change; no traversal is required.