1
$\begingroup$

I have and array with many graphs to which I want to apply an algorithm (below) to each graph in an array that contains all my graphs.

(* g & g1 represent the directed graphs*)
g = Graph[{1 -> 2, 1 -> 3, 1 -> 4, 4 -> 5, 5 -> 6, 3 -> 7, 2 -> 8, 7 -> 9, 9 -> 10, 9 -> 11, 9 -> 12, 8 -> 13, 5 -> 14}];
g1= = Graph[{1 -> 2, 1 -> 3, 1 -> 4, 4 -> 5, 5 -> 6, 3 -> 7, 2 -> 8, 7 -> 9, 9 -> 10, 9 -> 11, 9 -> 12, 8 -> 13, 5 -> 14, 14->13, 14->15, 15->16, 1->17, 1->8}];

(* I define an array with all the graphs*)
allgraphs={g,g1};

(* I define f11 function to use later in the algorithm*)
f11[n_] := n == 0;

(Now, I'm especially having trouble here: I've played around with Map and putting a Table within a Table but my problem is always that I am not able to iterate x[Distributed] for every graph in my array of graphs!)

Table[Probability[x == k,  x \[Distributed] VertexDegree[g]], {k, 
   Max[VertexDegree[g]]}];

Aside: I've tried things like to iterate on all graphs: Table[Probability[x == k, x [Distributed] VertexDegree[j]], {k, Max[VertexDegree[j]]}, {j, allgraphs]; And obviously it doesn't like doing that iteration.

(* I create an array that will list the vertex degrees from 1 to the max degree of the graph*)
Table[a, {a, 1, Max[VertexDegree[g]]}];

(* Combining the two arrays gives me a form {x,y} *)
Table[{%[[i]], %%[[i]]}, {i, Max[VertexDegree[ac]]}];
(* Here I call on my f11 function defined above to delete values with degree=0 because it gives me trouble when I try to plot the log-log form*)
DeleteCases[%, {x_, y_} /; f11[x] \[Or] f11[y]];
Log@%;

(* I define gPlot here, because later I want to populate a grid with the algorithm run on all of my graphs. Currently I copy and paste "gPlot" into my grid and that's why I define the name here*)
gPlot = ListPlot[%, PlotLabel -> "ACC", Filling -> Axis, 
   PlotMarkers -> Style["\[FilledCircle]", 8, Black], 
   Joined -> {True}, InterpolationOrder -> 1, Mesh -> Full, 
   PlotRange -> {{0, 3.5}, {0, -6}}];

So there is it. I want to be able to run the algorithm on every graph in the array allgraphs, so I know I need some sort of iterator but I'm not sure the terminology of the correct process.

$\endgroup$
  • $\begingroup$ Hi! Can you narrow your problem down a bit and remove antything that is not essential for the reproduction/solution of your task? A shorter description (if possible) may help getting more answers. $\endgroup$ – Yves Klett Nov 19 '14 at 15:18
  • $\begingroup$ Regarding your "obviously it doesn't like doing that" snippet: the outermost iterator should be given first. Like this: Table[stuff, {j, allgraphs}, {k, Max[VertexDegree[j]]}] $\endgroup$ – Simon Woods Nov 19 '14 at 15:42
2
$\begingroup$
plotOne[g_Graph] := Module[{probs, purgedTab, r = Range@Max@VertexDegree@g}, 
  probs = {#, Probability[x == #, x \[Distributed] VertexDegree[g]]} & /@ r;
  purgedTab = DeleteCases[probs, {x_, y_} /; x y == 0];
  ListLinePlot[Log@purgedTab, Filling -> Axis,
               Mesh -> Full, MeshStyle -> Directive[PointSize[Large], Black],
               PlotRange -> {{0, 2}, {0, -4}}]]

 GraphicsRow[plotOne /@ allgraphs]

Mathematica graphics

$\endgroup$
1
$\begingroup$

I'm hardly sure that I have understood your question fully, but does this give you the result that you want?

Table[Probability[x == k, x \[Distributed] VertexDegree[j]], {j, 
  allgraphs}, {k, Max[VertexDegree[j]]}]

You need to specify the j iterator first, as the specification of k depends on j.

On a more general note, I would say that if I want to apply an algorithm to a list of elements of some kind, then I would

  1. Make an algorithm that acts on one element, f[e_]:=Block[...]
  2. Map that function onto a (perhaps complicated) list of elements

If the list is as simple as yours, list = {e1,e2,...,ek} then the shorthand way to apply this function to all the elements is

f /@ list
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.