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

How to draw the following graph in sagemath?

asked 5 years ago

Captcha gravatar image

updated 4 years ago

FrédéricC gravatar image

How to draw the following graph in sagemath?

I am uploading the image here:

https://imgur.com/Q3sUXn8

I know how to draw a graph but I am finding it difficult to draw the graph in exactly the same way as shown in the picture.

Is it possible to do this?

I will be very grateful if someone could kindly help me out

Preview: (hide)

Comments

It looks like you've asked the same question on LaTeX Stack Exchange here. LaTeX, through the tkz-berge package, is probably the best way to specify the position to create a diagram.

dazedANDconfused gravatar imagedazedANDconfused ( 5 years ago )

2 Answers

Sort by » oldest newest most voted
1

answered 5 years ago

tmonteil gravatar image

updated 5 years ago

If G is you graph, you can define coordinates of the vertices by using the set_pos method, see:

sage: G.set_pos?

for more details.

EDIT Having a look at the set_pos documentation, it is actually lacking meaningful examples, so here is how to use set_pos: you have to define a dictionary that associates to each vertex a coordinate (i.e. a tuple of two numbers), as follows:

sage: G = Graph(3)
sage: G.set_pos({0:(0,0), 1:(1,1), 2:(0,1)})
sage: G.plot()
Preview: (hide)
link

Comments

A better answer can be obtained via a clever use of LATEX and Graphviz. See the relevant documentation.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 5 years ago )
1

I consider Python/Sagemath a better programming language than LaTeX, so it is easier to produce a graph with Sage and then convert it into latex/tikz, with:

sage: latex(G)

This is especially true for complex graphs.

tmonteil gravatar imagetmonteil ( 5 years ago )

Thierry, that's exactly what I thought of ; but my expresson wasn't exactly precise...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 5 years ago )
1

answered 5 years ago

Emmanuel Charpentier gravatar image

updated 5 years ago

Alternate solution:

sage: G=Graph([[0,u] for u in (1..7)])
sage: G.add_edges([[u,4] for u in (1..3)])
sage: G.add_edges([[u,4] for u in (5..7)])
sage: D={0:[4,2]}
sage: D.update({4:[4,0]})
sage: for u in (1..3): D.update({u:[u,1]})
sage: for u in (5..7): D.update({u:[u,1]})
sage: G.plot(pos=D)

The required graph (approximately...)

EDIT : If you insist on the order of the nodes:

sage: Dpos=[4, 3, 5, 2, 4, 6, 1, 7]
sage: G.plot(pos={u:[Dpos[u], 1+(u==0)-(u==4)] for u in (0..7)})

A better approximation

Preview: (hide)
link

Comments

You define the same pos dictionary to provide coordinates to the vertices, how is it alternate ?

tmonteil gravatar imagetmonteil ( 5 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: 5 years ago

Seen: 715 times

Last updated: Oct 09 '19