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
Related
I realize you can find the diameter or the max distance of a undirected unweighted graph by using BFS twice, my question is about the specifics of this algorithm.
If I were to implement this would I literally just do BFS twice and it would return the max distance? or do I have to set throughout the BFS algorithm the distance and weight values for each node and calculate if the new max is greater than the old max, etc? Because I have heard if you use BFS then the last visited value will be the max distance from your original node, which means I wouldn't need to do all that stuff, right?
You have to run BFS n times, once from each node. Distances must be calculated from scratch every time: distances from some node u have no sense when you run bfs from some other node v, so you have to recalculate them entirely.
Now, for each node v you store the maximum distance to any other node. The diameter of the graph is maximum of these maximums.
However, as I understood from your comment, you are solving the problem on for a tree rather than a general graph. In case of the tree, it is simpler. Run BFS from any node v. Find any of the farthest nodes from v; let it be d1. Now run BFS again from node d1 and find any of the farthest nodes from it; let it be d2. Then, a path from d1 to d2 is a diameter of the tree (one of them). There is a proof in the answer to this question.
Note that these two BFS's still compute all distances from scratch. So yes, you just have to run BFS twice.
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 know a few algorithms that are able to find the lowest cost path for directed graph (just as Dijkstra and Floyd).
Is there any algorithm that works for non-directed graphs?
My problem is: I need to find the lowest cost path from a to b passing through all vertexes (undirected graph).
My problem is: I need to find the lowest cost path from a to b passing
through all vertexes (non-oriented graph)
This is the Traveling Salesman Problem, which is NP-Hard, so there is no known efficient solution to it.
However, if the graph is fairly small, there are some techniques to solve it optimally (in exponential time), like Dynamic Programming.
In general, changing an undirected graph to a directed one is fairly easy and is done by changing an undirected edge {u,v} to two directed edges (u,v) and (v,u)
Provided you have nonnegative edge values, you could consider every edge in an undirected graph as two edges in a directed graph, one pointed to and from connected vertices. Then you could use one of many algorithms including the ones you listed.
Given a connected undirected graph, the problem of finding the spanning tree with the minimum max degree has been well-studied (M. F¨urer, B. Raghvachari, "Approximating the minimum degree spanning tree to within one from the optimal degree", ACM-SIAM Symposium on Discrete Algorithms (SODA), 1992). The problem is NP-hard and an approximation algorithm has been described in the reference.
I am interested in the following problem - given a connected undirected graph G = (V1,V2,E), find the spanning tree with the maximum min degree over all internal nodes (non-leaf nodes). Can someone please tell me if this problem has been studied; is it NP-hard or does there exist a polynomial-time algorithm for solving it? Also, the graph can be considered to be bipartite for convenience.
As noted in Evgeny Kluev's comment, the leaves of a (finite) tree have degree 1. (Else, cycles would exist and the structure would not be a tree.)
If instead you mean to find a spanning tree with a node of maximum degree, from among all possible spanning trees on a connected undirected graph G, then just form a spanning tree whose root R is a node M of G with maximal degree among all the nodes of G, and all neighbors of M are children of R.
It looks like exact cover by 3-sets can be reduced to this problem. Represent the 3-sets by vertices of degree 4, each with 3 edges connecting it to 3 nodes representing its elements in the original problem instance. The additional 4th edge connects all the "3-set" nodes to a single vertex V.
This graph is biparite - every edge is between a "3-set" node and an "element" node (or V). Now this graph has a spanning tree of max min degree = 4 if and only if the original problem has a solution.
Obviously there need to be enough of the 3-sets so that the node V doesn't lower the max min degree of the tree, but this limit doesn't change the NP-hardness of the problem.
I would like to know if there is an algorithm which computes a minimum spanning tree (optimum branching) in a directed graph given a set of root vertices between all of these root vertices, but not only one root vertex and all other vertices in a graph.
Given a set of root vertices [1,4,6] and a graph G like the one on the following picture:
...the algorighm should return something like a green sub-graph on the same picture.
I would like to get such an MST that connects all the root vertices provided to the algorithm. I tend to think that the result of the would-be algorithm is a sub-graph of the graph G which contains all root vertices and some other vertices from G.
Notes:
I know that there is no MST for a directed graph, but there is Chu–Liu/Edmonds algorithm.
I guess that a result of such an algorithm (if it is actually possible) will return an optimum branching, which includes some vertices of a graph along with all root vertices.
Minimum Spanning Trees are supposed to span all the vertices. I think you might be actually dealing with a Steiner Tree problem, given that you only need to connect a subset of them. Unfortunately, the traditional Steiner tree problem with undirected edges is already NP complete so you have a tough road ahead of you.