Docsity

Data Structures and Algorithms: An Introduction to Abstract Data Types - Prof. Rouskas, Study notes of Computer Science

Partial preview of the text

Download Data Structures and Algorithms: An Introduction to Abstract Data Types - Prof. Rouskas and more Study notes Computer Science in PDF only on Docsity!

CSC 316 – Data Structures

Introduction

George N. Rouskas Department of Computer Science North Carolina State University CSC 316 – Fall 2011: Introduction Copyright c© 2011by George N. Rouskas

Outline

Problems and Problem Instances

Algorithms and Their Properties

Abstract Data Types

Pseudo-Code CSC 316 – Fall 2011: Introduction Copyright c© 2011by George N. Rouskas

Example: The Traveling Salesman Problem (TSP)

Parameters: a finite set

C

c 1 , c 2

, c n

of n “cities” for each pair

c i , c j

of cities, the distance d

c i , c j

between them Solution: an ordering < c π (1) , c π (2)

, c π ( n )

of the n cities that minimizes the tour length : { n − 1 ∑^ i = d

c π ( i ) , c π ( i +1)

d

c π ( n ) , c π (1)

CSC 316 – Fall 2011: Introduction Copyright c© 2011by George N. Rouskas

Example: An Instance of TSP

n

cities,

C

c 1 , c 2 , c 3 , c 4

d

c 1 , c 2

,^ d

c 1 , c 3

,^ d

c 1 , c 4

,^ d

c 2 , c 3

, d

c 2 , c 4

,^ d

c 3 , c 4

CSC 316 – Fall 2011: Introduction Copyright c© 2011by George N. Rouskas

Example: An Instance of TSP

n

cities,

C

c 1 , c 2 , c 3 , c 4

d

c 1 , c 2

,^ d

c 1 , c 3

,^ d

c 1 , c 4

,^ d

c 2 , c 3

, d

c 2 , c 4

,^ d

c 3 , c 4

5 C C C C 1 2 3 4 10 6 9 9 3 Solution:

c 2 , c 4 , c 3 , c 1

, length= CSC 316 – Fall 2011: Introduction Copyright c© 2011by George N. Rouskas

Algorithms

Algorithm: description of a finite number of steps to solve a problem Algorithm

A

is said to “ solve ” problem

if: A can be applied to any instance

I

of

always produces a solution for any instance

I

Note: solution must satisfy all properties set forth in

’s specification → an algorithm does not solve TSP unless it always constructs a tour of minimum length CSC 316 – Fall 2011: Introduction Copyright c© 2011by George N. Rouskas

Objectives

Our primary interest is in algorithms per se and their properties Expressing algorithms in syntax of Java is of secondary interest → you must already be fluent in Java Objectives:^ 1. study most important algorithms for frequently arising problems

selecting the “best” algorithm whenever we have a choice CSC 316 – Fall 2011: Introduction Copyright c© 2011by George N. Rouskas

Pseudo-Code

Pseudo-Code: high-level descriptions of algorithms combine natural language and structures from programminglanguage clear and informative not computer programs

for human eyes only more compact than Java code

easier to read/understand facilitate analysis of correctness and running time of algorithms CSC 316 – Fall 2011: Introduction Copyright c© 2011by George N. Rouskas

Pseudo-Code Example

procedure MergeSort (Array

T

[

, n

1]

, int f irst , int last )

int middle

if ( f irst < last

) then

middle

f irst

last

MergeSort ( T, f irst, middle )

MergeSort ( T, middle

, last )

Merge ( T, f irst, middle, middle

, last ) CSC 316 – Fall 2011: Introduction Copyright c© 2011by George N. Rouskas

Important Properties of Algorithms

Correctness

always gives the right answer Termination:

no infinite loops Robustness:

able to handle unexpected inputs Complexity of resulting program:

easily translates into code Adaptability:

easy to adapt to small changes in problem specs Efficiency:

low utilization of computational resources

CSC 316 – Fall 2011: Introduction Copyright c© 2011by George N. Rouskas

Data Structures

Data structure: a systematic way of organizing and accessing data logical organization of computer memory Classification: amount of data contained in the structure can’t change w/o recreating it from scratch

static DS

array may increase/decrease at will

dynamic DS

linked list CSC 316 – Fall 2011: Introduction Copyright c© 2011by George N. Rouskas

Design Principles

Abstraction: decomposes complex system into fundamental parts describes functionality of each part in simple, precise terms Information hiding: conceals internal details of implementation allows freedom in implementing the desired functionality CSC 316 – Fall 2011: Introduction Copyright c© 2011by George N. Rouskas

Example: Stack ADT

Stack: container of objects operating under the LIFO principle objects may be inserted into a stack at any time only most-recently inserted object can be removed at any time Stack is an ADT supporting two operations (methods):^ 1. push( o ): insert object o at top of stack

pop(): remove and return top object of stack; error if stack empty CSC 316 – Fall 2011: Introduction Copyright c© 2011by George N. Rouskas