Asymptotic Analysis For Dup1: Best-Case
Asymptotic Analysis For Dup1: Best-Case
Best-case:
O(1) – In this scenario the function will return false if there are no duplicate elements in
the array. The ‘dup1’ function will execute the inner loop ‘N – 1’ times in the first
iteration of the outer loop, ‘N – 2’ times in the second iteration, and so on until it
executes the inner loop once in the last iteration of the outer loop. The total number of
iterations of the inner loop is:
Worst case:
O(N²) – In this scenario, the function will return true if all the elements in the array are
the same. The ‘dup1’ function will execute the inner loop ‘N – 1’ times in the first
iteration of the outer loop, ‘N – 2’ times in the second iteration, and so on until it
executes the inner loop ‘1’ time in the last iteration of the outer loop. The total number
of iterations of the inner loop is:
Overall
The ‘dup1’ function will return either true or false depending on the input array. In worst
case scenario, the function will execute the inner loop N(N – 1)/2 times, which is O(N²).
The time complexity of the function in the overall scenario is also O(N²).
2. Recurrence relations
The first two terms on the right-hand side of the equation correspond to the recursive
calls to g with goal unchanged and step multiplied by 2. The third term corresponds to
the loop that prints ‘1’ to the console goal/step times. Since step is always “1” in the
initial call to ‘g’, the loop will execute ‘goal’ times, so the time complexity of the loop is
‘O(N)’.
b) The first 3 levels and the final level of a recurrence diagram representing
runtime of g(N, 1).
In the above tree, each node represents a recursive call to ‘g’, with the value of ‘goal’
and ‘step’ at that call. The root node represents the initial call to ‘g(N, 1)’, and the leaf
nodes represent the base cases where ‘step > goal’. The number of nodes at each level of
the tree is ‘2k’, where ‘k’ is the level number, and the total number of nodes in the tree is
‘2(log N) = N)’.
The time complexity of the function is proportional to the sum of the values of ‘N’ at
each level of the tree,which is ‘N + 2N/2 + 4N/4 + …. + N/log N’ and this sum can be
simplified to ‘N log N’, so the time complexity of the function is ‘O(N log N)’.
3. BSTMaps
I first drew the initial tree with a single root node containing ‘SAN JOSE, 2.9’. I then
added the remaining nodes in the order that they are in in the problem statement. When I
encountered a node with a key that already existed in the tree, I updated the value of that
node with the new value. For example, when I added ‘SD, 1.9’ and found that ‘SD’
already existed in the tree, I updated the value of the existing node to 1.9 instead of
creating a new node. I followed this process till I completed the tree.
4. Tries
a) A trie containing all words in: “Peter Piper picked a peck of pickled peppers.”
b) Subtree containing all the words starting with “pi”?
5. Graph modeling
I would model the flight schedule as a directed path whereby each vertex represents an
airport and each edge represents a direct flight between two airports.
1. The vertices in the graph store airport information such as name, location and IATA
code. The edges store flight information such as the flight number, departure time,
arrival time and duration.
2. A flight search engine program can be implemented with this graph. Given a starting
airport and a destination airport, the program could use the graph to find all possible
direct flights between the two airports. Also using the graph, indirect flights can be
found by searching for paths that connect the two airports with one or more intermediate
stops.
6. Breadth-first search
If we change the code to add the current vertex ‘from’ to the ‘visited’ set, the traversal
process will change thus:
• The BFS algorithm will visit the current vertex ‘from’ first before visiting its
neighboring vertices.
• Since the current vertex is already visited, it will not be added to the ‘perimeter’
queue again.
• The traversal process would continue with the next vertex in the ‘perimeter’
queue.
The change will cause the BFS algorithm to visit the vertices in a depth-wise manner
instead of a level-wise manner. The traversal process will be slower and less efficient
than the original BFS algorithm.
7. Prim’s Algorithm
The order in which edges are added to the MST by Prim’s algorithm is:
• H
• G
• F
• D
• E
• B
• A
1. Edge from A-D will cause the Dijkstra’s algorithm to compute an incorrect shortest
paths tree from A if replaced with the weight -100
2. Because Dijkstra's approach requires that the shortest path from the source to every
other vertex is non-negative, it might not function if the network has negative edge
weights. In cases where edge weights are negative, the algorithm might not identify the
shortest path. This is due to the fact that the algorithm selects the vertex that is closest to
the source vertex and then relaxes all of its neighboring vertices. The method could
choose a vertex that is farther away from the source vertex than the shortest path if there
are negative edge weights.
3. True. Dijkstra's algorithm will produce the right shortest path tree in a directed graph
if the only edges with negative weight are those that leave the start node. This is due to
the fact that, even in the case of negative edge weights, the algorithm will still be able to
determine the shortest path from the start node to any other vertex. This is because the
shortest path to any vertex will be determined in the first iteration of the algorithm
because it never goes back to a vertex after it has been examined and because there are
no negative cycles. The technique might not identify the shortest path, though, if the
start node is a part of a cycle.