1
$\begingroup$

Consider the complete graph on 3 vertices.

enter image description here

Then, a method gives me a list of interesting edges, say $\{(1,2),(2,3)\}$. I want to get a subgraph of $g$ that corresponds to precisely the given edgelist. How can I do this?

If I use Subgraph[g,e] with e = {1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3}, I get back the original graph. This does preserve the colors, but includes an extra edge, namely $(3,1)$ that I don't want. However, Subgraph does have a pattern as an argument. That might do the trick, but perhaps there's an easier way.

$\endgroup$
1
  • $\begingroup$ Isn't your expected output exactly the same as your input, i.e. Graph[Most[e]]? What exactly are you trying to do? $\endgroup$ Commented May 31, 2013 at 20:08

1 Answer 1

2
$\begingroup$

First, be careful with the Subgraph function. Here's the description of the function from the help: "The subgraph generated by the vertices $v_1$, $v_2$,... includes the vertices $v_i$ and all edges connecting them in the original graph g.' In other words, the second argument is not a graph, it is a set of vertices (to be taken from the graph specified in the first argument). It then show those vertices and all connected edges.

Perhaps the simplest way to get a picture of the subgraph (by removing edges) is to remove the edges from the graph object. In this case:

Graph[e, Options[g]] 

which gives the connectivity you want.

enter image description here

If your graph g is already defined, then you can use EdgeList[g] to get the edges, then remove the ones you want .

$\endgroup$
6
  • $\begingroup$ Seems like removing the unwanted edges form the definition is the most straightforward way: in this case, Graph[Most[e]] will give you the same connectivity as what you are asking for. (The picture doesn't look the same, but the graph is the same). $\endgroup$ Commented May 31, 2013 at 20:04
  • $\begingroup$ If your graph g is already defined, then you can use EdgeList[g] to get the edges, then remove the ones you want. $\endgroup$ Commented May 31, 2013 at 20:35
  • $\begingroup$ Your question answers perfectly my original question. Would you happen to know how I can preserve the EdgeStyle associated with the edges? Seems like Options[g,{EdgeStyle}] doesn't work because it includes an EdgeStyle for every edge in the original graph g. Anyway, since I changed the meanings of my question after you had answered, I will definitely accept your answer. I will keep working on the problem and update your question if I crack it. $\endgroup$ Commented May 31, 2013 at 20:51
  • $\begingroup$ I'm not sure how to do it staying within EdgeStyle. A more straightforward way would be to define the styling via a list and to then index into that list exactly as you are indexing into the edgelist. That way the styling would stay consistent. $\endgroup$ Commented May 31, 2013 at 21:09
  • 1
    $\begingroup$ If you want to preserve the layout of the graph, check out this answer about keep node locations mathematica.stackexchange.com/a/11106/2393 $\endgroup$ Commented Jun 1, 2013 at 5:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.