Click here to Skip to main content
15,884,879 members
Articles / Programming Languages / XSLT

Big O Algorithm Analyzer for .NET

,
Rate me:
Please Sign up or sign in to vote.
4.88/5 (20 votes)
16 Feb 2010CPOL10 min read 115.9K   1.7K   78  
A heurisitc graphing tool to help discover 'Big O Notation' function thru infinite asymptotic's and instrumentation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WPFBigOAnalyzer;
namespace WPFBigOAnalyzer.BigOFunction
{
    class Functions
    {
        #region Linear Functions

        //O(1)
        static double n1(int n)
        {
            Random r = new Random(n);

            return r.Next(Convert.ToInt32(WPFBigOAnalyzer.AnalysisFactory.Min),Convert.ToInt32(WPFBigOAnalyzer.AnalysisFactory.Max));
        }

        public static DecoratedMarker n1Line(int n)
        {
            double[] ret = new double[n];

            for (int y = 0; y < n; y++)
            {
                if (y == 0)
                    ret[y] = Functions.n1(y);
                else
                    ret[y] = Functions.n1(y) + ret[y - 1];
            }

            return new DecoratedMarker(ret,"O(1)",null);
        }

        //O(n)
        static double n(int n)
        {
            return n*2;
        }

        public static DecoratedMarker nLine(int n)
        {
            double[] ret = new double[n];

            for (int y = 0; y < n; y++)
                ret[y] = Functions.n(y);

            return new DecoratedMarker(ret, "O(n)", null);
        }

        #endregion

        #region Log Functions

        //O(n log n)
        static double nLogn(int n)
        {
            double r = Math.Log(n) * n;

            if (r.ToString() == "NaN" || r.ToString().IndexOf("Infinity") != -1)
                return 0;
            else
                return r;
        }

        public static DecoratedMarker nLognLine(int n)
        {
            double[] ret = new double[n];

            for (int y = 0; y < n; y++)
                ret[y] = Functions.nLogn(y);

            return new DecoratedMarker(ret, "O(n log n)", null);
        }
        //O(log n)
        static double logn(int n)
        {
            double r = Math.Log(n);

            if (r.ToString() == "NaN" || r.ToString().IndexOf("Infinity") != -1)
                return 0;
            else
                return r;
        }
        public static DecoratedMarker lognLine(int n)
        {
            double[] ret = new double[n];

            for (int y = 0; y < n; y++)
                ret[y] = Functions.logn(y);

            return new DecoratedMarker(ret, "O(log n)", null);
        }

        //O(log log n)
        static double loglogn(int n)
        {
            double r = Math.Log(Math.Log(n));

            if (r.ToString() == "NaN" || r.ToString().IndexOf("Infinity") != -1)
                return 0;
            else
                return r;
        }
        public static DecoratedMarker loglognLine(int n)
        {
            double[] ret = new double[n];

            for (int y = 0; y < n; y++)
                ret[y] = Functions.loglogn(y);

            return new DecoratedMarker(ret, "O(log log n)", null);
        }        
        #endregion

        #region Exponential Functions

        //O(n^2)
        static double nPow2(int n)
        {
            double r = Math.Pow(n, 2);

            if (r.ToString() == "NaN" || r.ToString().IndexOf("Infinity") != -1)
                return 0;
            else
                return r;
        }
        public static DecoratedMarker nPow2Line(int n)
        {
            double[] ret = new double[n];

            for (int y = 0; y < n; y++)
                ret[y] = Functions.nPow2(y);

            return new DecoratedMarker(ret, "O(n^2)", null);
        }

        //O(n^3)
        static double nPow3(int n)
        {
            double r = Math.Pow(n, 3);

            if (r.ToString() == "NaN" || r.ToString().IndexOf("Infinity") != -1)
                return 0;
            else
                return r;
        }
        public static DecoratedMarker nPow3Line(int n)
        {
            double[] ret = new double[n];

            for (int y = 0; y < n; y++)
                ret[y] = Functions.nPow3(y);

            return new DecoratedMarker(ret, "O(n^3)", null);
        }
        //O(n^n)
        static double nPown(int n)
        {
            double r = Math.Pow(n, n);

            if (r.ToString() == "NaN" || r.ToString().IndexOf("Infinity") != -1)
                return 0;
            else
                return r;
        }
        public static DecoratedMarker nPownLine(int n)
        {
            double[] ret = new double[n];

            for (int y = 0; y < n; y++)
                ret[y] = Functions.nPown(y);

            return new DecoratedMarker(ret, "O(n^n)", null);
        }
        #endregion

        #region Factorial 
        static double nFractorial(int n)
        {
            double r = Meta.Numerics.Functions.AdvancedIntegerMath.Factorial(n);

            if (r.ToString() == "NaN" || r.ToString().IndexOf("Infinity") != -1)
                return 0;
            else
                return r;
        }
        public static DecoratedMarker nFractorialLine(int n)
        {
            double[] ret = new double[n];

            for (int y = 0; y < n; y++)
                ret[y] = Functions.nFractorial(y);

            return new DecoratedMarker(ret, "O(n!)", null);
        }
        #endregion

        //Not Implimented yet...
        //O(α(n))
        //O((log n)^c)
        //O(n^c)
        //O(n^c)
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I am a .NET developer who works daily on enterprise-scale applications using C#, SQL, XML, ASP.NET, and myriad other technologies. My academic background is in physics and economics.

I am the original architect of Sandcastle managed reference documentation engine and of the Meta.Numerics library for scientific computation.

Written By
CEO AW Proto-Code, Inc.
United States United States
I’m a self learned wiz kid turned Architect. Stared with an Apple IIe, using AppleSoft Basic, ProDos and Begal Basic at age 10.

Picked up LPC in college before the Dot Com Explosion. Wrote some Object C in the USAF for one of my instructors. Got a break from a booming computer manufacture testing server software. Left the Boom and went to work for a Dot Com built from the ashes of Sun Micro in CS. Mentoring in Java solidified me as a professional developer. Danced in the light of the sun for 13 years, before turning to the dark side. An evil MVP mentored me in the ways of C# .NET. I have not looked back since.

Interests include:

~ Windows Presentation Foundation and Silverlight
~ Parallel Programming
~ Instruction Set Simulation and Visualization
~ CPU to GPU code conversion
~ Performance Optimizations
~ Mathematics and Number Theory
~ Domain Specific Languages
~ Virtual Machine Design and Optimization
~ Graphics Development
~ Compiler Theory and Assembler Conversion Methodology
~ CUDA, OpenCL, Direct Compute, Quantum Mechanics

IEEE Associate Member 2000
This is a Organisation (No members)


Comments and Discussions