New answers tagged

2

As an answer from @thorumur's comment: You might find Szabolcs' wonderful IGraph package useful, possibly the function IGGetSubisomorphism in particular, which (from the documentation) seems to find a mapping that realizes its first argument as a subgraph of its second argument if such a mapping exists.


2

You can use Graph3D with a custom VertexShapeFunction as follows: ClearAll[reScale, vSF] reScale[s_List, g_Graph, rmin_: 0] := Rescale[s, MinMax@s, {rmin, BoundingRegion[GraphEmbedding[g], "MinDisk"][[2]]/2}]; vSF[s_, g_, rmin_: 0] := Cylinder[{#, # + {0, 0, reScale[s, g, rmin][[#2]]}}, .1] & Examples: SeedRandom[7] rg = ...


8

g = PetersenGraph[]; (* get the 3d coordinates of the graph vertices *) coords = Append[#, 0] & /@ GraphEmbedding[g]; points = Point[coords]; (* create lines between the edges *) connections = EdgeList[g]; lines = Line[coords[[#]]] & /@ (connections /. UndirectedEdge -> List); (* generate some signals on each vertex as lines pointing upwards *) ...


5

Graph objects render as a visual representation by default. This is merely for convenience, not a way to visualize the graph. If you want to explicitly visualize it, use GraphPlot. Once the graph gets large, visualization becomes expensive. I wouldn't want my work to be slowed down (or at worst: the notebook interface hang) when working with large graphs ...


3

To add newoptions to a graph object g, you can use Graph[g, newoptions] Annotate[g, newoptions] SetProperty[g, newoptions] Examples: options = {VertexSize -> Large, PlotTheme -> "IndexLabeled", VertexStyle -> {1 -> Red}}; SeedRandom[1]; Grid[Partition[#,2], Dividers -> All]&[Labeled[#, HoldForm @ #, Top] & /@ {...


2

We can use AdjacencyList[g,v] to find the neighbors of a vertex v in graph g. Construct a subgraph of g formed by neighbors of the focal node. Then we can use FindHamiltonianPath on the subgraph starting with the east neighbor and ending with the south-east neighbor of the focal node to get the neighbors in desired order. Use the desired list of colors and ...


2

Perhaps something like: ClearAll[coNormalProduct] coNormalProduct[g__?UndirectedGraphQ, opts : OptionsPattern[Graph]] := RelationGraph[Or @@ MapThread[EdgeQ, {{g}, UndirectedEdge @@@ Transpose[{##}]}] &, Tuples[VertexList /@ {g}], opts, EdgeShapeFunction -> "CurvedArc"] Example: g1 = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}];...


2

Here's what I came up with. It's messy, but it seems to work. I'm sure someone would be able to do a better implementation. I don't even know how to store a result so I don't have to type out cpF[cpF[VertexList[g1], VertexList[g2]],cpF[VertexList[g1], VertexList[g2]]], nor can I figure out why Cartesian Product of a list isn't working normally, thus why I ...


4

BFS just gives you the distance from the starting vertex. So do that: compute the distance. g = Graph[DirectedEdge @@@ {{1, 2}, {2, 3}, {3, 4}, {3, 5}, {3, 6}, {4, 7}}] root = 1; GroupBy[ Transpose@{VertexList[g], GraphDistance[g, root]}, Last -> First ] (* <|0 -> {1}, 1 -> {2}, 2 -> {3}, 3 -> {4, 5, 6}, 4 -> {7}|> *)


4

Try: GraphLevels[graph_, rootvertex_] := Last@Reap[ BreadthFirstScan[graph, rootvertex, {"DiscoverVertex" -> (Sow[#1, #3] &)}], _, Rule] (* Default for no vertex provided: *) GraphLevels[graph_] := If[EmptyGraphQ[graph], {}, GraphLevels[graph, First@VertexList[graph]]] (* Test: *) g = Graph[{{1, 2}, {2, 3}, {3, 4}, {3,...


5

You can use VertexReplace: VertexReplace[CycleGraph[4, VertexLabels -> "Name"], {1 -> "foo", 3 -> "bar"}]


Top 50 recent answers are included