0
Dr James Mountstephens
Introductions <ul><li>I am Dr James Mountstephens and I will be teaching Algorithm Analysis (AA) this semester </li></ul><...
About The Course  <ul><li>AA is a  crucial  and  useful  course for computer scientists </li></ul><ul><li>AA is a  difficu...
Course Delivery <ul><li>14 Lectures and 14 Tutorials  </li></ul><ul><li>Quizzes (5%)  </li></ul><ul><ul><li>You will be in...
Lectures <ul><li>Introduction </li></ul><ul><li>Fundamentals of the Analysis of Algorithm Efficiency </li></ul><ul><li>Bru...
The Textbook <ul><li>Please read the textbook </li></ul><ul><ul><li>“ Introduction to The Design and Analysis of Algorithm...
Dr James Mountstephens 1: Introduction
Contents <ul><li>What is an Algorithm? </li></ul><ul><li>Fundamentals of Algorithmic Problem Solving </li></ul><ul><li>Imp...
What is an Algorithm? <ul><li>An  algorithm  is a   sequence of unambiguous instructions for solving a problem in a finite...
What is an Algorithm? <ul><li>Algorithms are the  core  of Computer Science </li></ul><ul><ul><li>Computers are  algorithm...
What is an Algorithm? <ul><li>An  algorithm  is a   sequence   of   unambiguous   instructions   for   solving a problem  ...
What is an Algorithm? <ul><li>Algorithms are  not  computer programs </li></ul><ul><ul><li>Programs  represent  and  imple...
Example Problem: Greatest Common Divisor <ul><li>GCD of two integers  m,n  is the largest positive integer that divides th...
Example: Euclid’s Algorithm for GCD <ul><li>Euclid’s Algorithm  for the GCD of two numbers is the earliest recorded algori...
Two Descriptions of Euclid’s algorithm <ul><li>while n ≠ 0 do  </li></ul><ul><li>r ← m mod n </li></ul><ul><li>m← n  </li>...
Other Algorithms for GCD <ul><li>Consecutive Integer Checking Algorithm </li></ul><ul><li>Step 1  Assign the value of min{...
Other Algorithms for GCD <ul><li>Consecutive Integer Checking Algorithm </li></ul><ul><li>Step 1  Assign the value of min{...
Other Algorithms for GCD <ul><li>“ Middle-school” procedure </li></ul><ul><li>Step 1  Find the prime factorisation of  m <...
Other Algorithms for GCD <ul><li>We need an algorithm for  prime factorisation  to make the middle-school procedure into a...
Other Algorithms for GCD <ul><li>“ Middle-school” procedure </li></ul><ul><li>Step 1  Find the  prime factorisation  of  m...
Fundamentals of Algorithmic Problem Solving <ul><li>Understand the Problem </li></ul><ul><li>Ascertain the Capabilities of...
Algorithm Design Techniques <ul><li>An  algorithm design technique  (or “strategy” or “paradigm”) is… </li></ul><ul><ul><l...
Analysing an Algorithm <ul><li>Analysis is essential. We want to know that our algorithms have good characteristics: </li>...
Important Problem Types <ul><li>Sorting </li></ul><ul><li>Searching </li></ul><ul><li>String Processing </li></ul><ul><li>...
Sorting Problems <ul><li>Problem:  rearrange the  items  of a given list in ascending order . </li></ul><ul><ul><li>Input:...
Sorting Problems <ul><li>Examples of sorting algorithms </li></ul><ul><ul><li>Selection Sort </li></ul></ul><ul><ul><li>Bu...
Sorting Problems SelectionSort(A[0..n-1]) //Input: An array A[0..n-1] of orderable elements //Output: Array A[0..n-1] sort...
Searching Problems <ul><li>Problem: find a given value, called a  search key,  in a given set. </li></ul><ul><ul><li>Eg ID...
Searching Problems <ul><li>Examples of searching algorithms </li></ul><ul><ul><li>Sequential search (Time O(n)) </li></ul>...
String Processing Problems <ul><li>A string is a sequence of characters from an alphabet. </li></ul><ul><ul><li>“ This is ...
Graph Problems <ul><li>(Informally) A  graph  is a collection of points called  vertices , some of which are connected by ...
Graph Problems <ul><li>Example:  Travelling Salesman Problem </li></ul><ul><ul><li>For a set of cities linked by roads, fi...
Combinatorial Problems <ul><li>Finding a  Combinatorial Object  that satisfies certain constraints or has certain properti...
Combinatorial Problems <ul><li>Example:  The Knapsack Problem </li></ul><ul><ul><li>Given a set of items, each with a weig...
Geometric Problems <ul><li>Problems dealing with Geometric Objects such as points, lines, polygons </li></ul><ul><li>Examp...
Numerical Problems <ul><li>Problems dealing with mathematical objects </li></ul><ul><ul><li>Numbers, Equations, Matrices… ...
Fundamental Data Structures <ul><li>Linear Data Structures </li></ul><ul><ul><li>Array </li></ul></ul><ul><ul><li>String  ...
Linear Data Structures <ul><li>Arrays </li></ul><ul><ul><li>A sequence of n items of the same data type that are stored co...
Linear Data Structures <ul><li>Linked List </li></ul><ul><ul><li>A sequence of zero or more nodes each containing two kind...
Stacks and Queues <ul><li>Stacks </li></ul><ul><ul><li>A stack of plates  </li></ul></ul><ul><ul><ul><li>insertion/deletio...
Priority Queues and Heaps <ul><li>Priority queues (implemented using heaps) </li></ul><ul><ul><li>A data structure for mai...
Graphs <ul><li>Formal definition </li></ul><ul><ul><li>A  graph   G = <V, E>  is defined by a pair of sets: a finite set  ...
Graph Representation <ul><li>Nodes are said to be  adjacent  if an edge exists between them </li></ul><ul><li>Adjacency Ma...
Weighted Graphs <ul><li>Weighted graphs </li></ul><ul><ul><li>Graphs or digraphs with numbers assigned to the edges. </li>...
Graph Properties -- Paths and Connectivity <ul><li>Paths </li></ul><ul><ul><li>A path from vertex u to v of a graph G is d...
Graph Properties -- Paths and Connectivity <ul><li>Connected graphs </li></ul><ul><ul><li>A graph is said to be connected ...
Graph Properties -- Acyclicity <ul><li>Cycle </li></ul><ul><ul><li>A simple path of a positive length that starts and ends...
Trees <ul><li>Trees </li></ul><ul><ul><li>A tree (or free tree) is a connected acyclic graph. </li></ul></ul><ul><ul><li>F...
Rooted Trees <ul><li>This “single path” property makes it possible to select an arbitrary vertex in a free tree and consid...
Rooted Trees <ul><li>Ancestors </li></ul><ul><ul><li>For any vertex  v  in a tree  T , all the vertices on the simple path...
Rooted Trees <ul><li>Depth of a vertex </li></ul><ul><ul><li>The length of the simple path from the root to the vertex. </...
Ordered Trees <ul><li>Ordered trees </li></ul><ul><ul><li>An ordered tree is a rooted tree in which all the children of ea...
Ordered Trees <ul><li>Binary search trees </li></ul><ul><ul><li>Each vertex is assigned a number. </li></ul></ul><ul><ul><...
Sets and Dictionaries <ul><li>I will assume you already know what a set is! </li></ul><ul><ul><li>Elements, subsets, union...
Upcoming SlideShare
Loading in...5
×

Kk20503 1 introduction

942

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
942
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
79
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Kk20503 1 introduction"

  1. 1. Dr James Mountstephens
  2. 2. Introductions <ul><li>I am Dr James Mountstephens and I will be teaching Algorithm Analysis (AA) this semester </li></ul><ul><li>Who are you? </li></ul><ul><li>I will be teaching in English </li></ul><ul><ul><li>Please tell me if you don’t understand what I am saying </li></ul></ul><ul><li>Some rules for lectures </li></ul><ul><ul><li>Please be punctual. </li></ul></ul><ul><ul><ul><li>Students more than 10 minutes late will not be allowed to enter the lecture or tutorial </li></ul></ul></ul><ul><ul><li>Please, no talking . </li></ul></ul><ul><ul><li>Please ask questions. I will be asking you questions. </li></ul></ul><ul><ul><li>Please think about the material! </li></ul></ul>
  3. 3. About The Course <ul><li>AA is a crucial and useful course for computer scientists </li></ul><ul><li>AA is a difficult course </li></ul><ul><ul><li>I am going to assume that you are intelligent people. </li></ul></ul><ul><ul><li>I will not dumb the subject down and I will discuss complex topics. Again, please ask questions. </li></ul></ul><ul><ul><li>You will fail if you are lazy but each of you can pass if you work hard and really think about the material </li></ul></ul><ul><ul><li>The first two lectures will probably be the hardest …! </li></ul></ul>
  4. 4. Course Delivery <ul><li>14 Lectures and 14 Tutorials </li></ul><ul><li>Quizzes (5%) </li></ul><ul><ul><li>You will be in groups, to be assigned soon </li></ul></ul><ul><li>Group Assignment (15%), Individual Assignment (10%) </li></ul><ul><li>Midterm (20%) and Final Exam (50%) </li></ul><ul><ul><li>All students, please sign up to Opencommittee </li></ul></ul><ul><ul><li>I will post lecture slides, assignments etc. </li></ul></ul><ul><ul><li>I will make announcements so please check regularly </li></ul></ul><ul><ul><li>http://www.opencommittee.com </li></ul></ul><ul><ul><li>Join the committee for this course ( code 102923 ) </li></ul></ul><ul><ul><li>USE YOUR PROPER NAME! </li></ul></ul>
  5. 5. Lectures <ul><li>Introduction </li></ul><ul><li>Fundamentals of the Analysis of Algorithm Efficiency </li></ul><ul><li>Brute Force </li></ul><ul><li>Divide and Conquer </li></ul><ul><li>Decrease and Conquer </li></ul><ul><li>Transform and Conquer I </li></ul><ul><li>Transform and Conquer II </li></ul><ul><li>Space and Time Tradeoffs </li></ul><ul><li>Dynamic Programming </li></ul><ul><li>Greedy Technique </li></ul><ul><li>Iterative Improvement </li></ul><ul><li>Limitations of Algorithmic Power </li></ul><ul><li>Coping with the Limitations of Algorithmic Power </li></ul><ul><li>Revision </li></ul>
  6. 6. The Textbook <ul><li>Please read the textbook </li></ul><ul><ul><li>“ Introduction to The Design and Analysis of Algorithms” by Anany Levitin (Second Edition) </li></ul></ul><ul><ul><li>Expensive but please buy it and read it </li></ul></ul>
  7. 7. Dr James Mountstephens 1: Introduction
  8. 8. Contents <ul><li>What is an Algorithm? </li></ul><ul><li>Fundamentals of Algorithmic Problem Solving </li></ul><ul><li>Important Problem Types </li></ul><ul><li>Fundamental Data Structures </li></ul>
  9. 9. What is an Algorithm? <ul><li>An algorithm is a sequence of unambiguous instructions for solving a problem in a finite time </li></ul><ul><ul><li>Algorithmic problem solving means obtaining a required output for any legitimate input </li></ul></ul>“ computer” problem algorithm input output solution
  10. 10. What is an Algorithm? <ul><li>Algorithms are the core of Computer Science </li></ul><ul><ul><li>Computers are algorithmic machines </li></ul></ul><ul><ul><li>EVERY piece of software that you use is an instance of an algorithm </li></ul></ul><ul><ul><ul><li>Take time to think about that…Google, Facebook, MS Word etc. are all algorithms. </li></ul></ul></ul><ul><li>Every Computer Scientist/Software Engineer/IT person should have a toolkit of known algorithms </li></ul><ul><ul><li>For practically doing your job! </li></ul></ul><ul><ul><li>Provide a framework for designing and analysing algorithms for new problems </li></ul></ul>
  11. 11. What is an Algorithm? <ul><li>An algorithm is a sequence of unambiguous instructions for solving a problem in a finite time </li></ul><ul><ul><li>Solving a problem </li></ul></ul><ul><ul><li>Sequence </li></ul></ul><ul><ul><li>Instructions </li></ul></ul><ul><ul><li>Non-ambiguity </li></ul></ul><ul><ul><li>Finite time </li></ul></ul><ul><ul><li>The range of inputs must be carefully specified </li></ul></ul><ul><li>We can consider algorithms to be procedural solutions to problems </li></ul>
  12. 12. What is an Algorithm? <ul><li>Algorithms are not computer programs </li></ul><ul><ul><li>Programs represent and implement algorithms </li></ul></ul><ul><li>The same algorithm may be represented in several different ways </li></ul><ul><ul><li>Natural language, Programming language, Pseudocode, flowcharts… </li></ul></ul><ul><li>Several different algorithms for solving the same problem may exist </li></ul><ul><ul><li>Each may be based on different design ideas and have very different performance </li></ul></ul>
  13. 13. Example Problem: Greatest Common Divisor <ul><li>GCD of two integers m,n is the largest positive integer that divides them both with no remainder </li></ul><ul><ul><li>m,n are nonnegative and not both zero </li></ul></ul><ul><li>Examples: </li></ul><ul><ul><li>gcd(60,24) = 12 </li></ul></ul><ul><ul><li>gcd(60,0) = 60 </li></ul></ul><ul><ul><li>gcd(57, 13) = 1 </li></ul></ul><ul><li>GCD is used in number theory, solution of equations and cryptography (RSA algorithm) </li></ul>
  14. 14. Example: Euclid’s Algorithm for GCD <ul><li>Euclid’s Algorithm for the GCD of two numbers is the earliest recorded algorithm (c. 300 BC) </li></ul><ul><li>It is based on repeated application of the following equality until the second number becomes 0, leaving the first number as the answer </li></ul><ul><li>gcd( m,n ) = gcd( n, m mod n ) </li></ul><ul><li>Examples </li></ul><ul><ul><li>gcd(60,24) = gcd(24,12) = gcd(12,0) = 12 </li></ul></ul><ul><ul><li>gcd(57,13) = gcd(13,5) = gcd(5,3) = gcd(3,2) = gcd(2,1) = gcd(1,0) = 1 </li></ul></ul>m mod n is the remainder of m/n
  15. 15. Two Descriptions of Euclid’s algorithm <ul><li>while n ≠ 0 do </li></ul><ul><li>r ← m mod n </li></ul><ul><li>m← n </li></ul><ul><li>n ← r </li></ul><ul><li>return m </li></ul>Step 1 If n = 0, return m and stop; otherwise go to Step 2 Step 2 Divide m by n and assign the value of the remainder to r Step 3 Assign the value of n to m and the value of r to n. Go to Step 1. <ul><li>Remember, the same algorithm may be represented in several different ways … </li></ul>
  16. 16. Other Algorithms for GCD <ul><li>Consecutive Integer Checking Algorithm </li></ul><ul><li>Step 1 Assign the value of min{m,n} to t </li></ul><ul><li>Step 2 Divide m by t. If the remainder is 0, go to Step 3; otherwise, go to Step 4 </li></ul><ul><li>Step 3 Divide n by t. If the remainder is 0, return t and stop; otherwise, go to Step 4 </li></ul><ul><li>Step 4 Decrease t by 1 and go to Step 2 </li></ul><ul><li>Remember, several different algorithms for solving the same problem may exist </li></ul><ul><ul><li>Each may be based on different design ideas and have very different performance… </li></ul></ul><ul><li>Consecutive Integer Checking Algorithm for GCD </li></ul>
  17. 17. Other Algorithms for GCD <ul><li>Consecutive Integer Checking Algorithm </li></ul><ul><li>Step 1 Assign the value of min{m,n} to t </li></ul><ul><li>Step 2 Divide m by t. If the remainder is 0, go to Step 3; otherwise, go to Step 4 </li></ul><ul><li>Step 3 Divide n by t. If the remainder is 0, return t and stop; otherwise, go to Step 4 </li></ul><ul><li>Step 4 Decrease t by 1 and go to Step 2 </li></ul><ul><li>Consecutive Integer Checking Algorithm example: gcd(60,24) </li></ul><ul><ul><li>t = min(60,24) = 24 </li></ul></ul><ul><ul><li>60/24 = 2 remainder 12, t = 23 </li></ul></ul><ul><ul><li>60/23 = 2 remainder 14, t = 22 </li></ul></ul><ul><ul><li>60/22 = 2 remainder 16, t =21 </li></ul></ul><ul><ul><li>60/21 = 2 remainder 18, t=20 </li></ul></ul><ul><ul><li>60/20 = 3 remainder 0 </li></ul></ul><ul><ul><li>24/20 = 1 remainder 4, t=19 </li></ul></ul><ul><ul><li>… ..(t = 19-16 not shown)… t=15 </li></ul></ul><ul><ul><li>60/15 = 4 remainder 0 </li></ul></ul><ul><ul><li>24/15 = 1 remainder 9 </li></ul></ul><ul><ul><li>… ..(t = 14-13 not shown)… t = 12 </li></ul></ul><ul><ul><li>60/12 = 5 remainder 0 </li></ul></ul><ul><ul><li>24/12 = 2 remainder 0 </li></ul></ul><ul><ul><li>gcd(24,12) = 12 </li></ul></ul><ul><li>How does the performance of this algorithm compare to Euclid’s? </li></ul><ul><ul><li>It is clearly worse for this pair of numbers, at least </li></ul></ul><ul><ul><li>Soon we shall see that Euclid’s is O(log n) and this is O(n) </li></ul></ul>
  18. 18. Other Algorithms for GCD <ul><li>“ Middle-school” procedure </li></ul><ul><li>Step 1 Find the prime factorisation of m </li></ul><ul><li>Step 2 Find the prime factorisation of n </li></ul><ul><li>Step 3 Find all the common prime factors </li></ul><ul><li>Step 4 Compute the product of all the common prime factors and return it as gcd (m,n ) </li></ul><ul><li>Is this an algorithm? </li></ul><ul><ul><li>Consider the non-ambiguity requirement… </li></ul></ul>48 180 gcd = 12
  19. 19. Other Algorithms for GCD <ul><li>We need an algorithm for prime factorisation to make the middle-school procedure into an algorithm </li></ul><ul><li>We just repeatedly divide n by the prime numbers less than it, with no remainder, until the quotient becomes 1 </li></ul><ul><li>But to do this prime factorisation, we need an algorithm to find all the prime numbers, up to n… </li></ul><ul><li>This can be done with the “Sieve of Eratosthenes” </li></ul>
  20. 20. Other Algorithms for GCD <ul><li>“ Middle-school” procedure </li></ul><ul><li>Step 1 Find the prime factorisation of m </li></ul><ul><li>Step 2 Find the prime factorisation of n </li></ul><ul><li>Step 3 Find all the common prime factors </li></ul><ul><li>Step 4 Compute the product of all the common prime factors and return it as gcd (m,n ) </li></ul>Prime Factorisation Input: Integer x ≥ 2, Output: List F of prime factors of x P ← Sieve(x) while n > 1 do while n mod P[i] = 0 do F ← F + P[i] x ← x / P[i] i ← i + 1 Sieve of Eratosthenes Input: Integer x ≥ 2 Output: List of primes less than or equal to x for p ← 2 to x do A [ p ] ← p for p ← 2 to x do if A [ p ]  0 j ← p * p while j ≤ x do A [ j ] ← 0 j ← j + p <ul><li>How does the performance of this algorithm compare to Euclid’s and Consecutive Integer Checking? </li></ul><ul><li>Note also that an algorithm will be required to find all common prime factors too… </li></ul>
  21. 21. Fundamentals of Algorithmic Problem Solving <ul><li>Understand the Problem </li></ul><ul><li>Ascertain the Capabilities of the Computational Device </li></ul><ul><li>Choosing between Exact and Approximate Problem Solving </li></ul><ul><li>Deciding on Appropriate Data Structures </li></ul><ul><li>Algorithm Design Techniques </li></ul><ul><li>Methods of Specifying an Algorithm </li></ul><ul><li>Proving an Algorithm’s Correctness </li></ul><ul><li>Analysing an Algorithm </li></ul><ul><li>Coding an Algorithm </li></ul><ul><li>Please read this section in the textbook in much more detail… </li></ul>
  22. 22. Algorithm Design Techniques <ul><li>An algorithm design technique (or “strategy” or “paradigm”) is… </li></ul><ul><ul><li>a general approach to solving problems algorithmically that is applicable to a wide range of problems from different areas of computing </li></ul></ul><ul><li>This course is organised by design techniques </li></ul><ul><ul><li>See the lecture topics from earlier… </li></ul></ul><ul><ul><li>Usually, AA courses are organised by problem type not algorithm design technique </li></ul></ul>
  23. 23. Analysing an Algorithm <ul><li>Analysis is essential. We want to know that our algorithms have good characteristics: </li></ul><ul><ul><li>Good Time Efficiency ( or Complexity ) </li></ul></ul><ul><ul><li>Good Space Efficiency ( or Complexity ) </li></ul></ul><ul><ul><li>Simplicity </li></ul></ul><ul><ul><li>Generality </li></ul></ul><ul><li>Analysis allows comparison of algorithms and allows us to know if they can be used practically or not </li></ul><ul><li>We will study a framework for analysis in the next lecture </li></ul>
  24. 24. Important Problem Types <ul><li>Sorting </li></ul><ul><li>Searching </li></ul><ul><li>String Processing </li></ul><ul><li>Graph Problems </li></ul><ul><li>Combinatorial Problems </li></ul><ul><li>Geometric Problems </li></ul><ul><li>Numerical Problems </li></ul>
  25. 25. Sorting Problems <ul><li>Problem: rearrange the items of a given list in ascending order . </li></ul><ul><ul><li>Input: A sequence of n items <a 1 , a 2 , …, a n > </li></ul></ul><ul><ul><li>Output: A reordering <a * 1 , a * 2 , …, a * n > of the input sequence such that a * 1 ≤ a * 2 ≤ … ≤ a * n. </li></ul></ul><ul><li>Importance of sorting </li></ul><ul><ul><li>Can help searching tremendously </li></ul></ul><ul><ul><li>Algorithms often use sorting as a key subroutine. </li></ul></ul><ul><li>Sorting key </li></ul><ul><ul><li>A specially chosen piece of information used to guide sorting. E.g., sort student records by names. </li></ul></ul>
  26. 26. Sorting Problems <ul><li>Examples of sorting algorithms </li></ul><ul><ul><li>Selection Sort </li></ul></ul><ul><ul><li>Bubble sort </li></ul></ul><ul><ul><li>Insertion sort </li></ul></ul><ul><ul><li>Merge sort </li></ul></ul><ul><ul><li>Heap sort …and others </li></ul></ul><ul><li>We usually evaluate sorting algorithm complexity by the number of key comparisons . </li></ul><ul><li>Two important properties </li></ul><ul><ul><li>Stability : A sorting algorithm is called stable if it preserves the relative order of any two equal elements in its input. </li></ul></ul><ul><ul><li>In place : A sorting algorithm is in place if it does not require extra memory, except, possibly for a few memory units. </li></ul></ul>
  27. 27. Sorting Problems SelectionSort(A[0..n-1]) //Input: An array A[0..n-1] of orderable elements //Output: Array A[0..n-1] sorted in ascending order for i ← 0 to n – 2 do min ← i for j ← i + 1 to n – 1 do if A[j] < A[min] min ← j swap A[i] and A[min] <ul><li>An example of a simple (and often low-performance) sorting algorithm is Selection Sort </li></ul>
  28. 28. Searching Problems <ul><li>Problem: find a given value, called a search key, in a given set. </li></ul><ul><ul><li>Eg ID, ref number, name, etc </li></ul></ul><ul><li>Search is a hugely important problem </li></ul><ul><ul><li>Retrieving data from database storage </li></ul></ul><ul><ul><ul><li>Student records, company accounts etc. </li></ul></ul></ul><ul><li>And more general search… </li></ul><ul><ul><li>Searching the web for information </li></ul></ul><ul><ul><li>Searching to solve problems in AI </li></ul></ul>
  29. 29. Searching Problems <ul><li>Examples of searching algorithms </li></ul><ul><ul><li>Sequential search (Time O(n)) </li></ul></ul><ul><ul><li>Binary search (Time: O(log n)) </li></ul></ul>Binary Search // Input: sorted array a_i < … < a_j and key x; m  (i+j)/2; while i < j and x != a_m do if x < a_m then j  m-1 else i  m+1; if x = a_m then output a_m;
  30. 30. String Processing Problems <ul><li>A string is a sequence of characters from an alphabet. </li></ul><ul><ul><li>“ This is a string” is a string </li></ul></ul><ul><li>Text strings: letters, numbers, and special characters. </li></ul><ul><li>String matching: searching for a given word/pattern in a text. </li></ul><ul><li>Examples </li></ul><ul><ul><li>searching for a word or phrase on WWW or in a Word document </li></ul></ul><ul><ul><li>searching for a particular sequence of {A,C,G,T} in a reference genomic sequence </li></ul></ul>
  31. 31. Graph Problems <ul><li>(Informally) A graph is a collection of points called vertices , some of which are connected by line segments called edges . </li></ul><ul><li>Graphs are extremely important for modeling real-life problems </li></ul><ul><ul><li>Modeling WWW </li></ul></ul><ul><ul><li>Social relationships </li></ul></ul><ul><ul><li>Communication networks </li></ul></ul><ul><ul><li>Project scheduling … </li></ul></ul><ul><li>Examples of graph algorithms </li></ul><ul><ul><li>Graph traversal algorithms </li></ul></ul><ul><ul><li>Shortest-path algorithms </li></ul></ul><ul><ul><li>Topological sorting </li></ul></ul>1 2 3 4
  32. 32. Graph Problems <ul><li>Example: Travelling Salesman Problem </li></ul><ul><ul><li>For a set of cities linked by roads, find the shortest route that visits them all only once. </li></ul></ul><ul><ul><li>Important to </li></ul></ul><ul><ul><ul><li>Businesses and route planning </li></ul></ul></ul><ul><ul><ul><li>Circuit board and VLSI design and manufacture </li></ul></ul></ul><ul><ul><ul><li>X Ray crystallography </li></ul></ul></ul><ul><ul><ul><li>Genetic engineering </li></ul></ul></ul><ul><li>TSP, like many other graph problems, is extremely difficult and no general efficient algorithms are known </li></ul><ul><ul><li>In fact, they may not exist… </li></ul></ul>
  33. 33. Combinatorial Problems <ul><li>Finding a Combinatorial Object that satisfies certain constraints or has certain properties </li></ul><ul><ul><li>Sets, subsets </li></ul></ul><ul><ul><li>Permutations </li></ul></ul><ul><ul><ul><li>Set P of all ordered sequences of length r from a set S of n elements </li></ul></ul></ul><ul><ul><ul><li>Eg. for S = {1,2,3} and r=3, P = {(1,2,3), (1,3,2), (2,1,3),(2,3,1), (3,1,2), (3,2,1)} </li></ul></ul></ul><ul><ul><ul><li>Eg. for S = {1,2,3} and r=2, P = {(1,2), (1,3), (2,1),(2,3), (3,1), (3,2)} </li></ul></ul></ul><ul><ul><ul><li>The number of permutations is n P r = n!/(n-r)! </li></ul></ul></ul><ul><ul><li>Combinations </li></ul></ul><ul><ul><ul><li>Set C of all unordered sequences of length r from a set S of n elements </li></ul></ul></ul><ul><ul><ul><li>Eg. for S = {1,2,3} and r=2, C = {(1,2), (1,3), (2,3),} </li></ul></ul></ul><ul><ul><ul><li>Eg. for S = {1,2,3} and r=3, C = {(1,2,3)} </li></ul></ul></ul><ul><ul><ul><li>The number of combinations is n C r = n!/(n-r)! r! </li></ul></ul></ul><ul><li>Combinatorial problems are among the hardest known </li></ul>
  34. 34. Combinatorial Problems <ul><li>Example: The Knapsack Problem </li></ul><ul><ul><li>Given a set of items, each with a weight and a value, determine the most valuable subset of items with total weight less than or equal to a given limit </li></ul></ul>
  35. 35. Geometric Problems <ul><li>Problems dealing with Geometric Objects such as points, lines, polygons </li></ul><ul><li>Examples </li></ul><ul><ul><li>Finding closest pair of points </li></ul></ul><ul><ul><li>Finding convex hull of a set of points </li></ul></ul>
  36. 36. Numerical Problems <ul><li>Problems dealing with mathematical objects </li></ul><ul><ul><li>Numbers, Equations, Matrices… </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>Efficient arithmetic </li></ul></ul><ul><ul><li>Evaluating functions </li></ul></ul><ul><ul><li>Solving systems of linear equations with Gaussian Elimination </li></ul></ul><ul><ul><li>Evaulating Definite Integrals </li></ul></ul><ul><ul><li>Optimising a function subject to constraints </li></ul></ul>
  37. 37. Fundamental Data Structures <ul><li>Linear Data Structures </li></ul><ul><ul><li>Array </li></ul></ul><ul><ul><li>String </li></ul></ul><ul><ul><li>Linked list </li></ul></ul><ul><ul><li>Stack </li></ul></ul><ul><ul><li>Queue </li></ul></ul><ul><ul><ul><li>Priority queue/heap </li></ul></ul></ul><ul><li>Graph </li></ul><ul><ul><li>General Graph </li></ul></ul><ul><ul><li>Tree </li></ul></ul><ul><ul><ul><li>Binary Tree </li></ul></ul></ul><ul><li>Set and Dictionary </li></ul>
  38. 38. Linear Data Structures <ul><li>Arrays </li></ul><ul><ul><li>A sequence of n items of the same data type that are stored contiguously in computer memory and made accessible by specifying a value of the array’s index. </li></ul></ul><ul><li>Array properties </li></ul><ul><ul><li>fixed length (need preliminary reservation of memory) </li></ul></ul><ul><ul><li>contiguous memory locations </li></ul></ul><ul><ul><li>direct access </li></ul></ul><ul><ul><li>Operations of insert/delete </li></ul></ul><ul><ul><li>Often used to implement Lists and Strings </li></ul></ul>
  39. 39. Linear Data Structures <ul><li>Linked List </li></ul><ul><ul><li>A sequence of zero or more nodes each containing two kinds of information: some data and one or more links called pointers to other nodes of the linked list. </li></ul></ul><ul><ul><li>Singly linked list (next pointer) </li></ul></ul><ul><ul><li>Doubly linked list (next + previous pointers) </li></ul></ul><ul><li>Linked List Properties </li></ul><ul><ul><li>dynamic length </li></ul></ul><ul><ul><li>arbitrary memory locations </li></ul></ul><ul><ul><li>access by following links </li></ul></ul><ul><ul><li>Operations of Insert/delete </li></ul></ul>
  40. 40. Stacks and Queues <ul><li>Stacks </li></ul><ul><ul><li>A stack of plates </li></ul></ul><ul><ul><ul><li>insertion/deletion can be done only at the top. </li></ul></ul></ul><ul><ul><ul><li>LIFO </li></ul></ul></ul><ul><ul><li>Two operations (push and pop) </li></ul></ul><ul><li>Queues </li></ul><ul><ul><li>A queue of customers waiting for services </li></ul></ul><ul><ul><ul><li>Insertion/enqueue from the rear and deletion/dequeue from the front. </li></ul></ul></ul><ul><ul><ul><li>FIFO </li></ul></ul></ul><ul><ul><li>Two operations (enqueue and dequeue) </li></ul></ul>
  41. 41. Priority Queues and Heaps <ul><li>Priority queues (implemented using heaps) </li></ul><ul><ul><li>A data structure for maintaining a set of elements, each associated with a key/priority, with the following operations </li></ul></ul><ul><li>Operations </li></ul><ul><ul><li>Finding the element with the highest priority </li></ul></ul><ul><ul><li>Deleting the element with the highest priority </li></ul></ul><ul><ul><li>Inserting a new element </li></ul></ul>9 6 8 5 2 3 9 6 5 8 2 3
  42. 42. Graphs <ul><li>Formal definition </li></ul><ul><ul><li>A graph G = <V, E> is defined by a pair of sets: a finite set V of items called vertices and a set E of vertex pairs called edges. </li></ul></ul><ul><li>Undirected and directed graphs (digraphs). </li></ul><ul><ul><li>What’s the maximum number of edges in an undirected graph with |V| vertices? </li></ul></ul><ul><li>Complete graphs </li></ul><ul><ul><li>A graph with every pair of its vertices connected by an edge is called complete, K |V| </li></ul></ul><ul><li>Dense and sparse graphs </li></ul><ul><ul><li>Dense graphs have edges between most nodes </li></ul></ul><ul><ul><li>Sparse graphs have few edges between nodes </li></ul></ul>Complete, undirected graph Incomplete, directed graph Both graphs here are dense 1 2 3 4 1 2 3 4
  43. 43. Graph Representation <ul><li>Nodes are said to be adjacent if an edge exists between them </li></ul><ul><li>Adjacency Matrix </li></ul><ul><ul><li>n x n boolean matrix if number of nodes |V| is n. </li></ul></ul><ul><ul><li>The element on the ith row and jth column is 1 if there’s an edge from ith vertex to the jth vertex; otherwise 0. </li></ul></ul><ul><ul><li>The adjacency matrix of an undirected graph is symmetric. </li></ul></ul><ul><li>Adjacency Linked Lists </li></ul><ul><ul><li>A collection of linked lists, one for each vertex, that contain all the vertices adjacent to the list’s vertex. </li></ul></ul><ul><li>Which data structure would you use for dense and sparse graphs? </li></ul>0 1 1 1 0 0 0 1 0 0 0 1 0 0 0 0 Incomplete, directed graph G Adjacency Matrix for G Adjacency Linked List for G 2 3 4 4 4 1 2 3 4
  44. 44. Weighted Graphs <ul><li>Weighted graphs </li></ul><ul><ul><li>Graphs or digraphs with numbers assigned to the edges. </li></ul></ul>6 8 5 7 9 1 2 3 4
  45. 45. Graph Properties -- Paths and Connectivity <ul><li>Paths </li></ul><ul><ul><li>A path from vertex u to v of a graph G is defined as a sequence of adjacent vertices that starts with u and ends with v. </li></ul></ul><ul><ul><li>Simple paths: All edges of a path are distinct. </li></ul></ul><ul><ul><li>Path lengths: the number of edges, or the number of vertices – 1. </li></ul></ul>Simple Path from 4 to 3 (4,1,2,3) Path Length 3 Path from 4 to 3 (4,1,2,5,4,1,2,3) Path Length 7 1 2 4 5 3 1 2 4 5 3
  46. 46. Graph Properties -- Paths and Connectivity <ul><li>Connected graphs </li></ul><ul><ul><li>A graph is said to be connected if for every pair of its vertices u and v there is a path from u to v. </li></ul></ul><ul><li>Connected component </li></ul><ul><ul><li>The maximum connected subgraph of a given graph . </li></ul></ul>Connected Graph G1 Non-Connected Graph G2 1 2 4 5 3 1 2 4 5 3
  47. 47. Graph Properties -- Acyclicity <ul><li>Cycle </li></ul><ul><ul><li>A simple path of a positive length that starts and ends a the same vertex. </li></ul></ul><ul><li>Acyclic graph </li></ul><ul><ul><li>A graph without cycles </li></ul></ul><ul><ul><li>DAG (Directed Acyclic Graph) </li></ul></ul>Directed Acyclic Graph Cycle (1,2,5,4,1) 1 2 3 4 1 2 4 5 3
  48. 48. Trees <ul><li>Trees </li></ul><ul><ul><li>A tree (or free tree) is a connected acyclic graph. </li></ul></ul><ul><ul><li>Forest: a graph that has no cycles but is not necessarily connected. </li></ul></ul><ul><li>Properties of trees </li></ul><ul><ul><li>|E| = |V| - 1 </li></ul></ul><ul><ul><li>For every two vertices in a tree there always exists exactly one simple path from one of these vertices to the other…. </li></ul></ul>Free Tree Free Tree that is also a forest 1 3 2 4 5 1 3 2 4 5 6 7
  49. 49. Rooted Trees <ul><li>This “single path” property makes it possible to select an arbitrary vertex in a free tree and consider it as the root of the so-called rooted tree . </li></ul><ul><li>A root then defines levels in the tree. </li></ul><ul><ul><li>Number of edges from the root </li></ul></ul>Free Tree Rooted Tree with 2 levels 1 3 2 4 5 1 3 2 4 5
  50. 50. Rooted Trees <ul><li>Ancestors </li></ul><ul><ul><li>For any vertex v in a tree T , all the vertices on the simple path from the root to that vertex are called ancestors. </li></ul></ul><ul><li>Descendants </li></ul><ul><ul><li>All the vertices for which a vertex v is an ancestor are said to be descendants of v . </li></ul></ul><ul><li>Parent, child and siblings </li></ul><ul><ul><li>If (u, v) is the last edge of the simple path from the root to vertex v , u is said to be the parent of v and v is called a child of u . </li></ul></ul><ul><ul><li>Vertices that have the same parent are called siblings. </li></ul></ul><ul><li>Leaves </li></ul><ul><ul><li>A vertex without children is called a leaf. </li></ul></ul><ul><li>Subtree </li></ul><ul><ul><li>A vertex v with all its descendants is called the subtree of T rooted at v. </li></ul></ul><ul><li>Examples: </li></ul><ul><li>1 is an ancestor of 6 and 7 </li></ul><ul><li>8 is a descendant of 5 </li></ul><ul><li>1 is parent (and ancestor) of 6 and 7 </li></ul><ul><li>8 and 9 are children (and descendants) of 5 </li></ul><ul><li>6 and 7 are siblings </li></ul><ul><li>8 and 9 are siblings </li></ul><ul><li>4,6,7,8,9 are leaves </li></ul><ul><li>5 is the subtree consisting of 5,8,9 </li></ul>1 3 2 4 5 7 6 8 9
  51. 51. Rooted Trees <ul><li>Depth of a vertex </li></ul><ul><ul><li>The length of the simple path from the root to the vertex. </li></ul></ul><ul><li>Height of a tree </li></ul><ul><ul><li>The length of the longest simple path from the root to a leaf. </li></ul></ul><ul><li>Examples: </li></ul><ul><li>Vertex 2 has depth d=2 </li></ul><ul><li>Vertex 7 has depth d=3 </li></ul><ul><li>The tree itself has height h=3 </li></ul>1 3 2 4 5 7 6 8 9
  52. 52. Ordered Trees <ul><li>Ordered trees </li></ul><ul><ul><li>An ordered tree is a rooted tree in which all the children of each vertex are ordered. </li></ul></ul><ul><li>Binary trees </li></ul><ul><ul><li>A binary tree is an ordered tree in which every vertex has no more than two children and each children is designated s either a left child or a right child of its parent. </li></ul></ul>Binary Tree L R L 9 6 8 5 2 3
  53. 53. Ordered Trees <ul><li>Binary search trees </li></ul><ul><ul><li>Each vertex is assigned a number. </li></ul></ul><ul><ul><li>A number assigned to each parental vertex is larger than all the numbers in its left subtree and smaller than all the numbers in its right subtree. </li></ul></ul><ul><ul><li> log 2 n   h  n – 1, where h is the height of a binary tree and n the size. </li></ul></ul>6 3 9 2 5 8
  54. 54. Sets and Dictionaries <ul><li>I will assume you already know what a set is! </li></ul><ul><ul><li>Elements, subsets, union, intersection etc.. </li></ul></ul><ul><li>Dictionaries are Sets with 3 operations: </li></ul><ul><ul><li>Searching for a given element </li></ul></ul><ul><ul><li>Inserting a new element </li></ul></ul><ul><ul><li>Deleting an existing element </li></ul></ul><ul><li>Please read the textbook for more detail… </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×