# How can I get precisely the subgraph corresponding to a set of edges?

Consider the complete graph on 3 vertices. 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.

• Isn't your expected output exactly the same as your input, i.e. Graph[Most[e]]? What exactly are you trying to do? – Szabolcs May 31 '13 at 20:08

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. If your graph g is already defined, then you can use EdgeList[g] to get the edges, then remove the ones you want .

• 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). – bill s May 31 '13 at 20:04
• If your graph g is already defined, then you can use EdgeList[g] to get the edges, then remove the ones you want. – bill s May 31 '13 at 20:35
• 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. – Juho May 31 '13 at 20:51
• 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. – bill s May 31 '13 at 21:09
• If you want to preserve the layout of the graph, check out this answer about keep node locations mathematica.stackexchange.com/a/11106/2393 – Cameron Murray Jun 1 '13 at 5:58