Comparing AlgorithmsCSCI 1900 – Discrete Structures CSCI 1900 Discrete Structures Complexity Reading: Kolman, Sections 4.6, 5.2, and 5.3.

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

Hashing.
Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
MATH 224 – Discrete Mathematics
Programming and Data Structure
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CPSC 335 Dr. Marina Gavrilova Computer Science University of Calgary Canada.
Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.
Data Structures Using C++ 2E
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree.
HST 952 Computing for Biomedical Scientists Lecture 10.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Chapter 9: Searching, Sorting, and Algorithm Analysis
CS0007: Introduction to Computer Programming Array Algorithms.
Data Structures Hash Tables
Hash Table indexing and Secondary Storage Hashing.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Lecture 10: Search Structures and Hashing
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Hashing General idea: Get a large array
An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Chapter 3: Arrays, Linked Lists, and Recursion
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
Cmpt-225 Simulation. Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
CHP - 9 File Structures. INTRODUCTION In some of the previous chapters, we have discussed representations of and operations on data structures. These.
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
MATH 224 – Discrete Mathematics
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Hash Table March COP 3502, UCF.
Chapter 13 File Structures. Understand the file access methods. Describe the characteristics of a sequential file. After reading this chapter, the reader.
Lists in Python.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
File Structures Foundations of Computer Science  Cengage Learning.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
Indexed and Relative File Processing
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Hashing Hashing is another method for sorting and searching data.
EEL 3801C EEL 3801 Part I Computing Basics. EEL 3801C Data Representation Digital computers are binary in nature. They operate only on 0’s and 1’s. Everything.
March 23 & 28, Csci 2111: Data and File Structures Week 10, Lectures 1 & 2 Hashing.
March 23 & 28, Hashing. 2 What is Hashing? A Hash function is a function h(K) which transforms a key K into an address. Hashing is like indexing.
Lecture 12COMPSCI.220.FS.T Symbol Table and Hashing A ( symbol) table is a set of table entries, ( K,V) Each entry contains: –a unique key, K,
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
Chapter 10 Hashing. The search time of each algorithm depend on the number n of elements of the collection S of the data. A searching technique called.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
1 CSCD 326 Data Structures I Hashing. 2 Hashing Background Goal: provide a constant time complexity method of searching for stored data The best traditional.
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
Searching Topics Sequential Search Binary Search.
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CHP - 9 File Structures.
Arrays: Checkboxes and Textareas
Review Graph Directed Graph Undirected Graph Sub-Graph
Chapter 8 Arrays Objectives
Chapter 8 Arrays Objectives
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Chapter 8 Arrays Objectives
Presentation transcript:

Comparing AlgorithmsCSCI 1900 – Discrete Structures CSCI 1900 Discrete Structures Complexity Reading: Kolman, Sections 4.6, 5.2, and 5.3

Comparing AlgorithmsCSCI 1900 – Discrete Structures Comparing Algorithms This presentation uses an example to present the material from the following three sections of our text book. –Section 4.6 – Computer Representations –Section 5.2 – Hashing –Section 5.3 – Comparing algorithms based on computational method

Comparing AlgorithmsCSCI 1900 – Discrete Structures Example – Access Control System This entire lecture is based on the example of a database listing valid student id's to be used in an access control system. If the id is in the database, then the student is allowed in the door. Each method of storing the data is compared based on the complexity it takes to store a new record and what it takes to search for a record.

Comparing AlgorithmsCSCI 1900 – Discrete Structures Unsorted lists – new record Each time a new student id is added, simply add it to the end of the list: students[next_empty_entry] = NEW_ID next_empty_entry = next_empty_entry + 1

Comparing AlgorithmsCSCI 1900 – Discrete Structures Unsorted lists – search records To search for an id, simply go through the list looking for a match found = 0 for count=0 to (next_empty_entry-1) if students[count] = FIND_ID found = 1 exit for endif next count

Comparing AlgorithmsCSCI 1900 – Discrete Structures Cycle Counts In an effort to estimate the relative speeds of each of these situations, assume the following cycle counts for each instruction. –assignment/equals = 3 cycles –increment = 1 cycle –for loop or while loop = 3 cycles –if = 2 cycles –switch/case = 4 cycles

Comparing AlgorithmsCSCI 1900 – Discrete Structures Unsorted lists Cycles for new record To add an id, there is one assignment and one increment for a total of 4 cycles

Comparing AlgorithmsCSCI 1900 – Discrete Structures Unsorted lists Cycles for searching records To search, assume that on average, we have to go through half of the items before finding a match –1 assignment = 3 cycles –N/2 loops = N/2 * 3 cycles –N/2 ifs = N/2 * 2 cycles –1 assignment in if = 3 cycles –Total = (5 * N/2) + 6

Comparing AlgorithmsCSCI 1900 – Discrete Structures Sorted list The best sorted list is one where there is a unique, sequential key that identifies the argument to the array.

Comparing AlgorithmsCSCI 1900 – Discrete Structures Sorted array lists – new record Depending on the algorithm used to derive the index, a sorted list that has ascending indexes could be a bit more complex. index=index from NEW_ID for i=next_empty_entry to (index+1) step -1 students[i] = students[i-1] next i students[index] = NEW_ID next_empty_entry = next_empty_entry + 1

Comparing AlgorithmsCSCI 1900 – Discrete Structures Sorted array lists – new record...or it could be a great deal simpler by making the size of the array equal to the size of the set of all possible keys. index=index from NEW_ID students[index] = NEW_ID For us this isn’t practical, i.e., our array size would need to be able to contain 10 9 records.

Comparing AlgorithmsCSCI 1900 – Discrete Structures Sorted array lists – search records For a sorted list that has ascending indexes, the search algorithm is simple. found = 0 index=index from FIND_ID if students[index] = FIND_ID found = 1 end if

Comparing AlgorithmsCSCI 1900 – Discrete Structures Sorted array lists Cycles for adding record To add an id, there is an assignment followed by a for loop with, on average, N/2 loops. Inside the for loop, there is an assignment, and since the for loop is executed an average of N/2 times, there are N/2 assignments. The for loop is followed by an assignment and an increment. This gives us a total of: 3 + (N/2*3) + (N/2*3) = 3*N + 7

Comparing AlgorithmsCSCI 1900 – Discrete Structures Sorted array lists Cycles for searching records To find a record, there are two assignments followed by an if and then a possible third assignment. This gives us a total of: = 11 cycles

Comparing AlgorithmsCSCI 1900 – Discrete Structures Sorted list – using pointers We don't have a sorted index here. Our unique key could take on any value from to Instead, what we will do is make 10 lists, each representing a student id that begins with a different decimal digit.

Comparing AlgorithmsCSCI 1900 – Discrete Structures Sorted lists – new record For the case where we have created ten lists, we’ve basically divided the unsorted list into ten. Each time a new student id is added, find the list it should be in, and add it to the end of the list index = first digit of NEW_ID students[index, next_empty_entry[index]] = NEW_ID next_empty_entry[index] = next_empty_entry [index] + 1

Comparing AlgorithmsCSCI 1900 – Discrete Structures Sorted lists – search records This is the same as finding a record in an unsorted list except that we need to select which list to search from. index = first digit of FIND_ID found = 0 for count=0 to (next_empty_entry[index]-1) if students[index, next_empty_entry[index]] = FIND_ID found = 1 exit for endif next count

Comparing AlgorithmsCSCI 1900 – Discrete Structures Sorted lists Cycles for adding record To add an id, there are two assignments, index and NEW_ID, and one increment for a total of 7 cycles

Comparing AlgorithmsCSCI 1900 – Discrete Structures Sorted lists Cycles for search records This is exactly the same as for the unsorted lists except for two things: –We need to assign the value of index (add 3 cycles) –The lists are about one tenth the length of the full list This gives us a new total of : 1/10 * (5 * N/2) + 9

Comparing AlgorithmsCSCI 1900 – Discrete Structures Unbalanced lists At ETSU, however, the list for student id’s beginning with ‘4’ would overwhelm the other lists, and the benefit would be negligible.

Comparing AlgorithmsCSCI 1900 – Discrete Structures Hashing In order to determine which list a student id should be assigned to, we need to create a function that maps each student id to a different list in a balanced way. The function needs to be reproducible so that when we go to find the record, it is in the list we originally assumed it would be in.

Comparing AlgorithmsCSCI 1900 – Discrete Structures Hashing A hashing algorithm randomly assigns an integer to one of n lists. The typical hashing algorithm is a “mod-n” function, i.e., a function that determines the remainder of an integer after a division by n. By using a mod-n function, we can have as many lists as we want.

Comparing AlgorithmsCSCI 1900 – Discrete Structures Cycle count for hashing The cycle count for hashing should be very close to the cycle count for the sorted list example with two difference: –Assigning the value for index will take more cycles because the mod-n involves division –The number of lists is up to our selection of n. Therefore, we could significantly shorten up the search time if we have the memory to add more lists.

Comparing AlgorithmsCSCI 1900 – Discrete Structures Linked Lists There is a problem with the sorted and hashed lists presented earlier. A certain amount of memory must be allocated to each list. For most of the lists, this is wasted space. For some of the lists, this may be a limiting factor forcing some student id’s to be refused because of no space while other lists have plenty of space.

Comparing AlgorithmsCSCI 1900 – Discrete Structures Linked Lists (continued) A linked list adds a second array to tell the computer where to find the next record in the sorted list. This way, the next element does not have to be stored sequentially in memory.

Comparing AlgorithmsCSCI 1900 – Discrete Structures Linked Lists (continued) Corresponding index in second array indicates index of next element in list If index of next element is listed as zero, that is the end of the list This means that the index count must start at 1 A separate variable must point to beginning of list Sometimes a third array is added so that we can go backwards in the list, i.e., corresponding index in third array points to index of previous element.

Comparing AlgorithmsCSCI 1900 – Discrete Structures In-class exercise See Figures 4.30 and 4.33 from the textbook Write the BASIC code to do the following: –Add a record to a linked list –Find a record in a linked list Estimate the number of cycles required for both adding and finding a record.