Can anyone explain to me why the space complexity of adjacency lists is Theta(m + n)?
Example graph:
a: b, c, d
b : e
c : d, e
d : empty
e : a
So here n = 5, m = 7.
Even crazier: The professor told us about a space complexity of Theta(m * log_2(n))
Let's look at Theta(m + n) ... there are constants c1 and c2 satisfying c1 * (m + n) <= space_complexity_of_this <= c2 * (m + n) ... but what if we have |E| = n choose 2 (maximum number of edges), then our space complexity would be something like n * (n - 1) and that doesn't fit c2.
I assume you refer to a simple graph.
Using the common notation - the graph contains n vertices and m edges, you can easily see that for storing the adjacency list you'll need θ(m+n) memory.
but what if we have |E| = n choose 2
So what? m = n(n-1)/2 but still, you'll need θ(m+n) memory.
The professor told us about a space complexity of Theta(m * log2(n))
If you'll index your nodes 0 .. n-1, you can use ceil(log2(n)) bits to store the index of each node, and hence, you'll need 2*ceil(log2(n)) bits to store each edge, or θ(m * log2(n)) bits for storing all the edges (you don't need to store the nodes, just use a count as a prefix for each neighbors list).
Related
Show that if the edge set of a graph G(V,E) with n nodes
can be partitioned into 2 trees,
then there is at least one vertex of degree less than 4 in G.
...................................................................................
I have tried to prove this problem with the help of the method of contradiction.
Assume that all vertices of the graph G has degree >= 4.
Assume the graph G is partitioned into two trees T1 and T2.
With the help of the above assumptions the only observation I could make is that for every vertex v in G
degree of v must be greater than or equal to 2 in either T1 or T2.
I don't know how proceed with this. Please help.
If my approach for solving this problem is wrong then please provide a different solution.
You started with a good approach. Lets assume all vertices in G has degree of 4 (or above) and sssume the graph G is partitioned into two trees T1 and T2.
We know that number of edge in tree is n-1 (when n is number of vertices). Therefor in each of T1 and T2 we have n-1 edges (consider n to be |V|) -> combine we have 2n-2 edges in G -> |E| = 2n-2
From the other hand we know that each v in G -> d(v) > 4 . And we know that sum of degree in graph equal to 2|E|. therefor, 2*|E| >= 4*n (I took the minimum degree for each vertex and each edge contribute 2 to the sum of the degree). So we got |E| >= 2*n.
Contradiction -> There is have to be one vertex with degree less then 4
I am looking for a sequence of inputs for the Dijsktra algorigthm implemented with a regular heap, where Dijsktras actual complexity would be Θ((e+v)logv).
I know how to implement Dijsktra and how it works, I also understand that the most time consuming operations are adding a vertex to the heap and changing the distance of a vertex. However, I am not sure how to find a graph (sequence of graphs) that would be the worst case inputs for Dijkstra.
Also if you had any general tips on how to find a sequence of inputs for the worst case complexity, that would be helpful.
Let vertices be numbered from 1 to n and you want to find path from vertex 1 to vertex n. Let e[i][j] be length of edge, connecting i and j. Initially e[1][2] = e[2][3] = ... = e[n - 1][n] = 1. Now we iterate through the vertices from n - 2 to 1. In i-th vertex for each j in [i + 2, n] we make e[i][j] = e[i][i + 1] + e[i + 1][j] + 1.
Now we have full graph. In each iteration dijkstra will update O(n) vertices, so it's O(n ^ 2) = O(E) actions working in O(log n).
So final asymptotics will be O(n log(n) + E log(n))
I'm trying to figure out to solve this problem: Given a graph G = (V, E) prove e <= n(n-1)/2 for all n, where e is the number of edges and n is the number of vertices.
I'm thinking that I should somehow be using math induction to figure out the correct answer and use n = 1 or 0 for my hypothesis, but I'm getting a little stuck on what to do after -- if I assume n = k, then: e <= (k+1)k/2. and if n = k+1 then e <= k(k-1)/2.
As I understand it, each vertex has n-1 possible edges coming out, and there are n total vertices, which is where n(n-1) comes from and dividing by 2 gets rid of the repeats. But I am unsure how I am to prove this.
The statement is false for multi-graphs. Take the graph:
/---\
O-----O
There are two vertices (O) and two edges; therefore n=2,e=2 and substituting into n(n-1)/2 <= e gives 1 <= 2 which is false.
However, if you restrict the graph to be simple - disallowing looping edges (where both ends of the edge terminate at the same vertex), multi-edges (where two edges connect the same pair of vertices) and that the graph is undirected - then the property holds.
Consider a complete graph K_n (with n vertices): each of the n vertices is incident to the other n-1 vertices via a connecting edge therefore there are n(n-1) connections from one vertex to another; given that edges are undirected then this will count each edge twice (i.e counting from vertex A to vertex B and vice versa) then the total number of edges will be n(n-1)/2.
Any graph G_n (with n vertices) will be a sub-graph of K_n (since you cannot add any more edges to K_n without creating multi- or looping edges) then there must be equal or fewer edges in G_n than in K_n.
Thus e <= n(n-1)/2 for all simple graphs.
If you further restrict the graph to be planar then you can state that e <= 3n - 6 (when n > 2).
I am currently trying to create a render of a path in clojure (although I am happy with answers in other languages if they're easy to understand). A simple explanation is that I want to draw a line between two points with a given thickness - however I only want to find vertices of the line so that I can output it to a Wavefront file (a 3d model file with the extension .obj).
So, for example given points A and B which can be joined up like so:
I wish to find points A1 and B1
This could also be thought of given a border to a shape. For example given A, B, C and D:
I would wish to find A1, B2, C1 and D1:
The actual shape would be much more complicated however and may have a few hundred points.
My original thought was to do an enlargement from the centre of the shape with a scale factor of less than 1, like so:
(defn shrink-pos [centre-x centre-y x y]
(let [diff-y (- y centre-y)
diff-x (- x centre-x)
dist (Math/sqrt (+ (* diff-y diff-y) (* diff-x diff-x)))
n-x (+ centre-x (* diff-x 0.8))
n-y (+ centre-y (* diff-y 0.8))]
[n-x n-y]))
Unfortunately this does not seem to work. The width of the border/stroke is not uniform and there is no border between the last point and the first which join to close the shape.
Is there a way to do this programmatically?
For 'thick' line:
Let's AB is vector from A to B.
ab is normalized (unit length) vector (vector normalization)
ab = normalized(AB)
p is perpendicular vector to ab
p.x = -ab.y, p.y = ab.x
needed points' coordinates:
B' = B + thickness * p
A' = A + thickness * p
For polygon offseting:
Let's two neighbour edges are AB and BC (intersecting in vertice B).
Find normalized (unit) vectors ab and cb.
Calc unit bisector vector
b = normalized(ab + cb)
Calc length of bisector segments as l=d/sin(fi)
where d is offset, and fi is angle between vectors b and ab.
It could be found as:
fi = atan2(crossproduct(b,ab), dotproduct(b,ab))
And find offset polygon vertice (for inner and outer offset polygons):
B' = B + l * b
B'' = B - l * b
P.S. Don't forget about inner polygon vertice vanishing for large offsets and weird loops for non-convex polygons
Given sum of A and B let S
i have to find maximum product of A*B
but there is one condition value of A will be in range [P,Q]
How to proceed for it ?
If there is no range then task is pretty simple.
Using derivation method .
How to find Maximum product for eg.
A+B=99
value of A will be range [10,20]
then what will be the maximum product of A and B.
O(N) will not sufficient for the problem
Clearly, B = S - A and you need to maximize A * (S - A).
You know from algebra that A * (S - A) achieves a maximum when A = S / 2.
If S / 2 falls in the allowed range [P Q], then the maximum value is A^2 / 4.
Otherwise, by monotonicity the maximum value is reached at one of the bounds and is the largest of P * (S - P) and Q * (S - Q).
This is an O(1) solution.
This is actually a maths question, nothing to do with programming
Even if you are given it as a programming question, You should first understand it mathematically.
You can think of it geometrically as "if the perimeter of my rectangle is fixed at S, how can I achieve the maximum area?"
The answer is by making sides of equal length, and turning it into a square. If you're constrained, you have to get as close to a square as possible.
You can use calculus to show it formally:
A+B = S, so B = S-A
AB therefore = A(S-A)
A is allowed to vary, so write it as x
y = x(S-x) = -x^2 + Sx
This is a quadratic, its graph Will look like an upsidedown parabola
You want the maximum, so you're looking for the top of the parabola
dy/dx = 0
-2x + S = 0
x = S/2
A better way of looking at it would be to start with our rectangle Pq = A, and say P is the longer edge.
So now make it more oblique, by making the longer edge P slightly longer and the shorter edge q slightly shorter, both by the same amount, so P+q does not change, and we can show that the area goes down:
Pq = A
(P+delta) * (q-delta)
= Pq + (q-P)*delta + delta^2
= A + (q-P)delta
and I throw away the delta^2 as it vanishes as delta shrinks to 0
= A + (something negative)*delta
= A - something positive
i.e. < A