Transcript Slide 1

Lecture 1:

Introduction to Computer Algorithms

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.

top back front

Queue List

root

Tree

paren t node child node

Stack

path vertex edge cycle leaf nodes

Graph

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: 1. Search an unordered list of n items to find a particular item x. 2. Search an ordered list of n items to find a particular item x. 3. Find the smallest value in an unordered list of n items. 4. Find the largest value in an ordered list of n items. 5. Determine if an unordered list contains a repeated value.

6. Find the kth largest value in an unordered list of n items (k<=n). 7. Find the kth largest value in an ordered list of n items (k<=n). 8. Sort an unordered list of n items into ascending order (sort by key comparison). 9. Find a pair of items x and y in an unordered list that are the most similar. 10. Find a pair of items x and y in an ordered list that are the most similar. 11. Find a pair of items in an ordered list that are the least similar. 12. Arrange a list so that the sum of the abs. differences between adj. items is a minimum. 13. Arrange a list so that the sum of the abs. differences between adj. items is a maximum. 14. Find the two pairs of points (in R

2

) whose separations are the most similar.

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 R 2 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 R 2 .

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.

Lists & Arrays

Reading Values from a Text File into an Indexed Array

public static int [] A; public static void { string string int fname; str; n = 0; readarray() Console .Write( "Enter filename..... " ); fname = Console .ReadLine(); TextReader do { } while sr = str = sr.ReadLine(); n += 1; (str != sr.Close(); new null StreamReader ); (fname);

used to count the number of values so that the Array A[n] can be created.

A = new int [n]; TextReader tr = new StreamReader (fname); for ( int i = 0; i < n; i++) A[i] = Convert .ToInt32(tr.ReadLine()); tr.Close(); }

Reading Values from a Text File into a List

public static List < int > S = new List < int >(); public static void { string string int s; fname; str; readlist() Console .Write( "Enter filename..... " ); fname = Co ns ole .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(); }

Searching an Unordered List

Searching an Ordered List

A Common Sort Routine

a procedural approach

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) // 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

Sorting the Values in a List

public static void { int tmp; sortlist() for ( int for i = 0; i < S.Count() - 1; i++) ( int j = i + 1; j S[j]) tmp = S[i]; S[i] = S[j]; S[j] = tmp; } }

Sorting the Values in an Indexed Array

public static void { int tmp = a; a = b; b = tmp; } exchange( ref int a, ref int b) public static void { for ( int for ( int if sortarray() i = 0; i < A.Length; i++) j = i + 1; j < A.Length; j++) (A[i] > A[j]) exchange( ref A[i], ref A[j]); }

Algorithm Growth Rate Analysis

Best Case -- Worst Case

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.

Plug the hole with the cork. The cork is to big.

Trim the cork with the knife.

The knife is too dull.

The Hazards of Recursion

There's a hole in the bucket.

Bring water from the well.

There is no water.

Wet the stone with water.

The sharpening stone is too dry.

Sharpen the Knife.

Trees

function foob(n) if n>0 return foob(n-1) + foob(n-1)

For Example...

function feeb(n) if n>0 return feeb(n-1)

A A D B A B A B D C A C E B A E F C A H F C A H F I G C A G 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?

pre-order

1.eval node

2.left-child 3.right-child A B D E C F H I G

in-order

1.left-child

2.eval node

3.right-child D B E A H F I C G

post-order

1.left-child 2.right-child

3.eval node

D E B H I F G C A

2

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

Graphs

Types of Graphs

ring complete graph bipartite graph directed acyclic graph

Graph Representations

A B C D E F G H

node list

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 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 n x n 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 -

edge 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 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

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.

A B C D E F G H 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

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.

A B C D E F G H 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( ).

Graph Depth-First Traversal (DFT) Algorithm Implementation

text file representation

Given a graph G(V,E) and a starting node v start nodes in the order they are traversed.

perform a depth-first traversal. Provide a list of the Graph is defined in a text file of the following format: 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 A C D F H 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 B E G

Graph Depth-First Traversal (DFT) Algorithm Implementation

pseudo-code

DFT( node { v k ) // deal with current node for each node, vi { if (node_avail(v i )) then DFT(v i ) } }

node_avail( v i

) is a Boolean function that returns true if node v

i

which means that v

i

has not been traversed, and v

i

is adjacent to v

k

.

is available for traversal,

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