Graph - Square of a directed graph - algorithm

Yes, this will be a homework (I am self-learning not for university) question but I am not asking for solution. Instead, I am hoping to clarify the question itself.
In CLRS 3rd edition, page 593, excise 22.1-5,
The square of a directed graph G = (V, E) is the graph G2 = (V, E2) such that (u,v) ∈ E2 if and only if G contains a path with at most two edges between u and v. Describe efficient algorithms for computing G2 from G for both the adjacency-list and adjacency-matrix representations of G. Analyze the running times of your algorithms.
However, in CLRS 2nd edition (I can't find the book link any more), page 530, the same exercise but with slightly different description:
The square of a directed graph G = (V, E) is the graph G2 = (V, E2) such that (u,w) ∈ E2 if and only if for some v ∈ V, both (u,v) ∈ E and (v,w) ∈ E. That is, G2 contains an edge between u and w whenever G contains a path with exactly two edges between u and w. Describe efficient algorithms for computing G2 from G for both the adjacency-list and adjacency-matrix representations of G. Analyze the running times of your algorithms.
For the old exercise with "exactly two edges", I can understand and can solve it. For example, for adjacency-list, I just do v->neighbour->neighbour.neighbour, then add (v, neighbour.neighbour) to the new E2.
But for the new exercise with "at most two edges", I am confused.
What does "if and only if G contains a path with at most two edges between u and v" mean?
Since one edge can meet the condition "at most two edges", if u and v has only one path which contains only one edge, should I add (u, v) to E2?
What if u and v has a path with 2 edges, but also has another path with 3 edges, can I add (u, v) to E2?

Yes, that's exactly what it means. E^2 should contain (u,v) iff E contains (u,v) or there is w in V, such that E contains both (u,w) and (w,v).
In other words, E^2 according to the new definition is the union of E and E^2 according to the old definition.
Regarding to your last question: it doesn't matter what other paths between u and v exist (if they do). So, if there are two paths between u and v, one with 2 edges and one with 3 edges, then (u,v) should be in E^2 (according to both definitions).

The square of a graph G, G^2 defined by those vertices V' for which d(u,v)<=2 and the eges G' of G^2 is all those edges of G which have both the end vertices From V'

Related

Construct an efficient, minimum spanning tree such that given subset of vertices in G are leaves + proof

I am trying to design an algorithm where, given a connected weighted graph G = (V, E) and a subset of vertices U that is in V, will construct a minimum spanning tree such that all vertices in U are leaves (other vertices may also be leaves), or returns that no such tree exists (False).
This is all I got, adapting Prim's algorithm (fair warning, its really bad; don't even know if it works/is efficient or what data structures to use, I will accept literally any other correct algorithm instead):
Let x be an arbitrary node in G
Set S = {x}
While S != V:
Let (u,v) be the cheapest edge with u in S and v not in S
Add (u,v) to tree T if u is not in U, add v to S
If all u in U is in the tree T:
return T
Else:
return False
I also have a picture of what I think it would do to this graph I drew:
pic here
A proof that the algorithm is correct would also give me some peace of mind.
If all vertices u ∈ U are to be leaves in a solution, no u can be used in that solution to connect two other vertices. All vertices not in U must be connected by edges not incident to any u.
Remove U and all edges incident to U. Find the minimum spanning tree, then connect each u to the tree by the smallest-weighted edge available from those we removed.

How to calculate the expected value of random graph generation

Hello this is my first question. I met a homework in algorithm and probability that I can't find a clue to calculate.
Question:
Computing Number of Triangles in a Graph: Given an undirected graph G = (V, E), a triangle in G is a clique of size 3 (formally, a set of nodes {u, v, w} is a triangle in G if (u, v), (v, w), (u, w) are all edges of G). Consider the following algorithm for approximating the number of triangles in a graph. First construct a sampled graph G' = (V, E') as follows. The vertex set of G' is same as that of G. For every e ∈ E, put e in E' with probability p (think of p as, say, 0.1). In this new sampled graph G', count the number of triangles and let T' be the number of triangles in G' (assume that you have given a black box subroutine to count the number of triangles in G' ). Then output T̃= T'/p.
Show that the expected value of T̃=T ,T is the triangle number of original graph G.
I am confusing that the edge in G or G' to form a triangle is not independent since two adjacent triangles in G might share the edge. And not the all the pair of vertices in G can form a edge in G', only those edges are in G will be present in G' with p. It's hard for me to think of the relationship of number of edges and number of triangles in G or G'.
Hope someone can give me some hints, even not the whole solution is OK.
the edge in G or G' to form a triangle is not independent since two adjacent triangles in G might share the edge
Doesn't matter. The sum of expectations is the expectation of the sum regardless of correlation, so you can reason about the triangles individually. (Higher moments, were you concerned about analyzing the estimation quality of this algorithm, would be trickier.)

How to find MST of a subset of the vertices in a graph?

Given a graph G = (V, E) and a subset v of V how do you compute a "minimal spanning tree" m that connects all the nodes in v together? That is to say, it can have paths that go through vertices not in v.
My first thought was that m must be a subset of M where M is the MST of G but here is a counter-example through this diagram: Find the MST of {B, C}. It is clearly the shortest path, the edge with weight 17, which is not a subset of M.
I'm having trouble reducing this problem/defining exactly what to run a classic MST algorithm on.
Thanks in advance!

single-source shortest algorithm

Let G = (V, E) be a weighted, directed graph with weight function w : E → R. Give an O(V E)-time algorithm to find, for each vertex v ∈ V , the value δ*(v) = min{u∈V} {δ(u, v)}.
I don't understand the question. Could someone give me some ideas?
This basically means:
G = (V, E) having a graph with V vertices and E edges
weighted, directed graph with weight function w : E → R the graph is directed and weighted, where each edge has real value weight
O(V E)-time algorithm find the algorithm that runs in number of operations proportional to number of vertices multiplied by number of edges
for each vertex v ∈ V , the value δ*(v) = min{u∈V} {δ(u, v)} here they have not described what δ(u, v) means, but most probably this is the sum of weights of edges from vertex u to v. This basically asks you to find the minimum distance from vertex u to all vertices v.
And the answer to your question Bellman Ford.

Decide whether there is a MST that contains some edges of 2 distinct edge sets

Let G = (V, E) be a weighted, connected and undirected graph. Let T1 and T2 be 2 different MST's. Suppose we can write E = (A1 U B U A2) such that:
B is the intersection of the edges of T1 and T2, and
A1 = T1 - B
A2 = T2 - B
Assuming that every MST T in G contains all the edges of B, find an algorithm that decides whether there is a MST T that contains at least one edge in A1 and at least one edge in A2.
Edit: I've dropped the part that was here. I think that it does more harm than good.
you should sort your edge that the red edge is prefer to blue edge for choose.then you can use any MST algorithm same as Prim's algorithm :
If a graph is empty then we are done immediately. Thus, we assume
otherwise. The algorithm starts with a tree consisting of a single
vertex, and continuously increases its size one edge at a time, until
it spans all vertices. Input: A non-empty connected weighted graph
with vertices V and edges E (the weights can be negative). Initialize:
Vnew = {x}, where x is an arbitrary node (starting point) from V, Enew
= {} Repeat until Vnew = V: Choose an edge {u, v} with minimal weight such that u is in Vnew and v is not (if there are multiple edges with
the same weight, any of them may be picked) Add v to Vnew, and {u, v}
to Enew Output: Vnew and Enew describe a minimal spanning tree

Resources