Read!
Read the exercise again to make sure the instructions have been followed:
- edge cases
- proper output format
Task 1 Theory
Asymptotic growth
Common mistakes:
- this is very wrong!
- Showing the right thing then misinterpreting: show thus false, when we wanted to show and thus it’s correct.
Recursion Asymptotic growth
Assume and for . Prove . We can use induction to prove exercises like these.
B.C.: For , we have . I.H.: We assume for some . I.S.: Which means which is smaller than which is smaller than and thus our recursion works.
Asymptotic trick
Series Trick: Given we can do then factor out for and divide to get .
Integral Trick: Simply integrate the sum (for example ) you have to get an asymptotic bound.
While Loop to for Loop
For a while loop like this:
j = 1
while j <= n do
j = 2j
f()the runtime is not but instead . We go from to not from .
Theory Runtime for Graphs
Building Graph
Building the graph takes time. We give in the case of an undirected graph with two vertex sets, one of size and another of size : . The set is not connected, there are only edges from all ( edges) and then set is connected (fully connected at most) thus as each is linked to all others. Thus the total runtime is .
Running the Algorithm
The algorithms we have are given in relation to and which means that Prim’s on a connected graph for example like here takes which in our previous case would come out to .
Extracting Solution
Extracting the solution can be reading off a distance from an array in . In our case it’s as we iterate over all edges to compare them and filter out edges in .
Theory Pseudocode
DFS Pseudocode
Make sure to not forget to include a for loop over all vertices that are unmarked if we are not sure the graph is connected.
DFS(g):
all vertices unmarked
for u unmarked:
visit(u)
visit(u):
mark u
for v adjacent to u:Otherwise, we aren’t checking the vertices that aren’t in the ZHK of our chosen start node.
DP Tasks
Recursion
Make sure not to forget the or whatever is needed in the case where we successfully gain something.
Calculation Order
Give the calculation order as a for loop, like so:
for i \in {1, ..., n}:
for j \in {i + 1, ..., n}:
compute DP[i][j]
Runtime
Explicitly state the runtime for the calculation of an entry: “Calculating the entry takes time, which we can upper bound by since .” Then the number of entries: “We have entries” Then the total runtime: “Thus the total runtime is .”
Code Expert
Shortest Path
The solution can sometimes be to set the distance of not only the start vertex, but “all start vertices” to 0, instead of using ASSP algorithms.
DFS vs. BFS
Use DFS when we want to find a toposort or something, BFS when we need shortest paths in an unweighted!!!
BFS Implementation
Note that for BFS we set visited to true before/after we add it to the queue, not once we pop it!!!
Queue<Integer> Q = new LinkedList<>();
Q.add(s);
int[] dist = new int[n];
dist[s] = 0;
boolean[] visited = new boolean[n];
visited[s] = true;
int reached = 0;
while (Q.size() > 0) {
s = Q.remove();
for (int i = 0; i < E.get(s).size(); i++) {
int u = E.get(s).get(i);
if (T[u] > dist[s] && !visited[u]) {
dist[u] = dist[s] + 1; // And set the distance her
Q.add(u);
visited[u] = true; // NOTE: We set visited to true here
reached++;
}
}
}
return reached + 1;