+ All Categories
Home > Documents > Lecture 1:

Lecture 1:

Date post: 17-Jan-2016
Category:
Upload: affrica
View: 29 times
Download: 0 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
27
Lecture 1: Introduction to Computer Algorithms
Transcript
Page 1: Lecture 1:

Lecture 1:

Introduction to Computer Algorithms

Page 2: Lecture 1:

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.

List

top

Stack

Queue

back front

Tree

root

leaf nodes

childnode

parent node

Graph

edge

vertex

cycle

path

Page 3: Lecture 1:

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 R2 ) whose separations are the most similar.

What you Need to Know and Do

Page 4: Lecture 1:

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 matrixAdd (or subtract) a value to (from) every value in a row or columnExchange two rows or two columns in a matrix.

Try These

Page 5: Lecture 1:

Lists & Arrays

Page 6: Lecture 1:

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();}

Reading Values from a Text File into an Indexed Array

used to count the numberof values so that the Array

A[n] can be created.

Page 7: Lecture 1:

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();}

Reading Values from a Text File into a List

Page 8: Lecture 1:

Searching an Unordered List

Page 9: Lecture 1:

Searching an Ordered List

Page 10: Lecture 1:

A Common Sort Routinea 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

Page 11: Lecture 1:

public static void 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; }}

Sorting the Values in a List

Page 12: Lecture 1:

public static void exchange(ref int a, ref int b){ int tmp = a; a = b; b = tmp;}

public static void 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]);}

Sorting the Values in an Indexed Array

Page 13: Lecture 1:

Algorithm Growth Rate AnalysisBest Case -- Worst Case

Page 14: Lecture 1:

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

Recursion Hides most of the Details

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.

Page 15: Lecture 1:

There's a hole in the bucket.

Plug the hole with the cork.

Sharpen the Knife.

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.

The Hazards of Recursion

Page 16: Lecture 1:

Trees

Page 17: Lecture 1:

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

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

For Example...

Page 18: Lecture 1:

A

B C

D E F G

H I

DBA

BA

EBA

A

CA

FCA

HFCA

IFCA

GCA

D B E A H F I C G D E B H I F G C A

Sort Tree Traversals

A B D E C F H I G

pre-order1.eval node2.left-child3.right-child

1.left-child2.eval node3.right-child

1.left-child2.right-child3.eval node

in-order post-order

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?

Page 19: Lecture 1:

16 17 18 19

8 9

20 21 22 23 24 25 26 27 28 29 30 31

10 11 12 13 14 15

4 5 6 7

2 3

1

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) parent = child / 2

(2) left_child = 2 x parent

(3) right_child = 2 x parent + 1

Binary Tree Embedded in an Indexed List

Page 20: Lecture 1:

Graphs

Page 21: Lecture 1:

ring complete graph bipartite graph

directed acyclic graph

Types of Graphs

Page 22: Lecture 1:

Graph Representations

adjacency matrix

node list edge listA D F

C H

B E G

A B C D E F G HA - 1 1 1 1 1 0 0B 1 - 1 0 1 0 0 1C 1 1 - 1 1 0 0 1D 1 0 1 - 0 1 1 1E 1 1 1 0 - 1 1 0F 1 0 0 1 1 - 1 1G 0 0 0 1 1 1 - 1H 0 1 1 1 0 1 1 -

A - B C D E FB - A C E HC - A B D E HD - A C F G HE - A B C F GF - A D E G HG - D E F HH - B C D F G

A BA CA DA EA FB AB CB EB HC AC BC DC EC HD AD CD FD GD H

E AE BE CE FE GF AF DF EF GF HG DG EG FG HH BH CH DH FH 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

Page 23: Lecture 1:

A D F

C H

B E G

Graph Breadth-First Traversal

Given a graph G(V,E) and a starting vertex s, performa breadth-first traversal (BFT) of G. such that eachreachable vertex is entered exactly once.

If all vertices are reachable, the edges traversed andthe set of vertices will represent a spanning treeembedded 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

Page 24: Lecture 1:

Graph Depth-First Traversal

A D F

C H

B E G

Given a graph G(V,E) and a starting vertex s, performa depth-first traversal (BFT) of G. such that eachreachable vertex is entered exactly once.

If all vertices are reachable, the edges traversed andthe set of vertices will represent a spanning treeembedded 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( ).

Page 25: Lecture 1:

Graph Depth-First Traversal (DFT) Algorithm Implementationtext 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:

1st Line - # of nodes, N # of edges, E2nd Line - List of node labels (you may assume 1 char each)3rd Line - through N+3 Linean N x N adjacency matrix

A D F

C H

B E G

8 A B C D E F G H0 1 1 1 1 1 0 01 0 1 0 1 0 0 11 1 0 1 1 0 0 11 0 1 0 0 1 1 11 1 1 0 0 1 1 01 0 0 1 1 0 1 10 0 0 1 1 1 0 10 1 1 1 0 1 1 0

Page 26: Lecture 1:

Graph Depth-First Traversal (DFT) Algorithm Implementationpseudo-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 vi has not been traversed, and vi is adjacent to vk.

Page 27: Lecture 1:

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


Recommended