Group Abstract Group Abstract

Message Boards Message Boards

2
|
5.1K Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Given a graph and a subset of its nodes, how to get the induced subgraph?

Posted 7 years ago
POSTED BY: Szabolcs Horvát
2 Replies

I believe I found a robust way to do it:

Subgraph[g, Verbatim /@ vs]

The problem is that it kills performance.

rg = RandomGraph[{10000, 30000}];
vs = RandomSample[VertexList[rg], 100];

Subgraph[rg, vs]; // RepeatedTiming
(* {0.00024, Null} *)

Subgraph[rg, Verbatim /@ vs]; // RepeatedTiming
(* {5.14, Null} *)

Thus is it not really a practically usable solution.

I think that a workable fix would be to always interpret a List-second argument as a list in Subgraph (and never as a single vertex or single pattern). Thus the single-vertex syntax, Subgraph[g, v] would still be problematic, but at least the Subgraph[g, {v1, v2, ...}] syntax would always be unambiguous.

This, however, would become a usability problem with VertexDelete where people will use the single-vertex syntax much more frequently.

POSTED BY: Szabolcs Horvát

Just to illustrate how things go wrong when Subgraph is used to implement another function:

Mathematica graphics

Oops!

It wasn't a difficult guess that ConnectedGraphComponents might use Subgraph.

It seems that Subgraph, like many other Mathematica functions, is designed for convenient interactive use: run it as a single concise input and look at the result immediately. If this comes at the cost of robustness and predictability, as it does here, that makes the programming language not suitable, for, well, implementing programs (other than one-liners) ... Mathematica is great at interactive use, which is, and has always been one of its great advantages. But if it doesn't get better at being suitable for actual software development (i.e. writing packages, and implementing parts of Mathematica itself), that might be its downfall.

The competitors are not yet there with suitability for interactive use, but they're quickly catching up. See the story of Jupyter. WRI invented the notebook paradigm, but now Jupyter gets the credit. And the competitors are way ahead of Mathematica at supporting development, attracting a community, sustaining a package ecosystem, attracting open source development (which is already essential to academic work) ...

POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard