Importance of Big O notation

September 2, 2009 at 10:27 am | In Hacking, Programming | Leave a Comment
Tags: , , , , , ,

From a longer time I used to think that Big O notation O(n) , isn’t really much practical. I was reading section 1.4 of Data Structures and Algorithms by Aho, Hocraft and Ullman and was unable to understand how big O notation was being discussed. I asked for help on comp.programming and saw not many people agree with me. I hope this discussion will help you broaden your programming skills

Here is the relevant discussion. It is not the full discussion, it is filtered. I have posted here only those replies the ones I trust. Any posts, that I don’t trust or I have no idea about whether to trust them or not, are not included:

Original Question by me:

This is from “Data Structures and Algorithms” by Aho, Hopcraft and
Ullman, Page 31, Example 1.4

Suppose T(0) = 1 and T(1) = 4, and in general T(n) = (n+1)^2 . Then we
see that T(n) is O(n^2) .

I did not get the last line. If T(n) = (n+1)^2 , how it can be deduced
that it is T(n) = O(n^2). n^2 is never equal to (n+1)^2

if this discussion of the authors is only theoretical. I mean if can
understand Data Structures and Algorithms and write code without
understanding this section, then please tell me, I will leave it as its f
no use at a practical level.

Replies:

> This is from “Data Structures and Algorithms” by Aho, Hopcraft and
> Ullman, Page 31, Example 1.4

> Suppose T(0) = 1 and T(1) = 4, and in general T(n) = (n+1)^2 . Then we
> see that T(n) is O(n^2) .

> I did not get the last line. If T(n) = (n+1)^2 , how it can be deduced
> that it is T(n) = O(n^2). n^2 is never equal to (n+1)^2

That’s the very point of the O(.) equivalence classes!

It doesn’t matter whether you’re considering n or n+1, the question is
that you’re on the same curve, the square curve, and that sooner or
later you will go beyond resources.

Well, on a square curve you will go beyond resources much sooner than
on a linear curve. And on an exponential curve, you’ll go beyond
resources much much much sooner.

Read again http://en.wikipedia.org/wiki/Big_O_notation

T(n) = O(n²) as n->∞
∃M∈ℝ, ∃n₀∈ℝ, ∀n, n>n₀ => |T(n)| ≤ M|n²|

Now, T(n) = (n+1)² = n²+2n+1 ≤ n²+2n²+1n² = 4n², so you can take M=4, and

∃n₀∈ℝ, ∀n, n>n₀ => |T(n)| ≤ 4|n²|
=> ∃M∈ℝ, ∃n₀∈ℝ, ∀n, n>n₀ => |T(n)| ≤ M|n²|

> if this discussion of the authors is only theoretical. I mean if can
> understand Data Structures and Algorithms and write code without
> understanding this section, then please tell me, I will leave it as it IS OF
> no use at a practical level.

Yes, it is of use at a practical level. Being able to do complexity
analysis on your algorithms, will let you know whether your program
will have enough time and enough memory to process the user data.

It can always work on the two or three data items you use in your
tests. But what will happen when the users will feed it a thousands,
a million or a billion data items to process? Will it still be as
fast and and thrifty, or will it need 500 ExaBytes of RAM and 15
billion years of processing time?


__Pascal Bourguignon__

—————————————————————-

> This is from “Data Structures and Algorithms” by Aho, Hopcraft and
> Ullman, Page 31, Example 1.4

> Suppose T(0) = 1 and T(1) = 4, and in general T(n) = (n+1)^2 . Then
> we see that T(n) is O(n^2) .

I assume you mean that T() is a function – something like:

fun T(integer n)
integer d = n + 1
yield d * d
nuf

> I did not get the last line. If T(n) = (n+1)^2 , how it can be
> deduced that it is T(n) = O(n^2). n^2 is never equal to (n+1)^2

Big-O is a rough-and-ready measure of worst-case workload in relation
to the amount of data that must be processed.

Multiply 123 by itself *by hand*, showing all your working and taking
no shortcuts, and count how many separate multiplications you have to
do. Now repeat the process for a four-digit number, eg 9287. Then do
it for a five-digit number, and so on up to ten digits or so. Plot
your results in a graph, and observe the shape of that graph.

For 123, for example (and remember we are not taking any shortcuts),
we need to multiply 3 * 3, 3 * 2, 3 * 1, 2 * 3, 2 * 2, 2 * 1, 1 * 3,
1 * 2, and 1 * 1. We also need some shifts and adds, but never mind
those. Nine multiplications for a datum three digits wide. For four
digits, we have to do sixteen multiplications. And so on.


Richard Heathfield
Email: -http://www. +rjh@
“Usenet is a strange place” – dmr 29 July 1999
Sig line vacant – apply within

—————————————————————-

>I did not get the last line. If T(n) = (n+1)^2 , how it can be deduced
>that it is T(n) = O(n^2). n^2 is never equal to (n+1)^2

One way to think of this is that you’re usually interested in how well
an algorithm scales when n gets large, and when n is large, n^2 and
(n+1)^2 become more and more identical.

>if this discussion of the authors is only theoretical. I mean if can
>understand Data Structures and Algorithms and write code without
>understanding this section, then please tell me, I will leave it as its f
>no use at a practical level.

I’m going to go with “you really should understand this”.

I was on a project (I was doing the front-end, another guy was doing the
back end), and things were working swimmingly–until we opened the site
for beta. As soon as we got about 2000 registered users, the DB died
under the load. Now, mind you, this wasn’t 2000 simultaneous
users–this was just 2000 users registered.

I never found out exactly what was happening in the back-end code, but I
suspect there was some kind of O(N^2) process (or worse) running back
there. It was fixed, and the site went on to get 100K or so users
during its short lifespan (it was a site to market another product.)

But you have my sympathies, because there’s so much completely accurate instruction on this topic that is utterly impenetrable to most
newcomers. (In their defense, it is a difficult concept to present
well.) Here are a few links that seem popular and not too bad (I only
skimmed them):

http://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/
http://www.perlmonks.org/?node_id=573138
http://www.perlmonks.org/?node_id=227909

HTH,
-Beej

 

 


Copyright © 2006, 2007, 2008 Arnuld Uttre, #331/type-2/sector-1, Naya Nangal, Distt. – Ropar, Punjab (INDIA) – 140126

Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.

The Craft of Programming

August 31, 2009 at 11:16 am | In Patterns, Programming | Leave a Comment
Tags: , , , , , ,

Here are some quotes I have gathered over the years. They are written by some of the best known Programmers and Hackers with occasionally some very good programmers thrown in, if not great. They inspired me, changed the way I look at programming and especially at some programming languages and methods to solve problems:

“Any sufficiently complicated C or Fortran program contains an ad hoc informally-specified bug-ridden slow implementation of half of Common Lisp.” Philip Greenspun


“Something we didn’t want was an object-oriented language. OO languages remain a popular fad, but our experience using C++ in the EROS system was that it actively got in the way of understanding what was going on.”

The Origins of the BitC Programming Language


When you want to use a language that gets compiled and runs at high speed, the best language to use is C. Using another language is like using a non-standard feature.
GNU Coding Standards


“First off, I’d suggest printing out a copy of the GNU coding standards,
and NOT read it. Burn them, it’s a great symbolic gesture.”
Linux kernel coding guidelines


“C++ will rot your brain”
— someone from #lisp at irc.freenode.net


“pointer arithmetic and array indexing [that] are equivalent in C, pointers and arrays are different.” Wayne Throop


“An array is not a pointer, nor vice versa” — Steve Summit in C FAQs


“Attitude is no substitute for competence”
— Eric S. Raymond in How to become a Hacker


Q: I’m having problems with my Windows software. Will you help me?

A: Yes. Go to a DOS prompt and type “format c:”. Any problems you are experiencing will cease within a few minutes.

Eric S. Raymond


“This answer cannot be decided by current law—the law should conform to ethics, not the other way around” — Richard M. Stallman


“Lisp is a programmable programming language.”
John Foderaro, CACM, September 1991

Q: “My company needs a proprietary operating system to get a competitive edge.”

A: GNU will remove operating system software from the realm of competition. You will not be able to get an edge in this area, but neither will your competitors be able to get an edge over you. You and they will compete in other areas, while benefiting mutually in this one.


“There is nothing wrong with wanting pay for work, or seeking to maximize one’s income, as long as one does not use means that are destructive. But the means customary in the field of software today are based on destruction.” — Richard M. Stallman


“Haskell saves lives


“In general, functional languages offer powerful new ways to encapsulate abstractions” — Haskell Wiki


“I invented the term ‘Object-Oriented’, and I can tell you I did not have C++ in mind.”
— Alan Kay.


“C++ is the only current language making COBOL look good”
Bertrand Meyer


“It’s 5.50 a.m…. Do you know where your stack pointer is ?”
— Anonymous


“I understand the philosophy that developer cycles are more important than cpu cycles, but frankly that’s just a bumper-sticker slogan and not fair to the people who are complaining about performance.”
Joel Spolsky


“The standard — either one — is not the End of All C. Writing ‘strictly conforming’ C code, however, has an enormous benefit.”
Chris Torek


“Wait a minute, I want to modify that statement. I’m not claiming, in this particular article, that there’s anything wrong with Java as an implementation language. There are lots of things wrong with it but those will have to wait for a different article.” — Joel Spolsky


“Without understanding functional programming, you can’t invent MapReduce, the algorithm that makes Google so massively scalable”
— Joel Spolsky


“It [Java] might be successful – after all, MS DOS was – and it might be a profitable thing for all your readers to learn Java, but it has no intellectual value whatsoever. Look at their implementation of hash tables. Look at the sorting routines that come with their “cool” sorting applet. ”
– Alexander Stepanov


“Java isn’t platform independent; it is a platform. Like Windows, it is a proprietary commercial platform. ”
Bjarne Stroustrup

 

 


Copyright © 2006, 2007, 2008 Arnuld Uttre, #331/type-2/sector-1, Naya Nangal, Distt. – Ropar, Punjab (INDIA) – 140126

Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.

KDE and the new friend

August 5, 2009 at 5:12 pm | In community | Leave a Comment
Tags: , , , ,

I was just browsing through net and I came across this article. I was a little shocked on reading just the title. Why would anyone want to leave GNU software ? .. So it sparked my interest and I spent next 30 minutes reading the whole article (or rather technical viewpoint of KDE team). That did not convince me though in anyway.

I agree that autotools are very old but so is gcc and so are other basic tools that come with all distros. Reading the article convinced me of one thing, not many people on the KDE team know much about autotools. They have used one small part (Makefile) of it but they don’t know what exactly the autotools are and how they work. I even somehow got a feeling that many KDE developers are scared of autotools. Reason: autotools have a longer and hard learning curve. It is true, I agree. I was able to write a CMakeLists.txt in just a day, right from the scratch without any book or help except the online documentation available at their web-site, while with autoconf, the story was different, even after 2 days of work, I was unable to came with with anything. Autoconf manual felt like a classic GNU manual written in the spirit of GNU by people who are extremely technical. It pinned me, it binned me and it made my heart bleed. Are most of KDE developers scared of autotools ? I don’t know and after reading their reasoning , I will say yes. Most of them wanted a short and less painful path to software building: here come Scons and CMake.

I was also just put off by the technical-expertise either required as a prerequisite or being used to read the manual but was I scared …… hell no…. . Say no to the hell and yes to the reasoning and rationality. There glows the bulb on my head, the people who write these kind of manuals are very good at basics , they are the people who have gotten their fundamentals straight, with experience. Reading their manuals always gives you an insight into the things. This is what exactly what you will learn from Newsgroups.

I am not saying that KDE people need to switch back to autotools. No, I am not agreeing on that. All I am saying is, switching to a different tools needs to be based on purely technical reasons, not on the basis that people can’t learn them because they are scared. I really wonder they are not trying waf, which actually does not try to fix the problems of other tools but rather built as a software-construction framework.

I, myself, want to use waf but then I have to learn Python in order to debug my scripts which I don’t want to do. If I ever get time to learn a new language, I will learn Common Lisp. So rather I will start learning autotools again and will see what people are scared of.

 

 


Copyright © 2006, 2007, 2008 Arnuld Uttre, #331/type-2/sector-1, Naya Nangal, Distt. – Ropar, Punjab (INDIA) – 140126

Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.

High-Tech fraud and Optima Resources

July 21, 2009 at 12:02 pm | In community | 1 Comment
Tags: , , ,

One or two years ago, I paid $4500 to Optima Resources for H1B visa. You heard right, they were selling H1B for $4500. I had a talk with a woman named Richa (I am not sure but may be her actual name is Ankita) who sent me an email on this and initiated this visa contract. She told me this at that time:


“We will process your H1 VISA and if you are able to get it, you will pay 20% of your salary for 18 months as our fee and your $4500 will be returned back to you in that same 18 months period. If your VISA application does not get approved, we will cut 25,000 INR from the amount you paid and will refund the rest.”

Now, my VISA was not approved and hence they cut 25,000 rupees and refunded me the rest. If there were no friend of mine in USA, I am sure they may not have refunded my money. My friend actually paid them the money, he lives in US and hence may be they did not have any option other than to return the money. I think I learned 2 lessons:

  1. I think they send emails to almost everyone in the India and lets say they get 1000 applications then they process some applications of say 3-10 applicants (just a wild guess) and get them the VISA and cut 25,000 rupees in the name of expenses from rest of the 990 applicants. That makes: 25,000 x 990 = 2,47,50,000 of rupees , That is a big money my friend.
  2. If you want to have a visa, go by real, legal rules. Don’t ever try to use either illegal or legal but stupid means. Always respect the hard-earned money of yours and especially of your parents.

If you feel trouble believing that Optima Resources is actually a fraud, you can see this list of 25 domain names that trace back to the IP address of Optimaresources:

Optimaresources.com
Optimaresourcesapplicationdevelopmentandmaintainence.com
Optimaresourcesbusinessconsulting.com
Optimaresourcesclients.com
Optimaresourcescontactus.com
Optimaresourcescorecompetencies.com
Optimaresourcescorevalues.com 0 listings
Optimaresourcescurrentopenings.com
Optimaresourceshome.com
Optimaresourceshouseofexcellence.com
Optimaresourcesmission.com
Optimaresourcesofferings.com
Optimaresourcesoverview.com
Optimaresourcesprocess.com
Optimaresourcesqms.com
Optimaresourcesquality.com
Optimaresourcesranddcenter.com
Optimaresourcessitemap.com
Optimaresourcesstrength.com
Optimaresourcestechnology.com
Optimaresourcestechnologyinfrastructureservices.com
Optimaresourcestestimonials.com
Optimaresourceswhy.com
Optimaresourcesyou.com

go and map the IP addresses (I used http://dawhois.com/ ). No genuine company keeps so many domain names mapping back to same IP address. When I asked Richa about this, she said “This is none of your business”. Well, I say, it is your business because you are paying lacs of rupees to them. 2nd, you will always have to call them from India to US at your own expanses, they never call you. I never heard any company doing like that. Now many of my friends are placed in countries like Australia, Canada and Switzerland, all of the time, companies called them, they mailed them, they never asked my friends (their future employee) to call back. 3rd, when I told Rich that I do know C++ and Linux and I do have C++ skills but I don’t have any industry experience. She asked if I can get some fake experience/certificate or something. is this how a corporation works ? Even after years of this I am still wondering how did they get the accreditation of Better Business Bureau in a country like US ?

you can see lots of threads on fraudwatchers for the other companies who do this kind of fraud business. Keep your mind open and you will be saved.

 

 


Copyright © 2006, 2007, 2008 Arnuld Uttre, #331/type-2/sector-1, Naya Nangal, Distt. – Ropar, Punjab (INDIA) – 140126

Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.

A LIFO in C using Singly-Linked List

May 25, 2009 at 11:03 am | In Hacking | Leave a Comment
Tags: , ,

Here is the implementation of a LIFO in C using Singly-Linked list. Remember pop() may look different than what you will read in an academic book. Actually, I think if my program malloc()s some memory then its my responsibility to free() it while some others may not agree on that and will stress that pop() should only give the element, not free() it. Based on my experience with programming I completely disagree, when you give someone a program and tell him what it does, the other person uses it like a black-box, he does not know what is inside the program or how it is working, he just needs to know what this programs requires as input and what is its output. In the end, the only thing he needs to know is it should work fine without giving any strange errors on his LCD monitor. Thats it, and that shifts all the responsibility of handling the program back to the programmer, not the user, which is inherently a good idea I think. If you are a C programmer and you don’t know where your memory is going and what the pointers are doing and where they are at one point in your program, you should forget working in C. As usual, you can always find the original discussion on Usenet.

/* A Stack implementation of a singly linked list with 4 operations: Pop, Push, Top and Print elements in the list.
 *
 * VERISON 0.4
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum { STACK_ARR_SIZE = 10 };  

struct my_stack
{
  char arrc[STACK_ARR_SIZE]; /* Thar array must not have more than (STACK_ARR_SIZE - 1) elements */
  struct my_stack* next;
};

struct stack_list
{
  struct my_stack* head;
};

struct stack_list* push( struct stack_list*, const char* );
struct stack_list* pop( struct stack_list* );
struct my_stack* top( struct stack_list* );
struct my_stack* make_null( struct stack_list* );
struct my_stack* is_empty( struct stack_list* );

struct stack_list* stack_new( void );
void stack_print( const struct stack_list* );
void stack_print_element( const struct my_stack* );
struct stack_list* stack_free( struct stack_list* );

int main( void )
{
  struct my_stack* p = NULL;
  struct stack_list* ms = NULL;
  ms = stack_new();

  stack_print(ms);

  push(ms, "comppppppppppppppppp");
  push(ms, "(dot)");
  push(ms, "lang");
  push(ms, "(dot)");
  push(ms, "c");

  stack_print(ms);

  pop(ms);
  p = top(ms);

  stack_print(ms);
  stack_free(ms);
  free(ms);
  ms = NULL;

  return 0;
}

struct stack_list* push(struct stack_list* s, const char* c )
{
  struct my_stack* p = malloc( 1 * sizeof *p );

  if( NULL == p )
    {
      fprintf(stderr, "malloc() failed\n");
      return s;
    }

  /* If use gave us more characters than what we want, then its his problem */
  if( strlen(c) < STACK_ARR_SIZE ) strcpy(p->arrc, c);
  p->next = NULL;

  if( NULL == s )
    {
      fprintf(stderr, "Stack not initialized ?\n");
      free(p);
      return s;
    }
  else if( NULL == s->head )
    {
      /*      printf("Stack is Empty, adding first element\n"); */
      s->head = p;
      return s;
    }
  else
    {
      /*      printf("Stack not Empty, adding in front of first element\n"); */
      p->next = s->head;
      s->head = p;  /* push new element onto the head */
    }

  return s;
}

struct stack_list* pop( struct stack_list* s )
{
  struct my_stack* p = NULL;

  if( NULL == s )
    {
      printf("There is no stack list ?\n");
    }
  else if( NULL == s->head )
    {
      printf("There is no element on the stack\n");
    }
  else
    {
      p = s->head;
      s->head = s->head->next;
      free(p);
    }

  return s;
}

struct my_stack* top( struct stack_list* s)
{
  if( NULL == s )
    {
      printf("There is no stack list ?\n");
      return NULL;
    }
  else if( NULL == s->head )
    {
      printf("There is no element on the stack\n");
    }

  return s->head;
}

/* Make a Stack empty */
struct my_stack* make_null( struct stack_list* s )
{
  if( NULL == s )
    {
      printf("Can not make NULL when there is no Stack List\n");
      return NULL;
    }
  else if( NULL == s->head )
    {
      printf("Stack is already Empty\n");
    }
  else
    {
      stack_free(s);
    }

  return s->head;
}

struct my_stack* is_empty( struct stack_list* s )
{
  if( NULL == s )
    {
      printf("There is no Stack\n");
      return NULL;
    }
  else if( NULL == s->head )
    {
      printf("Stack is Empty\n");
    }
  else
    {
      printf("Stack is not Empty\n");
    }

  return s->head;
}

/* ---------- small helper functions -------------------- */
struct stack_list* stack_free( struct stack_list* s )
{
  if( NULL == s )
    {
      printf("Can't free a NULL stack list\n");
    }

  while( s->head ) pop(s);

  return s;
}

struct stack_list* stack_new( void )
{
  struct stack_list* p = malloc( 1 * sizeof *p );

  if( NULL == p )
    {
      fprintf(stderr, "malloc() in Stack Initialization failed\n");
      exit( EXIT_FAILURE );  /* There is no point in going beyond this point */
    }

  p->head = NULL;

  return p;
}

void stack_print( const struct stack_list* s )
{
  struct my_stack* p = NULL;

  if( NULL == s )
    {
      printf("Can not print an Empty Stack\n");
    }
  else
    {
      for( p = s->head; p; p = p->next ) stack_print_element(p);
    }

  printf("-------------------------- \n");
}

void stack_print_element(const struct my_stack* s)
{
  if( s ) printf("arrc = %s\n", s->arrc);
}

 

 


Copyright © 2006, 2007, 2008 Arnuld Uttre, #331/type-2/sector-1, Naya Nangal, Distt. – Ropar, Punjab (INDIA) – 140126

Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.

Next Page »

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.