Create a Mathematica / Combinatorica graph with named vertex edges

How to create a Mathematica graph from edges with named vertices? EG:

http://pastebin.com/Se1Nhe3M

I tried the above and a few options, but Combinatorica never quite agree with that. Apparently, Graph [] wants the coordinates of the position that I want Combinatorica to understand.

AddVertex to EmptyGraph [0] (or something else) also fails.

GraphUtilities is not an option, since I want to do a rather complicated analysis on my graphs.

This seems like a simple problem. Graphviz easily creates graphs from edges with named vertices, so I'm sure Mathematica can too?

I read:

ShowGraph [{{e1, e2}, {e1, e3}}, {e1, e2, e3}]; // what is the problem?

but it doesn't seem to help in my particular case.

+3
source share
5 answers

If the anchor point is the nodes represented as strings, and the functions of the analysis of heavy functions require them as integers, you can consider matching strings with integers and vice versa:

nodes = DeleteDuplicates@Flatten[graph /. Rule -> List]

{"Conga", "Egypt", "Sarah Desert", "Europe", "Arabia", "UK", "Iceland", 
 "Greenland", "Germany", "Russia", "Irakistan", "Austr(al)ia", "China", "Canada",
 "More Russia", "USA", "Andy Mountains", "Brazil"}

You now have a list of nodes. Then match to and from integers:

each[{i_, s_}, Transpose[{Range@Length@nodes, nodes}],
  numify[s] = i;
  namify[i] = s]

Now you can easily convert nodes to integers and from them:

numify["Europe"]

4

namify[4]

"Europe"

Convert the entire graph as follows:

graph /. s_String -> numify[s]

Note that eachthis is the following utility feature discussed here: ForEach loop in Mathematica

SetAttributes[each, HoldAll];               (* each[pattern, list, body]      *)
each[pat_, lst_, bod_] := ReleaseHold[      (*  converts pattern to body for  *)
  Hold[Cases[Evaluate@lst, pat:>bod];]];    (*   each element of list.        *)
+2
source

If you have Mathematica 7, try the built-in GraphPlot:

GraphPlot[{"Conga" -> "Egypt", "Egypt" -> "Conga", 
  "Conga" -> "Sarah Desert", "Sarah Desert" -> "Conga", 
  "Egypt" -> "Europe", "Europe" -> "Egypt", "Egypt" -> "Arabia", 
  "Arabia" -> "Egypt", "Egypt" -> "Sarah Desert", 
  "Sarah Desert" -> "Egypt", "UK" -> "Europe", "Europe" -> "UK", 
  "UK" -> "Iceland", "Iceland" -> "UK", "UK" -> "Greenland", 
  "Greenland" -> "UK", "Europe" -> "Arabia", "Arabia" -> "Europe", 
  "Europe" -> "Germany", "Germany" -> "Europe", "Europe" -> "Iceland",
   "Iceland" -> "Europe", "Europe" -> "Sarah Desert", 
  "Sarah Desert" -> "Europe", "Germany" -> "Russia", 
  "Russia" -> "Germany", "Germany" -> "Arabia", "Arabia" -> "Germany",
   "Germany" -> "Iceland", "Iceland" -> "Germany", 
  "Germany" -> "Irakistan", "Irakistan" -> "Germany", 
  "Austr(al)ia" -> "China", "China" -> "Austr(al)ia", 
  "Arabia" -> "Irakistan", "Irakistan" -> "Arabia", 
  "Canada" -> "More Russia", "More Russia" -> "Canada", 
  "Canada" -> "USA", "USA" -> "Canada", 
  "Canada" -> "Andy Mountains", "Andy Mountains" -> "Canada", 
  "More Russia" -> "Russia", "Russia" -> "More Russia", 
  "More Russia" -> "China", "China" -> "More Russia", 
  "More Russia" -> "Irakistan", "Irakistan" -> "More Russia", 
  "China" -> "Irakistan", "Irakistan" -> "China", 
  "USA" -> "Greenland", "Greenland" -> "USA", 
  "USA" -> "Andy Mountains", "Andy Mountains" -> "USA", 
  "Brazil" -> "Sarah Desert", "Sarah Desert" -> "Brazil", 
  "Brazil" -> "Andy Mountains", "Andy Mountains" -> "Brazil", 
  "Russia" -> "Irakistan", "Irakistan" -> "Russia"}, 
 DirectedEdges -> True]

This will give you the following, for example:

alt text http://img638.imageshack.us/img638/7803/plotm.png

, , ..

+2

Combinatorica, , , , :

Load Combinatorica < , . :

edge = {{ "" β†’ "" }, { "" β†’ "" }, { "" β†’ " " } { "-" β†’ "" }, { "" β†’ "" }, { "" β†’ "" } { "" β†’ "" }, { "" β†’ "" }, { "" β†’ " " } { " " β†’ "" }, { "" β†’ "" }, { "" β†’ "" } { "" β†’ "" }, { "" β†’ "" }, { "" β†’ "" } { "" β†’ "" }, { "" β†’ "" }, { "" β†’ "" } { "" β†’ "" }, { "" β†’ "" }, { "" β†’ "" } { "" β†’ "" }, { "" β†’ " " }, { " " β†’ "" }, { "" β†’ "" }, { "" β†’ "" } { "" β†’ "" }, { "" β†’ "" }, { "" β†’ "" } { "" β†’ "" }, { "" β†’ "" }, { "" β†’ "" } { " (al) ia" β†’ "" }, { "" β†’ " (al) ia" }, { "" β†’ "" } { "" β†’ "" }, { "" β†’ " " }, { " " β†’ "" } { "" β†’ "" }, { "" β†’ "" }, { "" β†’ " " } { " " β†’ "" }, { " " β†’ "" } { "" β†’ " " }, { " " β†’ "" }, { "" β†’ " " } { " " β†’ "" }, { "" β†’ " " } { "" β†’ "" }, { "" β†’ "" }, { "" β†’ "" } { "" β†’ "" }, { "" β†’ " " } { " " β†’ "" }, { "" β†’ " " }, { " " β†’ "" }, { "" β†’ " " }, { " " β†’ "" }, { "" β†’ "" } { "" β†’ "" }}/. [from_, to _] β†’ {from, to};

labels = { "" , "" , "" , "" , " " , "" , "" , "" , "" , "" , " " "", "" , "" , "", "" , "-", "" );

numberededges = [ [/.Thread [ [, [ []]]]], 2];

ShowGraph [AddEdges [EmptyGraph [ []], numberededges], VertexLabel- > , PlotRange- > ]

, , , .

, , , . , , .

" : " . , , Combinatorica, , . , , , Combinatorica, .

+1

, Combinatorica FromAdjacencyMatrix.

(* Converts lists of edges into adjacency matrix, saving "nodename->column #" mapping into global variable nodeMap *)
graph2mat[edges_] := Module[{nodes, mat, n},
   nodes = Sequence @@@ edges // Union // Sort;
   nodeMap = (# -> (Position[nodes, #] // Flatten // First)) & /@ 
     nodes;
   mat = (({#1, #2} -> 1) & @@@ (edges /. nodeMap)) // SparseArray // 
     Normal;
   n = Max[Length[#] & /@ {mat, Transpose[mat]}];
   PadRight[mat, {n, n}]
   ];

g = FromAdjacencyMatrix[graph2mat[{"a" -> "b", "b" -> "a"}]];
reverseNodeMap = Reverse /@ nodeMap;
MaximumClique[g] /. reverseNodeMap
0

[] . www.sagenb.org? Sage Graph .

0

Source: https://habr.com/ru/post/1758313/


All Articles