Algorithms Assignment 1
Algorithms Assignment 1
Our algorithm will simply loop over the set of vertices of a graph and the linked list associated with
each vertex containing its outbound edges.
reverse_adj_list(list):
Initialize new adjacency list with same vertices as original list.
for each vertex v:
for each edge(u,v):
Insert node v into the linked list associated with vertex u in the new
adjacency list
return reversed adjacency list
Let’s now analyze the total steps needed to execute our algorithm. Initializing our new adjacency list
will take O(n). The first for-loop will run n times guaranteed every time while the second for-loop will
run a total of m times. Since insertion of an element to a linked list takes constant time, our total steps
will be 2n + m which is O(n + m).
For our basis step, we can take a tree with two vertices. A tree with two vertices would have only one
edge between them, and hence the degree of each of the vertices would be 1, making them both leaves.
Hence, our basis step is proven.
We can now define our inductive hypothesis, by saying that every tree with k vertices, where k ≥ 2,
contains atleast 2 leaves.
Moving on to the inductive step, we now have to prove the step for a tree with k + 1 vertices. Lets
assume we have a tree T with k vertices. According to our inductive hypothesis, the tree will contain
atleast 2 leaves. To prove for k + 1 vertices, we would have to add a vertex v to T . We can divide
this into 3 cases, the first case being one where we connect the new vertex to any other vertex or ver-
tices which are not leaves, the second case being where we connect the new vertex with one leaf and any
other vertex/vertices, and the third case being the one where we connect the vertex with 2 or more leaves.
For the first case, we can either connect v to one vertex in T , or multiple vertices which are not leaves.
If we connect v to a single non-leaf vertex in T , v would now have a degree of 1, making it a leaf. In this
1
case, the number of leaves would increase by one, and our inductive hypothesis would still hold. If we
connect v with multiple other vertices, however, then we would have a cyclic subgraph within T . This
would lead to a contradiction, as a tree is acyclic by definition, hence we cannot connect v to multiple
non-leave vertices. for our graph to remain a tree.
For our second case, we can connect the vertex to either one leaf or one leaf and atleast one non-leaf
vertex. If we connect v with only one leaf u, then the number of leaves would stay the same, as u now
has a degree of 2 and is not a leaf, but v has a degree of one and hence replaces u. If we connect v with
one leaf and other non leaf-vertices, however, then we would again have a cycle, which would contradict
with the definition of a tree, and hence it is not possible.
For our third case, where we connect v with multiple other leaves, we end up with the same contradiction
with the definition of a tree, as we would end up with a cycle, and hence the third case is not possible.
Overall, we can see that for all the possible ways we can add a vertex to T , making sure that T remains
a tree, the number of leaves either increases or stays the same as in the graph with k vertices. Hence,
via mathematical induction, our proposition is proven.
(b) To prove this bi-conditional statement, we can prove both sides separately. Let’s first prove that if T is
a tree, then a vertex x of G is a cut-vertex of T if x is not a leaf. We can prove this using contradiction.
Lets assume that x is a cut-vertex of T and x is a leaf. We know that, by the definition of a leaf,
deg(x) = 1. We also know that as x is a cut-vertex of T , T − x would have more connected components
than T . As T is a tree, we know that it only has one connected component. As x only has a degree
of 1, T − x would have the same number of connected components as T , and hence we would have a
contradiction, as x is supposed to be a cut-vertex of T . Hence, via proof by contradiction, we have
proven that if T is a tree, then a vertex x of G is a cut-vertex of T if x is not a leaf.
Now we have to prove that x is not a leaf if x is a cut-vertex of T . Again, by contradiction, we can
assume that x is not a leaf and that x is not a cut-vertex of T . As x is not a leaf, we know that it has a
degree of 2 or more. As x is not a cut vertex of T , we know that T −x would also only have one connected
component. As x has a degree of atleast 2, we know that it is atleast connected to 2 vertices v and u.
If x is removed, for the number of connected components to remain the same, we know that v and u
are still connected, and hence there needs to be another path from v to u and vice-versa. However, that
would mean that if we do not remove x, there would be a cycle in T , which is not possible as T is a tree.
This would mean that when we remove x, there would be 2 or more separate connected components in
T − x. This leads to a contradiction.Hence, via proof by contradiction, we have also proven that if T is
a tree, then x is not a leaf if x is a cut-vertex of T .
As we have proven both sides of the bi-conditional, we have proven the proposition provided in the
question.
For our basis step, we can take a tree T with 2 vertices which has 2 leaves. As we have 0 vertices with
degree of more than 2 and 2 leaves, we have atleast 2 more leaves than we have vertices of degree 3,
hence our basis step is proven.
Our inductive hypothesis states that if a tree T has k vertices, where k ≥ 2, as the tree would have
atleast 2 leaves (accroding to the first theorem proved in part (a)), then the number of leaves in T is
atleast 2 more than the number od its vertices with a degree of more than 2.
For our inductive step, we first assume we have a tree T with k + 1 vertices. We know that as k ≥ 2,
according to the theorem proved in part (a), that we have atleast 2 leaves in T . We can say that the
number of leaves is x, where x ≥ 2. We can remove a vertex from T to make a tree T ′ with k vertices
and x − 1 leaves. We can also say that the number of vertices with degree more than 2 in T ′ is u. We
can apply our inductive hypothesis on T ′ , and hence we know that there are atleast 2 more leaves than
there are vertices of degree more than 2, and hence u ≤ (x − 1) − 2. As T has one more leaf than T ′ and
the either the same or one more vertex with degree more than 2, and hence in T , either u ≤ x − 2 or
y ≤ x − 3, which shows that the number of leaves in T is still atleast 2 more than the number of vertices
in T with degree more than 2. Hence, we have proven the statement for a tree T with k + 1 vertices, and
hence every tree with atleast 2 leaves (according to theorem in (a). Hence, via mathematical induction,
we have proven the statement.
Assume we have a graph G which is connected, contains a vertex v of degree 1 and is unicyclic. We claim
that A vertex v of degree 1 cannot be part of a cycle since every vertex in a cycle has to have degree>= 2
at the minimum. This is because every vertex which is part of a path that can be categorized as a cycle
must have one inbound and outbound edge. This implies that vertex v and its associated edge can be
removed and the graph G − v will remain unicyclic.
For the reverse direction, Assume G − v is a unicyclic graph. We need to add a new vertex v in
this graph by means of a single edge(since v must have degree 1) and prove that the resulting graph
is unicyclic as well. Since we have already proved that a vertex of degree 1 cannot contribute to the
formation or destruction of a cycle, no matter which existing vertex we connect this new vertex v to,
the resulting graph will remain unicyclic.
(b) Prove that if a graph G is connected, |V (G)| = |E(G)|, and all vertices in G have degree at least 2, then
G is a cycle.
We claim that given a graph G that is connected, |V (G)| = |E(G)|, and all vertices in G have de-
gree at least 2, then we can conclude that all vertices in G have degree exactly 2. We know from the
handshake theorem:
Σv deg(u) = 2|E(G)| = 2|V (G)| (1)
However, we also know that that the degree of each vertex is at least 2 so the summation of degree can
be expressed alternately as follows:
which is a summation of |V (G)| terms with each term greater than 2. If even one vertex had degree
greater than 2, then the result would be greater than 2|V (G)| and not equal, hence all vertices have
degree 2.
Now suppose we start at some vertex v1 and form a path to some vk which we know is possible because
the graph is connected. However, this path contributed only once to the degree of vk which means it
still has an edge connecting to another vertex not already part of the path. If we proceed in this fashion,
we will find a path from v1 to vn which contains all vertices in the graph. This path contributes only
once to the first and last vertices and twice to all others, which means that there is an edge connecting
the first and last vertices in this path. This concludes that G is indeed a cycle.
(c) Prove that if a graph G is unicyclic, then |V (G)| = |E(G)|.
Suppose we have a unicyclic graph G. We know that G has exactly 1 subgraph which is a cycle and n
other subgraphs that are connected but do not contain any cycle. We know that for the cycle subgraph,
|V (G)| = |E(G)| is already satisfied. Hence, we need to prove that for each of the n other subgraphs of
G that are acyclic, |V (G)| = |E(G)| + 1
(d) Prove that if a graph G is connected and |V (G)| = |E(G)|, then G is unicyclic.
We will prove this through induction over the number of vertices n. Starting off with the base case,
a connected graph with 3 vertices and 3 edges forms a triangle which we know is unicyclic. For our
inductive hypothesis, we assume that the statement to be proven is true for a graph with k vertices.
Our task is to now prove that the statement holds true for the graph with k + 1 vertices as well, i.e that
it is unicyclic as well. To form a graph with k + 1 vertices that has k + 1 edges, we need to add one
vertex and edge to the graph woth K vertices. This means that our new graph has a vertex with degree
1 and we know from our earlier claim that a vertex with degree 1 cannot contribute to the formation of
a cycle. Hence, the graph with k + 1 vertices satisfies the theorem as well and our proof is complete.
(e) Give the pseudocode of an algorithm that, given a connected graph G, outputs whether it is unicyclic
or not in O(n) time where n is the number of vertices. Your algorithm should work with adjacency list
representation.
From the previous parts, to find out whether a graph is unicyclic or not, it is necessary and suffi-
cient to detect if |V (G)| = |E(G)|. We do this by looping over the linked list of each vertex in our
adjacency list and counting the number of edges. Counting each edge takes O(1) time and we will stop
when the number exceeds n which is the number of vertices. Hence, our algorithm will take at most n
steps so it is O(n):
unicyclic_check():
edges = 0
unicyclic = True
for vertex in adj-list:
for edge in vertex:
edges++
if edges > n:
unicyclic = False
return unicyclic
if edges < n:
unicyclic = False
return unicyclic
n3 + n2 ≤ c(n3 − n2 )
n3 + n2 ≤ 2n3 − 2n2
n3 ≥ 3n2
n≥3
We will prove by contradiction. Assume there exists some c and n0 such that the following inequal-
ity is true for all n ≥ n0 :
n3 + n3 log(n) ≤ c(n3 + n2 )
n3 (1 + log(n)) ≤ cn2 (n + 1)
n(1 + log(n))
≤c
n+1
which means that the LHS is bounded by some constant c but notice that it increases without bound
since log(n) → ∞ as n → ∞ and the two first degree polynomials cancel out. This means that there
cannot exist any constant c which results in making the inequality true. Hence, we have completed our
proof by contradiction.
(c) Formally prove that n + n/2 + n/4 + n/8 + n/16 + .... = Θ(n):
Let’s first manipulate the function f (n) into a more suitable form:
n 1 1
f (n) = Σ∞
r=0 = nΣ∞
r=0 r = n ∗ ( ) = 2n (3)
2r 2 1 − 0.5
Now, we prove f (n) is O(n) and Ω(n):
2n ≤ cn
2n ≥ dn
f (n) = n satisfies the relation and is both O(g(n)) and Ω(g(n)) where g(n) = n/2. The following
two inequalities arise:
n ≤ c ∗ n/2
n ≥ d ∗ n/2
f (n) = nn does not work. Assuming it does satisfy the relation given, then the following inequality
is true for some c and n ≥ n0 :
nn ≤ c(n/2)n/2
nn−n/2 ∗ 2n/2 ≤ c
nn/2 ∗ 2n/2 ≤ c
The expression on the LHS is not bounded as it is a continuously increasing function, hence there does
not exist some c and n0 such that the following inequality is true.