Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of Search and Sort

Similar presentations


Presentation on theme: "Review of Search and Sort"— Presentation transcript:

1 Review of Search and Sort
Lecture 1 Introduction of Computer Algorithms Review of Search and Sort

2 Course will Include Fundamentals - You should...
know how to implement, analyze and compare algorithms know a programming language understand data abstraction & basic data structures including collections Sorting - You will cover... various methods for ordering data by key comparison extend sorting methods beyond scalar data and simple lists 2D/3D data objects, natural clustering, pattern recognition priority queues, selection, merging... Searching - You will study and implement... binary search trees, permutation trees, game trees, queue, tree, & graph search methods methods for storing and retrieving information in very large data sets Graphs - You will learn... problem solving methods using graphs Depth-First Search (DFS), Breadth-First Search (BFS), Minimal Spanning Tree (MST) Traveling Salesman Problem (TSP) Algorithms, Single-Source Shortest Path (SSSP) & All-Pairs Shortest Path (APSP), Min-Flow Max-Cut, Bellman-Ford,... Strings - You will become familiar with... special searching and sorting algorithms for data in which the keys are strings regular expressions, string matching algorithms, data compression

3 General Problem-Solving Methods
Divide and Conquer - an algorithmic technique to solve a problem on an instance of size n, a solution is found either directly because solving that instance is easy (typically, because the instance is small) or the instance is separated into two or more smaller instances. Each of these smaller instances is solved and the solutions are combined to produce a solution for the original instance. Backtracking - a general algorithm for finding all (or some) solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons each partial candidate ("backtracks") as soon as it determines that it cannot possibly be completed to a valid solution. Greedy Method - is an algorithmic method that makes a series of locally optimal choices at each stage with the hope that these lead to a globally optimal solution. Dynamic Programming - is a method for solving a complex problem by breaking it down into a collection of simpler subproblems, solving each of those subproblems just once, and storing their solutions – ideally, using a memory-based data structure. Branch-and-Bound - a problem solving method that involves the systematic enumeration of all candidate solutions by means of state space search, a particular, possibly non-optimal solution and a measure of the lower bound on the cost of a solution involving particular steps in the candidate solutions rendering their further development as either promising or unpromising.

4 Programming Basics Be able to read/write textfiles
implement procedures from a natural language description decompose a problem/solution into functional elements be able to encapsulate problem-solving components into functions and methods access and use standard libraries in your chosen programming language program using a professional level IDE write programs that draw using primitives (point, line, shape,...) for graphical display

5 You Might Want to Try These
You should be proficient in at least one programming language. Many of the problem sets will be in the form of a text file containing a list or matrix of floating point values, integers, characters, or strings. You will be building programs and static methods to perform operations such as... Read and Write a list of integers, floats or characters to and from a text file. Read and Write a list of points in R2 to and from a text file. Read and Write a two-dimensional array of integers, floats or characters to and from a text file. Search a list to find a particular item. Sort a list into ascending or descending order. Exchange two values in a list. Compute the distance between two points in R2. Find the particular value (e.g. minimum value) in a row or column of a matrix Add (or subtract) a value to (from) every value in a row or column Exchange two rows or two columns in a matrix.

6 used to count the number of values so that the Array
Reading Values from a Text File into an Indexed Array public static int[] A; public static void readarray() { string fname; string str; int n = 0; Console.Write("Enter filename..... "); fname = Console.ReadLine(); TextReader sr = new StreamReader(fname); do str = sr.ReadLine(); n += 1; }while (str != null); sr.Close(); A = new int[n]; TextReader tr = new StreamReader(fname); for (int i = 0; i < n; i++) A[i] = Convert.ToInt32(tr.ReadLine()); tr.Close(); } used to count the number of values so that the Array A[n] can be created.

7 Reading Values from a Text File into a Generic List
public static List<int> S = new List<int>(); public static void readlist() { string fname; string str; int s; Console.Write("Enter filename..... "); fname = Console.ReadLine(); TextReader tr = new StreamReader(fname); do str = tr.ReadLine(); if (str != null) s = new int(); s = Convert.ToInt32(str); S.Add(s); } } while (str != null); tr.Close();

8 Data Structures You need to be familiar with the design and use of basic data structures such as Lists, Stacks, Queues, Trees, and Graphs. List top Stack Queue back front Tree root leaf nodes child node parent Graph edge vertex cycle path

9 Binary Tree Embedded in an Indexed List
1 (1) parent = child / 2 (2) left_child = 2 x parent (3) right_child = 2 x parent + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

10 Types of Graphs ring complete graph bipartite graph
directed acyclic graph

11 Graph Representations
node list edge list A D F C H B E G A - B C D E F B - A C E H C - A B D E H D - A C F G H E - A B C F G F - A D E G H G - D E F H H - B C D F G A B A C A D A E A F B A B C B E B H C A C B C D C E C H D A D C D F D G D H E A E B E C E F E G F A F D F E F G F H G D G E G F G H H B H C H D H F H G node list - lists the nodes connected to each node edge list - lists each of the edges as a pair of nodes undirected edges may be listed twice XY and YX in order to simplify algorithm implementation adjacency matrix - for an n-node graph we build an nxn array with 1's indicating edges and 0's no edge the main diagonal of the matrix is unused unless a node has an edge connected to itself. If graph is weighted, 1's are replaced with edge weight values adjacency matrix A B C D E F G H A B C D E F G H

12 Analysis of Algorithms

13 Estimating Order of Complexity using Big "O"

14 Analysis of Basic Search and Sort Agorithms
Search an unordered list of n items to find a particular item x. Search an ordered list of n items to find a particular item x. Find the smallest value in an unordered list of n items. Find the largest value in an ordered list of n items. Determine if an unordered list contains a repeated value. Find the kth largest value in an unordered list of n items (k<=n). Find the kth largest value in an ordered list of n items (k<=n). Sort an unordered list of n items into ascending order (sort by key comparison). Find a pair of items x and y in an unordered list that are the most similar. Find a pair of items x and y in an ordered list that are the most similar. Find a pair of items in an ordered list that are the least similar. Arrange a list so that the sum of the abs. differences between adj. items is a minimum. Arrange a list so that the sum of the abs. differences between adj. items is a maximum. Find the two pairs of points (in R2 ) whose separations are the most similar.

15 Recursion Hides most of the Details
function recurse(n) deal with n // there is usually something to do with the incoming value for each f(n) = m if(m is OK) // if no m is OK then this call has reached its termination condition recurse(m) deal with n // sometimes the incoming value is processed after the recursion It is important to understand that each recursive call interrupts the for-each loop until the call has returned Certain values of m may be OK at beginning of for-each loop but may become not OK by the time the loop reaches them Regardless of the number of valid recursive calls in any for-each loop, if each value of N can be accessed only once, the overall runtime complexity will be O(N). Recursion goes bad (exponential run-time complexity) when each value of problem of size N can be in more than one recursive call.

16 Sort Tree Traversals A B C D E F G H I
All traversals are in the same order The difference is when the node is evaluated (i.e. passed to the output) Sort Trees are normally implemented using dynamic memory and pointers Can you think of a practical alternative? A B D E C F H I G pre-order eval node left-child right-child in-order post-order left-child eval node right-child left-child right-child eval node D B E A H F I C G D E B H I F G C A

17 Related Fields of Study
During this course we will include in our study of algorithms several other fields, including scientific visualization, scientific computing, operations research, machine vision, and the theory of computing.

18 Our Case Study Shortest Route Problem

19 Review of Search and Sort

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36


Download ppt "Review of Search and Sort"

Similar presentations


Ads by Google