Loading [MathJax]/jax/output/HTML-CSS/jax.js
Ask Your Question
1

How to define a graph using Cartesian coordinates

asked 8 years ago

JEA gravatar image

updated 8 years ago

I am trying to figure out (1) how to input a graph into Sage where the vertices are described as Cartesian coordinates (3-tuples), and then (2) for each pair of vertices, compute the Euclidean distance between the two and, if the Euclidean distance is some fixed value d, add an edge between these two vertices.

Specifically, here are my questions:

  1. How do I input a graph into Sage where the vertices are described as Cartesian coordinates (3-tuples)?
  2. Is there a pre-defined function in Sage for computing the Euclidean distance between two Cartesian coordinates?
Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 8 years ago

tmonteil gravatar image

updated 8 years ago

For the first question, you can see the documentation of the Graph constructor by typing Graph?. You will see that it is possible to define a graph from its vertex set and a property defined by pairs of vertices that defines an edge if True:

  9. "Graph([V, f])" -- return a graph from a vertex set "V" and a *symmetric* function "f".
      The graph contains an edge u,v whenever "f(u,v)" is "True".. 
      Example: "Graph([ [1..10], lambda x,y: abs(x-y).is_square()])"

For your second question, you can use the .norm() method of vectors (since the distance between two vectors is the norm of the difference of the vectors): you can transform a tuple into a vector as follows:

sage: t=(3,4,1)
sage: v = vector(RDF, t)
sage: v.norm()
5.0990195135927845

So, you can combine everything into a one-liner as follows:

sage: d = 3
sage: L = [(0,0,0), (1,2,3), (1,1,1), (1,0,1)]
sage: G = Graph([L, lambda u,v: (vector(RDF, u)-vector(RDF, v)).norm() <= d])
sage: G
Looped graph on 4 vertices
sage: G.plot()
Preview: (hide)
link

Comments

This was very helpful. Thank you!

JEA gravatar imageJEA ( 8 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 8 years ago

Seen: 1,086 times

Last updated: May 28 '16