draw a graph where the distance between vertices correspond to the edge weights
Is there an algorithm that gives me coordinates of vertices in a graph, when I give him a weighted graph and the edge weights between vertices points to the distance between vertices?
Something like:
public _ArrayOfCoordinatesForVertices_ **super_h开发者_StackOverflowyper_algorithm**(weighted_graph){
return _foo_;
}
This is in general not possible: Imagine a graph with 3 nodes n1, n2, and n3.
now consider the following distances:
n1-n2: 4
n1-n3: 1
n2-n3: 1
(This violates the triangle inquality).
What you are referring to is called Multidimensional scaling (MDS) and you should find plenty of implementations now you know how to search for it.
Like others said, to some extent, it is impossible to draw a perfect graph without violating some of your constraints (the distances between points). MDS algorithms are specifically targeted at minimizing such violations.
If the graph is drawn in the Euclidean Space you can't do that, because, as pointed out in this answer you could violate the Triangle Inequality.
Usually, you can visually represent edges' weights by using different colour (i.e. by mapping weights to a colour-map), or by using different thickness of the edges (i.e. by mapping weights to a thickness scale).
OK i have found a library for python and it creates a graph image for me :) and i can give weights for edges like attribute: Weight of edge. In dot, the heavier the weight, the shorter, straighter and more vertical the edge is.
精彩评论