Docsity

Notes on Data Structures and Algorithm Analysis - Fall 2006 | CS 483, Study notes of Algorithms and Programming

Toggle sidebar
bg1
Outline Introduction Algorithms & Problems Fundamentals Problem Types Data Structures Homework
CS 483 - Data Structures and Algorithm Analysis
Lecture I: Chapter 1
R. Paul Wiegand
George Mason University, Department of Computer Science
January 25, 2006
R. Paul Wiegand George Mason University, Department of Computer Science
CS483 Lecture I
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Notes on Data Structures and Algorithm Analysis - Fall 2006 | CS 483 and more Study notes Algorithms and Programming in PDF only on Docsity!

CS 483 - Data Structures and Algorithm Analysis

Lecture I: Chapter 1

R. Paul Wiegand

George Mason University, Department of Computer Science

January 25, 2006

R. Paul Wiegand George Mason University, Department of Computer Science

Outline

1 Introduction

2 Algorithms & Problems

3 Fundamentals

4 Problem Types

5 Data Structures

6 Homework

R. Paul Wiegand George Mason University, Department of Computer Science

Motivating the Course

Why this course matters:

Forrest for the trees Making educated & informed decisions Need as designer AND implementor Engineer versus technician

Personal reflections:

“Don’t know Big-O stuff!” “The JDK comes with a Sort routine...” Etc.

Key ideas (from Henry Hamburger)

R. Paul Wiegand George Mason University, Department of Computer Science

Computational Problems

What is a computational problem?

Problem statement

The statement of a problem specifies in general terms the relationship between input and output Example Sort a set of numbers in non-decreasing order (sorting problem) Input: 〈a 1 , a 2 ,... an〉 Output: 〈a′ 1 , a′ 2 ,... a′ n〉 : a 1 ′ ≤ a′ 2 ≤ · · · ≤ a′ n

Problem instance

A problem instance consists of the input, satisfying whatever constraints are imposed by the problem statement) needed to compute a “solution” to the problem. Example problem instance: Input: 〈 4 , 6 , 7 , 1 , 9 , 3 , 8 , 10 , 5 , 2 〉 Output(solution): 〈 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 〉

Are problems inherently hard (or harder than others)?

R. Paul Wiegand George Mason University, Department of Computer Science

Example: Greatest Common Divisor

Input: m, n ∈ N, where (m ≥ 0 ∧ n > 0) ∨ (m > 0 ∧ n ≥ 0) Output: Largest integer that divides both m and n evenly

Euclid(m, n)

while n 6 = 0 do

r ← m mod n

m ← n

n ← r

return m

ConsecutiveInteger(m, n): step-1: t ← min{m, n} step-2: if m t ∈ N+, goto step- step-3: if n t ∈ N+, return t step-4: t ← t − 1 , goto step-

Are these algorithms guaranteed to stop?

Are there different input restrictions?

Look over the “middle-school method” in the book ...

R. Paul Wiegand George Mason University, Department of Computer Science

Steps for Designing Algorithms

Understand the problem

Assess computational resources (memory, speed, etc.)

Decide between an exact or approximate algorithm

Choose appropriate data structures

Specify an algorithm in pseudo-code

Prove correctness

Analyze the algorithm

Implement & test the algorithm

R. Paul Wiegand George Mason University, Department of Computer Science

Sorting

Arrange a set of values in a total or partial ordering

Often make use of a key for sorting more complicated data

With key-comparison based sorts, cannot do better than n lg n

time

Sorting algorithms are stable if given two elements with equal

key values at positions i and j such that i < j, after the sort

they will appear in positions i ′^ and j′^ such that i ′^ < j′.

Sorting algorithms are called in place sorts if they do not

require more than a constant amount of memory beyond what

is stored in the list.

R. Paul Wiegand George Mason University, Department of Computer Science

Searching & String Processing

Searching

Find a given value, called a search key, in a set of values A variety of algorithms exist (sequential search, binary search, etc.) Sometimes data are stored in data structures that make them more conducive for searching (hash maps, red-black trees, etc.) Engineers have to pay attention to applications where the underlying data may change frequently relative to the number of searches.

String Processing

A string is a sequence of characters from some well-defined alphabet (e.g., binary strings) Large class of problems dealing with the handling of strings An example problem is string matching: Find the positions of a substring in a master string.

R. Paul Wiegand George Mason University, Department of Computer Science

Geometric & Numerical Problems

Geometric Problems

Geometric problems deal with geometric objects (e.g., points, lines, polygons, etc.) For example: Closest-pair problem

Given n points, find the pair of points with the minimum distance between them Convex hull problem (^) Given n points in a set, find the smallest convex poly- gon that contains all these points.

These are different than graph problems!

Numerical Problems

Problems involving continuous mathematical objects For example: Solving systems of equations Computing derivatives & definite integrals Optimizing numerical functions, etc.

R. Paul Wiegand George Mason University, Department of Computer Science

Linear Data Structures: Elementary data structures

The following are two elementary data structures useful for produce more ab- stract linear data structures called lists (a finite sequence of data items) array — A sequence of n items of the same data type stored contiguously in memory and accessible using an index Pre-established, fixed size Constant time access, insertion and deletion can be challenging Example: bit string, 1 0 0 1 1 0 1 linked list — A sequence of zero or more nodes, each containing data and pointer(s) to other node(s) Not necessarily fixed in size Linear time access, insertion and deletion are simpler Linked lists can be single-linked or doubly-linked Linked lists can have a header, which stores useful information (e.g., length)

R. Paul Wiegand George Mason University, Department of Computer Science

Graphs: Simple

Graphs are collections of points called vertices and line segments, called edges, connecting (some of the) vertices Formally: G := 〈V , E 〉, where V is a finite set of labels corresponding to vertices (e.g., V := {a, b, c}) and E is a finite set of pairs of these items (e.g., E := {(a, b), (a, c)} Undirected graph: Edges are unordered, i.e., (a, b) = (b, a) Directed graph: Edges are ordered and thus imply a direction

a

b c

a

b c

Complete — every pair of vertices is connected by an edge Dense — most vertices are connected Sparse — few vertices are connected

R. Paul Wiegand George Mason University, Department of Computer Science

Graphs: Representation

adjacency matrix — Enumerate vertices in { 1... n}, create an n × n matrix of boolean values indicating whether an edge exists between the specified vertices Undirected graphs result in symmetric matrices Easily determine if an edge exists, requires space Good for dense graphs

a b c a 0 1 1 b 1 0 0 c 1 0 0

adjacency list — Create a linked list for each vertex containing the vertices to which that vertex is connected Somewhat more difficult to determine edge existence, more compact in space Good for sparse graphs

a → b → c b → a c → a

R. Paul Wiegand George Mason University, Department of Computer Science

Graphs: Trees

A (free) tree is a connected, acyclic graph. A forest is multiple trees, or an unconnected, acyclic graph. |E | = |V | − 1 For every two vertices, there’s always exactly one simple path between them ∴ we can select an arbitrary vertex to be the root For any v ∈ T , all vertices on the path between the root and v are called ancestors The last edge on that path before v is called the parent, v is the child of that node, etc. A vertex with no children is called a leaf A vertex with all its descendants is called a subtree The depth v is the length of the simple path from the root to v The height of a tree is the length of the longest simple path from R. Paul Wiegandthe root to a leaf George Mason University, Department of Computer Science

Sets & Dictionaries

What is a set? A set is an unordered collection (possibly empty) of distinct items. We can implement a set as a bit vector over the universal set We can implement a set with a list structure (with insertion constraints) A multiset or bag is a set without the uniqueness constraint (an unordered collection of objects) Basic operations of a multiset: Search, Insert, Delete A basic data structure that accomplishes these operations is a dictionary Sometimes we need to dynamically partition some n-element set into a collection of disjoint sets. Sometimes we need to take the union or intersection of sets

R. Paul Wiegand George Mason University, Department of Computer Science