1 / 27

Lecture 1:

Lecture 1:. Introduction to Computer Algorithms. top. back. front. Tree. Queue. root. List. Stack. cycle. paren t node. path. edge. child node. vertex. leaf nodes. Graph. This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

affrica
Download Presentation

Lecture 1:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 1: Introduction to Computer Algorithms

  2. top back front Tree Queue root List Stack cycle parent node path edge child node vertex leaf nodes Graph This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. You need to be familiar with the design and use of basic data structures such as Lists, Stacks, Queues, Trees, and Graphs.

  3. What you Need to Know and Do You should be able to understand and implement basic methods for searching and sorting. You should be able to determine the worst-case order of complexity (number of operations) required to solve a problem of size N using the best-known algorithm. Suggest an algorithm and give a worst-case order of complexity for each of the following: • 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.

  4. 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 need to build a set of software tools to perform the following operations: 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.

  5. Lists & Arrays

  6. Reading Values from a Text File into an Indexed Array publicstaticint[] A; publicstaticvoid readarray() { string fname; string str; int n = 0; Console.Write("Enter filename..... "); fname = Console.ReadLine(); TextReader sr = newStreamReader(fname); do { str = sr.ReadLine(); n += 1; }while (str != null); sr.Close(); A = newint[n]; TextReader tr = newStreamReader(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 List public static List<int> S = new List<int>(); publicstaticvoid readlist() { string fname; string str; int s; Console.Write("Enter filename..... "); fname = Console.ReadLine(); TextReader tr = newStreamReader(fname); do { str = tr.ReadLine(); if (str != null) { s = newint(); s = Convert.ToInt32(str); S.Add(s); } } while (str != null); tr.Close(); }

  8. Searching an Unordered List

  9. Searching an Ordered List

  10. A Common Sort Routine a procedural approach // reads a list of n values from the text file, filename // into the list S. n and S are passed back to the calling routine // if statement implements the key comparison // exchange swaps the position of two values in the list S // writes the sorted list, S back to the hard drive as filename ReadList(filename, n, S) for i = 1 to n - 1 for j = i + 1 to n if (S( i ) > S( j )) then exchange(S( i ), S( j )) end if end loop end loop WriteList(filename,S)

  11. Sorting the Values in a List publicstaticvoid sortlist() { int tmp; for (int i = 0; i < S.Count() - 1; i++) for (int j = i + 1; j<S.Count(); j++) if (S[i] > S[j]) { tmp = S[i]; S[i] = S[j]; S[j] = tmp; } }

  12. Sorting the Values in an Indexed Array publicstaticvoid exchange(refint a, refint b) { int tmp = a; a = b; b = tmp; } publicstaticvoid sortarray() { for (int i = 0; i < A.Length; i++) for (int j = i + 1; j < A.Length; j++) if (A[i] > A[j]) exchange(ref A[i], ref A[j]); }

  13. Algorithm Growth Rate Analysis Best Case -- Worst Case

  14. Recursion Hides most of the Details functionrecurse(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.

  15. The Hazards of Recursion There's a hole in the bucket. Plug the hole with the cork. Bring water from the well. The cork is to big. There is no water. Trim the cork with the knife. Wet the stone with water. The knife is too dull. The sharpening stone is too dry. Sharpen the Knife.

  16. Trees

  17. For Example... function foob(n) if n>0 return foob(n-1) + foob(n-1) function feeb(n) if n>0 return feeb(n-1)

  18. pre-order • eval node • left-child • right-child A B D E C F H I G A A B A C A F C A B C D B A E B A D E F G H I G C A H F C A I F C A Sort Tree Traversals 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? post-order in-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

  19. 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 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

  20. Graphs

  21. Types of Graphs ring bipartite graph complete graph directed acyclic graph

  22. A D F C H B E G Graph Representations edge list node list 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 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 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 - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0 1 C 1 1 - 1 1 0 0 1 D 1 0 1 - 0 1 1 1 E 1 1 1 0 - 1 1 0 F 1 0 0 1 1 - 1 1 G 0 0 0 1 1 1 - 1 H 0 1 1 1 0 1 1 -

  23. A D F C H B E G Graph Breadth-First Traversal Given a graph G(V,E) and a starting vertex s, perform a breadth-first traversal (BFT) of G. such that each reachable vertex is entered exactly once. If all vertices are reachable, the edges traversed and the set of vertices will represent a spanning tree embedded in the graph G. 1) BFT suggests an iterative process (rather than a recursive one) 2) BFT vertices order of traversal can be maintained using a Queue data structure 3) The preferred representation for the graph is an adjacency matrix 4) We will need a way to keep up with which vertices have been "used" (e.g. a Boolean list) 5) Process begins by placing the starting vertex in the Queue 6) A vertex is taken from the Queue, every unused vertex adj to this vertex is added to the Queue This operation is repeated until the Queue is empty. 8) The output (answer) is returned in the form of a list of vertices in the order they entered the Queue

  24. A D F C H B E G Graph Depth-First Traversal Given a graph G(V,E) and a starting vertex s, perform a depth-first traversal (BFT) of G. such that each reachable vertex is entered exactly once. If all vertices are reachable, the edges traversed and the set of vertices will represent a spanning tree embedded in the graph G. 1) DFT suggests a recursive process (rather than an iterative one) 2) DFT vertices order of traversal are maintained automatically by the recursion process (as a Stack) 3) The preferred representation for the graph is an adjacency matrix. 4) We will need a way to keep up with which vertices have been "used" (e.g. a Boolean list) 5) Process begins by passing the starting vertex to a recursive function DFT(s) 6) For the current vertex, s DFT(s) calls itself for each adjacent, unused vertex remaining. This operation is completed when all calls to DFT( ) are completed. 8) The output is returned as a of a list of vertices in the order they were passed to DFT( ).

  25. A D F C H B E G Graph Depth-First Traversal (DFT) Algorithm Implementation text file representation Given a graph G(V,E) and a starting node vstart perform a depth-first traversal. Provide a list of the nodes in the order they are traversed. Graph is defined in a text file of the following format: 8 A B C D E F G H 0 1 1 1 1 1 0 0 1 0 1 0 1 0 0 1 1 1 0 1 1 0 0 1 1 0 1 0 0 1 1 1 1 1 1 0 0 1 1 0 1 0 0 1 1 0 1 1 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 1st Line - # of nodes, N # of edges, E 2nd Line - List of node labels (you may assume 1 char each) 3rd Line - through N+3 Line an N x N adjacency matrix

  26. Graph Depth-First Traversal (DFT) Algorithm Implementation pseudo-code DFT( node vk ) { // deal with current node for each node, vi { if (node_avail(vi)) then DFT(vi) } } node_avail( vi ) is a Boolean function that returns true if node vi is available for traversal, which means that vihas not been traversed, and viis adjacent to vk.

  27. Summary • "Stuff You Should Already Know (SYSAK)... • Important Data Structures - lists, stacks, queues, trees, and graphs • 13 Problems to Consider • Reading Data from Text Files • Searching and Sorting • Methods for Growth-Rate Analysis • Recursion (good and bad) • Manipulating Lists and Matrices • Dealing with Trees • Working with Graphs

More Related