Group Abstract Group Abstract

Message Boards Message Boards

0
|
10.8K Views
|
9 Replies
|
6 Total Likes
View groups...
Share
Share this post:

I can't write a simple algorithm because 'ForEach' does not exist

Posted 11 years ago

I know that Select[] is some sort of Mathematica function that works with lists. However, I need to be able to grab each element by name into a variable so that I can use it just like in a ForEach loop. Below, you can see that I converted one of my loops into a Select, but I have two more nested ones. Mathematica states that it has full procedural potential, but it does not have the ForEach loop, which makes it much more difficult to operate (do more than one function/action on) each element in some given loop. Even if this could be converted into a functional list to be printed after, I'd rather have something that can print as it finds applicable graphs.

For[x = 0, x < 5, x = x + 1,
     n = RandomInteger[{1, 10}];
     m = RandomInteger[{n - 1, n * (n - 1) / 2}];
     G = RandomGraph[{n, m}];
     R = Radius[G];
     V = VertexList[G];
     P = Select[V, R + 1 == VertexEccentricity[G, #]];
     ForEach [p, P,
      N = AdjacencyList[G, p];
      test = true;
      ForEach[n, N,
       If[VertexEccentricity[G, n] == R,
         test = false;
         ];
       ];
      If [test == true,
       Print[G]
       ]
      ]
     ]
POSTED BY: Timothy Swan
9 Replies
POSTED BY: Bianca Eifert
POSTED BY: Bianca Eifert

Bianca this is a very nice answer. The line

Usually a solution with the structure of the problem.

Is phrasing I've reached for, and failed to find, on many occasions!

POSTED BY: David Gathercole
Posted 11 years ago
POSTED BY: Timothy Swan

You can also use Map

In[3]:= Map["a" <> # &, {"b", "c", "d"}]

Out[3]= {"ab", "ac", "ad"}

In[4]:= "a" <> # & /@ {"b", "c", "d"}

Out[4]= {"ab", "ac", "ad"}
POSTED BY: Frank Kampas

By inefficient, I mean slow.

POSTED BY: Frank Kampas
Posted 11 years ago

Except for library code being developed for parallelization or inner loop conversion to machine code optimizations, changes in time complexity by small constant factors are considered negligible in the context of problems. https://en.wikipedia.org/wiki/Time_complexity In other words, I'm not really interested in cutting operation time in half. I'm just interested in being able to quickly encode or transform an algorithm. Languages use lambdas to remove code repetition, which improves encoding process, and to lazily perform functions, which can potentially change the time complexity if it prevents unnecessary operations from being done. I suppose it's not common to communicate parallelization manually yet, so I'm wondering if those functions parallelize the calls. That might be unnecessary if I only search graphs of size 10. Could you explain the syntax of the Map function? It might be easier to code that, if I remember how, than to use loop variables.

POSTED BY: Timothy Swan

Standard looping constructions, such as For loops and Do loops are inefficient in Mathematica. It's better to use Table, which allows you to loop over lists.

In[1]:= Table["a" <> s, {s, {"b", "c", "d"}}]

Out[1]= {"ab", "ac", "ad"}
POSTED BY: Frank Kampas
Posted 11 years ago

What do you mean my inefficient? A loop takes O(n) time to compute and so does generating a table, so they would be the same time efficiency. A table may take linear space though, which is less efficient. So, the ForEach loops that I intended may be done with tables so I don't have to implement indices? I already implemented a for loop version of the for each which uses index and length accesses of the list.

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