Facebook
From Cukrowicz, 6 Years ago, written in C#.
This paste is a reply to Krawędzie grafu from Cukrowicz - go back
Embed
Viewing differences between Krawędzie grafu and Re: Krawędzie grafu
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace GraphEdgeCount
{
    class Program
    {
        static int MatrixSize = 10000;
        static bool[,] Matrix = new bool[10000,10000];
        static int Edges=0;
       static Semaphore semaphoreObject = new Semaphore(initialCount: 1, maximumCount: 1, name: "Edge");
        static void FillMatrix()
        {
            Random c = new Random();
            for(int i=0;i             {
                for(int j=0;j                 {
                    if(c.Next(0,10)%2==0)
                    {
                        Matrix[i,j] = true;
                        Matrix[j,i] = true;
                    }                   
                }
            }
        }
        static void AddToEdgesCount(int number)
        {
            semaphoreObject.WaitOne();
            Edges += number;
            semaphoreObject.Release();
        }
        static void CountEdgesFromNode(int NodeNumber)
        {
            int pom = 0;
            for(int i=NodeNumber+1;i             {
                if (Matrix[i, NodeNumber] == true)
                    pom++;
            }
             AddToEdgesCount(pom);
        }
        static void CountAllEdges()
        {
            for (int i = 0; i < MatrixSize-1; i++)
            {
                for(int j=i+1;j                 {
                    if (Matrix[i, j] == true)
                        Edges++;
                }

            }
        }
        static void DisplayMatrix()
        {
            for (int i = 0; i < MatrixSize; i++)
            {
                for (int j = 0; j                  {
                    if (Matrix[i,j]==true)
                        Console.Write(1);
                    else
                        Console.Write(0);
                }
                Console.WriteLine();
            }
        }
        static void Main(string[] args)
        {
            FillMatrix();
            List threadList = new List();
            //    DisplayMatrix();
            Stopwatch parallel = new Stopwatch();
            Stopwatch seq = new Stopwatch();

            parallel.Start();
            for (int i=0;i             {
                int n = i;
                Thread t = new Thread(() => CountEdgesFromNode(n));
                t.Start();
                threadList.Add(t);
            }
            foreach (var t in threadList)
                t.Join();

            parallel.Stop();
            Console.WriteLine("{0} Edges counted in parallel, it took {1}",Edges, parallel.Elapsed);
            Edges = 0;
            seq.Start();
            CountAllEdges();
            seq.Stop();
            Console.WriteLine("{0} Edges counted sequentially, it took {1}", Edges, seq.Elapsed);
        }
    }
}