how to write parallel prefix sum in c#? - c#

I want to write a Parallel prefix sum in c#. i used this algorithm:
initial condition: list of n >= 1 elements stored in A[0...(n-1)]
final condition: each element A[i] contains A[0]+A[1]+...+A[i]
begin
spawn (p1,p2,...,p(n-1))
foe all pi where 1 <= i <= n-1 do
for j←0 to ⌈logn⌉-1 do
if i - 2^j >= 0 then
A[i] ← A[i] + A[i - 2^j]
end if
end for
end for
end
and my final code in c# is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MPI;
namespace prefixsum3
{
class Program
{
static void Main(string[] args)
{
int[] A = new int[] { 4, 3, 8, 2, 9, 1, 3, 5, 6, 3 };
using (new MPI.Environment(ref args))
{
Intracommunicator comm = Communicator.world;
int size, rank, n, i;
size = comm.Size;
i = comm.Rank + 1;
n = A.Length;
int[] B = new int[10];
for (int j = 0; j <= (Math.Ceiling(Math.Log(n))) - 1; j++)
{
int t = Convert.ToInt32(Math.Pow(2, j));
if ( i - t >= 0)
{
B[i] = A[i] + A[i - t];
}
comm.Barrier();
A[i] = B[i];
comm.Barrier();
}
if (comm.Rank == 0)
{
for (int z = 0; z < n; z++)
{
Console.Write(A[z].ToString() + ",");
}
}
}
}
}
}
the wright output should be: [4,7,15,17,26,27,30,35,41,44]
but my output code is: [4,7,8,2,9,1,3,5,6,3]
is any one know what is wrong with my code?
EDIT:
i found every processors see array A locally. now the problem is how to define array A Globally that all processors see one array?

As for correctness the logarithm in the original algorithm is to the base 2, this is your error (or one of them).
Regarding efficiency, you didn't understand correctly the double buffer algorithm: you're supposed to write in B[i], synchronize, then swap the A and B arrays before the next iteration. You don't need two barriers or A[i] = B[i]. However you must do B[i] = A[i] when t is greater or equal to i.
Finally Math.Pow is inefficient, you should better start with t = 1 then multiply it by 2 on every iteration (t <<= 1). Anyway you should simply use the following loop that is faster and fixes the previously mentioned error (you no longer need the log):
for (int t = 1; t < n; t <<= 1)

Related

The biggest value in array and check if all elements in array have equal values. (C#)

I'm working on a project. The user inputs a value(n) and that value is multiplied by 2. Then the user inputs the exact same times a new number (n*2). So I have to figure out if every couple of numbers are equal and if not to print the biggest difference between one of the couples. In order to do that I need Biggest value in an array and somehow to check if all elements in an array are equal. I'm new to C# and I don't know much about arrays.(btw sorry about the use of language, I'm foreigner)
I came up with this code so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EqualPairs
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int sum = 0;
int diff = 0;
int[] sumAll = new int[n];
int[] differ = new int[n];
for (int i = 1; i <= n; i++)
{
sum = 0;
diff = 0;
for (int j = 1; j <= 2; j++)
{
int number = int.Parse(Console.ReadLine());
sum = sum + number;
diff = Math.Abs(diff - number);
}
sumAll[i - 1] = sum;
differ[i - 1] = diff;
}
}
}
}
I'm not sure exactly what you're trying to do with your program, but to answer your questions:
Find max value in an array:
int maxValue = yourArray.Max();
Check if all values in your array are equal (using System.Linq):
int first = yourArray.First();
bool allElementsEqual = yourArray.All(x => x == first);
Edit based on OP's comment:
If I were you, I'd create an intermediate array couplesArray. Not sure how you're planning to set up your inputs and all that, but I'm assuming yourArray has an even number of values. I don't know how you're defining max difference and all that, so perhaps drop some of my Math.Abs() calls:
int[] yourArray = { 1, 2, 0, 3, 4, -1};
int[] couplesArray = new int[yourArray.Count() / 2];
for (int i = 0; i < couplesArray.Length; i++)
{
couplesArray[i] = yourArray[2 * i] + yourArray[2 * i + 1];
}
int first = couplesArray.First();
bool allElementsEqual = couplesArray.All(x => x == first);
int maxDifference = Math.Max(Math.Abs(couplesArray.Max()), Math.Abs(couplesArray.Min()));
string outputString = allElementsEqual ? "Equal" : maxDifference.ToString();
Console.WriteLine(outputString);

a program that get 10 numbers and print them in reverse order

I'm trying to write a program that takes 10 inputs and prints them in reverse order. Here is my code in c#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace inverse
{
class Program
{
static void Main(string[] args)
{
int i ;
int[] n = new int[10];
Console.WriteLine("please enter 10 numbers");
for (i = 1; i <= 10; i++)
n[i] =int.Parse(Console.ReadLine());
for (i = 10; i >= 1; i--)
Console.WriteLine(n[i] + " ");
Console.ReadKey();
}
}
}
It doesn't get any errors but it also doesn't work when i'm trying to debug it. Could you please help me?
You should change your second for loop to:
for (i = 9; i >= 0; i--)
Why?
Because in you incorrect for loop, i starts at 10. In each iteration, it decrements, until i >= 1. In other words, i's value changes from 10 to 1.
But that's not how arrays work, is it? Arrays in C# are 0-based, meaning that the first item is at index 0, and the last item is at index (length - 1). In this specific case, the last item would be at index 9 (10 - 1 = 9)!
When you try to access the item at index 10, of course it throws an exception because the array ends at index 9! Also, even if it has an item at index 10, the first item of the array would not be printed because the loop stops at i = 1.
That is why you want i's value to go from 9 to 0 instead of 10 to 1.
EDIT:
I just realized you made another mistake, your first for loop is incorrect as well. It should loop from 0 to 9, not 1 to 10. This is the correct version:
for (int i = 0 ; i <= 9, i++)
Alternatively, instead of changing the loop's headers, you can just add - 1:
for (i = 1; i <= 10; i++)
n[i - 1] =int.Parse(Console.ReadLine());
for (i = 10; i >= 1; i--)
Console.WriteLine(n[i - 1] + " ");
You need to start from 0 to 9 and vice versa
for (i = 0; i <= 9 i++)
n[i] =int.Parse(Console.ReadLine());
for (i = 9; i >= 0; i--)
Console.WriteLine(n[i]);
Use the below code:
static void Main(string[] args)
{
int i;
int[] n = new int[11];
Console.WriteLine("please enter 10 numbers");
for (i = 1; i <= 10; i++)
n[i] = Convert.ToInt32((Console.ReadLine()));
for (i = 10; i >= 1; i--)
Console.WriteLine(n[i] + " ");
Console.ReadKey();
}
The problem was array index was out of range as the array starts from 0. It gave me error when I gave up to 10 inputs.

Add Int[] array into List<int[]>

I'm having trouble with int[] arrays and adding them to a List<>. I'd like to add the values of my int[] array to something each loop but every time I do this my "something" gets the same value for every element I add. Very annoying. I understand arrays are always reference vars. However even the "new" key word doesn't seem to help. What needs to happen is to add result to some enumerated object like a List or Array or ArrayList.
Here's the codility question:
You are given N counters, initially set to 0, and you have two possible operations on them:
increase(X) − counter X is increased by 1,
max_counter − all counters are set to the maximum value of any counter.
A non-empty zero-indexed array A of M integers is given. This array represents consecutive operations:
if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X),
if A[K] = N + 1 then operation K is max_counter.
For example, given integer N = 5 and array A such that:
A[0] = 3
A[1] = 4
A[2] = 4
A[3] = 6
A[4] = 1
A[5] = 4
A[6] = 4
the values of the counters after each consecutive operation will be:
(0, 0, 1, 0, 0)
(0, 0, 1, 1, 0)
(0, 0, 1, 2, 0)
(2, 2, 2, 2, 2)
(3, 2, 2, 2, 2)
(3, 2, 2, 3, 2)
(3, 2, 2, 4, 2)
The goal is to calculate the value of every counter after all operations.
I copied some code from others and the variable "result" does indeed load the data correctly. I just wanted to copy it back to the main program so I could see it. The only method that works is += add it into a string. Thus losing any efficiency I might have gained.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testarray
{
class Program
{
static void Main(string[] args)
{
int[] A = new int[7];
A[0] = 3;
A[1] = 4;
A[2] = 4;
A[3] = 6;
A[4] = 1;
A[5] = 4;
A[6] = 4;
List<int[]> finish = solution(5, A);
}
public static List<int[]> solution(int N, int[] A)
{
int[] result = new int[N];
int maximum = 0;
int resetlimit = 0;
int iter = 0;
List<int[]> collected_result = new List<int[]>;
for (int K = 0; K < A.Length; K++)
{
if (A[K] < 1 || A[K] > N + 1)
{
throw new InvalidOperationException();
}
if (A[K] >= 1 && A[K] <= N)
{
if (result[A[K] - 1] < resetlimit)
{
result[A[K] - 1] = resetlimit + 1;
}
else
{
result[A[K] - 1]++;
}
if (result[A[K] - 1] > maximum)
{
maximum = result[A[K] - 1];
}
}
else
{
resetlimit = maximum;
result = Enumerable.Repeat(maximum, result.Length).ToArray<int>();
}
collected_result.Add(result);
}
// for (int i = 0; i < result.Length; i++)
//result[i] = Math.max(resetLimit, result[i]);
return collected_result;
}
}
}
This doesn't work, the collected_result ends up like:
(0,0,1,2,0)
(0,0,1,2,0)
(0,0,1,2,0)
(3,2,2,4,2)
(3,2,2,4,2)
(3,2,2,4,2)
(3,2,2,4,2)
I know it's the line collected_result.Add(result); adding the reference each time to every instance of result in the List<>. Bother. I've tried adding "new" which is a compiler error. Finally in desperation I just added everything to a very long string. Can someone help me figure out how to properly load an object to pass back to main?
Easiest way to go:
Get a copy of your array before adding it to list:
collected_result.Add(result.ToArray());
Here is a Python solution:
def solution(A, N):
lenA = len(A)
k = 0
max_counter_value = 0
counters = [0 for x in range(0, N)]
for k in range(0, lenA):
if A[k] >= 1 and A[k] <= N:
counters[A[k] - 1] += 1
max_counter_value = max(counters)
if A[k] == N + 1:
counters = [max_counter_value for x in range(0, N)]
print counters
A = [3, 4, 4, 6, 1, 4, 4]
N = 5
solution(A, N)

How to find minimum number of steps to sort an integer array

I have an integer array int[] number = { 3,4,2,5,1};
The minimum number of steps to sort it should be 2. But I am getting 4.
static void Main(string[] args)
{
int[] number = { 3,4,2,5,1};
int result = get_order(number);
Console.ReadKey();
}
public static int get_order(int[] input1)
{
input1 = input1.OrderByDescending(o => o).ToArray();
bool flag = true;
int temp;
int numLength = input1.Length;
int passes = 0;
for (int i = 1; (i <= (numLength - 1)) && flag; i++)
{
flag = false;
for (int j = 0; j < (numLength - 1); j++)
{
if (input1[j + 1] > input1[j])
{
temp = input1[j];
input1[j] = input1[j + 1];
input1[j + 1] = temp;
flag = true;
}
}
passes++;
}
return passes+1;
}
What is the problem and what changes i need to do in my code?
Edit
implement #Patashu, algorithm,
public static int get_order(int[] input1)
{
var sorterArray = input1.OrderByDescending(o => o).ToArray();
var unsortedArray = input1;
int temp1;
int swap = 0;
int arrayLength = sorterArray.Length;
for (int i = 0; i < arrayLength; i++)
{
if (sorterArray[i] != unsortedArray[i])
{
temp1 = unsortedArray[i];
unsortedArray[i] = sorterArray[i];
for (int j = i + 1; j < arrayLength; j++)
{
if (unsortedArray[j] == sorterArray[i])
{
unsortedArray[j] = temp1;
swap++;
break;
}
}
}
}
return swap;
}
The problem with your algorithm is that it only attempts swapping adjacent elements.
3,4,2,5,1 is best sorted by swapping 3 with 5, which is an unadjacent swap, and then 2 with 3.
So, I suggest that you will find a better algorithm by doing the following:
1) First, sort the array into descending order using the built in sorting function of C#.
2) Now, you can use this sorted array as a comparison - iterate through the array from left to right. Every time you see an element in the unsorted array that is != to the element in the same space in the sorted array, look deeper into the unsorted array for the value the sorted array has there, and do one swap.
e.g.
3,4,2,5,1
Sort using Sort -> 5,4,3,2,1 is our sorted array
3 is != 5 - look in unsorted array for 5 - found it, swap them.
Unsorted is now 5,4,2,3,1
4 == 4
2 is != 3 - look in unsorted array for 3 - found it, swap them.
Unsorted is now 5,4,3,2,1
2 == 2
1 == 1
We're at the end of the unsorted array and we did two swaps.
EDIT: In your algorithm implementation, it looks almost right except
instead of
unsortedArray[j] = sorterArray[i];
unsortedArray[i] = temp1;
you had it backwards, you want
unsortedArray[j] = temp1;
unsortedArray[i] = sorterArray[i];
Since you're asking why you're getting 4 steps, and not how to calculate the passes, the correct way to do this is to simply step through your code. In your case the code is simple enough to step through on a piece of paper, in the debugger, or with added debug statements.
Original: 3, 4, 2, 5, 1
Pass: 1: 4, 3, 5, 2, 1
Pass: 2: 4, 5, 3, 2, 1
Pass: 3: 5, 4, 3, 2, 1
Pass: 4: 5, 4, 3, 2, 1
Basically what you see is that each iteration you sort one number into the correct position. At the end of pass one 2 is in the correct position. Then 3, 4, 5.
Ah! But this is only 3 passes you say. But you're actually incrementing passes regardless of flag, which shows you that you actually did one extra step where the array is sorted (in reverse order) but you didn't know this so you had to go through and double check (this was pass 4).
To improve performance, you do not need to start checking the array from the beginning.
Better than the last equal element.
static int MinimumSwaps(int[] arr)
{
int result = 0;
int temp;
int counter = 0;
for (int i = 0; i < arr.Length; ++i)
{
if (arr[i] - 1 == i)
{
//once all sorted then
if(counter==arr.Length)break;
counter++;
continue;
}
temp = arr[arr[i]-1];
arr[arr[i] - 1] = arr[i];
arr[i] = temp;
result++;//swapped
i = counter ;//needs to start from the last equal element
}
return result;
}
At the start:
{ 3,4,2,5,1}; // passes = 0
Round 1 reuslt:
{ 4,3,2,5,1};
{ 4,3,5,2,1}; // passes = 1
Round 2 reuslt:
{ 4,5,3,2,1}; // passes = 2
Round 3 reuslt:
{ 5,4,3,2,1}; // passes = 3 and flag is set to true
Round 4 reuslt:
{ 5,4,3,2,1}; // same result and passes is incremented to be 4
You fail to mention that the array is supposed to be sorted in descending order, which is usually not the default expected behavior (at least in "C" / C++). To turn:
3, 4, 2, 5, 1
into:
1, 2, 3, 4, 5
one indeed needs 4 (non-adjacent) swaps. However, to turn it into:
5, 4, 3, 2, 1
only two swaps suffice. The following algorithm finds the number of swaps in O(m) of swap operations where m is number of swaps, which is always strictly less than the number of items in the array, n (alternately the complexity is O(m + n) of loop iterations):
int n = 5;
size_t P[] = {3, 4, 2, 5, 1};
for(int i = 0; i < n; ++ i)
-- P[i];
// need zero-based indices (yours are 1-based)
for(int i = 0; i < n; ++ i)
P[i] = 4 - P[i];
// reverse order?
size_t count = 0;
for(int i = 0; i < n; ++ i) {
for(; P[i] != i; ++ count) // could be permuted multiple times
std::swap(P[P[i]], P[i]); // look where the number at hand should be
}
// count number of permutations
This indeed finds two swaps. Note that the permutation is destroyed in the process.
The test case for this algorithm can be found here (tested with Visual Studio 2008).
Here is the solution for your question :)
static int MinimumSwaps(int[] arr)
{
int result = 0;
int temp;
int counter = 0;
for (int i = 0; i < arr.Length; ++i)
{
if (arr[i] - 1 == i)
{
//once all sorted then
if(counter==arr.Length)break;
counter++;
continue;
}
temp = arr[arr[i]-1];
arr[arr[i] - 1] = arr[i];
arr[i] = temp;
result++;//swapped
i = 0;//needs to start from the beginning after every swap
counter = 0;//clearing the sorted array counter
}
return result;
}

Counting sort - implementation differences

I heard about Counting Sort and wrote my version of it based on what I understood.
public void my_counting_sort(int[] arr)
{
int range = 100;
int[] count = new int[range];
for (int i = 0; i < arr.Length; i++) count[arr[i]]++;
int index = 0;
for (int i = 0; i < count.Length; i++)
{
while (count[i] != 0)
{
arr[index++] = i;
count[i]--;
}
}
}
The above code works perfectly.
However, the algorithm given in CLRS is different. Below is my implementation
public int[] counting_sort(int[] arr)
{
int k = 100;
int[] count = new int[k + 1];
for (int i = 0; i < arr.Length; i++)
count[arr[i]]++;
for (int i = 1; i <= k; i++)
count[i] = count[i] + count[i - 1];
int[] b = new int[arr.Length];
for (int i = arr.Length - 1; i >= 0; i--)
{
b[count[arr[i]]] = arr[i];
count[arr[i]]--;
}
return b;
}
I've directly translated this from pseudocode to C#. The code doesn't work and I get an IndexOutOfRange Exception.
So my questions are:
What's wrong with the second piece of code ?
What's the difference algorithm wise between my naive implementation and the one given in the book ?
The problem with your version is that it won't work if the elements have satellite data.
CLRS version would work and it's stable.
EDIT:
Here's an implementation of the CLRS version in Python, which sorts pairs (key, value) by key:
def sort(a):
B = 101
count = [0] * B
for (k, v) in a:
count[k] += 1
for i in range(1, B):
count[i] += count[i-1]
b = [None] * len(a)
for i in range(len(a) - 1, -1, -1):
(k, v) = a[i]
count[k] -= 1
b[count[k]] = a[i]
return b
>>> print sort([(3,'b'),(2,'a'),(3,'l'),(1,'s'),(1,'t'),(3,'e')])
[(1, 's'), (1, 't'), (2, 'a'), (3, 'b'), (3, 'l'), (3, 'e')]
It should be
b[count[arr[i]]-1] = arr[i];
I'll leave it to you to track down why ;-).
I don't think they perform any differently. The second just pushes the correlation of counts out of the loop so that it's simplified a bit within the final loop. That's not necessary as far as I'm concerned. Your way is just as straightforward and probably more readable. In fact (I don't know about C# since I'm a Java guy) I would expect that you could replace that inner while-loop with a library array fill; something like this:
for (int i = 0; i < count.Length; i++)
{
arrayFill(arr, index, count[i], i);
index += count[i];
}
In Java the method is java.util.Arrays.fill(...).
The problem is that you have hard-coded the length of the array that you are using to 100. The length of the array should be m + 1 where m is the maximum element on the original array. This is the first reason that you would think using counting-sort, if you have information about the elements of the array are all minor that some constant and it would work great.

Categories

Resources