How to use Dijkstra's algorithm to generate an undirected, unweighted graph of a given diameter? - algorithm

My job is to generate a random undirected, unweighted graph with a given diameter d. What I did already is to generate a random distance matrix D where each element Dij represents the distance between the i-th and j-th vertices of the graph. So, basically I am doing this:
if (i == j) {
D[i][j] = 0;
} else {
D[i][j] = D[j][i] = random.nextInt(d + 1);
}
The diagonal is zero because it always needs zero effort to reach the same vertex, am I right?
Also, Dij = Dji because it is undirected. Are my assumptions right?
I want to use java, but I tagged the question as language-agnostic because I need an algorithm and not a code.
My next step is to use Dijkstra's algorithm to generate a random graph by generating an adjacency matrix. I think that Dijkstra's algorithm is to find the shortest path, but can I use it for my case?
EDIT #1:
As you can see in the figure above, the diameter is 4 because the most distanced vertices are 2 and 7 have a distance of 4. For that reason, we have D[2][7] = D[7][2] = 4. Another example is D[3][6] = D[6][3] = 2, because if we want to go from 3 to 6, we can go 3 -> 5 -> 6, or 3 -> 1 -> 6 and vice versa for going from 6 to 3.
What I am looking for is to generate a random graph by knowing the diameter which is the maximum distance between two vertices in the graph. I know there are a lot of possibilities of the graph, but I need any of them.
I have an idea which is assuming that the number of vertices is d + 1, then connecting each vertex to the following vertex. In this case we will have a linear graph.
Example (diameter = 2, number of vertices = 3):
v
1
2
3
1
0
1
2
2
1
0
1
3
2
1
0
The diagonal = zero
D1,2 = D2,1 = D2,3 = D3,2 = 1, because to go from 1 to 2, or 2 to 3, there is a direct link
D1,3 = D3,1 = 2, because to go from 1 to 3, the shortest path is 1
-> 2 -> 3
Here is the graph associated to the above distance matrix:
I am looking for a better approach.

Related

Finding two minimum spanning trees in graph such that their sum is minimal

I'm trying to solve pretty complex problem with graphs, namely we have given undirected graph with N(N <= 10)nodes and M (M <= 25)edges.
Let's say we have two sets of edges A and B, we can't have two same edges in both A and B, also there can be edges that wont be used in the any of those sets, each edge is assigned value to it. We want to minimize the total sum of the two trees.
Please note that in both sets A and B the edges should form connected graph with all N nodes.
Example
N = 2, M = 3
Edges: 1 - 2, value = 10, 1 - 2, value: 20, 2 - 1, value 30, we want to return the result 30, in the set A we take the first edge and in set B the second edge.
N = 5
M = 8
Edges: {
(1,2,10),
(1,3,10),
(1,4,10),
(1,4,20),
(1,5,20),
(2,3,20),
(3,4,20),
(4,5,30),
}
set A contains edges {(1,2,10), (1,3,10), (1,4,10), (1,5,20)}
while set B contains {(1,4,20), (2,3,20), (3,4,20), (4,5,30)}
What I tried
Firstly I coded greedy solution, I first generated the first minimum spanning tree and then I generated with the other edges the second one, but it fails on some test cases. So I started thinking about this solution:
We can see that we want to split the edges in two groups, also we can see that in each group we want to have N - 1 edges to make sure the graph doesn't contain not-wanted edges, Now we see that in worse-case we will use (N-1) + (N-1) edges, that is 18 edges. This is small numbers, so we can run backtracking algorithm with some optimizations to solve this problem.
I still haven't coded the backtracking because I'm not sure if it will work, please write what do you think. Thanks in advance.

Traveling Salesman Variation Algorithm

I'm having a trouble finding a contradicting example of the next variation of the TSP problem.
Input: G=(V,E) undirected complete graph which holds the triangle inequality, w:E->R+ weight function, and a source vertex s.
Output: Simple Hamilton cycle that starts and ends at s, with a minimum weight.
Algorithm:
1. S=Empty-Set
2. B=Sort E by weights.
3. Initialized array M of size |V|,
where each cell in the array holds a counter (Initialized to 0)
and a list of pointers to all the edges of that vertex (In B).
4. While |S|!=|V|-1
a. e(u,v)=removeHead(B).
b. If e does not close a cycle in S then
i. s=s union {e}
ii. Increase degree counter for u,v.
iii. If M[u].deg=2 then remove all e' from B s.t e'=(u,x).
iv. If M[v].deg=2 then remove all e' from B s.t e'=(v,x).
5. S=S union removeHead(B).
This will be done similar to the Kruskal Algorithm (Using union-find DS).
Steps 4.b.iii and 4.b.iv will be done using the List of pointers.
I highly doubt that this algorithm is true so I instantly turned into finding why it is wrong. Any help would be appreciated.
Lets say we have a graph with 4 vertices (a, b, c, d) with edge weights as follows:
w_ab = 5
w_bc = 6
w_bd = 7
w_ac = 8
w_da = 11
w_dc = 12
7
|--------------|
5 | 6 12 |
a ---- b ---- c ----- d
|______________| |
| 8 |
|_____________________|
11
The triangle inequality holds for each triangle in this graph.
Your algorithm will choose the cycle a-b-c-d-a (cost 34), when a better cycle is a-b-d-c-a (cost 32).
Your procedure may not terminate. Consider a graph with nodes { 1, 2, 3, 4 } and edges { (1,2), (1,3), (2,3), (2,4), (3,4) }. The only Hamiltonian cycle in this graph is { (1,2), (1,3), (2,4), (3,4) }. Suppose the lowest weighted edge is (2,3). Then your procedure will pick (2,3), pick one of { (1,2), (1,3) } and eliminate the other, pick one of { (2,4), (3,4) } and eliminate the other, then loop forever.
Nuances like this are what makes the Travelling Salesman problem so difficult.
Consider the complete graph on 4 vertices, where {a,b,c,d} are the nodes, imagined as the clockwise arranged corners of a square. Let the edge weights be as follows.
w({a,b}) := 2, // "edges"
w({b,c}) := 2,
w({c,d}) := 2,
w({d,a}) := 2,
w({a,c}) := 1, // "diagnoals"
w({b,d}) := M
where M is an integer larger than 2. On one hand, the hamiltonian cycle consisting of the "edges" has weight 8. On the other hand, the hamiltonian cycle containing {a,c} , which is the lightest edge, must contain {b,d} and has total weight
1 + M + 2 + 2 = 5 + M > 8
which is larger than the minimum possible weight. In total, this means that in general a hamitonian cycle of minimum weight does not necessarily contain the lightst edge, which is chosen by the algorithm in the original question. Furthermore, as M tends to infinity, the algorithm performs arbitrarily badly in terms of the approximation ratio, as
(5 + M) / 8
grows arbitrarily large.

Algorithm for diameter of graph

I have heard of algorithm of finding diameter of graph using the following algorithm:
Algorithm (start_vertex):
find out the set of vertices S having maximum value of
shortest distance from start_vertex.
ans = 0;
for each vertex v in S
temp = 0;
for each vertex u in graph G:
temp = max(temp, shortest distance between u and v).
ans = temp;
return ans;
The following algorithm fails several times: for example when n=7
we can draw the following graph:
1 2
1 3
2 4
3 4
2 6
2 7
4 5
With start_vertex=1 ,this algorithm gives shortest path as 3 but the answer is 4.
Can someone give the general case when does this algorithm fails.Also tell me whether this will work for n=8 where n is the no of vertices of the graph
No the answer should be 3 in this case.. It doesn't fail here

Find a minimum weight spanning tree in the undirected graph where there are negative edges

So I need to solve this graph, I have a general idea on how to solve it but if I am doing this wrong please correct me.
So to find the MST I need to perform Kruskals algorithm on the graph
here is my psuedocode for this Kruskals algorithm
Kruskal(V,E)
A = null;
for each v contains in V
make disjoint set(v)
Sort E by weight increasingly
for each(v1, v2) contains in E
if
Kruskal(V,E)
A = null;
for each v contains in V
make disjoint set(v)
Sort E by weight increasingly
for each(v1, v2) contains in E
if Find(v1) is not equal to Find(v2)
A = A Union {(v1,v2)}
Union(v1,v2)
Return A
So first thing I do is find the nodes with the shortest distances right?
1)
I assume S to H has the shortest distance since the d(h,s) = -3
so A = {(h,s)}
so now we follow this pattern
2) A = {(h,s),(s,f)}
3) A = {(h,s),(s,f)(s,n)}
4) A = {(h,s)(s,f)(s,n)(f,k)}
5) A = {(h,s)(s,f)(s,n)(f,k)(s,m)} (we skip H to N because a path is already made from h to n which is through s )
6) A = {(h,s)(s,f)(s,n)(f,k)(s,m)(d,b)}
7) A = {(h,s)(s,f)(s,n)(f,k)(s,m)(d,b)(b,m)}
so now since there is a path that connects to all edges we are good right?
but the thing i dont understand is there there are distance[u,v] which is shorter than the path[u,v] through multiple vertices. For example d[d,m] is shorter then p[d,m] which goes through B first. Am I doing something wrong?
You're not doing anything wrong. There's no guarantee that a MST preserves shortest distances between nodes. Eg: the three node complete graph ABC with edge weights 3, 2, 2 (with apologies for my ascii art):
A --- 2 --- B
| |
2 /
| /
C----3---/
The minimal spanning tree is C-A-B but the distance between B and C in the original graph is 3, and in the MST 4.

count unique pair in graph (vertices repetition not allowed)

Is there any Algorithm exists for counting unique pair (pair of vertices) in undirected graph (vertices repetition not allowed). I think it could be a variation of bipartite graph but if there is any better way to find out .. please comment.
[I think Problem Belongs to Perfect Matching Algorithm]
Problem Statement:
I have an undirected graph which consists of n vertexes and m edges. I can delete edges from the graph. Now I'm interested in one question : is it possible to delete edges in the graph so that the degree of each vertex in the graph will be equal 1.. There can be multiple edges in the graph, but can not be any loops
Example: n = #vertices, m = #edges
n = 4, m = 6
1 2
1 3
1 4
2 3
2 4
3 4
Unique sequence could be (1 2, 3 4) (1 4, 2 3) (1 3, 2 4)
The set of edges that covers the entire graph without using the same vertex multiple times is called a matching or independent edge set, see wikipedia.
In that article is also mentioned that the number of distinct matchings in a graph (which is the number you are after) is called the Hosoya index, see this wikipedia article.
Algorithms to compute this number are not trivial and Stack Overflow wouldn't be the right place to try to explain them, but I at least I hope you have enough pointers to investigate further.
Here is pseudo code, it should run in O(|E|) time, i.e. linear of number of edges :
Suppose G = (V, E) is your initial graph, with E - initial set of all edges
count = 0;
while(E is not empty) {
//1. pick up any edge e = (n1, n2) from E
//2. remove e from G
E = E - e;
//3. calculate number of edges in G remaining if nodes n1 and n2 were removed
// -> these are edges making pair with e
edges_not_connected_to_e = |E| - |n1| - |n2|;
// where |n1| - degree of n1 in updated G (already without edge e)
//4. update the count
count += edges_not_connected_to_e;
}
return count;
Let me know if you need more clarifications. And probably someone could fix my Graph math notations, in case they are incorrect.

Resources