How to design an algorithm that computes in linear time the diameter of a (graph theoretical) undirected, all-edges-have-weight-1 tree? The diameter of a tree is given by the length of the longest path between two vertices.
Any idea of how to aproach this problem?
Let v1 be any vertex in the tree.
Do a depth first search from v1 to get the distances of all other vertices from v1, choose v2 as the vertex with the highest distance.
Do a depth first search from v2 to get the distances of all other vertices from v2, choose v3 as the vertex with the highest distance.
D(v2, v3) is the tree diameter. The complexity is O(|V|), as DFS is linear for a tree.
Related
Given a undirected weighted graph with n vertices and m edges. How do I construct an algorithm which takes at most O((n+m) log(n+m)) for finding the vertex which it's min shortest path distance to a set of vertices S \in V is maximized?
I know I can loop through all vertices and using dijkstra's algorithm find the shortest path to each of the vertices in S but that will surely take much more than O((n+m) log(n+m)).
I am assuming from your ideas (possible use of dijkstra's shortest path algorithm) that weight of every edge is greater or equal than zero.
A clean solution to your problem is to create a new vertex v and add zero weight edges between v and every vertex in S. Run dijkstra's algorithm from v and the answer you look for is the farthest vertex from v.
You can prove by yourself that solution of your problem is equal to the one calculated on the new graph as it is straighforward. Also, it only run dijkstra's algorithm once so it fits on your time complexity constraints.
The diameter as in, the largest minimum distance between any two points in the graph.
To solve this, would we just do BFS from any node, and then choose a node among the farthest nodes from the original node. Do BFS on this new node, and then the largest distance here is the diameter of the graph.
Another post talks about weighted directed graphs. This is strictly for unweighted. Although the same algorithm might work here, I am asking if we can do it more efficiently w/ the algo I proposed here.
diameter does exactly this.
G = nx.lollipop_graph(5, 5)
nx.diameter(G)
Output: 6
I need to find the minimum distance between two nodes in a undirected graph, here are some details
The graph is undirected and huge(no of nodes is in order of 100,000)
The graph is sparse, no of edges is less than no of nodes
I am not interested in the actual path, just the distance.
What representation and algorithm should I use for a) Space efficiency b)time efficiency?
EDIT: If it matters,
The wieghts are all non zero positive integers.
No node connects to itself.
Only single edge between two adjacent nodes
It depends on the weights of the edges. If they are non-negative - Dijkstra suits you. If your graph is planar, you can use A*.
If you have negative weights, you have to use Bellman-Ford.
I'm trying to solve the problem presented above and here is my attempt:
Attempt: We can apply Dijkstra's shortest path algorithm instead of using Prim's and Kruskal's algorithms to find a MST as Dijkstra will visit all the nodes in the smallest weighted distance. Complexity: For G = (V,E), O(E log(V))
Questions:
(1) Is my approach correct ?
(2) Is it the most efficient answer to the question ?
If i'm completely wrong, I would appreciate a correct and efficient solution.
A cycle graph contains no edges other than those connecting the vertices in the cycle. So what we can do is iterate through all N edges and eliminate the maximum weighted edge forming a spanning tree of N - 1 edges containing the minimum sum of edges, forming a Minimum Spanning Tree.
I am learning minimum spanning tree. I go through Prim's algorithm for weighted directed graph.
Algorithm is simple
you have two set of vertices, visited and non-visited
set distance for all edges to infinity
start with any vertex in non-visited set and explore its edges
In all edges, update distance of the destination vertex with the weight of the edge if destination vertex it is not visited and if weight of the edge is less than the distance of destination vertex
pick the non-visited vertex with smallest distance and do it again until all vertex are visited
I believe with above algorithm, I will be able to find the spanning tree having minimum cost among all spanning trees, i.e. Minimum spanning tree.
But I applied it to the following example and I think it is failed.
Consider following example
Vertices are {v1,v2,v3,v4,v5} and edges with weight (x,y) : w =>
(v1,v2) : 8
(v1,v3) : 15
(v1,v4) : 7
(v2,v5) : 4
(v4,v5) : 7
First I explore v1, it has edges to v2,v3,v4 so graph become
Vertex v1 is visited and (vertex, distance) =>
(v2,8)
(v3,15)
(v4,7)
Now v4 has the least distance i.e. 7 so I explore v4, it has edge to v5 so following modification occur
Vertex v4 is visited and (vertex, distance) => (v5,7)
Now among all v5 is having the least distance , i.e. 7 , so I explore v5 and it does not have any edge so I just mark it visited
Vertex v5 is visited
Now, confusion starts from here
The vertex with the least distance is now v2, it has edge to v5 with the weight 4 and currently v5 having distance is 7, previously assigned by the edge (v4,v5) : 7 , so, I believe that to make minimum spanning tree, distance for v5 should be updated from 7 to 4 as 4 < 7 but it will not because v5 has already been visited and Prim's Algorithm do not update the distance of the vertex that already been visited and distance for v5 will remain 7 instead of 4 and this tree will not have minimum cost
Do I get it right ? or am I doing any mistake ?
Thanks
First I should mention that Prim's algorithm is just applicable to undirected graphs so if we consider the graph is undirected, this is the step by step progress of the algorithm on your case:
And you should consider that finding a minimum spanning tree is not even possible many times in the directed graphs, nevertheless the closest notion to MST for directed graphs is minimum cost arborescence.