[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci202] / alg
[Text Version] [Syllabus] [Schedule] [Glossary] [Resources] [Grading] [Contact] [Question] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Mon Jun 1 07:50:24 PDT 2009

Contents


    Algorithms

      What is an algorithm?

      An algorithm is a clearly described procedure that tells you how to solve a problem. Without a well defined problem to solve an algorithm is not much use.

      For example, if the problem is to find a match sock in a pile of similar socks that matches a given sock, then an algorithm is to take every sock in turn and see if it matches the given sock, and if so stop. This algorithm is called a linear search.

      Exercise: get two packs of cards an shuffle each one. Take one card from the first pack and search through the other pack to find the matching card. Repeat this until you get a feel for how it performs.

      If the problem is putting 14 socks away in matching pairs then one algorithm is to (1) clear a place to layout the unmatched socks, (2) for each sock in turn, take it to each unmatched sock and if they match put them both away as a pair. But, if the new sock matches none of them unmatched socks, add it to the unmatched socks. I call this the "SockSort" algorithm.

      Exercise. Get a pack of card and extract the Ace,2,3, .. 7 of the hearts and clubs. Shuffle them together. Now run the SockSort algorithm to pair them up by rank: Ace-Ace, 2-2, ... .

      If you had an array of 7 numbered boxes you could sort the cards faster than sorting the socks. Perhaps I should put number tags on my socks?

      So, changing what we are given, changes the problem, and so changes the best algorithm to solve it.

      Here is another example. Problem: to find a person's name in a telephone directory. Algorithm: use your finger to split the phone book in half. Pick the half that contains the name. Repeatedly, split the piece of the phone book that contains the name in half, ... This is a binary search algorithm.

      Here is another example using the same data as the previous one: Find my neighbor's phone number. Algorithm: look up my address using binary search, then read the whole directory looking for the address that is closest to mine.

      If we change the given data, the problem of my neighbor's phone number has a much faster algorithm. All we need is copy of the census listing of people by street and it is easy to find my neighbors name and so phone number.

      A problem best expressed in terms of

      Why should I learn about algorithms?

      1. Your knowledge will be tested in CSci 202, and all dependent CSci classes.
      2. Job interviews test your knowledge of algorithms.
      3. You can do things better if you know a better algorithm.
      4. Inventing and analyzing algorithms is a pleasant hobby.
      5. Publishing a novel algorithm can make you famous. Several Computer Scientists started their career with a new algorithm.

      What do I need to learn in CSci202 about algorithms?

      1. Know the definition above.
      2. Recognize well known problems.
      3. Recognize well known algorithms.
      4. Code an algorithm expressed in structured English.
      5. Know when to use an algorithm and where they relate to objects and classes,

      Where does the word algorithm come from?

      The word is a corruption of the name of a mathematician born in Khwarizm in Uzbekistan in the 820's(CE). Al-Khwarizmi (as he was known) was one of the first inducted into the "House of Wisdom" in Baghdad where he worked on algebra, arithmetic, astronomy, and solving equations. His work had a strongly computational flavor. Later, in his honor, medieval mathematicians in Europe called similar methods "algorismic" and then, later, "algorithmic". [pages 113-119, "The Rainbow of Mathematics", Ivor Grattan-Guiness, W W Norton & Co, NY, 1998]

      Should I use the standard algorithms in C++?

      If you are using C++ and understand the ideas used in the C++ <algorithm> library you can save a lot of time. Otherwise you will have to reinvent the wheel. As a rule, the standard algorithm will be faster than anything you could quickly code to do the same job. However, you still need to understand the theory of algorithms and data structures to be able to use them well.

      Bjarne Stroustroup, the developer of C++, has written a very comprehensive and deep introduction to the standard C++ library as part of his classic C++ book. This book is in the CSUSB library.

      As a general rule: a practical programmer starts searching the library for their language for known algorithms.

      Is there an easy way to grasp a new algorithm?

      No... unless the algorithm is simple or the author very good at writing.

      I find that a deck of playing cards is very helpful for sorting and searching algorithms.

      A pencil and paper is useful for doing a dry run of an algorithm. With a group, chalk and chalk board is very helpful.

      Programming a computer tends to take too long but is good for gathering statistics on the performance of algorithms.

      When there are loops it is well worth looking for things that the body of the loop does not change. They are called invariants. If an invariant is true before the loop, then it will be true afterward as well. You can often figure out precisely what an algorithm does by noting what it does not change...

      Are there any common problems that have algorithmic solutions?

      The two classic problems are called Searching and Sorting. In searching we are given a collection of objects and need to find one that passes some test. For example: finding the student whose student Id number ends "1234". In sorting, we are given a collection and need to reorganize it according to some rule. An example: placing a grade roster in order of the last four digits of student identifier,

      Finding the root of an equation: is this a search or a sort?

      You can't find things in your library so you decide to place the books in a particular order: is this a search or a sort?

      Optimization problems are another class of problems that have attracted a lot of attention. Here we have a large number of possibilities and want to pick the best. For example: what dress is the most attractive in a given price range? What is the shortest route home from the campus? What is the smallest amount of work needed to get a B in this class?

      What types of algorithm are there?

    Abbreviations

  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. class::="A description of a set of similar objects that have similar data plus the functions needed to manipulate the data".
  3. Data_Structure::=A small data base.
  4. Function::programming=A selfcontained and named piece of program that knows how to do something.
  5. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  6. KDE::="Kommon Desktop Environment".
  7. object::="A little bit of knowledge -- some data and some know how", and instance of a class".
  8. OOP::="Object-Oriented Programming", Current paradigm for programming.
  9. Semantics::=Rules determining the meaning of correct statements in a language.
  10. SP::="Structured Programming", a previous paradigm for programming.
  11. STL::="The standard C++ library of classes and functions" -- also called the "Standard Template Library" because many of the classes and functions will work with any kind of data.
  12. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  13. Q::software="A program I wrote to make software easier to develop",
  14. TBA::="To Be Announced", something I should do.
  15. TBD::="To Be Done", something you have to do.
  16. UML::="Unified Modeling Language".
  17. void::C++Keyword="Indicates a function that has no return".

End