Tag Info

Hot answers tagged

13

similar to kguler, but only remove edges (more likely op's image): g = GridGraph[{10, 10}]; g2 = Graph[VertexList[g], RandomSample[EdgeList[g], Floor[EdgeCount[g] .4]], VertexCoordinates -> GraphEmbedding[g], EdgeStyle -> Thickness[.01], VertexStyle -> EdgeForm[], VertexSize -> Medium] HighlightGraph[g2, Subgraph[g2, #] & /@ ...


9

You can get the vertex coordinates, e.g. g = GraphData[{"Wheel", 7}] vc=PropertyValue[g, VertexCoordinates] yielding: {{-0.5, -0.866}, {0.5, -0.866}, {1., 0.}, {0.5, 0.866}, {-0.5, 0.866}, {-1., 0.}, {0., 0.}} UPDATE To do the other objects (without the overlaid lines): grap = GraphicsComplex[vc, Line /@ List @@@ EdgeList[g]]; tr[p_] := ...


7

In Version 10, we can do this nicely even for 3D point sets: pointsToGraph[pts_, graph : (Graph | Graph3D)] := Module[{del = DelaunayMesh[pts], edges}, edges = UndirectedEdge @@@ MeshCells[del, 1][[All, 1]]; graph[Range@Length@pts, edges, VertexLabels -> "Name", VertexCoordinates -> pts] ] SeedRandom[2]; pts2d = RandomReal[10, {10, 2}]; ...


7

Update 3: A one-liner ClearAll[f]; f[g_, tr : {{_, _} ..}, opts : OptionsPattern[]] := Graphics[Translate[First@Show@g, {{0, 0}, ## & @@ tr}], opts] g = GraphData[{"Wheel", 7}]; tr1 = {{1, 0}, {-1/2, -Sin[Pi/3]}, {-1/2, Sin[Pi/3]}}; tr2 = {{0, 2 Sin[Pi/3]}}; Row[f[g, #, ImageSize -> 300] & /@ {tr1, tr2}] Update 2: The simplest ...


6

g = GridGraph[{10, 10}, VertexSize -> Large, EdgeStyle -> Thickness[.02]] SeedRandom[1]; vl= RandomSample[VertexList[g], 50]; sg = Subgraph[g, vl, VertexCoordinates -> GraphEmbedding[g][[vl]], VertexSize -> Large, EdgeStyle -> Thickness[.02]]; HighlightGraph[sg, Subgraph[sg, #] & /@ ConnectedComponents[sg], ...


4

I think what you really want is a directed Barabasi–Albert graph whose opposite edges (e.g. a -> b and b -> a) have different weight. We can use the built-in command BarabasiAlbertGraphDistribution for this: RandomWeightedBarabasiAlbertGraph[n_, k_, options:OptionsPattern[]] := WeightedAdjacencyGraph[ Replace[ Normal @ AdjacencyMatrix @ ...


4

In version 10, you can use MeshCoordinates[ConvexHullMesh[...]] as in RunnyKine's answer, but you need to re-order them using MeshCells: pentagon=N@Table[{Cos[2 Pi k /5], Sin[2 Pi k /5]}, {k, 5}] points = N@RandomSample[Join[pentagon, {{0, 0}}]] chm=ConvexHullMesh[points]; ordering=MeshCells[chm,2][[1,1]] out=MeshCoordinates[chm][[ordering]] ...


3

If you have Version 10, you could use ConvexHullMesh. pts = RandomReal[{-10, 10}, {6, 2}]; You can then order them by doing: chull = ConvexHullMesh[pts]; And here are the points: MeshCoordinates[chull] Note: This does not always order the points but one can use MeshCells which will give the ordering correctly. See @kguler's answer.


3

Here is a solution that doesn't depend on the graph functionality, but is still based on the same ideas. The plan is to find the adjacency matrix corresponding to a grid graph and then to remove a few edges before plotting the graph. We can figure out how to do build the adjacency matrix of a grid graph by inspection (the upper part of the matrix is ...


3

I may have misunderstood the aims, If so, I apologize. For the first question: f[n_] := GraphDistance[CompleteGraph[5, EdgeWeight -> #], 1, 2] & /@ RandomVariate[ExponentialDistribution[1], {n, 10}]; : This generates a sample of size n of graph distances between vertex 1 and 2. You can visualize: Histogram[f[10000]] Estimating ...


3

I post this but Teake Nutma is the better answer. Changing your code to directed edges: Needs["HierarchicalClustering`"]; MakeClusteredTree[data_, leaves_, opts : OptionsPattern[]] := Module[{clusters, expr, ett, edges, optsGraph = FilterRules[opts, Options[Graph]], optsAgglomerate = FilterRules[opts, Options[Agglomerate]]}, clusters = ...


3

First a question for you: is there any reason why the edges are undirected? Your graph looks like a tree graph to me, with a strict hierarchy. For this directed edges would be better. We can switch to directed edges as follows: Block[{UndirectedEdge = DirectedEdge}, graph = MakeClusteredTree[data, ToString /@ data, {Linkage -> "Average"}] ] You can ...


3

What you are looking for is a kind of Hasse diagram. Unfortunately, there is no GraphLayout that does exactly what you want out of the box. (Although kguler has shown in his answer you can coax "MultipartiteEmbedding" into the desired behaviour). However, it still can also be done with specifying the option VertexCoordinates of Graph. This requires us to ...


2

rg = RandomGraph[WattsStrogatzGraphDistribution[30, 0.3, 3]] gc = FindGraphCommunities[rg, Method -> "Hierarchical"] (* {{6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}, {4, 18, 19, 20, 21, 22, 23, 24, 25, 26}, {1, 2, 3, 5, 27, 28, 29, 30}} *) CommunityGraphPlot[rg, gc] expdata = Join @@ MapIndexed[Thread[{#1, First@#2}] &, gc] (* {{6, ...


2

Here is what I understood, I might be mistaken because it's quite unclear to me. Module[{source, target, weight, tree, g}, source = {"aaa", "aaa", "BBB", "ccc", "ddd", "aaa", "aaa", "aaa", "aaa", "aaa"}; target = {"ddd", "eee", "aaa", "aaa", "aaa", "fff", "ggg", "hhh", "iii", "jjj"}; weight = {4.8, 4.4, 4.2, 4.1, 3.6, 3.3, 3.2, 3, 2.7, 2.6}; tree = ...


2

I know of two methods to produce a fixed vertex size. Scaled Use Scaled for VertexSize and set AspectRatio -> 1: options = Sequence[VertexStyle -> Black, VertexLabels -> Placed["Name", {Center, Center}], VertexLabelStyle -> Directive[16, White], GraphLayout -> "CircularEmbedding", EdgeShapeFunction -> ef2, ImageSize -> 400, ...


2

vertices = Range[7]; labels = {10, 5, 2, 2, 2, 2, 1}; (* Note: if labels is not already sorted in descending order, use labels = Sort[labels, Greater] -- thanks: @TeakeNutma *) labels2 = Thread[vertices -> (Placed[#, Center] & /@ (Rotate[#, 90 Degree] & /@labels))]; vp = Last /@ Tally[labels]; (*thanks: Oska *) edges = ...


2

The converstion between sparse arrays and graphs can be done with AdjacencyMatrix and AdjacencyGraph or IncidenceMatrix and IncidenceGraph. m = AdjacencyMatrix[GraphData["PappusGraph"]] AdjacencyGraph[m]


2

Could use ConvexHull in the ComputationalGeometry standard add-on package. Needs["ComputationalGeometry`"] We'll create a simple example. pts = RandomReal[{-10, 10}, {6, 2}]; ListPlot[Append[pts, First[pts]], Joined -> True] Now find and plot the (ordered) outer points. hullindices = ConvexHull[pts]; hullpts = pts[[hullindices]]; ...


1

The following works in your special case but can't be generalized. l = {"+", "m", "π", "[]", "2"}; SeedRandom@0; rl = RandomSample[l, 5]; g = With[{cg = CycleGraph[5]}, Graph[UndirectedEdge @@@ Thread@{rl, RotateLeft@rl}, VertexCoordinates -> (Rule @@@ Thread@{rl, VertexCoordinates /. AbsoluteOptions[cg, VertexCoordinates]}), ...


1

Taking an example of a shortest-path vertex list and an edgelist from your code: spvertices = {4, 2, 1, 5}; edges = {1 \[UndirectedEdge] 2, 1 \[UndirectedEdge] 3, 2 \[UndirectedEdge] 3, 4 \[UndirectedEdge] 2, 5 \[UndirectedEdge] 1, 6 \[UndirectedEdge] 2, 7 \[UndirectedEdge] 3, 8 \[UndirectedEdge] 2}; spath = ...


1

There doesn't be to be a way to Import the colours. The only available information are the following: {#, Import["~/test.col", #]} & /@ Import["~/test.col", "Elements"] Thus you indeed need to Import your Graph from the .col file and add the colours afterwards: g = Import@"~/test.col"; data = Import["~/test.col", "List"]; vclist = ToExpression ...



Only top voted, non community-wiki answers of a minimum length are eligible